2014-02-10 09:10:30 +08:00
|
|
|
/**************************************************************************/
|
|
|
|
/* parallax_layer.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 07:50:27 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
#include "parallax_layer.h"
|
2017-08-19 07:02:56 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
#include "parallax_background.h"
|
|
|
|
|
|
|
|
void ParallaxLayer::set_motion_scale(const Size2 &p_scale) {
|
2017-08-22 21:13:31 +08:00
|
|
|
motion_scale = p_scale;
|
|
|
|
|
2017-08-25 04:58:51 +08:00
|
|
|
ParallaxBackground *pb = Object::cast_to<ParallaxBackground>(get_parent());
|
|
|
|
if (pb && is_inside_tree()) {
|
2022-09-29 17:53:28 +08:00
|
|
|
Vector2 final_ofs = pb->get_final_offset();
|
|
|
|
real_t scroll_scale = pb->get_scroll_scale();
|
|
|
|
set_base_offset_and_scale(final_ofs, scroll_scale);
|
2016-06-27 03:22:20 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Size2 ParallaxLayer::get_motion_scale() const {
|
|
|
|
return motion_scale;
|
|
|
|
}
|
|
|
|
|
2016-06-27 03:22:20 +08:00
|
|
|
void ParallaxLayer::set_motion_offset(const Size2 &p_offset) {
|
2017-08-22 21:13:31 +08:00
|
|
|
motion_offset = p_offset;
|
|
|
|
|
2017-08-25 04:58:51 +08:00
|
|
|
ParallaxBackground *pb = Object::cast_to<ParallaxBackground>(get_parent());
|
|
|
|
if (pb && is_inside_tree()) {
|
2022-09-29 17:53:28 +08:00
|
|
|
Vector2 final_ofs = pb->get_final_offset();
|
|
|
|
real_t scroll_scale = pb->get_scroll_scale();
|
|
|
|
set_base_offset_and_scale(final_ofs, scroll_scale);
|
2016-06-27 03:22:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Size2 ParallaxLayer::get_motion_offset() const {
|
|
|
|
return motion_offset;
|
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
void ParallaxLayer::_update_mirroring() {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (!is_inside_tree()) {
|
2019-08-14 02:59:41 +08:00
|
|
|
return;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2019-08-14 02:59:41 +08:00
|
|
|
|
2017-08-25 04:58:51 +08:00
|
|
|
ParallaxBackground *pb = Object::cast_to<ParallaxBackground>(get_parent());
|
2014-02-10 09:10:30 +08:00
|
|
|
if (pb) {
|
2018-04-08 06:42:11 +08:00
|
|
|
RID c = pb->get_canvas();
|
2014-02-10 09:10:30 +08:00
|
|
|
RID ci = get_canvas_item();
|
2024-03-16 22:55:26 +08:00
|
|
|
Point2 mirror_scale = mirroring * orig_scale;
|
|
|
|
RenderingServer::get_singleton()->canvas_set_item_mirroring(c, ci, mirror_scale);
|
2024-02-17 07:57:32 +08:00
|
|
|
RenderingServer::get_singleton()->canvas_item_set_interpolated(ci, false);
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParallaxLayer::set_mirroring(const Size2 &p_mirroring) {
|
2024-08-19 01:36:08 +08:00
|
|
|
mirroring = p_mirroring.maxf(0);
|
2016-06-27 03:51:13 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
_update_mirroring();
|
|
|
|
}
|
|
|
|
|
|
|
|
Size2 ParallaxLayer::get_mirroring() const {
|
|
|
|
return mirroring;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParallaxLayer::_notification(int p_what) {
|
|
|
|
switch (p_what) {
|
2014-11-06 08:20:42 +08:00
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2017-01-04 12:16:14 +08:00
|
|
|
orig_offset = get_position();
|
2014-02-10 09:10:30 +08:00
|
|
|
orig_scale = get_scale();
|
|
|
|
_update_mirroring();
|
|
|
|
} break;
|
2022-02-16 01:06:48 +08:00
|
|
|
|
2019-05-05 01:08:49 +08:00
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
2021-09-01 20:57:14 +08:00
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-05-05 01:08:49 +08:00
|
|
|
set_position(orig_offset);
|
|
|
|
set_scale(orig_scale);
|
|
|
|
} break;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-27 02:47:22 +08:00
|
|
|
void ParallaxLayer::set_base_offset_and_scale(const Point2 &p_offset, real_t p_scale) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (!is_inside_tree()) {
|
2014-02-10 09:10:30 +08:00
|
|
|
return;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
2014-02-10 09:10:30 +08:00
|
|
|
return;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2017-10-27 03:08:40 +08:00
|
|
|
|
2022-02-27 02:47:22 +08:00
|
|
|
Point2 new_ofs = p_offset * motion_scale + motion_offset * p_scale + orig_offset * p_scale;
|
2017-10-27 03:08:40 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
if (mirroring.x) {
|
2021-09-08 12:25:35 +08:00
|
|
|
real_t den = mirroring.x * p_scale;
|
2017-11-23 09:52:34 +08:00
|
|
|
new_ofs.x -= den * ceil(new_ofs.x / den);
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mirroring.y) {
|
2021-09-08 12:25:35 +08:00
|
|
|
real_t den = mirroring.y * p_scale;
|
2017-11-23 09:52:34 +08:00
|
|
|
new_ofs.y -= den * ceil(new_ofs.y / den);
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-01-04 12:16:14 +08:00
|
|
|
set_position(new_ofs);
|
2017-11-23 09:52:34 +08:00
|
|
|
set_scale(Vector2(1, 1) * p_scale * orig_scale);
|
|
|
|
|
|
|
|
_update_mirroring();
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2024-02-18 02:03:21 +08:00
|
|
|
PackedStringArray ParallaxLayer::get_configuration_warnings() const {
|
2024-06-19 22:40:50 +08:00
|
|
|
PackedStringArray warnings = Node2D::get_configuration_warnings();
|
2020-05-15 04:59:27 +08:00
|
|
|
|
2017-08-25 04:58:51 +08:00
|
|
|
if (!Object::cast_to<ParallaxBackground>(get_parent())) {
|
2022-03-28 21:24:14 +08:00
|
|
|
warnings.push_back(RTR("ParallaxLayer node only works when set as child of a ParallaxBackground node."));
|
2016-05-18 05:27:15 +08:00
|
|
|
}
|
|
|
|
|
2020-10-29 18:01:28 +08:00
|
|
|
return warnings;
|
2016-05-18 05:27:15 +08:00
|
|
|
}
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
void ParallaxLayer::_bind_methods() {
|
2017-02-13 19:47:24 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_motion_scale", "scale"), &ParallaxLayer::set_motion_scale);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_motion_scale"), &ParallaxLayer::get_motion_scale);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_motion_offset", "offset"), &ParallaxLayer::set_motion_offset);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_motion_offset"), &ParallaxLayer::get_motion_offset);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_mirroring", "mirror"), &ParallaxLayer::set_mirroring);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_mirroring"), &ParallaxLayer::get_mirroring);
|
2017-03-05 23:44:50 +08:00
|
|
|
|
2017-01-04 12:16:14 +08:00
|
|
|
ADD_GROUP("Motion", "motion_");
|
2022-06-14 22:33:44 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "motion_scale", PROPERTY_HINT_LINK), "set_motion_scale", "get_motion_scale");
|
2021-12-03 08:09:19 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "motion_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_motion_offset", "get_motion_offset");
|
2017-02-12 08:11:37 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "motion_mirroring"), "set_mirroring", "get_mirroring");
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ParallaxLayer::ParallaxLayer() {
|
2024-02-17 07:57:32 +08:00
|
|
|
// ParallaxLayer is always updated every frame so there is no need to interpolate.
|
|
|
|
set_physics_interpolation_mode(Node::PHYSICS_INTERPOLATION_MODE_OFF);
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|