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