navigation_link_2d_editor_plugin.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**************************************************************************/
  2. /* navigation_link_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 "navigation_link_2d_editor_plugin.h"
  31. #include "canvas_item_editor_plugin.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_settings.h"
  34. #include "editor/editor_undo_redo_manager.h"
  35. #include "scene/main/viewport.h"
  36. void NavigationLink2DEditor::_notification(int p_what) {
  37. switch (p_what) {
  38. case NOTIFICATION_ENTER_TREE: {
  39. get_tree()->connect("node_removed", callable_mp(this, &NavigationLink2DEditor::_node_removed));
  40. } break;
  41. case NOTIFICATION_EXIT_TREE: {
  42. get_tree()->disconnect("node_removed", callable_mp(this, &NavigationLink2DEditor::_node_removed));
  43. } break;
  44. }
  45. }
  46. void NavigationLink2DEditor::_node_removed(Node *p_node) {
  47. if (p_node == node) {
  48. node = nullptr;
  49. }
  50. }
  51. bool NavigationLink2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
  52. if (!node || !node->is_visible_in_tree()) {
  53. return false;
  54. }
  55. Viewport *vp = node->get_viewport();
  56. if (vp && !vp->is_visible_subviewport()) {
  57. return false;
  58. }
  59. real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
  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. if (mb->is_pressed()) {
  64. // Start position
  65. if (xform.xform(node->get_start_position()).distance_to(mb->get_position()) < grab_threshold) {
  66. start_grabbed = true;
  67. original_start_position = node->get_start_position();
  68. return true;
  69. } else {
  70. start_grabbed = false;
  71. }
  72. // End position
  73. if (xform.xform(node->get_end_position()).distance_to(mb->get_position()) < grab_threshold) {
  74. end_grabbed = true;
  75. original_end_position = node->get_end_position();
  76. return true;
  77. } else {
  78. end_grabbed = false;
  79. }
  80. } else {
  81. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  82. if (start_grabbed) {
  83. undo_redo->create_action(TTR("Set start_position"));
  84. undo_redo->add_do_method(node, "set_start_position", node->get_start_position());
  85. undo_redo->add_do_method(canvas_item_editor, "update_viewport");
  86. undo_redo->add_undo_method(node, "set_start_position", original_start_position);
  87. undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
  88. undo_redo->commit_action();
  89. start_grabbed = false;
  90. return true;
  91. }
  92. if (end_grabbed) {
  93. undo_redo->create_action(TTR("Set end_position"));
  94. undo_redo->add_do_method(node, "set_end_position", node->get_end_position());
  95. undo_redo->add_do_method(canvas_item_editor, "update_viewport");
  96. undo_redo->add_undo_method(node, "set_end_position", original_end_position);
  97. undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
  98. undo_redo->commit_action();
  99. end_grabbed = false;
  100. return true;
  101. }
  102. }
  103. }
  104. Ref<InputEventMouseMotion> mm = p_event;
  105. if (mm.is_valid()) {
  106. Vector2 point = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mm->get_position()));
  107. point = node->get_screen_transform().affine_inverse().xform(point);
  108. if (start_grabbed) {
  109. node->set_start_position(point);
  110. canvas_item_editor->update_viewport();
  111. return true;
  112. }
  113. if (end_grabbed) {
  114. node->set_end_position(point);
  115. canvas_item_editor->update_viewport();
  116. return true;
  117. }
  118. }
  119. return false;
  120. }
  121. void NavigationLink2DEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {
  122. if (!node || !node->is_visible_in_tree()) {
  123. return;
  124. }
  125. Viewport *vp = node->get_viewport();
  126. if (vp && !vp->is_visible_subviewport()) {
  127. return;
  128. }
  129. Transform2D gt = canvas_item_editor->get_canvas_transform() * node->get_screen_transform();
  130. Vector2 global_start_position = gt.xform(node->get_start_position());
  131. Vector2 global_end_position = gt.xform(node->get_end_position());
  132. // Only drawing the handles here, since the debug rendering will fill in the rest.
  133. const Ref<Texture2D> handle = get_editor_theme_icon(SNAME("EditorHandle"));
  134. p_overlay->draw_texture(handle, global_start_position - handle->get_size() / 2);
  135. p_overlay->draw_texture(handle, global_end_position - handle->get_size() / 2);
  136. }
  137. void NavigationLink2DEditor::edit(NavigationLink2D *p_node) {
  138. if (!canvas_item_editor) {
  139. canvas_item_editor = CanvasItemEditor::get_singleton();
  140. }
  141. if (p_node) {
  142. node = p_node;
  143. } else {
  144. node = nullptr;
  145. }
  146. canvas_item_editor->update_viewport();
  147. }
  148. ///////////////////////
  149. void NavigationLink2DEditorPlugin::edit(Object *p_object) {
  150. editor->edit(Object::cast_to<NavigationLink2D>(p_object));
  151. }
  152. bool NavigationLink2DEditorPlugin::handles(Object *p_object) const {
  153. return Object::cast_to<NavigationLink2D>(p_object) != nullptr;
  154. }
  155. void NavigationLink2DEditorPlugin::make_visible(bool p_visible) {
  156. if (!p_visible) {
  157. edit(nullptr);
  158. }
  159. }
  160. NavigationLink2DEditorPlugin::NavigationLink2DEditorPlugin() {
  161. editor = memnew(NavigationLink2DEditor);
  162. EditorNode::get_singleton()->get_gui_base()->add_child(editor);
  163. }