1 | /**************************************************************************/ |
2 | /* path_2d.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 "path_2d.h" |
32 | |
33 | #include "core/math/geometry_2d.h" |
34 | #include "scene/main/timer.h" |
35 | |
36 | #ifdef TOOLS_ENABLED |
37 | #include "editor/editor_scale.h" |
38 | #endif |
39 | |
40 | #ifdef TOOLS_ENABLED |
41 | Rect2 Path2D::_edit_get_rect() const { |
42 | if (!curve.is_valid() || curve->get_point_count() == 0) { |
43 | return Rect2(0, 0, 0, 0); |
44 | } |
45 | |
46 | Rect2 aabb = Rect2(curve->get_point_position(0), Vector2(0, 0)); |
47 | |
48 | for (int i = 0; i < curve->get_point_count(); i++) { |
49 | for (int j = 0; j <= 8; j++) { |
50 | real_t frac = j / 8.0; |
51 | Vector2 p = curve->sample(i, frac); |
52 | aabb.expand_to(p); |
53 | } |
54 | } |
55 | |
56 | return aabb; |
57 | } |
58 | |
59 | bool Path2D::_edit_use_rect() const { |
60 | return curve.is_valid() && curve->get_point_count() != 0; |
61 | } |
62 | |
63 | bool Path2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const { |
64 | if (curve.is_null()) { |
65 | return false; |
66 | } |
67 | |
68 | for (int i = 0; i < curve->get_point_count(); i++) { |
69 | Vector2 s[2]; |
70 | s[0] = curve->get_point_position(i); |
71 | |
72 | for (int j = 1; j <= 8; j++) { |
73 | real_t frac = j / 8.0; |
74 | s[1] = curve->sample(i, frac); |
75 | |
76 | Vector2 p = Geometry2D::get_closest_point_to_segment(p_point, s); |
77 | if (p.distance_to(p_point) <= p_tolerance) { |
78 | return true; |
79 | } |
80 | |
81 | s[0] = s[1]; |
82 | } |
83 | } |
84 | |
85 | return false; |
86 | } |
87 | #endif |
88 | |
89 | void Path2D::_notification(int p_what) { |
90 | switch (p_what) { |
91 | // Draw the curve if path debugging is enabled. |
92 | case NOTIFICATION_DRAW: { |
93 | if (!curve.is_valid()) { |
94 | break; |
95 | } |
96 | |
97 | if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_paths_hint()) { |
98 | return; |
99 | } |
100 | |
101 | if (curve->get_point_count() < 2) { |
102 | return; |
103 | } |
104 | |
105 | #ifdef TOOLS_ENABLED |
106 | const real_t line_width = get_tree()->get_debug_paths_width() * EDSCALE; |
107 | #else |
108 | const real_t line_width = get_tree()->get_debug_paths_width(); |
109 | #endif |
110 | real_t interval = 10; |
111 | const real_t length = curve->get_baked_length(); |
112 | |
113 | if (length > CMP_EPSILON) { |
114 | const int sample_count = int(length / interval) + 2; |
115 | interval = length / (sample_count - 1); // Recalculate real interval length. |
116 | |
117 | Vector<Transform2D> frames; |
118 | frames.resize(sample_count); |
119 | |
120 | { |
121 | Transform2D *w = frames.ptrw(); |
122 | |
123 | for (int i = 0; i < sample_count; i++) { |
124 | w[i] = curve->sample_baked_with_rotation(i * interval, false); |
125 | } |
126 | } |
127 | |
128 | const Transform2D *r = frames.ptr(); |
129 | // Draw curve segments |
130 | { |
131 | PackedVector2Array v2p; |
132 | v2p.resize(sample_count); |
133 | Vector2 *w = v2p.ptrw(); |
134 | |
135 | for (int i = 0; i < sample_count; i++) { |
136 | w[i] = r[i].get_origin(); |
137 | } |
138 | draw_polyline(v2p, get_tree()->get_debug_paths_color(), line_width, false); |
139 | } |
140 | |
141 | // Draw fish bones |
142 | { |
143 | PackedVector2Array v2p; |
144 | v2p.resize(3); |
145 | Vector2 *w = v2p.ptrw(); |
146 | |
147 | for (int i = 0; i < sample_count; i++) { |
148 | const Vector2 p = r[i].get_origin(); |
149 | const Vector2 side = r[i].columns[0]; |
150 | const Vector2 forward = r[i].columns[1]; |
151 | |
152 | // Fish Bone. |
153 | w[0] = p + (side - forward) * 5; |
154 | w[1] = p; |
155 | w[2] = p + (-side - forward) * 5; |
156 | |
157 | draw_polyline(v2p, get_tree()->get_debug_paths_color(), line_width * 0.5, false); |
158 | } |
159 | } |
160 | } |
161 | } break; |
162 | } |
163 | } |
164 | |
165 | void Path2D::_curve_changed() { |
166 | if (!is_inside_tree()) { |
167 | return; |
168 | } |
169 | |
170 | if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_paths_hint()) { |
171 | return; |
172 | } |
173 | |
174 | queue_redraw(); |
175 | for (int i = 0; i < get_child_count(); i++) { |
176 | PathFollow2D *follow = Object::cast_to<PathFollow2D>(get_child(i)); |
177 | if (follow) { |
178 | follow->path_changed(); |
179 | } |
180 | } |
181 | } |
182 | |
183 | void Path2D::set_curve(const Ref<Curve2D> &p_curve) { |
184 | if (curve.is_valid()) { |
185 | curve->disconnect_changed(callable_mp(this, &Path2D::_curve_changed)); |
186 | } |
187 | |
188 | curve = p_curve; |
189 | |
190 | if (curve.is_valid()) { |
191 | curve->connect_changed(callable_mp(this, &Path2D::_curve_changed)); |
192 | } |
193 | |
194 | _curve_changed(); |
195 | } |
196 | |
197 | Ref<Curve2D> Path2D::get_curve() const { |
198 | return curve; |
199 | } |
200 | |
201 | void Path2D::_bind_methods() { |
202 | ClassDB::bind_method(D_METHOD("set_curve" , "curve" ), &Path2D::set_curve); |
203 | ClassDB::bind_method(D_METHOD("get_curve" ), &Path2D::get_curve); |
204 | |
205 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve" , PROPERTY_HINT_RESOURCE_TYPE, "Curve2D" , PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_curve" , "get_curve" ); |
206 | } |
207 | |
208 | ///////////////////////////////////////////////////////////////////////////////// |
209 | |
210 | void PathFollow2D::path_changed() { |
211 | if (update_timer && !update_timer->is_stopped()) { |
212 | update_timer->start(); |
213 | } else { |
214 | _update_transform(); |
215 | } |
216 | } |
217 | |
218 | void PathFollow2D::_update_transform() { |
219 | if (!path) { |
220 | return; |
221 | } |
222 | |
223 | Ref<Curve2D> c = path->get_curve(); |
224 | if (!c.is_valid()) { |
225 | return; |
226 | } |
227 | |
228 | real_t path_length = c->get_baked_length(); |
229 | if (path_length == 0) { |
230 | return; |
231 | } |
232 | |
233 | if (rotates) { |
234 | Transform2D xform = c->sample_baked_with_rotation(progress, cubic); |
235 | xform.translate_local(v_offset, h_offset); |
236 | set_rotation(xform[1].angle()); |
237 | set_position(xform[2]); |
238 | } else { |
239 | Vector2 pos = c->sample_baked(progress, cubic); |
240 | pos.x += h_offset; |
241 | pos.y += v_offset; |
242 | set_position(pos); |
243 | } |
244 | } |
245 | |
246 | void PathFollow2D::_notification(int p_what) { |
247 | switch (p_what) { |
248 | case NOTIFICATION_READY: { |
249 | if (Engine::get_singleton()->is_editor_hint()) { |
250 | update_timer = memnew(Timer); |
251 | update_timer->set_wait_time(0.2); |
252 | update_timer->set_one_shot(true); |
253 | update_timer->connect("timeout" , callable_mp(this, &PathFollow2D::_update_transform)); |
254 | add_child(update_timer, false, Node::INTERNAL_MODE_BACK); |
255 | } |
256 | } break; |
257 | |
258 | case NOTIFICATION_ENTER_TREE: { |
259 | path = Object::cast_to<Path2D>(get_parent()); |
260 | if (path) { |
261 | _update_transform(); |
262 | } |
263 | } break; |
264 | |
265 | case NOTIFICATION_EXIT_TREE: { |
266 | path = nullptr; |
267 | } break; |
268 | } |
269 | } |
270 | |
271 | void PathFollow2D::set_cubic_interpolation_enabled(bool p_enabled) { |
272 | cubic = p_enabled; |
273 | } |
274 | |
275 | bool PathFollow2D::is_cubic_interpolation_enabled() const { |
276 | return cubic; |
277 | } |
278 | |
279 | void PathFollow2D::_validate_property(PropertyInfo &p_property) const { |
280 | if (p_property.name == "offset" ) { |
281 | real_t max = 10000.0; |
282 | if (path && path->get_curve().is_valid()) { |
283 | max = path->get_curve()->get_baked_length(); |
284 | } |
285 | |
286 | p_property.hint_string = "0," + rtos(max) + ",0.01,or_less,or_greater" ; |
287 | } |
288 | } |
289 | |
290 | PackedStringArray PathFollow2D::get_configuration_warnings() const { |
291 | PackedStringArray warnings = Node::get_configuration_warnings(); |
292 | |
293 | if (is_visible_in_tree() && is_inside_tree()) { |
294 | if (!Object::cast_to<Path2D>(get_parent())) { |
295 | warnings.push_back(RTR("PathFollow2D only works when set as a child of a Path2D node." )); |
296 | } |
297 | } |
298 | |
299 | return warnings; |
300 | } |
301 | |
302 | void PathFollow2D::_bind_methods() { |
303 | ClassDB::bind_method(D_METHOD("set_progress" , "progress" ), &PathFollow2D::set_progress); |
304 | ClassDB::bind_method(D_METHOD("get_progress" ), &PathFollow2D::get_progress); |
305 | |
306 | ClassDB::bind_method(D_METHOD("set_h_offset" , "h_offset" ), &PathFollow2D::set_h_offset); |
307 | ClassDB::bind_method(D_METHOD("get_h_offset" ), &PathFollow2D::get_h_offset); |
308 | |
309 | ClassDB::bind_method(D_METHOD("set_v_offset" , "v_offset" ), &PathFollow2D::set_v_offset); |
310 | ClassDB::bind_method(D_METHOD("get_v_offset" ), &PathFollow2D::get_v_offset); |
311 | |
312 | ClassDB::bind_method(D_METHOD("set_progress_ratio" , "ratio" ), &PathFollow2D::set_progress_ratio); |
313 | ClassDB::bind_method(D_METHOD("get_progress_ratio" ), &PathFollow2D::get_progress_ratio); |
314 | |
315 | ClassDB::bind_method(D_METHOD("set_rotates" , "enabled" ), &PathFollow2D::set_rotation_enabled); |
316 | ClassDB::bind_method(D_METHOD("is_rotating" ), &PathFollow2D::is_rotation_enabled); |
317 | |
318 | ClassDB::bind_method(D_METHOD("set_cubic_interpolation" , "enabled" ), &PathFollow2D::set_cubic_interpolation_enabled); |
319 | ClassDB::bind_method(D_METHOD("get_cubic_interpolation" ), &PathFollow2D::is_cubic_interpolation_enabled); |
320 | |
321 | ClassDB::bind_method(D_METHOD("set_loop" , "loop" ), &PathFollow2D::set_loop); |
322 | ClassDB::bind_method(D_METHOD("has_loop" ), &PathFollow2D::has_loop); |
323 | |
324 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "progress" , PROPERTY_HINT_RANGE, "0,10000,0.01,or_less,or_greater,suffix:px" ), "set_progress" , "get_progress" ); |
325 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "progress_ratio" , PROPERTY_HINT_RANGE, "0,1,0.0001,or_less,or_greater" , PROPERTY_USAGE_EDITOR), "set_progress_ratio" , "get_progress_ratio" ); |
326 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "h_offset" ), "set_h_offset" , "get_h_offset" ); |
327 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "v_offset" ), "set_v_offset" , "get_v_offset" ); |
328 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rotates" ), "set_rotates" , "is_rotating" ); |
329 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cubic_interp" ), "set_cubic_interpolation" , "get_cubic_interpolation" ); |
330 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop" ), "set_loop" , "has_loop" ); |
331 | } |
332 | |
333 | void PathFollow2D::set_progress(real_t p_progress) { |
334 | ERR_FAIL_COND(!isfinite(p_progress)); |
335 | progress = p_progress; |
336 | if (path) { |
337 | if (path->get_curve().is_valid()) { |
338 | real_t path_length = path->get_curve()->get_baked_length(); |
339 | |
340 | if (loop && path_length) { |
341 | progress = Math::fposmod(progress, path_length); |
342 | if (!Math::is_zero_approx(p_progress) && Math::is_zero_approx(progress)) { |
343 | progress = path_length; |
344 | } |
345 | } else { |
346 | progress = CLAMP(progress, 0, path_length); |
347 | } |
348 | } |
349 | |
350 | _update_transform(); |
351 | } |
352 | } |
353 | |
354 | void PathFollow2D::set_h_offset(real_t p_h_offset) { |
355 | h_offset = p_h_offset; |
356 | if (path) { |
357 | _update_transform(); |
358 | } |
359 | } |
360 | |
361 | real_t PathFollow2D::get_h_offset() const { |
362 | return h_offset; |
363 | } |
364 | |
365 | void PathFollow2D::set_v_offset(real_t p_v_offset) { |
366 | v_offset = p_v_offset; |
367 | if (path) { |
368 | _update_transform(); |
369 | } |
370 | } |
371 | |
372 | real_t PathFollow2D::get_v_offset() const { |
373 | return v_offset; |
374 | } |
375 | |
376 | real_t PathFollow2D::get_progress() const { |
377 | return progress; |
378 | } |
379 | |
380 | void PathFollow2D::set_progress_ratio(real_t p_ratio) { |
381 | if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length()) { |
382 | set_progress(p_ratio * path->get_curve()->get_baked_length()); |
383 | } |
384 | } |
385 | |
386 | real_t PathFollow2D::get_progress_ratio() const { |
387 | if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length()) { |
388 | return get_progress() / path->get_curve()->get_baked_length(); |
389 | } else { |
390 | return 0; |
391 | } |
392 | } |
393 | |
394 | void PathFollow2D::set_rotation_enabled(bool p_enabled) { |
395 | rotates = p_enabled; |
396 | _update_transform(); |
397 | } |
398 | |
399 | bool PathFollow2D::is_rotation_enabled() const { |
400 | return rotates; |
401 | } |
402 | |
403 | void PathFollow2D::set_loop(bool p_loop) { |
404 | loop = p_loop; |
405 | } |
406 | |
407 | bool PathFollow2D::has_loop() const { |
408 | return loop; |
409 | } |
410 | |