1 /*************************************************************************/
2 /*  parallax_background.cpp                                              */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 
31 #include "parallax_background.h"
32 #include "parallax_layer.h"
33 
_notification(int p_what)34 void ParallaxBackground::_notification(int p_what) {
35 
36 	switch (p_what) {
37 
38 		case NOTIFICATION_ENTER_TREE: {
39 
40 			group_name = "__cameras_" + itos(get_viewport().get_id());
41 			add_to_group(group_name);
42 
43 		} break;
44 		case NOTIFICATION_EXIT_TREE: {
45 
46 			remove_from_group(group_name);
47 		} break;
48 	}
49 }
50 
_camera_moved(const Transform2D & p_transform,const Point2 & p_screen_offset)51 void ParallaxBackground::_camera_moved(const Transform2D &p_transform, const Point2 &p_screen_offset) {
52 
53 	screen_offset = p_screen_offset;
54 
55 	set_scroll_scale(p_transform.get_scale().dot(Vector2(0.5, 0.5)));
56 	set_scroll_offset(p_transform.get_origin());
57 }
58 
set_scroll_scale(float p_scale)59 void ParallaxBackground::set_scroll_scale(float p_scale) {
60 
61 	scale = p_scale;
62 }
63 
get_scroll_scale() const64 float ParallaxBackground::get_scroll_scale() const {
65 
66 	return scale;
67 }
68 
set_scroll_offset(const Point2 & p_ofs)69 void ParallaxBackground::set_scroll_offset(const Point2 &p_ofs) {
70 
71 	offset = p_ofs;
72 
73 	_update_scroll();
74 }
75 
_update_scroll()76 void ParallaxBackground::_update_scroll() {
77 
78 	if (!is_inside_tree())
79 		return;
80 
81 	Vector2 ofs = base_offset + offset * base_scale;
82 
83 	Size2 vps = get_viewport_size();
84 
85 	ofs = -ofs;
86 	if (limit_begin.x < limit_end.x) {
87 
88 		if (ofs.x < limit_begin.x)
89 			ofs.x = limit_begin.x;
90 		else if (ofs.x + vps.x > limit_end.x)
91 			ofs.x = limit_end.x - vps.x;
92 	}
93 
94 	if (limit_begin.y < limit_end.y) {
95 
96 		if (ofs.y < limit_begin.y)
97 			ofs.y = limit_begin.y;
98 		else if (ofs.y + vps.y > limit_end.y)
99 			ofs.y = limit_end.y - vps.y;
100 	}
101 	ofs = -ofs;
102 
103 	final_offset = ofs;
104 
105 	for (int i = 0; i < get_child_count(); i++) {
106 
107 		ParallaxLayer *l = Object::cast_to<ParallaxLayer>(get_child(i));
108 		if (!l)
109 			continue;
110 
111 		if (ignore_camera_zoom)
112 			l->set_base_offset_and_scale(ofs, 1.0, screen_offset);
113 		else
114 			l->set_base_offset_and_scale(ofs, scale, screen_offset);
115 	}
116 }
117 
get_scroll_offset() const118 Point2 ParallaxBackground::get_scroll_offset() const {
119 
120 	return offset;
121 }
122 
set_scroll_base_offset(const Point2 & p_ofs)123 void ParallaxBackground::set_scroll_base_offset(const Point2 &p_ofs) {
124 
125 	base_offset = p_ofs;
126 	_update_scroll();
127 }
128 
get_scroll_base_offset() const129 Point2 ParallaxBackground::get_scroll_base_offset() const {
130 
131 	return base_offset;
132 }
133 
set_scroll_base_scale(const Point2 & p_ofs)134 void ParallaxBackground::set_scroll_base_scale(const Point2 &p_ofs) {
135 
136 	base_scale = p_ofs;
137 	_update_scroll();
138 }
139 
get_scroll_base_scale() const140 Point2 ParallaxBackground::get_scroll_base_scale() const {
141 
142 	return base_scale;
143 }
144 
set_limit_begin(const Point2 & p_ofs)145 void ParallaxBackground::set_limit_begin(const Point2 &p_ofs) {
146 
147 	limit_begin = p_ofs;
148 	_update_scroll();
149 }
150 
get_limit_begin() const151 Point2 ParallaxBackground::get_limit_begin() const {
152 
153 	return limit_begin;
154 }
155 
set_limit_end(const Point2 & p_ofs)156 void ParallaxBackground::set_limit_end(const Point2 &p_ofs) {
157 
158 	limit_end = p_ofs;
159 	_update_scroll();
160 }
161 
get_limit_end() const162 Point2 ParallaxBackground::get_limit_end() const {
163 
164 	return limit_end;
165 }
166 
set_ignore_camera_zoom(bool ignore)167 void ParallaxBackground::set_ignore_camera_zoom(bool ignore) {
168 
169 	ignore_camera_zoom = ignore;
170 }
171 
is_ignore_camera_zoom()172 bool ParallaxBackground::is_ignore_camera_zoom() {
173 
174 	return ignore_camera_zoom;
175 }
176 
get_final_offset() const177 Vector2 ParallaxBackground::get_final_offset() const {
178 
179 	return final_offset;
180 }
181 
_bind_methods()182 void ParallaxBackground::_bind_methods() {
183 
184 	ClassDB::bind_method(D_METHOD("_camera_moved"), &ParallaxBackground::_camera_moved);
185 	ClassDB::bind_method(D_METHOD("set_scroll_offset", "ofs"), &ParallaxBackground::set_scroll_offset);
186 	ClassDB::bind_method(D_METHOD("get_scroll_offset"), &ParallaxBackground::get_scroll_offset);
187 	ClassDB::bind_method(D_METHOD("set_scroll_base_offset", "ofs"), &ParallaxBackground::set_scroll_base_offset);
188 	ClassDB::bind_method(D_METHOD("get_scroll_base_offset"), &ParallaxBackground::get_scroll_base_offset);
189 	ClassDB::bind_method(D_METHOD("set_scroll_base_scale", "scale"), &ParallaxBackground::set_scroll_base_scale);
190 	ClassDB::bind_method(D_METHOD("get_scroll_base_scale"), &ParallaxBackground::get_scroll_base_scale);
191 	ClassDB::bind_method(D_METHOD("set_limit_begin", "ofs"), &ParallaxBackground::set_limit_begin);
192 	ClassDB::bind_method(D_METHOD("get_limit_begin"), &ParallaxBackground::get_limit_begin);
193 	ClassDB::bind_method(D_METHOD("set_limit_end", "ofs"), &ParallaxBackground::set_limit_end);
194 	ClassDB::bind_method(D_METHOD("get_limit_end"), &ParallaxBackground::get_limit_end);
195 	ClassDB::bind_method(D_METHOD("set_ignore_camera_zoom", "ignore"), &ParallaxBackground::set_ignore_camera_zoom);
196 	ClassDB::bind_method(D_METHOD("is_ignore_camera_zoom"), &ParallaxBackground::is_ignore_camera_zoom);
197 
198 	ADD_GROUP("Scroll", "scroll_");
199 	ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_offset"), "set_scroll_offset", "get_scroll_offset");
200 	ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_base_offset"), "set_scroll_base_offset", "get_scroll_base_offset");
201 	ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_base_scale"), "set_scroll_base_scale", "get_scroll_base_scale");
202 	ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_limit_begin"), "set_limit_begin", "get_limit_begin");
203 	ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_limit_end"), "set_limit_end", "get_limit_end");
204 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_ignore_camera_zoom"), "set_ignore_camera_zoom", "is_ignore_camera_zoom");
205 }
206 
ParallaxBackground()207 ParallaxBackground::ParallaxBackground() {
208 
209 	scale = 1.0;
210 	set_layer(-100); //behind all by default
211 
212 	base_scale = Vector2(1, 1);
213 	ignore_camera_zoom = false;
214 }
215