forked from godotengine/godot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallax_layer.cpp
165 lines (137 loc) · 5.96 KB
/
parallax_layer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**************************************************************************/
/* 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. */
/**************************************************************************/
#include "parallax_layer.h"
#include "parallax_background.h"
void ParallaxLayer::set_motion_scale(const Size2 &p_scale) {
motion_scale = p_scale;
ParallaxBackground *pb = Object::cast_to<ParallaxBackground>(get_parent());
if (pb && is_inside_tree()) {
Vector2 final_ofs = pb->get_final_offset();
real_t scroll_scale = pb->get_scroll_scale();
set_base_offset_and_scale(final_ofs, scroll_scale);
}
}
Size2 ParallaxLayer::get_motion_scale() const {
return motion_scale;
}
void ParallaxLayer::set_motion_offset(const Size2 &p_offset) {
motion_offset = p_offset;
ParallaxBackground *pb = Object::cast_to<ParallaxBackground>(get_parent());
if (pb && is_inside_tree()) {
Vector2 final_ofs = pb->get_final_offset();
real_t scroll_scale = pb->get_scroll_scale();
set_base_offset_and_scale(final_ofs, scroll_scale);
}
}
Size2 ParallaxLayer::get_motion_offset() const {
return motion_offset;
}
void ParallaxLayer::_update_mirroring() {
if (!is_inside_tree()) {
return;
}
ParallaxBackground *pb = Object::cast_to<ParallaxBackground>(get_parent());
if (pb) {
RID c = pb->get_canvas();
RID ci = get_canvas_item();
Point2 mirrorScale = mirroring * get_scale();
RenderingServer::get_singleton()->canvas_set_item_mirroring(c, ci, mirrorScale);
}
}
void ParallaxLayer::set_mirroring(const Size2 &p_mirroring) {
mirroring = p_mirroring;
if (mirroring.x < 0) {
mirroring.x = 0;
}
if (mirroring.y < 0) {
mirroring.y = 0;
}
_update_mirroring();
}
Size2 ParallaxLayer::get_mirroring() const {
return mirroring;
}
void ParallaxLayer::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
orig_offset = get_position();
orig_scale = get_scale();
_update_mirroring();
} break;
case NOTIFICATION_EXIT_TREE: {
if (Engine::get_singleton()->is_editor_hint()) {
break;
}
set_position(orig_offset);
set_scale(orig_scale);
} break;
}
}
void ParallaxLayer::set_base_offset_and_scale(const Point2 &p_offset, real_t p_scale) {
if (!is_inside_tree()) {
return;
}
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
Point2 new_ofs = p_offset * motion_scale + motion_offset * p_scale + orig_offset * p_scale;
if (mirroring.x) {
real_t den = mirroring.x * p_scale;
new_ofs.x -= den * ceil(new_ofs.x / den);
}
if (mirroring.y) {
real_t den = mirroring.y * p_scale;
new_ofs.y -= den * ceil(new_ofs.y / den);
}
set_position(new_ofs);
set_scale(Vector2(1, 1) * p_scale * orig_scale);
_update_mirroring();
}
PackedStringArray ParallaxLayer::get_configuration_warnings() const {
PackedStringArray warnings = Node::get_configuration_warnings();
if (!Object::cast_to<ParallaxBackground>(get_parent())) {
warnings.push_back(RTR("ParallaxLayer node only works when set as child of a ParallaxBackground node."));
}
return warnings;
}
void ParallaxLayer::_bind_methods() {
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", PROPERTY_HINT_LINK), "set_motion_scale", "get_motion_scale");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "motion_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_motion_offset", "get_motion_offset");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "motion_mirroring"), "set_mirroring", "get_mirroring");
}
ParallaxLayer::ParallaxLayer() {
}