1 /*************************************************************************/
2 /* path_editor_plugin.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_editor_plugin.h"
31 #include "os/keyboard.h"
32 #include "scene/resources/curve.h"
33 #include "spatial_editor_plugin.h"
34
get_handle_name(int p_idx) const35 String PathSpatialGizmo::get_handle_name(int p_idx) const {
36
37 Ref<Curve3D> c = path->get_curve();
38 if (c.is_null())
39 return "";
40
41 if (p_idx < c->get_point_count()) {
42
43 return TTR("Curve Point #") + itos(p_idx);
44 }
45
46 p_idx = p_idx - c->get_point_count() + 1;
47
48 int idx = p_idx / 2;
49 int t = p_idx % 2;
50 String n = TTR("Curve Point #") + itos(idx);
51 if (t == 0)
52 n += " In";
53 else
54 n += " Out";
55
56 return n;
57 }
get_handle_value(int p_idx) const58 Variant PathSpatialGizmo::get_handle_value(int p_idx) const {
59
60 Ref<Curve3D> c = path->get_curve();
61 if (c.is_null())
62 return Variant();
63
64 if (p_idx < c->get_point_count()) {
65
66 original = c->get_point_pos(p_idx);
67 return original;
68 }
69
70 p_idx = p_idx - c->get_point_count() + 1;
71
72 int idx = p_idx / 2;
73 int t = p_idx % 2;
74
75 Vector3 ofs;
76 if (t == 0)
77 ofs = c->get_point_in(idx);
78 else
79 ofs = c->get_point_out(idx);
80
81 original = ofs + c->get_point_pos(idx);
82
83 return ofs;
84 }
set_handle(int p_idx,Camera * p_camera,const Point2 & p_point)85 void PathSpatialGizmo::set_handle(int p_idx, Camera *p_camera, const Point2 &p_point) {
86
87 Ref<Curve3D> c = path->get_curve();
88 if (c.is_null())
89 return;
90
91 Transform gt = path->get_global_transform();
92 Transform gi = gt.affine_inverse();
93 Vector3 ray_from = p_camera->project_ray_origin(p_point);
94 Vector3 ray_dir = p_camera->project_ray_normal(p_point);
95
96 if (p_idx < c->get_point_count()) {
97
98 Plane p(gt.xform(original), p_camera->get_transform().basis.get_axis(2));
99
100 Vector3 inters;
101
102 if (p.intersects_ray(ray_from, ray_dir, &inters)) {
103
104 if (SpatialEditor::get_singleton()->is_snap_enabled()) {
105 float snap = SpatialEditor::get_singleton()->get_translate_snap();
106 inters.snap(snap);
107 }
108
109 Vector3 local = gi.xform(inters);
110 c->set_point_pos(p_idx, local);
111 }
112
113 return;
114 }
115
116 p_idx = p_idx - c->get_point_count() + 1;
117
118 int idx = p_idx / 2;
119 int t = p_idx % 2;
120
121 Vector3 base = c->get_point_pos(idx);
122
123 Plane p(gt.xform(original), p_camera->get_transform().basis.get_axis(2));
124
125 Vector3 inters;
126
127 if (p.intersects_ray(ray_from, ray_dir, &inters)) {
128
129 Vector3 local = gi.xform(inters) - base;
130 if (t == 0) {
131 c->set_point_in(idx, local);
132 } else {
133 c->set_point_out(idx, local);
134 }
135 }
136 }
137
commit_handle(int p_idx,const Variant & p_restore,bool p_cancel)138 void PathSpatialGizmo::commit_handle(int p_idx, const Variant &p_restore, bool p_cancel) {
139
140 Ref<Curve3D> c = path->get_curve();
141 if (c.is_null())
142 return;
143
144 UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
145
146 if (p_idx < c->get_point_count()) {
147
148 if (p_cancel) {
149
150 c->set_point_pos(p_idx, p_restore);
151 return;
152 }
153 ur->create_action(TTR("Set Curve Point Pos"));
154 ur->add_do_method(c.ptr(), "set_point_pos", p_idx, c->get_point_pos(p_idx));
155 ur->add_undo_method(c.ptr(), "set_point_pos", p_idx, p_restore);
156 ur->commit_action();
157
158 return;
159 }
160
161 p_idx = p_idx - c->get_point_count() + 1;
162
163 int idx = p_idx / 2;
164 int t = p_idx % 2;
165
166 Vector3 ofs;
167
168 if (p_cancel) {
169
170 return;
171 }
172
173 if (t == 0) {
174
175 if (p_cancel) {
176
177 c->set_point_in(p_idx, p_restore);
178 return;
179 }
180 ur->create_action(TTR("Set Curve In Pos"));
181 ur->add_do_method(c.ptr(), "set_point_in", idx, c->get_point_in(idx));
182 ur->add_undo_method(c.ptr(), "set_point_in", idx, p_restore);
183 ur->commit_action();
184
185 } else {
186 if (p_cancel) {
187
188 c->set_point_out(idx, p_restore);
189 return;
190 }
191 ur->create_action(TTR("Set Curve Out Pos"));
192 ur->add_do_method(c.ptr(), "set_point_out", idx, c->get_point_out(idx));
193 ur->add_undo_method(c.ptr(), "set_point_out", idx, p_restore);
194 ur->commit_action();
195 }
196 }
197
redraw()198 void PathSpatialGizmo::redraw() {
199
200 clear();
201
202 Ref<Curve3D> c = path->get_curve();
203 if (c.is_null())
204 return;
205
206 Vector3Array v3a = c->tesselate();
207 //Vector3Array v3a=c->get_baked_points();
208
209 int v3s = v3a.size();
210 if (v3s == 0)
211 return;
212 Vector<Vector3> v3p;
213 Vector3Array::Read r = v3a.read();
214
215 for (int i = 0; i < v3s - 1; i++) {
216
217 v3p.push_back(r[i]);
218 v3p.push_back(r[i + 1]);
219 //v3p.push_back(r[i]);
220 //v3p.push_back(r[i]+Vector3(0,0.2,0));
221 }
222
223 add_lines(v3p, PathEditorPlugin::singleton->path_material);
224 add_collision_segments(v3p);
225
226 if (PathEditorPlugin::singleton->get_edited_path() == path) {
227 v3p.clear();
228 Vector<Vector3> handles;
229 Vector<Vector3> sec_handles;
230
231 for (int i = 0; i < c->get_point_count(); i++) {
232
233 Vector3 p = c->get_point_pos(i);
234 handles.push_back(p);
235 if (i > 0) {
236 v3p.push_back(p);
237 v3p.push_back(p + c->get_point_in(i));
238 sec_handles.push_back(p + c->get_point_in(i));
239 }
240
241 if (i < c->get_point_count() - 1) {
242 v3p.push_back(p);
243 v3p.push_back(p + c->get_point_out(i));
244 sec_handles.push_back(p + c->get_point_out(i));
245 }
246 }
247
248 add_lines(v3p, PathEditorPlugin::singleton->path_thin_material);
249 add_handles(handles);
250 add_handles(sec_handles, false, true);
251 }
252 }
253
PathSpatialGizmo(Path * p_path)254 PathSpatialGizmo::PathSpatialGizmo(Path *p_path) {
255
256 path = p_path;
257 set_spatial_node(p_path);
258 }
259
create_spatial_gizmo(Spatial * p_spatial)260 Ref<SpatialEditorGizmo> PathEditorPlugin::create_spatial_gizmo(Spatial *p_spatial) {
261
262 if (p_spatial->cast_to<Path>()) {
263
264 return memnew(PathSpatialGizmo(p_spatial->cast_to<Path>()));
265 }
266
267 return Ref<SpatialEditorGizmo>();
268 }
269
forward_spatial_input_event(Camera * p_camera,const InputEvent & p_event)270 bool PathEditorPlugin::forward_spatial_input_event(Camera *p_camera, const InputEvent &p_event) {
271
272 if (!path)
273 return false;
274 Ref<Curve3D> c = path->get_curve();
275 if (c.is_null())
276 return false;
277 Transform gt = path->get_global_transform();
278 Transform it = gt.affine_inverse();
279
280 static const int click_dist = 10; //should make global
281
282 if (p_event.type == InputEvent::MOUSE_BUTTON) {
283
284 const InputEventMouseButton &mb = p_event.mouse_button;
285 Point2 mbpos(mb.x, mb.y);
286
287 if (mb.pressed && mb.button_index == BUTTON_LEFT && (curve_create->is_pressed() || (curve_edit->is_pressed() && mb.mod.control))) {
288 //click into curve, break it down
289 Vector3Array v3a = c->tesselate();
290 int idx = 0;
291 int rc = v3a.size();
292 int closest_seg = -1;
293 Vector3 closest_seg_point;
294 float closest_d = 1e20;
295
296 if (rc >= 2) {
297 Vector3Array::Read r = v3a.read();
298
299 if (p_camera->unproject_position(gt.xform(c->get_point_pos(0))).distance_to(mbpos) < click_dist)
300 return false; //nope, existing
301
302 for (int i = 0; i < c->get_point_count() - 1; i++) {
303 //find the offset and point index of the place to break up
304 int j = idx;
305 if (p_camera->unproject_position(gt.xform(c->get_point_pos(i + 1))).distance_to(mbpos) < click_dist)
306 return false; //nope, existing
307
308 while (j < rc && c->get_point_pos(i + 1) != r[j]) {
309
310 Vector3 from = r[j];
311 Vector3 to = r[j + 1];
312 real_t cdist = from.distance_to(to);
313 from = gt.xform(from);
314 to = gt.xform(to);
315 if (cdist > 0) {
316 Vector2 s[2];
317 s[0] = p_camera->unproject_position(from);
318 s[1] = p_camera->unproject_position(to);
319 Vector2 inters = Geometry::get_closest_point_to_segment_2d(mbpos, s);
320 float d = inters.distance_to(mbpos);
321
322 if (d < 10 && d < closest_d) {
323
324 closest_d = d;
325 closest_seg = i;
326 Vector3 ray_from = p_camera->project_ray_origin(mbpos);
327 Vector3 ray_dir = p_camera->project_ray_normal(mbpos);
328
329 Vector3 ra, rb;
330 Geometry::get_closest_points_between_segments(ray_from, ray_from + ray_dir * 4096, from, to, ra, rb);
331
332 closest_seg_point = it.xform(rb);
333 }
334 }
335 j++;
336 }
337 if (idx == j)
338 idx++; //force next
339 else
340 idx = j; //swap
341
342 if (j == rc)
343 break;
344 }
345 }
346
347 UndoRedo *ur = editor->get_undo_redo();
348 if (closest_seg != -1) {
349 //subdivide
350
351 ur->create_action(TTR("Split Path"));
352 ur->add_do_method(c.ptr(), "add_point", closest_seg_point, Vector3(), Vector3(), closest_seg + 1);
353 ur->add_undo_method(c.ptr(), "remove_point", closest_seg + 1);
354 ur->commit_action();
355 return true;
356
357 } else {
358
359 Vector3 org;
360 if (c->get_point_count() == 0)
361 org = path->get_transform().get_origin();
362 else
363 org = gt.xform(c->get_point_pos(c->get_point_count()));
364 Plane p(org, p_camera->get_transform().basis.get_axis(2));
365 Vector3 ray_from = p_camera->project_ray_origin(mbpos);
366 Vector3 ray_dir = p_camera->project_ray_normal(mbpos);
367
368 Vector3 inters;
369 if (p.intersects_ray(ray_from, ray_dir, &inters)) {
370
371 ur->create_action(TTR("Add Point to Curve"));
372 ur->add_do_method(c.ptr(), "add_point", it.xform(inters), Vector3(), Vector3(), -1);
373 ur->add_undo_method(c.ptr(), "remove_point", c->get_point_count());
374 ur->commit_action();
375 return true;
376 }
377
378 //add new at pos
379 }
380
381 } else if (mb.pressed && ((mb.button_index == BUTTON_LEFT && curve_del->is_pressed()) || (mb.button_index == BUTTON_RIGHT && curve_edit->is_pressed()))) {
382
383 int erase_idx = -1;
384 for (int i = 0; i < c->get_point_count(); i++) {
385 //find the offset and point index of the place to break up
386 if (p_camera->unproject_position(gt.xform(c->get_point_pos(i))).distance_to(mbpos) < click_dist) {
387
388 erase_idx = i;
389 break;
390 }
391 }
392
393 if (erase_idx != -1) {
394
395 UndoRedo *ur = editor->get_undo_redo();
396 ur->create_action(TTR("Remove Path Point"));
397 ur->add_do_method(c.ptr(), "remove_point", erase_idx);
398 ur->add_undo_method(c.ptr(), "add_point", c->get_point_pos(erase_idx), c->get_point_in(erase_idx), c->get_point_out(erase_idx), erase_idx);
399 ur->commit_action();
400 return true;
401 }
402 }
403 }
404
405 return false;
406 }
407
edit(Object * p_object)408 void PathEditorPlugin::edit(Object *p_object) {
409
410 if (p_object) {
411 path = p_object->cast_to<Path>();
412 if (path) {
413
414 if (path->get_curve().is_valid()) {
415 path->get_curve()->emit_signal("changed");
416 }
417 }
418 } else {
419 Path *pre = path;
420 path = NULL;
421 if (pre) {
422 pre->get_curve()->emit_signal("changed");
423 }
424 }
425 // collision_polygon_editor->edit(p_object->cast_to<Node>());
426 }
427
handles(Object * p_object) const428 bool PathEditorPlugin::handles(Object *p_object) const {
429
430 return p_object->is_type("Path");
431 }
432
make_visible(bool p_visible)433 void PathEditorPlugin::make_visible(bool p_visible) {
434
435 if (p_visible) {
436
437 curve_create->show();
438 curve_edit->show();
439 curve_del->show();
440 curve_close->show();
441 sep->show();
442 } else {
443
444 curve_create->hide();
445 curve_edit->hide();
446 curve_del->hide();
447 curve_close->hide();
448 sep->hide();
449
450 {
451 Path *pre = path;
452 path = NULL;
453 if (pre && pre->get_curve().is_valid()) {
454 pre->get_curve()->emit_signal("changed");
455 }
456 }
457 }
458 }
459
_mode_changed(int p_idx)460 void PathEditorPlugin::_mode_changed(int p_idx) {
461
462 curve_create->set_pressed(p_idx == 0);
463 curve_edit->set_pressed(p_idx == 1);
464 curve_del->set_pressed(p_idx == 2);
465 }
466
_close_curve()467 void PathEditorPlugin::_close_curve() {
468
469 Ref<Curve3D> c = path->get_curve();
470 if (c.is_null())
471 return;
472 if (c->get_point_count() < 2)
473 return;
474 c->add_point(c->get_point_pos(0), c->get_point_in(0), c->get_point_out(0));
475 }
476
_notification(int p_what)477 void PathEditorPlugin::_notification(int p_what) {
478
479 if (p_what == NOTIFICATION_ENTER_TREE) {
480
481 curve_create->connect("pressed", this, "_mode_changed", make_binds(0));
482 curve_edit->connect("pressed", this, "_mode_changed", make_binds(1));
483 curve_del->connect("pressed", this, "_mode_changed", make_binds(2));
484 curve_close->connect("pressed", this, "_close_curve");
485 }
486 }
487
_bind_methods()488 void PathEditorPlugin::_bind_methods() {
489
490 ObjectTypeDB::bind_method(_MD("_mode_changed"), &PathEditorPlugin::_mode_changed);
491 ObjectTypeDB::bind_method(_MD("_close_curve"), &PathEditorPlugin::_close_curve);
492 }
493
494 PathEditorPlugin *PathEditorPlugin::singleton = NULL;
495
PathEditorPlugin(EditorNode * p_node)496 PathEditorPlugin::PathEditorPlugin(EditorNode *p_node) {
497
498 path = NULL;
499 editor = p_node;
500 singleton = this;
501
502 path_material = Ref<FixedMaterial>(memnew(FixedMaterial));
503 path_material->set_parameter(FixedMaterial::PARAM_DIFFUSE, Color(0.5, 0.5, 1.0, 0.8));
504 path_material->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
505 path_material->set_line_width(3);
506 path_material->set_flag(Material::FLAG_DOUBLE_SIDED, true);
507 path_material->set_flag(Material::FLAG_UNSHADED, true);
508
509 path_thin_material = Ref<FixedMaterial>(memnew(FixedMaterial));
510 path_thin_material->set_parameter(FixedMaterial::PARAM_DIFFUSE, Color(0.5, 0.5, 1.0, 0.4));
511 path_thin_material->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
512 path_thin_material->set_line_width(1);
513 path_thin_material->set_flag(Material::FLAG_DOUBLE_SIDED, true);
514 path_thin_material->set_flag(Material::FLAG_UNSHADED, true);
515
516 // SpatialEditor::get_singleton()->add_gizmo_plugin(this);
517
518 sep = memnew(VSeparator);
519 sep->hide();
520 SpatialEditor::get_singleton()->add_control_to_menu_panel(sep);
521 curve_edit = memnew(ToolButton);
522 curve_edit->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveEdit", "EditorIcons"));
523 curve_edit->set_toggle_mode(true);
524 curve_edit->hide();
525 curve_edit->set_focus_mode(Control::FOCUS_NONE);
526 curve_edit->set_tooltip(TTR("Select Points") + "\n" + TTR("Shift+Drag: Select Control Points") + "\n" + keycode_get_string(KEY_MASK_CMD) + TTR("Click: Add Point") + "\n" + TTR("Right Click: Delete Point"));
527 SpatialEditor::get_singleton()->add_control_to_menu_panel(curve_edit);
528 curve_create = memnew(ToolButton);
529 curve_create->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveCreate", "EditorIcons"));
530 curve_create->set_toggle_mode(true);
531 curve_create->hide();
532 curve_create->set_focus_mode(Control::FOCUS_NONE);
533 curve_create->set_tooltip(TTR("Add Point (in empty space)") + "\n" + TTR("Split Segment (in curve)"));
534 SpatialEditor::get_singleton()->add_control_to_menu_panel(curve_create);
535 curve_del = memnew(ToolButton);
536 curve_del->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveDelete", "EditorIcons"));
537 curve_del->set_toggle_mode(true);
538 curve_del->hide();
539 curve_del->set_focus_mode(Control::FOCUS_NONE);
540 curve_del->set_tooltip(TTR("Delete Point"));
541 SpatialEditor::get_singleton()->add_control_to_menu_panel(curve_del);
542 curve_close = memnew(ToolButton);
543 curve_close->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("CurveClose", "EditorIcons"));
544 curve_close->hide();
545 curve_close->set_focus_mode(Control::FOCUS_NONE);
546 curve_close->set_tooltip(TTR("Close Curve"));
547 SpatialEditor::get_singleton()->add_control_to_menu_panel(curve_close);
548
549 curve_edit->set_pressed(true);
550 /*
551 collision_polygon_editor = memnew( PathEditor(p_node) );
552 editor->get_viewport()->add_child(collision_polygon_editor);
553
554 collision_polygon_editor->set_margin(MARGIN_LEFT,200);
555 collision_polygon_editor->set_margin(MARGIN_RIGHT,230);
556 collision_polygon_editor->set_margin(MARGIN_TOP,0);
557 collision_polygon_editor->set_margin(MARGIN_BOTTOM,10);
558
559
560 collision_polygon_editor->hide();
561 */
562 }
563
~PathEditorPlugin()564 PathEditorPlugin::~PathEditorPlugin() {
565 }
566