Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/2d/path_2d.cpp
9896 views
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/themes/editor_scale.h"
38
#endif
39
40
#ifdef DEBUG_ENABLED
41
Rect2 Path2D::_edit_get_rect() const {
42
if (curve.is_null() || 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 segment_a = curve->get_point_position(i);
70
71
for (int j = 1; j <= 8; j++) {
72
real_t frac = j / 8.0;
73
const Vector2 segment_b = curve->sample(i, frac);
74
75
Vector2 p = Geometry2D::get_closest_point_to_segment(p_point, segment_a, segment_b);
76
if (p.distance_to(p_point) <= p_tolerance) {
77
return true;
78
}
79
80
segment_a = segment_b;
81
}
82
}
83
84
return false;
85
}
86
#endif
87
88
void Path2D::_notification(int p_what) {
89
switch (p_what) {
90
case NOTIFICATION_ENTER_TREE: {
91
#ifdef DEBUG_ENABLED
92
_debug_create();
93
#endif
94
} break;
95
96
case NOTIFICATION_EXIT_TREE: {
97
#ifdef DEBUG_ENABLED
98
_debug_free();
99
#endif
100
} break;
101
// Draw the curve if path debugging is enabled.
102
case NOTIFICATION_DRAW: {
103
#ifdef DEBUG_ENABLED
104
_debug_update();
105
#endif
106
} break;
107
}
108
}
109
110
#ifdef DEBUG_ENABLED
111
void Path2D::_debug_create() {
112
ERR_FAIL_NULL(RS::get_singleton());
113
114
if (debug_mesh_rid.is_null()) {
115
debug_mesh_rid = RS::get_singleton()->mesh_create();
116
}
117
118
if (debug_instance.is_null()) {
119
debug_instance = RS::get_singleton()->instance_create();
120
}
121
122
RS::get_singleton()->instance_set_base(debug_instance, debug_mesh_rid);
123
RS::get_singleton()->instance_geometry_set_cast_shadows_setting(debug_instance, RS::SHADOW_CASTING_SETTING_OFF);
124
}
125
126
void Path2D::_debug_free() {
127
ERR_FAIL_NULL(RS::get_singleton());
128
129
if (debug_instance.is_valid()) {
130
RS::get_singleton()->free(debug_instance);
131
debug_instance = RID();
132
}
133
if (debug_mesh_rid.is_valid()) {
134
RS::get_singleton()->free(debug_mesh_rid);
135
debug_mesh_rid = RID();
136
}
137
}
138
139
void Path2D::_debug_update() {
140
ERR_FAIL_NULL(RS::get_singleton());
141
142
RenderingServer *rs = RS::get_singleton();
143
144
ERR_FAIL_NULL(SceneTree::get_singleton());
145
ERR_FAIL_NULL(RenderingServer::get_singleton());
146
147
const bool path_debug_enabled = (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_paths_hint());
148
149
if (!path_debug_enabled) {
150
_debug_free();
151
return;
152
}
153
154
if (debug_mesh_rid.is_null() || debug_instance.is_null()) {
155
_debug_create();
156
}
157
158
rs->mesh_clear(debug_mesh_rid);
159
160
if (curve.is_null()) {
161
return;
162
}
163
if (curve->get_point_count() < 2) {
164
return;
165
}
166
167
const real_t baked_length = curve->get_baked_length();
168
169
if (baked_length <= CMP_EPSILON) {
170
return;
171
}
172
173
const Color debug_color = get_tree()->get_debug_paths_color();
174
175
bool debug_paths_show_fish_bones = true;
176
177
real_t sample_interval = 10.0;
178
179
const int sample_count = int(baked_length / sample_interval) + 2;
180
sample_interval = baked_length / (sample_count - 1); // Recalculate real interval length.
181
182
Vector<Transform2D> samples;
183
samples.resize(sample_count);
184
Transform2D *samples_ptrw = samples.ptrw();
185
186
for (int i = 0; i < sample_count; i++) {
187
samples_ptrw[i] = curve->sample_baked_with_rotation(i * sample_interval, false);
188
}
189
190
const Transform2D *samples_ptr = samples.ptr();
191
192
// Draw curve segments
193
{
194
Vector<Vector2> ribbon;
195
ribbon.resize(sample_count);
196
Vector2 *ribbon_ptrw = ribbon.ptrw();
197
198
for (int i = 0; i < sample_count; i++) {
199
ribbon_ptrw[i] = samples_ptr[i].get_origin();
200
}
201
202
Array ribbon_array;
203
ribbon_array.resize(Mesh::ARRAY_MAX);
204
ribbon_array[Mesh::ARRAY_VERTEX] = ribbon;
205
Vector<Color> ribbon_color;
206
ribbon_color.resize(ribbon.size());
207
ribbon_color.fill(debug_color);
208
ribbon_array[Mesh::ARRAY_COLOR] = ribbon_color;
209
210
rs->mesh_add_surface_from_arrays(debug_mesh_rid, RS::PRIMITIVE_LINE_STRIP, ribbon_array, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);
211
}
212
213
// Render path fish bones.
214
if (debug_paths_show_fish_bones) {
215
int fish_bones_interval = 4;
216
217
const int vertex_per_bone = 4;
218
Vector<Vector2> bones;
219
bones.resize(sample_count * vertex_per_bone);
220
Vector2 *bones_ptrw = bones.ptrw();
221
222
for (int i = 0; i < sample_count; i += fish_bones_interval) {
223
const Transform2D &sample_transform = samples_ptr[i];
224
225
const Vector2 point = sample_transform.get_origin();
226
const Vector2 &side = sample_transform.columns[1];
227
const Vector2 &forward = sample_transform.columns[0];
228
229
const int bone_idx = i * vertex_per_bone;
230
231
bones_ptrw[bone_idx] = point;
232
bones_ptrw[bone_idx + 1] = point + (side - forward) * 5;
233
bones_ptrw[bone_idx + 2] = point;
234
bones_ptrw[bone_idx + 3] = point + (-side - forward) * 5;
235
}
236
237
Array bone_array;
238
bone_array.resize(Mesh::ARRAY_MAX);
239
bone_array[Mesh::ARRAY_VERTEX] = bones;
240
Vector<Color> bones_color;
241
bones_color.resize(bones.size());
242
bones_color.fill(debug_color);
243
bone_array[Mesh::ARRAY_COLOR] = bones_color;
244
245
rs->mesh_add_surface_from_arrays(debug_mesh_rid, RS::PRIMITIVE_LINES, bone_array, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES);
246
}
247
248
rs->canvas_item_clear(get_canvas_item());
249
rs->canvas_item_add_mesh(get_canvas_item(), debug_mesh_rid, Transform2D());
250
}
251
#endif // DEBUG_ENABLED
252
253
void Path2D::_curve_changed() {
254
if (!is_inside_tree()) {
255
return;
256
}
257
258
for (int i = 0; i < get_child_count(); i++) {
259
PathFollow2D *follow = Object::cast_to<PathFollow2D>(get_child(i));
260
if (follow) {
261
follow->path_changed();
262
}
263
}
264
265
if (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_paths_hint()) {
266
queue_redraw();
267
}
268
}
269
270
void Path2D::set_curve(const Ref<Curve2D> &p_curve) {
271
if (curve.is_valid()) {
272
curve->disconnect_changed(callable_mp(this, &Path2D::_curve_changed));
273
}
274
275
curve = p_curve;
276
277
if (curve.is_valid()) {
278
curve->connect_changed(callable_mp(this, &Path2D::_curve_changed));
279
}
280
281
_curve_changed();
282
}
283
284
Ref<Curve2D> Path2D::get_curve() const {
285
return curve;
286
}
287
288
void Path2D::_bind_methods() {
289
ClassDB::bind_method(D_METHOD("set_curve", "curve"), &Path2D::set_curve);
290
ClassDB::bind_method(D_METHOD("get_curve"), &Path2D::get_curve);
291
292
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_curve", "get_curve");
293
}
294
295
/////////////////////////////////////////////////////////////////////////////////
296
297
void PathFollow2D::path_changed() {
298
if (update_timer && !update_timer->is_stopped()) {
299
update_timer->start();
300
} else {
301
_update_transform();
302
}
303
}
304
305
void PathFollow2D::_update_transform() {
306
if (!path) {
307
return;
308
}
309
310
Ref<Curve2D> c = path->get_curve();
311
if (c.is_null()) {
312
return;
313
}
314
315
real_t path_length = c->get_baked_length();
316
if (path_length == 0) {
317
return;
318
}
319
320
if (rotates) {
321
Transform2D xform = c->sample_baked_with_rotation(progress, cubic);
322
xform.translate_local(h_offset, v_offset);
323
set_rotation(xform[0].angle());
324
set_position(xform[2]);
325
} else {
326
Vector2 pos = c->sample_baked(progress, cubic);
327
pos.x += h_offset;
328
pos.y += v_offset;
329
set_position(pos);
330
}
331
}
332
333
void PathFollow2D::_notification(int p_what) {
334
switch (p_what) {
335
case NOTIFICATION_READY: {
336
if (Engine::get_singleton()->is_editor_hint()) {
337
update_timer = memnew(Timer);
338
update_timer->set_wait_time(0.2);
339
update_timer->set_one_shot(true);
340
update_timer->connect("timeout", callable_mp(this, &PathFollow2D::_update_transform));
341
add_child(update_timer, false, Node::INTERNAL_MODE_BACK);
342
}
343
} break;
344
345
case NOTIFICATION_ENTER_TREE: {
346
path = Object::cast_to<Path2D>(get_parent());
347
if (path) {
348
_update_transform();
349
}
350
} break;
351
352
case NOTIFICATION_EXIT_TREE: {
353
path = nullptr;
354
} break;
355
}
356
}
357
358
void PathFollow2D::set_cubic_interpolation_enabled(bool p_enabled) {
359
cubic = p_enabled;
360
}
361
362
bool PathFollow2D::is_cubic_interpolation_enabled() const {
363
return cubic;
364
}
365
366
void PathFollow2D::_validate_property(PropertyInfo &p_property) const {
367
if (!Engine::get_singleton()->is_editor_hint()) {
368
return;
369
}
370
if (p_property.name == "offset") {
371
real_t max = 10000.0;
372
if (path && path->get_curve().is_valid()) {
373
max = path->get_curve()->get_baked_length();
374
}
375
376
p_property.hint_string = "0," + rtos(max) + ",0.01,or_less,or_greater";
377
}
378
}
379
380
PackedStringArray PathFollow2D::get_configuration_warnings() const {
381
PackedStringArray warnings = Node2D::get_configuration_warnings();
382
383
if (is_visible_in_tree() && is_inside_tree()) {
384
if (!Object::cast_to<Path2D>(get_parent())) {
385
warnings.push_back(RTR("PathFollow2D only works when set as a child of a Path2D node."));
386
}
387
}
388
389
return warnings;
390
}
391
392
void PathFollow2D::_bind_methods() {
393
ClassDB::bind_method(D_METHOD("set_progress", "progress"), &PathFollow2D::set_progress);
394
ClassDB::bind_method(D_METHOD("get_progress"), &PathFollow2D::get_progress);
395
396
ClassDB::bind_method(D_METHOD("set_h_offset", "h_offset"), &PathFollow2D::set_h_offset);
397
ClassDB::bind_method(D_METHOD("get_h_offset"), &PathFollow2D::get_h_offset);
398
399
ClassDB::bind_method(D_METHOD("set_v_offset", "v_offset"), &PathFollow2D::set_v_offset);
400
ClassDB::bind_method(D_METHOD("get_v_offset"), &PathFollow2D::get_v_offset);
401
402
ClassDB::bind_method(D_METHOD("set_progress_ratio", "ratio"), &PathFollow2D::set_progress_ratio);
403
ClassDB::bind_method(D_METHOD("get_progress_ratio"), &PathFollow2D::get_progress_ratio);
404
405
ClassDB::bind_method(D_METHOD("set_rotates", "enabled"), &PathFollow2D::set_rotation_enabled);
406
ClassDB::bind_method(D_METHOD("is_rotating"), &PathFollow2D::is_rotation_enabled);
407
408
ClassDB::bind_method(D_METHOD("set_cubic_interpolation", "enabled"), &PathFollow2D::set_cubic_interpolation_enabled);
409
ClassDB::bind_method(D_METHOD("get_cubic_interpolation"), &PathFollow2D::is_cubic_interpolation_enabled);
410
411
ClassDB::bind_method(D_METHOD("set_loop", "loop"), &PathFollow2D::set_loop);
412
ClassDB::bind_method(D_METHOD("has_loop"), &PathFollow2D::has_loop);
413
414
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "progress", PROPERTY_HINT_RANGE, "0,10000,0.01,or_less,or_greater,suffix:px"), "set_progress", "get_progress");
415
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");
416
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "h_offset"), "set_h_offset", "get_h_offset");
417
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "v_offset"), "set_v_offset", "get_v_offset");
418
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rotates"), "set_rotates", "is_rotating");
419
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cubic_interp"), "set_cubic_interpolation", "get_cubic_interpolation");
420
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");
421
}
422
423
void PathFollow2D::set_progress(real_t p_progress) {
424
ERR_FAIL_COND(!std::isfinite(p_progress));
425
progress = p_progress;
426
if (path) {
427
if (path->get_curve().is_valid()) {
428
real_t path_length = path->get_curve()->get_baked_length();
429
430
if (loop && path_length) {
431
progress = Math::fposmod(progress, path_length);
432
if (!Math::is_zero_approx(p_progress) && Math::is_zero_approx(progress)) {
433
progress = path_length;
434
}
435
} else {
436
progress = CLAMP(progress, 0, path_length);
437
}
438
}
439
440
_update_transform();
441
}
442
}
443
444
void PathFollow2D::set_h_offset(real_t p_h_offset) {
445
h_offset = p_h_offset;
446
if (path) {
447
_update_transform();
448
}
449
}
450
451
real_t PathFollow2D::get_h_offset() const {
452
return h_offset;
453
}
454
455
void PathFollow2D::set_v_offset(real_t p_v_offset) {
456
v_offset = p_v_offset;
457
if (path) {
458
_update_transform();
459
}
460
}
461
462
real_t PathFollow2D::get_v_offset() const {
463
return v_offset;
464
}
465
466
real_t PathFollow2D::get_progress() const {
467
return progress;
468
}
469
470
void PathFollow2D::set_progress_ratio(real_t p_ratio) {
471
ERR_FAIL_NULL_MSG(path, "Can only set progress ratio on a PathFollow2D that is the child of a Path2D which is itself part of the scene tree.");
472
ERR_FAIL_COND_MSG(path->get_curve().is_null(), "Can't set progress ratio on a PathFollow2D that does not have a Curve.");
473
ERR_FAIL_COND_MSG(!path->get_curve()->get_baked_length(), "Can't set progress ratio on a PathFollow2D that has a 0 length curve.");
474
set_progress(p_ratio * path->get_curve()->get_baked_length());
475
}
476
477
real_t PathFollow2D::get_progress_ratio() const {
478
if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length()) {
479
return get_progress() / path->get_curve()->get_baked_length();
480
} else {
481
return 0;
482
}
483
}
484
485
void PathFollow2D::set_rotation_enabled(bool p_enabled) {
486
rotates = p_enabled;
487
_update_transform();
488
}
489
490
bool PathFollow2D::is_rotation_enabled() const {
491
return rotates;
492
}
493
494
void PathFollow2D::set_loop(bool p_loop) {
495
loop = p_loop;
496
}
497
498
bool PathFollow2D::has_loop() const {
499
return loop;
500
}
501
502