1 | /**************************************************************************/ |
2 | /* parallax_background.cpp */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
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 | |
33 | #include "parallax_layer.h" |
34 | |
35 | void ParallaxBackground::_notification(int p_what) { |
36 | switch (p_what) { |
37 | case NOTIFICATION_ENTER_TREE: { |
38 | group_name = "__cameras_" + itos(get_viewport().get_id()); |
39 | add_to_group(group_name); |
40 | } break; |
41 | |
42 | case NOTIFICATION_EXIT_TREE: { |
43 | remove_from_group(group_name); |
44 | } break; |
45 | } |
46 | } |
47 | |
48 | void ParallaxBackground::_camera_moved(const Transform2D &p_transform, const Point2 &p_screen_offset) { |
49 | screen_offset = p_screen_offset; |
50 | |
51 | set_scroll_scale(p_transform.get_scale().dot(Vector2(0.5, 0.5))); |
52 | set_scroll_offset(p_transform.get_origin()); |
53 | } |
54 | |
55 | void ParallaxBackground::set_scroll_scale(real_t p_scale) { |
56 | scale = p_scale; |
57 | } |
58 | |
59 | real_t ParallaxBackground::get_scroll_scale() const { |
60 | return scale; |
61 | } |
62 | |
63 | void ParallaxBackground::set_scroll_offset(const Point2 &p_ofs) { |
64 | offset = p_ofs; |
65 | |
66 | _update_scroll(); |
67 | } |
68 | |
69 | void ParallaxBackground::_update_scroll() { |
70 | if (!is_inside_tree()) { |
71 | return; |
72 | } |
73 | |
74 | Vector2 scroll_ofs = base_offset + offset * base_scale; |
75 | |
76 | Size2 vps = get_viewport_size(); |
77 | |
78 | scroll_ofs = -scroll_ofs; |
79 | if (limit_begin.x < limit_end.x) { |
80 | if (scroll_ofs.x < limit_begin.x) { |
81 | scroll_ofs.x = limit_begin.x; |
82 | } else if (scroll_ofs.x + vps.x > limit_end.x) { |
83 | scroll_ofs.x = limit_end.x - vps.x; |
84 | } |
85 | } |
86 | |
87 | if (limit_begin.y < limit_end.y) { |
88 | if (scroll_ofs.y < limit_begin.y) { |
89 | scroll_ofs.y = limit_begin.y; |
90 | } else if (scroll_ofs.y + vps.y > limit_end.y) { |
91 | scroll_ofs.y = limit_end.y - vps.y; |
92 | } |
93 | } |
94 | scroll_ofs = -scroll_ofs; |
95 | |
96 | final_offset = scroll_ofs; |
97 | |
98 | for (int i = 0; i < get_child_count(); i++) { |
99 | ParallaxLayer *l = Object::cast_to<ParallaxLayer>(get_child(i)); |
100 | if (!l) { |
101 | continue; |
102 | } |
103 | |
104 | if (ignore_camera_zoom) { |
105 | l->set_base_offset_and_scale((scroll_ofs + screen_offset * (scale - 1)) / scale, 1.0); |
106 | } else { |
107 | l->set_base_offset_and_scale(scroll_ofs, scale); |
108 | } |
109 | } |
110 | } |
111 | |
112 | Point2 ParallaxBackground::get_scroll_offset() const { |
113 | return offset; |
114 | } |
115 | |
116 | void ParallaxBackground::set_scroll_base_offset(const Point2 &p_ofs) { |
117 | base_offset = p_ofs; |
118 | _update_scroll(); |
119 | } |
120 | |
121 | Point2 ParallaxBackground::get_scroll_base_offset() const { |
122 | return base_offset; |
123 | } |
124 | |
125 | void ParallaxBackground::set_scroll_base_scale(const Point2 &p_ofs) { |
126 | base_scale = p_ofs; |
127 | _update_scroll(); |
128 | } |
129 | |
130 | Point2 ParallaxBackground::get_scroll_base_scale() const { |
131 | return base_scale; |
132 | } |
133 | |
134 | void ParallaxBackground::set_limit_begin(const Point2 &p_ofs) { |
135 | limit_begin = p_ofs; |
136 | _update_scroll(); |
137 | } |
138 | |
139 | Point2 ParallaxBackground::get_limit_begin() const { |
140 | return limit_begin; |
141 | } |
142 | |
143 | void ParallaxBackground::set_limit_end(const Point2 &p_ofs) { |
144 | limit_end = p_ofs; |
145 | _update_scroll(); |
146 | } |
147 | |
148 | Point2 ParallaxBackground::get_limit_end() const { |
149 | return limit_end; |
150 | } |
151 | |
152 | void ParallaxBackground::set_ignore_camera_zoom(bool ignore) { |
153 | ignore_camera_zoom = ignore; |
154 | } |
155 | |
156 | bool ParallaxBackground::is_ignore_camera_zoom() { |
157 | return ignore_camera_zoom; |
158 | } |
159 | |
160 | Vector2 ParallaxBackground::get_final_offset() const { |
161 | return final_offset; |
162 | } |
163 | |
164 | void ParallaxBackground::_bind_methods() { |
165 | ClassDB::bind_method(D_METHOD("_camera_moved" ), &ParallaxBackground::_camera_moved); |
166 | ClassDB::bind_method(D_METHOD("set_scroll_offset" , "offset" ), &ParallaxBackground::set_scroll_offset); |
167 | ClassDB::bind_method(D_METHOD("get_scroll_offset" ), &ParallaxBackground::get_scroll_offset); |
168 | ClassDB::bind_method(D_METHOD("set_scroll_base_offset" , "offset" ), &ParallaxBackground::set_scroll_base_offset); |
169 | ClassDB::bind_method(D_METHOD("get_scroll_base_offset" ), &ParallaxBackground::get_scroll_base_offset); |
170 | ClassDB::bind_method(D_METHOD("set_scroll_base_scale" , "scale" ), &ParallaxBackground::set_scroll_base_scale); |
171 | ClassDB::bind_method(D_METHOD("get_scroll_base_scale" ), &ParallaxBackground::get_scroll_base_scale); |
172 | ClassDB::bind_method(D_METHOD("set_limit_begin" , "offset" ), &ParallaxBackground::set_limit_begin); |
173 | ClassDB::bind_method(D_METHOD("get_limit_begin" ), &ParallaxBackground::get_limit_begin); |
174 | ClassDB::bind_method(D_METHOD("set_limit_end" , "offset" ), &ParallaxBackground::set_limit_end); |
175 | ClassDB::bind_method(D_METHOD("get_limit_end" ), &ParallaxBackground::get_limit_end); |
176 | ClassDB::bind_method(D_METHOD("set_ignore_camera_zoom" , "ignore" ), &ParallaxBackground::set_ignore_camera_zoom); |
177 | ClassDB::bind_method(D_METHOD("is_ignore_camera_zoom" ), &ParallaxBackground::is_ignore_camera_zoom); |
178 | |
179 | ADD_GROUP("Scroll" , "scroll_" ); |
180 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_offset" , PROPERTY_HINT_NONE, "suffix:px" ), "set_scroll_offset" , "get_scroll_offset" ); |
181 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_base_offset" , PROPERTY_HINT_NONE, "suffix:px" ), "set_scroll_base_offset" , "get_scroll_base_offset" ); |
182 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_base_scale" , PROPERTY_HINT_LINK), "set_scroll_base_scale" , "get_scroll_base_scale" ); |
183 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_limit_begin" , PROPERTY_HINT_NONE, "suffix:px" ), "set_limit_begin" , "get_limit_begin" ); |
184 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_limit_end" , PROPERTY_HINT_NONE, "suffix:px" ), "set_limit_end" , "get_limit_end" ); |
185 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_ignore_camera_zoom" ), "set_ignore_camera_zoom" , "is_ignore_camera_zoom" ); |
186 | } |
187 | |
188 | ParallaxBackground::ParallaxBackground() { |
189 | set_layer(-100); //behind all by default |
190 | } |
191 | |