navigation_link_2d_editor_plugin.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "servers/navigation_server_3d.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. real_t grab_threshold = EDITOR_GET("editors/polygon_editor/point_grab_radius");
  56. Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_global_transform();
  57. Ref<InputEventMouseButton> mb = p_event;
  58. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
  59. if (mb->is_pressed()) {
  60. // Start position
  61. if (xform.xform(node->get_start_position()).distance_to(mb->get_position()) < grab_threshold) {
  62. start_grabbed = true;
  63. original_start_position = node->get_start_position();
  64. return true;
  65. } else {
  66. start_grabbed = false;
  67. }
  68. // End position
  69. if (xform.xform(node->get_end_position()).distance_to(mb->get_position()) < grab_threshold) {
  70. end_grabbed = true;
  71. original_end_position = node->get_end_position();
  72. return true;
  73. } else {
  74. end_grabbed = false;
  75. }
  76. } else {
  77. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  78. if (start_grabbed) {
  79. undo_redo->create_action(TTR("Set start_position"));
  80. undo_redo->add_do_method(node, "set_start_position", node->get_start_position());
  81. undo_redo->add_do_method(canvas_item_editor, "update_viewport");
  82. undo_redo->add_undo_method(node, "set_start_position", original_start_position);
  83. undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
  84. undo_redo->commit_action();
  85. start_grabbed = false;
  86. return true;
  87. }
  88. if (end_grabbed) {
  89. undo_redo->create_action(TTR("Set end_position"));
  90. undo_redo->add_do_method(node, "set_end_position", node->get_end_position());
  91. undo_redo->add_do_method(canvas_item_editor, "update_viewport");
  92. undo_redo->add_undo_method(node, "set_end_position", original_end_position);
  93. undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
  94. undo_redo->commit_action();
  95. end_grabbed = false;
  96. return true;
  97. }
  98. }
  99. }
  100. Ref<InputEventMouseMotion> mm = p_event;
  101. if (mm.is_valid()) {
  102. Vector2 point = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mm->get_position()));
  103. point = node->get_global_transform().affine_inverse().xform(point);
  104. if (start_grabbed) {
  105. node->set_start_position(point);
  106. canvas_item_editor->update_viewport();
  107. return true;
  108. }
  109. if (end_grabbed) {
  110. node->set_end_position(point);
  111. canvas_item_editor->update_viewport();
  112. return true;
  113. }
  114. }
  115. return false;
  116. }
  117. void NavigationLink2DEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {
  118. if (!node || !node->is_visible_in_tree()) {
  119. return;
  120. }
  121. Transform2D gt = canvas_item_editor->get_canvas_transform() * node->get_global_transform();
  122. Vector2 global_start_position = gt.xform(node->get_start_position());
  123. Vector2 global_end_position = gt.xform(node->get_end_position());
  124. // Only drawing the handles here, since the debug rendering will fill in the rest.
  125. const Ref<Texture2D> handle = get_theme_icon(SNAME("EditorHandle"), SNAME("EditorIcons"));
  126. p_overlay->draw_texture(handle, global_start_position - handle->get_size() / 2);
  127. p_overlay->draw_texture(handle, global_end_position - handle->get_size() / 2);
  128. }
  129. void NavigationLink2DEditor::edit(NavigationLink2D *p_node) {
  130. if (!canvas_item_editor) {
  131. canvas_item_editor = CanvasItemEditor::get_singleton();
  132. }
  133. if (p_node) {
  134. node = p_node;
  135. } else {
  136. node = nullptr;
  137. }
  138. canvas_item_editor->update_viewport();
  139. }
  140. ///////////////////////
  141. void NavigationLink2DEditorPlugin::edit(Object *p_object) {
  142. editor->edit(Object::cast_to<NavigationLink2D>(p_object));
  143. }
  144. bool NavigationLink2DEditorPlugin::handles(Object *p_object) const {
  145. return Object::cast_to<NavigationLink2D>(p_object) != nullptr;
  146. }
  147. void NavigationLink2DEditorPlugin::make_visible(bool p_visible) {
  148. if (!p_visible) {
  149. edit(nullptr);
  150. }
  151. }
  152. NavigationLink2DEditorPlugin::NavigationLink2DEditorPlugin() {
  153. editor = memnew(NavigationLink2DEditor);
  154. EditorNode::get_singleton()->get_gui_base()->add_child(editor);
  155. }