cast_2d_editor_plugin.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**************************************************************************/
  2. /* cast_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. #include "cast_2d_editor_plugin.h"
  31. #include "canvas_item_editor_plugin.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_undo_redo_manager.h"
  34. #include "scene/2d/physics/ray_cast_2d.h"
  35. #include "scene/2d/physics/shape_cast_2d.h"
  36. #include "scene/main/viewport.h"
  37. void Cast2DEditor::_notification(int p_what) {
  38. switch (p_what) {
  39. case NOTIFICATION_ENTER_TREE: {
  40. get_tree()->connect("node_removed", callable_mp(this, &Cast2DEditor::_node_removed));
  41. } break;
  42. case NOTIFICATION_EXIT_TREE: {
  43. get_tree()->disconnect("node_removed", callable_mp(this, &Cast2DEditor::_node_removed));
  44. } break;
  45. }
  46. }
  47. void Cast2DEditor::_node_removed(Node *p_node) {
  48. if (p_node == node) {
  49. node = nullptr;
  50. }
  51. }
  52. bool Cast2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
  53. if (!node || !node->is_visible_in_tree()) {
  54. return false;
  55. }
  56. Viewport *vp = node->get_viewport();
  57. if (vp && !vp->is_visible_subviewport()) {
  58. return false;
  59. }
  60. Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_screen_transform();
  61. Ref<InputEventMouseButton> mb = p_event;
  62. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
  63. Vector2 target_position = node->get("target_position");
  64. Vector2 gpoint = mb->get_position();
  65. if (mb->is_pressed()) {
  66. if (xform.xform(target_position).distance_to(gpoint) < 8) {
  67. pressed = true;
  68. original_target_position = target_position;
  69. original_mouse_pos = gpoint;
  70. return true;
  71. } else {
  72. pressed = false;
  73. return false;
  74. }
  75. } else if (pressed) {
  76. if (original_mouse_pos != gpoint) {
  77. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  78. undo_redo->create_action(TTR("Set Target Position"));
  79. undo_redo->add_do_property(node, "target_position", target_position);
  80. undo_redo->add_do_method(canvas_item_editor, "update_viewport");
  81. undo_redo->add_undo_property(node, "target_position", original_target_position);
  82. undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
  83. undo_redo->commit_action();
  84. }
  85. pressed = false;
  86. return true;
  87. }
  88. }
  89. Ref<InputEventMouseMotion> mm = p_event;
  90. if (mm.is_valid() && pressed) {
  91. Vector2 point = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mm->get_position()));
  92. point = node->get_screen_transform().affine_inverse().xform(point);
  93. node->set("target_position", point);
  94. canvas_item_editor->update_viewport();
  95. return true;
  96. }
  97. return false;
  98. }
  99. void Cast2DEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {
  100. if (!node || !node->is_visible_in_tree()) {
  101. return;
  102. }
  103. Viewport *vp = node->get_viewport();
  104. if (vp && !vp->is_visible_subviewport()) {
  105. return;
  106. }
  107. Transform2D gt = canvas_item_editor->get_canvas_transform() * node->get_screen_transform();
  108. const Ref<Texture2D> handle = get_editor_theme_icon(SNAME("EditorHandle"));
  109. p_overlay->draw_texture(handle, gt.xform((Vector2)node->get("target_position")) - handle->get_size() / 2);
  110. }
  111. void Cast2DEditor::edit(Node2D *p_node) {
  112. if (!canvas_item_editor) {
  113. canvas_item_editor = CanvasItemEditor::get_singleton();
  114. }
  115. if (Object::cast_to<RayCast2D>(p_node) || Object::cast_to<ShapeCast2D>(p_node)) {
  116. node = p_node;
  117. } else {
  118. node = nullptr;
  119. }
  120. canvas_item_editor->update_viewport();
  121. }
  122. ///////////////////////
  123. void Cast2DEditorPlugin::edit(Object *p_object) {
  124. cast_2d_editor->edit(Object::cast_to<Node2D>(p_object));
  125. }
  126. bool Cast2DEditorPlugin::handles(Object *p_object) const {
  127. return Object::cast_to<RayCast2D>(p_object) != nullptr || Object::cast_to<ShapeCast2D>(p_object) != nullptr;
  128. }
  129. void Cast2DEditorPlugin::make_visible(bool p_visible) {
  130. if (!p_visible) {
  131. edit(nullptr);
  132. }
  133. }
  134. Cast2DEditorPlugin::Cast2DEditorPlugin() {
  135. cast_2d_editor = memnew(Cast2DEditor);
  136. EditorNode::get_singleton()->get_gui_base()->add_child(cast_2d_editor);
  137. }