1 /*************************************************************************/
2 /* path_2d.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2019 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 #include "path_2d.h"
31 #include "scene/scene_string_names.h"
32
_notification(int p_what)33 void Path2D::_notification(int p_what) {
34
35 if (p_what == NOTIFICATION_DRAW && curve.is_valid()) {
36 //draw the curve!!
37
38 if (!get_tree()->is_editor_hint() && !get_tree()->is_debugging_navigation_hint()) {
39 return;
40 }
41
42 for (int i = 0; i < curve->get_point_count(); i++) {
43
44 Vector2 prev_p = curve->get_point_pos(i);
45
46 for (int j = 1; j <= 8; j++) {
47
48 real_t frac = j / 8.0;
49 Vector2 p = curve->interpolate(i, frac);
50 draw_line(prev_p, p, Color(0.5, 0.6, 1.0, 0.7), 2);
51 prev_p = p;
52 }
53 }
54 }
55 }
56
_curve_changed()57 void Path2D::_curve_changed() {
58
59 if (is_inside_tree() && get_tree()->is_editor_hint())
60 update();
61 }
62
set_curve(const Ref<Curve2D> & p_curve)63 void Path2D::set_curve(const Ref<Curve2D> &p_curve) {
64
65 if (curve.is_valid()) {
66 curve->disconnect("changed", this, "_curve_changed");
67 }
68
69 curve = p_curve;
70
71 if (curve.is_valid()) {
72 curve->connect("changed", this, "_curve_changed");
73 }
74
75 _curve_changed();
76 }
77
get_curve() const78 Ref<Curve2D> Path2D::get_curve() const {
79
80 return curve;
81 }
82
_bind_methods()83 void Path2D::_bind_methods() {
84
85 ObjectTypeDB::bind_method(_MD("set_curve", "curve:Curve2D"), &Path2D::set_curve);
86 ObjectTypeDB::bind_method(_MD("get_curve:Curve2D", "curve"), &Path2D::get_curve);
87 ObjectTypeDB::bind_method(_MD("_curve_changed"), &Path2D::_curve_changed);
88
89 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve2D"), _SCS("set_curve"), _SCS("get_curve"));
90 }
91
Path2D()92 Path2D::Path2D() {
93
94 set_curve(Ref<Curve2D>(memnew(Curve2D))); //create one by default
95 }
96
97 /////////////////////////////////////////////////////////////////////////////////
98
_update_transform()99 void PathFollow2D::_update_transform() {
100
101 if (!path)
102 return;
103
104 Ref<Curve2D> c = path->get_curve();
105 if (!c.is_valid())
106 return;
107
108 float o = offset;
109 if (loop)
110 o = Math::fposmod(o, c->get_baked_length());
111
112 Vector2 pos = c->interpolate_baked(o, cubic);
113
114 if (rotate) {
115
116 Vector2 n = (c->interpolate_baked(o + lookahead, cubic) - pos).normalized();
117 Vector2 t = -n.tangent();
118 pos += n * h_offset;
119 pos += t * v_offset;
120
121 set_rot(t.angle());
122
123 } else {
124
125 pos.x += h_offset;
126 pos.y += v_offset;
127 }
128
129 set_pos(pos);
130 }
131
_notification(int p_what)132 void PathFollow2D::_notification(int p_what) {
133
134 switch (p_what) {
135
136 case NOTIFICATION_ENTER_TREE: {
137
138 Node *parent = get_parent();
139 if (parent) {
140
141 path = parent->cast_to<Path2D>();
142 if (path) {
143 _update_transform();
144 }
145 }
146
147 } break;
148 case NOTIFICATION_EXIT_TREE: {
149
150 path = NULL;
151 } break;
152 }
153 }
154
set_cubic_interpolation(bool p_enable)155 void PathFollow2D::set_cubic_interpolation(bool p_enable) {
156
157 cubic = p_enable;
158 }
159
get_cubic_interpolation() const160 bool PathFollow2D::get_cubic_interpolation() const {
161
162 return cubic;
163 }
164
_set(const StringName & p_name,const Variant & p_value)165 bool PathFollow2D::_set(const StringName &p_name, const Variant &p_value) {
166
167 if (p_name == SceneStringNames::get_singleton()->offset) {
168 set_offset(p_value);
169 } else if (p_name == SceneStringNames::get_singleton()->unit_offset) {
170 set_unit_offset(p_value);
171 } else if (p_name == SceneStringNames::get_singleton()->rotate) {
172 set_rotate(p_value);
173 } else if (p_name == SceneStringNames::get_singleton()->v_offset) {
174 set_v_offset(p_value);
175 } else if (p_name == SceneStringNames::get_singleton()->h_offset) {
176 set_h_offset(p_value);
177 } else if (String(p_name) == "cubic_interp") {
178 set_cubic_interpolation(p_value);
179 } else if (String(p_name) == "loop") {
180 set_loop(p_value);
181 } else if (String(p_name) == "lookahead") {
182 set_lookahead(p_value);
183 } else
184 return false;
185
186 return true;
187 }
188
_get(const StringName & p_name,Variant & r_ret) const189 bool PathFollow2D::_get(const StringName &p_name, Variant &r_ret) const {
190
191 if (p_name == SceneStringNames::get_singleton()->offset) {
192 r_ret = get_offset();
193 } else if (p_name == SceneStringNames::get_singleton()->unit_offset) {
194 r_ret = get_unit_offset();
195 } else if (p_name == SceneStringNames::get_singleton()->rotate) {
196 r_ret = is_rotating();
197 } else if (p_name == SceneStringNames::get_singleton()->v_offset) {
198 r_ret = get_v_offset();
199 } else if (p_name == SceneStringNames::get_singleton()->h_offset) {
200 r_ret = get_h_offset();
201 } else if (String(p_name) == "cubic_interp") {
202 r_ret = cubic;
203 } else if (String(p_name) == "loop") {
204 r_ret = loop;
205 } else if (String(p_name) == "lookahead") {
206 r_ret = lookahead;
207 } else
208 return false;
209
210 return true;
211 }
_get_property_list(List<PropertyInfo> * p_list) const212 void PathFollow2D::_get_property_list(List<PropertyInfo> *p_list) const {
213
214 float max = 10000;
215 if (path && path->get_curve().is_valid())
216 max = path->get_curve()->get_baked_length();
217 p_list->push_back(PropertyInfo(Variant::REAL, "offset", PROPERTY_HINT_RANGE, "0," + rtos(max) + ",0.01"));
218 p_list->push_back(PropertyInfo(Variant::REAL, "unit_offset", PROPERTY_HINT_RANGE, "0,1,0.0001", PROPERTY_USAGE_EDITOR));
219 p_list->push_back(PropertyInfo(Variant::REAL, "h_offset"));
220 p_list->push_back(PropertyInfo(Variant::REAL, "v_offset"));
221 p_list->push_back(PropertyInfo(Variant::BOOL, "rotate"));
222 p_list->push_back(PropertyInfo(Variant::BOOL, "cubic_interp"));
223 p_list->push_back(PropertyInfo(Variant::BOOL, "loop"));
224 p_list->push_back(PropertyInfo(Variant::REAL, "lookahead", PROPERTY_HINT_RANGE, "0.001,1024.0,0.001"));
225 }
226
get_configuration_warning() const227 String PathFollow2D::get_configuration_warning() const {
228
229 if (!is_visible() || !is_inside_tree())
230 return String();
231
232 if (!get_parent() || !get_parent()->cast_to<Path2D>()) {
233 return TTR("PathFollow2D only works when set as a child of a Path2D node.");
234 }
235
236 return String();
237 }
238
_bind_methods()239 void PathFollow2D::_bind_methods() {
240
241 ObjectTypeDB::bind_method(_MD("set_offset", "offset"), &PathFollow2D::set_offset);
242 ObjectTypeDB::bind_method(_MD("get_offset"), &PathFollow2D::get_offset);
243
244 ObjectTypeDB::bind_method(_MD("set_h_offset", "h_offset"), &PathFollow2D::set_h_offset);
245 ObjectTypeDB::bind_method(_MD("get_h_offset"), &PathFollow2D::get_h_offset);
246
247 ObjectTypeDB::bind_method(_MD("set_v_offset", "v_offset"), &PathFollow2D::set_v_offset);
248 ObjectTypeDB::bind_method(_MD("get_v_offset"), &PathFollow2D::get_v_offset);
249
250 ObjectTypeDB::bind_method(_MD("set_unit_offset", "unit_offset"), &PathFollow2D::set_unit_offset);
251 ObjectTypeDB::bind_method(_MD("get_unit_offset"), &PathFollow2D::get_unit_offset);
252
253 ObjectTypeDB::bind_method(_MD("set_rotate", "enable"), &PathFollow2D::set_rotate);
254 ObjectTypeDB::bind_method(_MD("is_rotating"), &PathFollow2D::is_rotating);
255
256 ObjectTypeDB::bind_method(_MD("set_cubic_interpolation", "enable"), &PathFollow2D::set_cubic_interpolation);
257 ObjectTypeDB::bind_method(_MD("get_cubic_interpolation"), &PathFollow2D::get_cubic_interpolation);
258
259 ObjectTypeDB::bind_method(_MD("set_loop", "loop"), &PathFollow2D::set_loop);
260 ObjectTypeDB::bind_method(_MD("has_loop"), &PathFollow2D::has_loop);
261 }
262
set_offset(float p_offset)263 void PathFollow2D::set_offset(float p_offset) {
264
265 offset = p_offset;
266 if (path)
267 _update_transform();
268 _change_notify("offset");
269 _change_notify("unit_offset");
270 }
271
set_h_offset(float p_h_offset)272 void PathFollow2D::set_h_offset(float p_h_offset) {
273
274 h_offset = p_h_offset;
275 if (path)
276 _update_transform();
277 }
278
get_h_offset() const279 float PathFollow2D::get_h_offset() const {
280
281 return h_offset;
282 }
283
set_v_offset(float p_v_offset)284 void PathFollow2D::set_v_offset(float p_v_offset) {
285
286 v_offset = p_v_offset;
287 if (path)
288 _update_transform();
289 }
290
get_v_offset() const291 float PathFollow2D::get_v_offset() const {
292
293 return v_offset;
294 }
295
get_offset() const296 float PathFollow2D::get_offset() const {
297
298 return offset;
299 }
300
set_unit_offset(float p_unit_offset)301 void PathFollow2D::set_unit_offset(float p_unit_offset) {
302
303 if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length())
304 set_offset(p_unit_offset * path->get_curve()->get_baked_length());
305 }
306
get_unit_offset() const307 float PathFollow2D::get_unit_offset() const {
308
309 if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length())
310 return get_offset() / path->get_curve()->get_baked_length();
311 else
312 return 0;
313 }
314
set_lookahead(float p_lookahead)315 void PathFollow2D::set_lookahead(float p_lookahead) {
316
317 lookahead = p_lookahead;
318 }
319
get_lookahead() const320 float PathFollow2D::get_lookahead() const {
321
322 return lookahead;
323 }
324
set_rotate(bool p_rotate)325 void PathFollow2D::set_rotate(bool p_rotate) {
326
327 rotate = p_rotate;
328 _update_transform();
329 }
330
is_rotating() const331 bool PathFollow2D::is_rotating() const {
332
333 return rotate;
334 }
335
set_loop(bool p_loop)336 void PathFollow2D::set_loop(bool p_loop) {
337
338 loop = p_loop;
339 }
340
has_loop() const341 bool PathFollow2D::has_loop() const {
342
343 return loop;
344 }
345
PathFollow2D()346 PathFollow2D::PathFollow2D() {
347
348 offset = 0;
349 h_offset = 0;
350 v_offset = 0;
351 path = NULL;
352 rotate = true;
353 cubic = true;
354 loop = true;
355 lookahead = 4;
356 }
357