Debian Codegraph @

editor/scene/2d/physics/collision_shape_2d_editor_plugin.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* collision_shape_2d_editor_plugin.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 "collision_shape_2d_editor_plugin.h"
32
33 #include "core/os/keyboard.h"
34 #include "editor/editor_node.h"
35 #include "editor/editor_undo_redo_manager.h"
36 #include "editor/scene/canvas_item_editor_plugin.h"
37 #include "editor/settings/editor_settings.h"
38 #include "scene/main/viewport.h"
39 #include "scene/resources/2d/capsule_shape_2d.h"
40 #include "scene/resources/2d/circle_shape_2d.h"
41 #include "scene/resources/2d/concave_polygon_shape_2d.h"
42 #include "scene/resources/2d/convex_polygon_shape_2d.h"
43 #include "scene/resources/2d/rectangle_shape_2d.h"
44 #include "scene/resources/2d/segment_shape_2d.h"
45 #include "scene/resources/2d/separation_ray_shape_2d.h"
46 #include "scene/resources/2d/world_boundary_shape_2d.h"
47
48 CollisionShape2DEditor::CollisionShape2DEditor() {
49 grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
50 }
51
52 void CollisionShape2DEditor::_node_removed(Node *p_node) {
53 if (p_node == node) {
54 node = nullptr;
55 }
56 }
57
58 Variant CollisionShape2DEditor::get_handle_value(int idx) const {
59 switch (shape_type) {
60 case CAPSULE_SHAPE: {
61 Ref<CapsuleShape2D> capsule = node->get_shape();
62 return Vector2(capsule->get_radius(), capsule->get_height());
63
64 } break;
65
66 case CIRCLE_SHAPE: {
67 Ref<CircleShape2D> circle = node->get_shape();
68
69 if (idx == 0) {
70 return circle->get_radius();
71 }
72
73 } break;
74
75 case CONCAVE_POLYGON_SHAPE: {
76 Ref<ConcavePolygonShape2D> shape = node->get_shape();
77 const Vector<Vector2> &segments = shape->get_segments();
78 return segments[idx];
79
80 } break;
81
82 case CONVEX_POLYGON_SHAPE: {
83 Ref<ConvexPolygonShape2D> shape = node->get_shape();
84 const Vector<Vector2> &points = shape->get_points();
85 return points[idx];
86
87 } break;
88
89 case WORLD_BOUNDARY_SHAPE: {
90 Ref<WorldBoundaryShape2D> world_boundary = node->get_shape();
91
92 if (idx == 0) {
93 return world_boundary->get_distance();
94 } else {
95 return world_boundary->get_normal();
96 }
97
98 } break;
99
100 case SEPARATION_RAY_SHAPE: {
101 Ref<SeparationRayShape2D> ray = node->get_shape();
102
103 if (idx == 0) {
104 return ray->get_length();
105 }
106
107 } break;
108
109 case RECTANGLE_SHAPE: {
110 Ref<RectangleShape2D> rect = node->get_shape();
111
112 if (idx < 8) {
113 return rect->get_size().abs();
114 }
115
116 } break;
117
118 case SEGMENT_SHAPE: {
119 Ref<SegmentShape2D> seg = node->get_shape();
120
121 if (idx == 0) {
122 return seg->get_a();
123 } else if (idx == 1) {
124 return seg->get_b();
125 }
126
127 } break;
128 }
129
130 return Variant();
131 }
132
133 void CollisionShape2DEditor::set_handle(int idx, Point2 &p_point) {
134 switch (shape_type) {
135 case CAPSULE_SHAPE: {
136 if (idx < 2) {
137 Ref<CapsuleShape2D> capsule = node->get_shape();
138
139 real_t parameter = Math::abs(p_point[idx]);
140
141 if (idx == 0) {
142 capsule->set_radius(parameter);
143 } else if (idx == 1) {
144 capsule->set_height(parameter * 2);
145 }
146 }
147
148 } break;
149
150 case CIRCLE_SHAPE: {
151 Ref<CircleShape2D> circle = node->get_shape();
152 circle->set_radius(p_point.length());
153 } break;
154
155 case CONCAVE_POLYGON_SHAPE: {
156 Ref<ConcavePolygonShape2D> concave_shape = node->get_shape();
157
158 Vector<Vector2> segments = concave_shape->get_segments();
159
160 ERR_FAIL_INDEX(idx, segments.size());
161 segments.write[idx] = p_point;
162
163 concave_shape->set_segments(segments);
164
165 } break;
166
167 case CONVEX_POLYGON_SHAPE: {
168 Ref<ConvexPolygonShape2D> convex_shape = node->get_shape();
169
170 Vector<Vector2> points = convex_shape->get_points();
171
172 ERR_FAIL_INDEX(idx, points.size());
173 points.write[idx] = p_point;
174
175 convex_shape->set_points(points);
176
177 } break;
178
179 case WORLD_BOUNDARY_SHAPE: {
180 if (idx < 2) {
181 Ref<WorldBoundaryShape2D> world_boundary = node->get_shape();
182
183 if (idx == 0) {
184 Vector2 normal = world_boundary->get_normal();
185 world_boundary->set_distance(p_point.dot(normal) / normal.length_squared());
186 } else {
187 real_t dir = world_boundary->get_distance() < 0 ? -1 : 1;
188 world_boundary->set_normal(p_point.normalized() * dir);
189 }
190 }
191 } break;
192
193 case SEPARATION_RAY_SHAPE: {
194 Ref<SeparationRayShape2D> ray = node->get_shape();
195
196 ray->set_length(Math::abs(p_point.y));
197 } break;
198
199 case RECTANGLE_SHAPE: {
200 if (idx < 8) {
201 Ref<RectangleShape2D> rect = node->get_shape();
202 Vector2 size = (Point2)original;
203
204 if (RECT_HANDLES[idx].x != 0) {
205 size.x = p_point.x * RECT_HANDLES[idx].x * 2;
206 }
207 if (RECT_HANDLES[idx].y != 0) {
208 size.y = p_point.y * RECT_HANDLES[idx].y * 2;
209 }
210
211 if (Input::get_singleton()->is_key_pressed(Key::ALT)) {
212 rect->set_size(size.abs());
213 node->set_global_position(original_transform.get_origin());
214 } else {
215 rect->set_size(((Point2)original + (size - (Point2)original) * 0.5).abs());
216 Point2 pos = original_transform.affine_inverse().xform(original_transform.get_origin());
217 pos += (size - (Point2)original) * 0.5 * RECT_HANDLES[idx] * 0.5;
218 node->set_global_position(original_transform.xform(pos));
219 }
220 }
221
222 } break;
223
224 case SEGMENT_SHAPE: {
225 if (edit_handle < 2) {
226 Ref<SegmentShape2D> seg = node->get_shape();
227
228 if (idx == 0) {
229 seg->set_a(p_point);
230 } else if (idx == 1) {
231 seg->set_b(p_point);
232 }
233 }
234 } break;
235 }
236 }
237
238 void CollisionShape2DEditor::commit_handle(int idx, Variant &p_org) {
239 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
240 undo_redo->create_action(TTR("Set Handle"));
241
242 switch (shape_type) {
243 case CAPSULE_SHAPE: {
244 Ref<CapsuleShape2D> capsule = node->get_shape();
245
246 Vector2 values = p_org;
247
248 if (idx == 0) {
249 undo_redo->add_do_method(capsule.ptr(), "set_radius", capsule->get_radius());
250 } else if (idx == 1) {
251 undo_redo->add_do_method(capsule.ptr(), "set_height", capsule->get_height());
252 }
253 undo_redo->add_undo_method(capsule.ptr(), "set_radius", values[0]);
254 undo_redo->add_undo_method(capsule.ptr(), "set_height", values[1]);
255
256 } break;
257
258 case CIRCLE_SHAPE: {
259 Ref<CircleShape2D> circle = node->get_shape();
260
261 undo_redo->add_do_method(circle.ptr(), "set_radius", circle->get_radius());
262 undo_redo->add_undo_method(circle.ptr(), "set_radius", p_org);
263
264 } break;
265
266 case CONCAVE_POLYGON_SHAPE: {
267 Ref<ConcavePolygonShape2D> concave_shape = node->get_shape();
268
269 Vector2 values = p_org;
270
271 Vector<Vector2> undo_segments = concave_shape->get_segments();
272
273 ERR_FAIL_INDEX(idx, undo_segments.size());
274 undo_segments.write[idx] = values;
275
276 undo_redo->add_do_method(concave_shape.ptr(), "set_segments", concave_shape->get_segments());
277 undo_redo->add_undo_method(concave_shape.ptr(), "set_segments", undo_segments);
278
279 } break;
280
281 case CONVEX_POLYGON_SHAPE: {
282 Ref<ConvexPolygonShape2D> convex_shape = node->get_shape();
283
284 Vector2 values = p_org;
285
286 Vector<Vector2> undo_points = convex_shape->get_points();
287
288 ERR_FAIL_INDEX(idx, undo_points.size());
289 undo_points.write[idx] = values;
290
291 undo_redo->add_do_method(convex_shape.ptr(), "set_points", convex_shape->get_points());
292 undo_redo->add_undo_method(convex_shape.ptr(), "set_points", undo_points);
293
294 } break;
295
296 case WORLD_BOUNDARY_SHAPE: {
297 Ref<WorldBoundaryShape2D> world_boundary = node->get_shape();
298
299 if (idx == 0) {
300 undo_redo->add_do_method(world_boundary.ptr(), "set_distance", world_boundary->get_distance());
301 undo_redo->add_undo_method(world_boundary.ptr(), "set_distance", p_org);
302 } else {
303 undo_redo->add_do_method(world_boundary.ptr(), "set_normal", world_boundary->get_normal());
304 undo_redo->add_undo_method(world_boundary.ptr(), "set_normal", p_org);
305 }
306
307 } break;
308
309 case SEPARATION_RAY_SHAPE: {
310 Ref<SeparationRayShape2D> ray = node->get_shape();
311
312 undo_redo->add_do_method(ray.ptr(), "set_length", ray->get_length());
313 undo_redo->add_undo_method(ray.ptr(), "set_length", p_org);
314
315 } break;
316
317 case RECTANGLE_SHAPE: {
318 Ref<RectangleShape2D> rect = node->get_shape();
319
320 undo_redo->add_do_method(rect.ptr(), "set_size", rect->get_size());
321 undo_redo->add_do_method(node, "set_global_transform", node->get_global_transform());
322 undo_redo->add_undo_method(rect.ptr(), "set_size", p_org);
323 undo_redo->add_undo_method(node, "set_global_transform", original_transform);
324
325 } break;
326
327 case SEGMENT_SHAPE: {
328 Ref<SegmentShape2D> seg = node->get_shape();
329 if (idx == 0) {
330 undo_redo->add_do_method(seg.ptr(), "set_a", seg->get_a());
331 undo_redo->add_undo_method(seg.ptr(), "set_a", p_org);
332 } else if (idx == 1) {
333 undo_redo->add_do_method(seg.ptr(), "set_b", seg->get_b());
334 undo_redo->add_undo_method(seg.ptr(), "set_b", p_org);
335 }
336
337 } break;
338 }
339
340 undo_redo->commit_action();
341 }
342
343 bool CollisionShape2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
344 if (!node) {
345 return false;
346 }
347
348 if (!node->is_visible_in_tree()) {
349 return false;
350 }
351
352 Viewport *vp = node->get_viewport();
353 if (vp && !vp->is_visible_subviewport()) {
354 return false;
355 }
356
357 if (shape_type == -1) {
358 return false;
359 }
360
361 Ref<InputEventMouseButton> mb = p_event;
362 Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_screen_transform();
363
364 if (mb.is_valid()) {
365 Vector2 gpoint = mb->get_position();
366
367 if (mb->get_button_index() == MouseButton::LEFT) {
368 if (mb->is_pressed()) {
369 for (int i = 0; i < handles.size(); i++) {
370 if (xform.xform(handles[i]).distance_to(gpoint) < grab_threshold) {
371 edit_handle = i;
372
373 break;
374 }
375 }
376
377 if (edit_handle == -1) {
378 pressed = false;
379
380 return false;
381 }
382
383 original_mouse_pos = gpoint;
384 original_point = handles[edit_handle];
385 original = get_handle_value(edit_handle);
386 original_transform = node->get_global_transform();
387 last_point = original;
388 pressed = true;
389
390 return true;
391
392 } else if (pressed) {
393 Ref<InputEventMouse> m = p_event;
394 if (m.is_valid() && original_mouse_pos != m->get_position()) {
395 commit_handle(edit_handle, original);
396 }
397
398 edit_handle = -1;
399 pressed = false;
400
401 return true;
402 }
403 } else if (pressed) {
404 // Cancel the drag.
405 if (ED_IS_SHORTCUT("canvas_item_editor/cancel_transform", p_event) ||
406 (mb->is_pressed() && mb->get_button_index() == MouseButton::RIGHT)) {
407 Ref<InputEventMouse> m = p_event;
408 if (m.is_valid() && original_mouse_pos != m->get_position()) {
409 set_handle(edit_handle, original_point);
410 }
411
412 edit_handle = -1;
413 pressed = false;
414
415 return true;
416 }
417 }
418
419 return false;
420 }
421
422 Ref<InputEventMouseMotion> mm = p_event;
423
424 if (mm.is_valid()) {
425 if (edit_handle == -1 || !pressed) {
426 return false;
427 }
428
429 Vector2 cpoint = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mm->get_position()));
430 cpoint = node->get_viewport()->get_popup_base_transform().affine_inverse().xform(cpoint);
431 cpoint = original_transform.affine_inverse().xform(cpoint);
432 last_point = cpoint;
433
434 set_handle(edit_handle, cpoint);
435
436 return true;
437 }
438
439 Ref<InputEventKey> k = p_event;
440
441 if (k.is_valid()) {
442 if (edit_handle == -1 || !pressed || k->is_echo()) {
443 return false;
444 }
445
446 if (shape_type == RECTANGLE_SHAPE && k->get_keycode() == Key::ALT) {
447 set_handle(edit_handle, last_point); // Update handle when Alt key is toggled.
448 }
449 }
450
451 return false;
452 }
453
454 void CollisionShape2DEditor::_shape_changed() {
455 canvas_item_editor->update_viewport();
456
457 if (current_shape.is_valid()) {
458 current_shape->disconnect_changed(callable_mp(canvas_item_editor, &CanvasItemEditor::update_viewport));
459 current_shape = Ref<Shape2D>();
460 shape_type = -1;
461 }
462
463 if (!node) {
464 return;
465 }
466
467 current_shape = node->get_shape();
468
469 if (current_shape.is_valid()) {
470 current_shape->connect_changed(callable_mp(canvas_item_editor, &CanvasItemEditor::update_viewport));
471 } else {
472 return;
473 }
474
475 if (Object::cast_to<CapsuleShape2D>(*current_shape)) {
476 shape_type = CAPSULE_SHAPE;
477 } else if (Object::cast_to<CircleShape2D>(*current_shape)) {
478 shape_type = CIRCLE_SHAPE;
479 } else if (Object::cast_to<ConcavePolygonShape2D>(*current_shape)) {
480 shape_type = CONCAVE_POLYGON_SHAPE;
481 } else if (Object::cast_to<ConvexPolygonShape2D>(*current_shape)) {
482 shape_type = CONVEX_POLYGON_SHAPE;
483 } else if (Object::cast_to<WorldBoundaryShape2D>(*current_shape)) {
484 shape_type = WORLD_BOUNDARY_SHAPE;
485 } else if (Object::cast_to<SeparationRayShape2D>(*current_shape)) {
486 shape_type = SEPARATION_RAY_SHAPE;
487 } else if (Object::cast_to<RectangleShape2D>(*current_shape)) {
488 shape_type = RECTANGLE_SHAPE;
489 } else if (Object::cast_to<SegmentShape2D>(*current_shape)) {
490 shape_type = SEGMENT_SHAPE;
491 }
492 }
493
494 void CollisionShape2DEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {
495 if (!node) {
496 return;
497 }
498
499 if (!node->is_visible_in_tree()) {
500 return;
501 }
502
503 Viewport *vp = node->get_viewport();
504 if (vp && !vp->is_visible_subviewport()) {
505 return;
506 }
507
508 if (shape_type == -1) {
509 return;
510 }
511
512 Transform2D gt = canvas_item_editor->get_canvas_transform() * node->get_screen_transform();
513
514 Ref<Texture2D> h = get_editor_theme_icon(SNAME("EditorHandle"));
515 Vector2 size = h->get_size() * 0.5;
516
517 handles.clear();
518
519 switch (shape_type) {
520 case CAPSULE_SHAPE: {
521 Ref<CapsuleShape2D> shape = current_shape;
522
523 handles.resize(2);
524 float radius = shape->get_radius();
525 float height = shape->get_height() / 2;
526
527 handles.write[0] = Point2(radius, 0);
528 handles.write[1] = Point2(0, height);
529
530 p_overlay->draw_texture(h, gt.xform(handles[0]) - size);
531 p_overlay->draw_texture(h, gt.xform(handles[1]) - size);
532
533 } break;
534
535 case CIRCLE_SHAPE: {
536 Ref<CircleShape2D> shape = current_shape;
537
538 handles.resize(1);
539 handles.write[0] = Point2(shape->get_radius(), 0);
540
541 p_overlay->draw_texture(h, gt.xform(handles[0]) - size);
542
543 } break;
544
545 case CONCAVE_POLYGON_SHAPE: {
546 Ref<ConcavePolygonShape2D> shape = current_shape;
547
548 const Vector<Vector2> &segments = shape->get_segments();
549
550 handles.resize(segments.size());
551 for (int i = 0; i < handles.size(); i++) {
552 handles.write[i] = segments[i];
553 p_overlay->draw_texture(h, gt.xform(handles[i]) - size);
554 }
555
556 } break;
557
558 case CONVEX_POLYGON_SHAPE: {
559 Ref<ConvexPolygonShape2D> shape = current_shape;
560
561 const Vector<Vector2> &points = shape->get_points();
562
563 handles.resize(points.size());
564 for (int i = 0; i < handles.size(); i++) {
565 handles.write[i] = points[i];
566 p_overlay->draw_texture(h, gt.xform(handles[i]) - size);
567 }
568
569 } break;
570
571 case WORLD_BOUNDARY_SHAPE: {
572 Ref<WorldBoundaryShape2D> shape = current_shape;
573
574 handles.resize(2);
575 handles.write[0] = shape->get_normal() * shape->get_distance();
576 handles.write[1] = shape->get_normal() * (shape->get_distance() + 30.0);
577
578 p_overlay->draw_texture(h, gt.xform(handles[0]) - size);
579 p_overlay->draw_texture(h, gt.xform(handles[1]) - size);
580
581 } break;
582
583 case SEPARATION_RAY_SHAPE: {
584 Ref<SeparationRayShape2D> shape = current_shape;
585
586 handles.resize(1);
587 handles.write[0] = Point2(0, shape->get_length());
588
589 p_overlay->draw_texture(h, gt.xform(handles[0]) - size);
590
591 } break;
592
593 case RECTANGLE_SHAPE: {
594 Ref<RectangleShape2D> shape = current_shape;
595
596 handles.resize(8);
597 Vector2 ext = shape->get_size() / 2;
598 for (int i = 0; i < handles.size(); i++) {
599 handles.write[i] = RECT_HANDLES[i] * ext;
600 p_overlay->draw_texture(h, gt.xform(handles[i]) - size);
601 }
602
603 } break;
604
605 case SEGMENT_SHAPE: {
606 Ref<SegmentShape2D> shape = current_shape;
607
608 handles.resize(2);
609 handles.write[0] = shape->get_a();
610 handles.write[1] = shape->get_b();
611
612 p_overlay->draw_texture(h, gt.xform(handles[0]) - size);
613 p_overlay->draw_texture(h, gt.xform(handles[1]) - size);
614
615 } break;
616 }
617 }
618
619 void CollisionShape2DEditor::_notification(int p_what) {
620 switch (p_what) {
621 case NOTIFICATION_ENTER_TREE: {
622 get_tree()->connect("node_removed", callable_mp(this, &CollisionShape2DEditor::_node_removed));
623 } break;
624
625 case NOTIFICATION_EXIT_TREE: {
626 get_tree()->disconnect("node_removed", callable_mp(this, &CollisionShape2DEditor::_node_removed));
627 } break;
628
629 case NOTIFICATION_PROCESS: {
630 if (node && node->get_shape() != current_shape) {
631 _shape_changed();
632 }
633 } break;
634
635 case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
636 if (EditorSettings::get_singleton()->check_changed_settings_in_group("editors/polygon_editor")) {
637 grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
638 }
639 } break;
640
641 case NOTIFICATION_WM_WINDOW_FOCUS_OUT: {
642 // Commit the drag if the window is focused out.
643 if (pressed && edit_handle > 0) {
644 commit_handle(edit_handle, original);
645
646 edit_handle = -1;
647 pressed = false;
648 }
649 } break;
650 }
651 }
652
653 void CollisionShape2DEditor::edit(Node *p_node) {
654 if (!canvas_item_editor) {
655 canvas_item_editor = CanvasItemEditor::get_singleton();
656 }
657
658 if (p_node) {
659 node = Object::cast_to<CollisionShape2D>(p_node);
660 set_process(true);
661 } else {
662 if (pressed) {
663 set_handle(edit_handle, original_point);
664 pressed = false;
665 }
666 edit_handle = -1;
667 node = nullptr;
668 set_process(false);
669 }
670 _shape_changed();
671 }
672
673 void CollisionShape2DEditorPlugin::edit(Object *p_obj) {
674 collision_shape_2d_editor->edit(Object::cast_to<Node>(p_obj));
675 }
676
677 bool CollisionShape2DEditorPlugin::handles(Object *p_obj) const {
678 return p_obj->is_class("CollisionShape2D");
679 }
680
681 void CollisionShape2DEditorPlugin::make_visible(bool visible) {
682 if (!visible) {
683 edit(nullptr);
684 }
685 }
686
687 CollisionShape2DEditorPlugin::CollisionShape2DEditorPlugin() {
688 collision_shape_2d_editor = memnew(CollisionShape2DEditor);
689 EditorNode::get_singleton()->get_gui_base()->add_child(collision_shape_2d_editor);
690 }