cast_2d_editor_plugin.cpp 5.7 KB

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