cast_2d_editor_plugin.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/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. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  70. undo_redo->create_action(TTR("Set Target Position"));
  71. undo_redo->add_do_property(node, "target_position", target_position);
  72. undo_redo->add_do_method(canvas_item_editor, "update_viewport");
  73. undo_redo->add_undo_property(node, "target_position", original_target_position);
  74. undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
  75. undo_redo->commit_action();
  76. pressed = false;
  77. return true;
  78. }
  79. }
  80. Ref<InputEventMouseMotion> mm = p_event;
  81. if (mm.is_valid() && pressed) {
  82. Vector2 point = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mm->get_position()));
  83. point = node->get_global_transform().affine_inverse().xform(point);
  84. node->set("target_position", point);
  85. canvas_item_editor->update_viewport();
  86. node->notify_property_list_changed();
  87. return true;
  88. }
  89. return false;
  90. }
  91. void Cast2DEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {
  92. if (!node || !node->is_visible_in_tree()) {
  93. return;
  94. }
  95. Transform2D gt = canvas_item_editor->get_canvas_transform() * node->get_global_transform();
  96. const Ref<Texture2D> handle = get_theme_icon(SNAME("EditorHandle"), SNAME("EditorIcons"));
  97. p_overlay->draw_texture(handle, gt.xform((Vector2)node->get("target_position")) - handle->get_size() / 2);
  98. }
  99. void Cast2DEditor::edit(Node2D *p_node) {
  100. if (!canvas_item_editor) {
  101. canvas_item_editor = CanvasItemEditor::get_singleton();
  102. }
  103. if (Object::cast_to<RayCast2D>(p_node) || Object::cast_to<ShapeCast2D>(p_node)) {
  104. node = p_node;
  105. } else {
  106. node = nullptr;
  107. }
  108. canvas_item_editor->update_viewport();
  109. }
  110. ///////////////////////
  111. void Cast2DEditorPlugin::edit(Object *p_object) {
  112. cast_2d_editor->edit(Object::cast_to<Node2D>(p_object));
  113. }
  114. bool Cast2DEditorPlugin::handles(Object *p_object) const {
  115. return Object::cast_to<RayCast2D>(p_object) != nullptr || Object::cast_to<ShapeCast2D>(p_object) != nullptr;
  116. }
  117. void Cast2DEditorPlugin::make_visible(bool p_visible) {
  118. if (!p_visible) {
  119. edit(nullptr);
  120. }
  121. }
  122. Cast2DEditorPlugin::Cast2DEditorPlugin() {
  123. cast_2d_editor = memnew(Cast2DEditor);
  124. EditorNode::get_singleton()->get_gui_base()->add_child(cast_2d_editor);
  125. }