1 /*************************************************************************/
2 /* path_2d.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 "path_2d.h"
32
33 #include "core/engine.h"
34 #include "scene/scene_string_names.h"
35
36 #ifdef TOOLS_ENABLED
37 #include "editor/editor_scale.h"
38 #endif
39
40 #ifdef TOOLS_ENABLED
_edit_get_rect() const41 Rect2 Path2D::_edit_get_rect() const {
42
43 if (!curve.is_valid() || curve->get_point_count() == 0)
44 return Rect2(0, 0, 0, 0);
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
50 for (int j = 0; j <= 8; j++) {
51
52 real_t frac = j / 8.0;
53 Vector2 p = curve->interpolate(i, frac);
54 aabb.expand_to(p);
55 }
56 }
57
58 return aabb;
59 }
60
_edit_use_rect() const61 bool Path2D::_edit_use_rect() const {
62 return curve.is_valid() && curve->get_point_count() != 0;
63 }
64
_edit_is_selected_on_click(const Point2 & p_point,double p_tolerance) const65 bool Path2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
66
67 if (curve.is_null()) {
68 return false;
69 }
70
71 for (int i = 0; i < curve->get_point_count(); i++) {
72 Vector2 s[2];
73 s[0] = curve->get_point_position(i);
74
75 for (int j = 1; j <= 8; j++) {
76 real_t frac = j / 8.0;
77 s[1] = curve->interpolate(i, frac);
78
79 Vector2 p = Geometry::get_closest_point_to_segment_2d(p_point, s);
80 if (p.distance_to(p_point) <= p_tolerance)
81 return true;
82
83 s[0] = s[1];
84 }
85 }
86
87 return false;
88 }
89 #endif
90
_notification(int p_what)91 void Path2D::_notification(int p_what) {
92
93 if (p_what == NOTIFICATION_DRAW && curve.is_valid()) {
94 //draw the curve!!
95
96 if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_navigation_hint()) {
97 return;
98 }
99
100 #ifdef TOOLS_ENABLED
101 const float line_width = 2 * EDSCALE;
102 #else
103 const float line_width = 2;
104 #endif
105 const Color color = Color(1.0, 1.0, 1.0, 1.0);
106
107 for (int i = 0; i < curve->get_point_count(); i++) {
108
109 Vector2 prev_p = curve->get_point_position(i);
110
111 for (int j = 1; j <= 8; j++) {
112
113 real_t frac = j / 8.0;
114 Vector2 p = curve->interpolate(i, frac);
115 draw_line(prev_p, p, color, line_width, true);
116 prev_p = p;
117 }
118 }
119 }
120 }
121
_curve_changed()122 void Path2D::_curve_changed() {
123 if (!is_inside_tree()) {
124 return;
125 }
126
127 if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_navigation_hint()) {
128 return;
129 }
130
131 update();
132 }
133
set_curve(const Ref<Curve2D> & p_curve)134 void Path2D::set_curve(const Ref<Curve2D> &p_curve) {
135
136 if (curve.is_valid()) {
137 curve->disconnect("changed", this, "_curve_changed");
138 }
139
140 curve = p_curve;
141
142 if (curve.is_valid()) {
143 curve->connect("changed", this, "_curve_changed");
144 }
145
146 _curve_changed();
147 }
148
get_curve() const149 Ref<Curve2D> Path2D::get_curve() const {
150
151 return curve;
152 }
153
_bind_methods()154 void Path2D::_bind_methods() {
155
156 ClassDB::bind_method(D_METHOD("set_curve", "curve"), &Path2D::set_curve);
157 ClassDB::bind_method(D_METHOD("get_curve"), &Path2D::get_curve);
158 ClassDB::bind_method(D_METHOD("_curve_changed"), &Path2D::_curve_changed);
159
160 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve2D"), "set_curve", "get_curve");
161 }
162
Path2D()163 Path2D::Path2D() {
164
165 set_curve(Ref<Curve2D>(memnew(Curve2D))); //create one by default
166 set_self_modulate(Color(0.5, 0.6, 1.0, 0.7));
167 }
168
169 /////////////////////////////////////////////////////////////////////////////////
170
_update_transform()171 void PathFollow2D::_update_transform() {
172
173 if (!path)
174 return;
175
176 Ref<Curve2D> c = path->get_curve();
177 if (!c.is_valid())
178 return;
179
180 float path_length = c->get_baked_length();
181 if (path_length == 0) {
182 return;
183 }
184 Vector2 pos = c->interpolate_baked(offset, cubic);
185
186 if (rotate) {
187 float ahead = offset + lookahead;
188
189 if (loop && ahead >= path_length) {
190 // If our lookahead will loop, we need to check if the path is closed.
191 int point_count = c->get_point_count();
192 if (point_count > 0) {
193 Vector2 start_point = c->get_point_position(0);
194 Vector2 end_point = c->get_point_position(point_count - 1);
195 if (start_point == end_point) {
196 // Since the path is closed we want to 'smooth off'
197 // the corner at the start/end.
198 // So we wrap the lookahead back round.
199 ahead = Math::fmod(ahead, path_length);
200 }
201 }
202 }
203
204 Vector2 ahead_pos = c->interpolate_baked(ahead, cubic);
205
206 Vector2 tangent_to_curve;
207 if (ahead_pos == pos) {
208 // This will happen at the end of non-looping or non-closed paths.
209 // We'll try a look behind instead, in order to get a meaningful angle.
210 tangent_to_curve =
211 (pos - c->interpolate_baked(offset - lookahead, cubic)).normalized();
212 } else {
213 tangent_to_curve = (ahead_pos - pos).normalized();
214 }
215
216 Vector2 normal_of_curve = -tangent_to_curve.tangent();
217
218 pos += tangent_to_curve * h_offset;
219 pos += normal_of_curve * v_offset;
220
221 set_rotation(tangent_to_curve.angle());
222
223 } else {
224
225 pos.x += h_offset;
226 pos.y += v_offset;
227 }
228
229 set_position(pos);
230 }
231
_notification(int p_what)232 void PathFollow2D::_notification(int p_what) {
233
234 switch (p_what) {
235
236 case NOTIFICATION_ENTER_TREE: {
237
238 path = Object::cast_to<Path2D>(get_parent());
239 if (path) {
240 _update_transform();
241 }
242
243 } break;
244 case NOTIFICATION_EXIT_TREE: {
245
246 path = NULL;
247 } break;
248 }
249 }
250
set_cubic_interpolation(bool p_enable)251 void PathFollow2D::set_cubic_interpolation(bool p_enable) {
252
253 cubic = p_enable;
254 }
255
get_cubic_interpolation() const256 bool PathFollow2D::get_cubic_interpolation() const {
257
258 return cubic;
259 }
260
_validate_property(PropertyInfo & property) const261 void PathFollow2D::_validate_property(PropertyInfo &property) const {
262
263 if (property.name == "offset") {
264
265 float max = 10000;
266 if (path && path->get_curve().is_valid())
267 max = path->get_curve()->get_baked_length();
268
269 property.hint_string = "0," + rtos(max) + ",0.01,or_lesser,or_greater";
270 }
271 }
272
get_configuration_warning() const273 String PathFollow2D::get_configuration_warning() const {
274
275 if (!is_visible_in_tree() || !is_inside_tree())
276 return String();
277
278 if (!Object::cast_to<Path2D>(get_parent())) {
279 return TTR("PathFollow2D only works when set as a child of a Path2D node.");
280 }
281
282 return String();
283 }
284
_bind_methods()285 void PathFollow2D::_bind_methods() {
286
287 ClassDB::bind_method(D_METHOD("set_offset", "offset"), &PathFollow2D::set_offset);
288 ClassDB::bind_method(D_METHOD("get_offset"), &PathFollow2D::get_offset);
289
290 ClassDB::bind_method(D_METHOD("set_h_offset", "h_offset"), &PathFollow2D::set_h_offset);
291 ClassDB::bind_method(D_METHOD("get_h_offset"), &PathFollow2D::get_h_offset);
292
293 ClassDB::bind_method(D_METHOD("set_v_offset", "v_offset"), &PathFollow2D::set_v_offset);
294 ClassDB::bind_method(D_METHOD("get_v_offset"), &PathFollow2D::get_v_offset);
295
296 ClassDB::bind_method(D_METHOD("set_unit_offset", "unit_offset"), &PathFollow2D::set_unit_offset);
297 ClassDB::bind_method(D_METHOD("get_unit_offset"), &PathFollow2D::get_unit_offset);
298
299 ClassDB::bind_method(D_METHOD("set_rotate", "enable"), &PathFollow2D::set_rotate);
300 ClassDB::bind_method(D_METHOD("is_rotating"), &PathFollow2D::is_rotating);
301
302 ClassDB::bind_method(D_METHOD("set_cubic_interpolation", "enable"), &PathFollow2D::set_cubic_interpolation);
303 ClassDB::bind_method(D_METHOD("get_cubic_interpolation"), &PathFollow2D::get_cubic_interpolation);
304
305 ClassDB::bind_method(D_METHOD("set_loop", "loop"), &PathFollow2D::set_loop);
306 ClassDB::bind_method(D_METHOD("has_loop"), &PathFollow2D::has_loop);
307
308 ClassDB::bind_method(D_METHOD("set_lookahead", "lookahead"), &PathFollow2D::set_lookahead);
309 ClassDB::bind_method(D_METHOD("get_lookahead"), &PathFollow2D::get_lookahead);
310
311 ADD_PROPERTY(PropertyInfo(Variant::REAL, "offset", PROPERTY_HINT_RANGE, "0,10000,0.01,or_lesser,or_greater"), "set_offset", "get_offset");
312 ADD_PROPERTY(PropertyInfo(Variant::REAL, "unit_offset", PROPERTY_HINT_RANGE, "0,1,0.0001,or_lesser,or_greater", PROPERTY_USAGE_EDITOR), "set_unit_offset", "get_unit_offset");
313 ADD_PROPERTY(PropertyInfo(Variant::REAL, "h_offset"), "set_h_offset", "get_h_offset");
314 ADD_PROPERTY(PropertyInfo(Variant::REAL, "v_offset"), "set_v_offset", "get_v_offset");
315 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rotate"), "set_rotate", "is_rotating");
316 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cubic_interp"), "set_cubic_interpolation", "get_cubic_interpolation");
317 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");
318 ADD_PROPERTY(PropertyInfo(Variant::REAL, "lookahead", PROPERTY_HINT_RANGE, "0.001,1024.0,0.001"), "set_lookahead", "get_lookahead");
319 }
320
set_offset(float p_offset)321 void PathFollow2D::set_offset(float p_offset) {
322
323 offset = p_offset;
324 if (path) {
325 if (path->get_curve().is_valid()) {
326 float path_length = path->get_curve()->get_baked_length();
327
328 if (loop) {
329 offset = Math::fposmod(offset, path_length);
330 if (!Math::is_zero_approx(p_offset) && Math::is_zero_approx(offset)) {
331 offset = path_length;
332 }
333 } else {
334 offset = CLAMP(offset, 0, path_length);
335 }
336 }
337
338 _update_transform();
339 }
340 _change_notify("offset");
341 _change_notify("unit_offset");
342 }
343
set_h_offset(float p_h_offset)344 void PathFollow2D::set_h_offset(float p_h_offset) {
345
346 h_offset = p_h_offset;
347 if (path)
348 _update_transform();
349 }
350
get_h_offset() const351 float PathFollow2D::get_h_offset() const {
352
353 return h_offset;
354 }
355
set_v_offset(float p_v_offset)356 void PathFollow2D::set_v_offset(float p_v_offset) {
357
358 v_offset = p_v_offset;
359 if (path)
360 _update_transform();
361 }
362
get_v_offset() const363 float PathFollow2D::get_v_offset() const {
364
365 return v_offset;
366 }
367
get_offset() const368 float PathFollow2D::get_offset() const {
369
370 return offset;
371 }
372
set_unit_offset(float p_unit_offset)373 void PathFollow2D::set_unit_offset(float p_unit_offset) {
374
375 if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length())
376 set_offset(p_unit_offset * path->get_curve()->get_baked_length());
377 }
378
get_unit_offset() const379 float PathFollow2D::get_unit_offset() const {
380
381 if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length())
382 return get_offset() / path->get_curve()->get_baked_length();
383 else
384 return 0;
385 }
386
set_lookahead(float p_lookahead)387 void PathFollow2D::set_lookahead(float p_lookahead) {
388
389 lookahead = p_lookahead;
390 }
391
get_lookahead() const392 float PathFollow2D::get_lookahead() const {
393
394 return lookahead;
395 }
396
set_rotate(bool p_rotate)397 void PathFollow2D::set_rotate(bool p_rotate) {
398
399 rotate = p_rotate;
400 _update_transform();
401 }
402
is_rotating() const403 bool PathFollow2D::is_rotating() const {
404
405 return rotate;
406 }
407
set_loop(bool p_loop)408 void PathFollow2D::set_loop(bool p_loop) {
409
410 loop = p_loop;
411 }
412
has_loop() const413 bool PathFollow2D::has_loop() const {
414
415 return loop;
416 }
417
PathFollow2D()418 PathFollow2D::PathFollow2D() {
419
420 offset = 0;
421 h_offset = 0;
422 v_offset = 0;
423 path = NULL;
424 rotate = true;
425 cubic = true;
426 loop = true;
427 lookahead = 4;
428 }
429