2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* parallax_layer.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2020-01-01 10:16:22 +00:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* 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-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "parallax_layer.h"
|
2017-08-18 23:02:56 +00:00
|
|
|
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/engine.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "parallax_background.h"
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ParallaxLayer::set_motion_scale(const Size2 &p_scale) {
|
2016-06-26 19:22:20 +00:00
|
|
|
|
2017-08-22 13:13:31 +00:00
|
|
|
motion_scale = p_scale;
|
|
|
|
|
2017-08-24 20:58:51 +00:00
|
|
|
ParallaxBackground *pb = Object::cast_to<ParallaxBackground>(get_parent());
|
|
|
|
if (pb && is_inside_tree()) {
|
2016-06-26 19:22:20 +00:00
|
|
|
Vector2 ofs = pb->get_final_offset();
|
|
|
|
float scale = pb->get_scroll_scale();
|
2017-10-26 19:08:40 +00:00
|
|
|
set_base_offset_and_scale(ofs, scale, screen_offset);
|
2016-06-26 19:22:20 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Size2 ParallaxLayer::get_motion_scale() const {
|
|
|
|
|
|
|
|
return motion_scale;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ParallaxLayer::set_motion_offset(const Size2 &p_offset) {
|
2016-06-26 19:22:20 +00:00
|
|
|
|
2017-08-22 13:13:31 +00:00
|
|
|
motion_offset = p_offset;
|
|
|
|
|
2017-08-24 20:58:51 +00:00
|
|
|
ParallaxBackground *pb = Object::cast_to<ParallaxBackground>(get_parent());
|
|
|
|
if (pb && is_inside_tree()) {
|
2016-06-26 19:22:20 +00:00
|
|
|
Vector2 ofs = pb->get_final_offset();
|
|
|
|
float scale = pb->get_scroll_scale();
|
2017-10-26 19:08:40 +00:00
|
|
|
set_base_offset_and_scale(ofs, scale, screen_offset);
|
2016-06-26 19:22:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Size2 ParallaxLayer::get_motion_offset() const {
|
|
|
|
|
|
|
|
return motion_offset;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
void ParallaxLayer::_update_mirroring() {
|
|
|
|
|
2019-08-13 18:59:41 +00:00
|
|
|
if (!is_inside_tree())
|
|
|
|
return;
|
|
|
|
|
2017-08-24 20:58:51 +00:00
|
|
|
ParallaxBackground *pb = Object::cast_to<ParallaxBackground>(get_parent());
|
2014-02-10 01:10:30 +00:00
|
|
|
if (pb) {
|
|
|
|
|
2018-04-07 22:42:11 +00:00
|
|
|
RID c = pb->get_canvas();
|
2014-02-10 01:10:30 +00:00
|
|
|
RID ci = get_canvas_item();
|
2017-11-23 01:52:34 +00:00
|
|
|
Point2 mirrorScale = mirroring * get_scale();
|
|
|
|
VisualServer::get_singleton()->canvas_set_item_mirroring(c, ci, mirrorScale);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ParallaxLayer::set_mirroring(const Size2 &p_mirroring) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
mirroring = p_mirroring;
|
|
|
|
if (mirroring.x < 0)
|
|
|
|
mirroring.x = 0;
|
|
|
|
if (mirroring.y < 0)
|
|
|
|
mirroring.y = 0;
|
2016-06-26 19:51:13 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
_update_mirroring();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Size2 ParallaxLayer::get_mirroring() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return mirroring;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParallaxLayer::_notification(int p_what) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (p_what) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
orig_offset = get_position();
|
|
|
|
orig_scale = get_scale();
|
2014-02-10 01:10:30 +00:00
|
|
|
_update_mirroring();
|
|
|
|
} break;
|
2019-05-04 17:08:49 +00:00
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
|
|
|
|
|
|
|
set_position(orig_offset);
|
|
|
|
set_scale(orig_scale);
|
|
|
|
} break;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-26 19:08:40 +00:00
|
|
|
void ParallaxLayer::set_base_offset_and_scale(const Point2 &p_offset, float p_scale, const Point2 &p_screen_offset) {
|
|
|
|
screen_offset = p_screen_offset;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
if (!is_inside_tree())
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2017-08-18 23:02:56 +00:00
|
|
|
if (Engine::get_singleton()->is_editor_hint())
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2017-10-26 19:08:40 +00:00
|
|
|
|
|
|
|
Point2 new_ofs = (screen_offset + (p_offset - screen_offset) * motion_scale) + motion_offset * p_scale + orig_offset * p_scale;
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (mirroring.x) {
|
2017-11-23 01:52:34 +00:00
|
|
|
double den = mirroring.x * p_scale;
|
|
|
|
new_ofs.x -= den * ceil(new_ofs.x / den);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mirroring.y) {
|
2017-11-23 01:52:34 +00:00
|
|
|
double den = mirroring.y * p_scale;
|
|
|
|
new_ofs.y -= den * ceil(new_ofs.y / den);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-01-04 04:16:14 +00:00
|
|
|
set_position(new_ofs);
|
2017-11-23 01:52:34 +00:00
|
|
|
set_scale(Vector2(1, 1) * p_scale * orig_scale);
|
|
|
|
|
|
|
|
_update_mirroring();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2016-05-17 21:27:15 +00:00
|
|
|
String ParallaxLayer::get_configuration_warning() const {
|
2020-03-22 08:31:09 +00:00
|
|
|
String warning = Node2D::get_configuration_warning();
|
2017-08-24 20:58:51 +00:00
|
|
|
if (!Object::cast_to<ParallaxBackground>(get_parent())) {
|
2020-03-22 08:31:09 +00:00
|
|
|
if (warning != String()) {
|
|
|
|
warning += "\n\n";
|
|
|
|
}
|
|
|
|
warning += TTR("ParallaxLayer node only works when set as child of a ParallaxBackground node.");
|
2016-05-17 21:27:15 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 08:31:09 +00:00
|
|
|
return warning;
|
2016-05-17 21:27:15 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void ParallaxLayer::_bind_methods() {
|
|
|
|
|
2017-03-05 15:44:50 +00: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);
|
|
|
|
|
|
|
|
ADD_GROUP("Motion", "motion_");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "motion_scale"), "set_motion_scale", "get_motion_scale");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "motion_offset"), "set_motion_offset", "get_motion_offset");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "motion_mirroring"), "set_mirroring", "get_mirroring");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ParallaxLayer::ParallaxLayer() {
|
|
|
|
motion_scale = Size2(1, 1);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|