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