2014-02-10 02:10:30 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* parallax_background.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "parallax_background.h"
|
2022-02-15 18:06:48 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "parallax_layer.h"
|
|
|
|
|
|
|
|
void ParallaxBackground::_notification(int p_what) {
|
|
|
|
switch (p_what) {
|
2014-11-06 01:20:42 +01:00
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2014-02-10 02:10:30 +01:00
|
|
|
group_name = "__cameras_" + itos(get_viewport().get_id());
|
|
|
|
add_to_group(group_name);
|
|
|
|
} break;
|
2022-02-15 18:06:48 +01:00
|
|
|
|
2014-11-06 01:20:42 +01:00
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
2014-02-10 02:10:30 +01:00
|
|
|
remove_from_group(group_name);
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-26 21:08:40 +02:00
|
|
|
void ParallaxBackground::_camera_moved(const Transform2D &p_transform, const Point2 &p_screen_offset) {
|
|
|
|
screen_offset = p_screen_offset;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
set_scroll_scale(p_transform.get_scale().dot(Vector2(0.5, 0.5)));
|
2017-10-26 21:08:40 +02:00
|
|
|
set_scroll_offset(p_transform.get_origin());
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2021-01-30 05:10:32 +01:00
|
|
|
void ParallaxBackground::set_scroll_scale(real_t p_scale) {
|
2014-02-10 02:10:30 +01:00
|
|
|
scale = p_scale;
|
|
|
|
}
|
|
|
|
|
2021-01-30 05:10:32 +01:00
|
|
|
real_t ParallaxBackground::get_scroll_scale() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParallaxBackground::set_scroll_offset(const Point2 &p_ofs) {
|
|
|
|
offset = p_ofs;
|
|
|
|
|
|
|
|
_update_scroll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParallaxBackground::_update_scroll() {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!is_inside_tree()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-09-29 11:53:28 +02:00
|
|
|
Vector2 scroll_ofs = base_offset + offset * base_scale;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Size2 vps = get_viewport_size();
|
|
|
|
|
2022-09-29 11:53:28 +02:00
|
|
|
scroll_ofs = -scroll_ofs;
|
2014-02-10 02:10:30 +01:00
|
|
|
if (limit_begin.x < limit_end.x) {
|
2022-09-29 11:53:28 +02:00
|
|
|
if (scroll_ofs.x < limit_begin.x) {
|
|
|
|
scroll_ofs.x = limit_begin.x;
|
|
|
|
} else if (scroll_ofs.x + vps.x > limit_end.x) {
|
|
|
|
scroll_ofs.x = limit_end.x - vps.x;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (limit_begin.y < limit_end.y) {
|
2022-09-29 11:53:28 +02:00
|
|
|
if (scroll_ofs.y < limit_begin.y) {
|
|
|
|
scroll_ofs.y = limit_begin.y;
|
|
|
|
} else if (scroll_ofs.y + vps.y > limit_end.y) {
|
|
|
|
scroll_ofs.y = limit_end.y - vps.y;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2022-09-29 11:53:28 +02:00
|
|
|
scroll_ofs = -scroll_ofs;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-09-29 11:53:28 +02:00
|
|
|
final_offset = scroll_ofs;
|
2016-06-26 21:22:20 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
for (int i = 0; i < get_child_count(); i++) {
|
2017-08-24 22:58:51 +02:00
|
|
|
ParallaxLayer *l = Object::cast_to<ParallaxLayer>(get_child(i));
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!l) {
|
2014-02-10 02:10:30 +01:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (ignore_camera_zoom) {
|
2022-09-29 11:53:28 +02:00
|
|
|
l->set_base_offset_and_scale((scroll_ofs + screen_offset * (scale - 1)) / scale, 1.0);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2022-09-29 11:53:28 +02:00
|
|
|
l->set_base_offset_and_scale(scroll_ofs, scale);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Point2 ParallaxBackground::get_scroll_offset() const {
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParallaxBackground::set_scroll_base_offset(const Point2 &p_ofs) {
|
|
|
|
base_offset = p_ofs;
|
|
|
|
_update_scroll();
|
|
|
|
}
|
|
|
|
|
|
|
|
Point2 ParallaxBackground::get_scroll_base_offset() const {
|
|
|
|
return base_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParallaxBackground::set_scroll_base_scale(const Point2 &p_ofs) {
|
|
|
|
base_scale = p_ofs;
|
|
|
|
_update_scroll();
|
|
|
|
}
|
|
|
|
|
|
|
|
Point2 ParallaxBackground::get_scroll_base_scale() const {
|
|
|
|
return base_scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParallaxBackground::set_limit_begin(const Point2 &p_ofs) {
|
|
|
|
limit_begin = p_ofs;
|
|
|
|
_update_scroll();
|
|
|
|
}
|
|
|
|
|
|
|
|
Point2 ParallaxBackground::get_limit_begin() const {
|
|
|
|
return limit_begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParallaxBackground::set_limit_end(const Point2 &p_ofs) {
|
|
|
|
limit_end = p_ofs;
|
|
|
|
_update_scroll();
|
|
|
|
}
|
|
|
|
|
|
|
|
Point2 ParallaxBackground::get_limit_end() const {
|
|
|
|
return limit_end;
|
|
|
|
}
|
|
|
|
|
2015-08-04 06:44:38 +02:00
|
|
|
void ParallaxBackground::set_ignore_camera_zoom(bool ignore) {
|
|
|
|
ignore_camera_zoom = ignore;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ParallaxBackground::is_ignore_camera_zoom() {
|
|
|
|
return ignore_camera_zoom;
|
|
|
|
}
|
|
|
|
|
2016-06-26 21:22:20 +02:00
|
|
|
Vector2 ParallaxBackground::get_final_offset() const {
|
|
|
|
return final_offset;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void ParallaxBackground::_bind_methods() {
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("_camera_moved"), &ParallaxBackground::_camera_moved);
|
2022-04-14 23:20:28 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_scroll_offset", "offset"), &ParallaxBackground::set_scroll_offset);
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_scroll_offset"), &ParallaxBackground::get_scroll_offset);
|
2022-04-14 23:20:28 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_scroll_base_offset", "offset"), &ParallaxBackground::set_scroll_base_offset);
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_scroll_base_offset"), &ParallaxBackground::get_scroll_base_offset);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_scroll_base_scale", "scale"), &ParallaxBackground::set_scroll_base_scale);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_scroll_base_scale"), &ParallaxBackground::get_scroll_base_scale);
|
2022-04-14 23:20:28 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_limit_begin", "offset"), &ParallaxBackground::set_limit_begin);
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_limit_begin"), &ParallaxBackground::get_limit_begin);
|
2022-04-14 23:20:28 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_limit_end", "offset"), &ParallaxBackground::set_limit_end);
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_limit_end"), &ParallaxBackground::get_limit_end);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_ignore_camera_zoom", "ignore"), &ParallaxBackground::set_ignore_camera_zoom);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_ignore_camera_zoom"), &ParallaxBackground::is_ignore_camera_zoom);
|
2017-03-05 16:44:50 +01:00
|
|
|
|
2017-01-04 05:16:14 +01:00
|
|
|
ADD_GROUP("Scroll", "scroll_");
|
2021-12-03 01:09:19 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_scroll_offset", "get_scroll_offset");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_base_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_scroll_base_offset", "get_scroll_base_offset");
|
2022-06-14 16:33:44 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_base_scale", PROPERTY_HINT_LINK), "set_scroll_base_scale", "get_scroll_base_scale");
|
2021-12-03 01:09:19 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_limit_begin", PROPERTY_HINT_NONE, "suffix:px"), "set_limit_begin", "get_limit_begin");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_limit_end", PROPERTY_HINT_NONE, "suffix:px"), "set_limit_end", "get_limit_end");
|
2017-02-12 01:11:37 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_ignore_camera_zoom"), "set_ignore_camera_zoom", "is_ignore_camera_zoom");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ParallaxBackground::ParallaxBackground() {
|
2019-04-05 15:24:54 +02:00
|
|
|
set_layer(-100); //behind all by default
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|