ray_cast_2d_editor_plugin.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**************************************************************************/
  2. /* ray_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 "ray_cast_2d_editor_plugin.h"
  31. #include "canvas_item_editor_plugin.h"
  32. #include "editor/editor_node.h"
  33. void RayCast2DEditor::_notification(int p_what) {
  34. switch (p_what) {
  35. case NOTIFICATION_ENTER_TREE: {
  36. get_tree()->connect("node_removed", this, "_node_removed");
  37. } break;
  38. case NOTIFICATION_EXIT_TREE: {
  39. get_tree()->disconnect("node_removed", this, "_node_removed");
  40. } break;
  41. }
  42. }
  43. void RayCast2DEditor::_node_removed(Node *p_node) {
  44. if (p_node == node) {
  45. node = nullptr;
  46. }
  47. }
  48. bool RayCast2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
  49. if (!node || !node->is_visible_in_tree()) {
  50. return false;
  51. }
  52. Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform();
  53. Ref<InputEventMouseButton> mb = p_event;
  54. if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT) {
  55. if (mb->is_pressed()) {
  56. if (xform.xform(node->get_cast_to()).distance_to(mb->get_position()) < 8) {
  57. pressed = true;
  58. original_cast_to = node->get_cast_to();
  59. return true;
  60. } else {
  61. pressed = false;
  62. return false;
  63. }
  64. } else if (pressed) {
  65. undo_redo->create_action(TTR("Set cast_to"));
  66. undo_redo->add_do_method(node, "set_cast_to", node->get_cast_to());
  67. undo_redo->add_do_method(canvas_item_editor, "update_viewport");
  68. undo_redo->add_undo_method(node, "set_cast_to", original_cast_to);
  69. undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
  70. undo_redo->commit_action();
  71. pressed = false;
  72. return true;
  73. }
  74. }
  75. Ref<InputEventMouseMotion> mm = p_event;
  76. if (mm.is_valid() && pressed) {
  77. Vector2 point = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mm->get_position()));
  78. point = node->get_global_transform().affine_inverse().xform(point);
  79. node->set_cast_to(point);
  80. canvas_item_editor->update_viewport();
  81. node->_change_notify();
  82. return true;
  83. }
  84. return false;
  85. }
  86. void RayCast2DEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {
  87. if (!node || !node->is_visible_in_tree()) {
  88. return;
  89. }
  90. Transform2D gt = canvas_item_editor->get_canvas_transform() * node->get_global_transform();
  91. const Ref<Texture> handle = get_icon("EditorHandle", "EditorIcons");
  92. p_overlay->draw_texture(handle, gt.xform(node->get_cast_to()) - handle->get_size() / 2);
  93. }
  94. void RayCast2DEditor::edit(Node *p_node) {
  95. if (!canvas_item_editor) {
  96. canvas_item_editor = CanvasItemEditor::get_singleton();
  97. }
  98. if (p_node) {
  99. node = Object::cast_to<RayCast2D>(p_node);
  100. } else {
  101. node = nullptr;
  102. }
  103. canvas_item_editor->update_viewport();
  104. }
  105. void RayCast2DEditor::_bind_methods() {
  106. ClassDB::bind_method("_node_removed", &RayCast2DEditor::_node_removed);
  107. }
  108. RayCast2DEditor::RayCast2DEditor() {
  109. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  110. }
  111. ///////////////////////
  112. void RayCast2DEditorPlugin::edit(Object *p_object) {
  113. ray_cast_2d_editor->edit(Object::cast_to<RayCast2D>(p_object));
  114. }
  115. bool RayCast2DEditorPlugin::handles(Object *p_object) const {
  116. return Object::cast_to<RayCast2D>(p_object) != nullptr;
  117. }
  118. void RayCast2DEditorPlugin::make_visible(bool p_visible) {
  119. if (!p_visible) {
  120. edit(nullptr);
  121. }
  122. }
  123. RayCast2DEditorPlugin::RayCast2DEditorPlugin(EditorNode *p_editor) {
  124. ray_cast_2d_editor = memnew(RayCast2DEditor);
  125. p_editor->get_gui_base()->add_child(ray_cast_2d_editor);
  126. }