navigation_polygon_editor_plugin.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**************************************************************************/
  2. /* navigation_polygon_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_polygon_editor_plugin.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/editor_settings.h"
  33. #include "editor/editor_undo_redo_manager.h"
  34. #include "scene/2d/navigation_region_2d.h"
  35. #include "scene/gui/dialogs.h"
  36. Ref<NavigationPolygon> NavigationPolygonEditor::_ensure_navpoly() const {
  37. Ref<NavigationPolygon> navpoly = node->get_navigation_polygon();
  38. if (navpoly.is_null()) {
  39. navpoly.instantiate();
  40. node->set_navigation_polygon(navpoly);
  41. }
  42. return navpoly;
  43. }
  44. Node2D *NavigationPolygonEditor::_get_node() const {
  45. return node;
  46. }
  47. void NavigationPolygonEditor::_set_node(Node *p_polygon) {
  48. node = Object::cast_to<NavigationRegion2D>(p_polygon);
  49. if (node) {
  50. Ref<NavigationPolygon> navpoly = node->get_navigation_polygon();
  51. if (navpoly.is_valid() && navpoly->get_outline_count() > 0 && navpoly->get_polygon_count() == 0) {
  52. // We have outlines drawn / added by the user but no polygons were created for this navmesh yet so let's bake once immediately.
  53. _rebake_timer_timeout();
  54. }
  55. }
  56. }
  57. int NavigationPolygonEditor::_get_polygon_count() const {
  58. Ref<NavigationPolygon> navpoly = node->get_navigation_polygon();
  59. if (navpoly.is_valid()) {
  60. return navpoly->get_outline_count();
  61. } else {
  62. return 0;
  63. }
  64. }
  65. Variant NavigationPolygonEditor::_get_polygon(int p_idx) const {
  66. Ref<NavigationPolygon> navpoly = node->get_navigation_polygon();
  67. if (navpoly.is_valid()) {
  68. return navpoly->get_outline(p_idx);
  69. } else {
  70. return Variant(Vector<Vector2>());
  71. }
  72. }
  73. void NavigationPolygonEditor::_set_polygon(int p_idx, const Variant &p_polygon) const {
  74. Ref<NavigationPolygon> navpoly = _ensure_navpoly();
  75. navpoly->set_outline(p_idx, p_polygon);
  76. if (rebake_timer && _rebake_timer_delay >= 0.0) {
  77. rebake_timer->start();
  78. }
  79. }
  80. void NavigationPolygonEditor::_action_add_polygon(const Variant &p_polygon) {
  81. Ref<NavigationPolygon> navpoly = _ensure_navpoly();
  82. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  83. undo_redo->add_do_method(navpoly.ptr(), "add_outline", p_polygon);
  84. undo_redo->add_undo_method(navpoly.ptr(), "remove_outline", navpoly->get_outline_count());
  85. if (rebake_timer && _rebake_timer_delay >= 0.0) {
  86. rebake_timer->start();
  87. }
  88. }
  89. void NavigationPolygonEditor::_action_remove_polygon(int p_idx) {
  90. Ref<NavigationPolygon> navpoly = _ensure_navpoly();
  91. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  92. undo_redo->add_do_method(navpoly.ptr(), "remove_outline", p_idx);
  93. undo_redo->add_undo_method(navpoly.ptr(), "add_outline_at_index", navpoly->get_outline(p_idx), p_idx);
  94. if (rebake_timer && _rebake_timer_delay >= 0.0) {
  95. rebake_timer->start();
  96. }
  97. }
  98. void NavigationPolygonEditor::_action_set_polygon(int p_idx, const Variant &p_previous, const Variant &p_polygon) {
  99. Ref<NavigationPolygon> navpoly = _ensure_navpoly();
  100. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  101. undo_redo->add_do_method(navpoly.ptr(), "set_outline", p_idx, p_polygon);
  102. undo_redo->add_undo_method(navpoly.ptr(), "set_outline", p_idx, p_previous);
  103. if (rebake_timer && _rebake_timer_delay >= 0.0) {
  104. rebake_timer->start();
  105. }
  106. }
  107. bool NavigationPolygonEditor::_has_resource() const {
  108. return node && node->get_navigation_polygon().is_valid();
  109. }
  110. void NavigationPolygonEditor::_create_resource() {
  111. if (!node) {
  112. return;
  113. }
  114. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  115. undo_redo->create_action(TTR("Create Navigation Polygon"));
  116. undo_redo->add_do_method(node, "set_navigation_polygon", Ref<NavigationPolygon>(memnew(NavigationPolygon)));
  117. undo_redo->add_undo_method(node, "set_navigation_polygon", Variant(Ref<RefCounted>()));
  118. undo_redo->commit_action();
  119. _menu_option(MODE_CREATE);
  120. }
  121. NavigationPolygonEditor::NavigationPolygonEditor() {
  122. bake_hbox = memnew(HBoxContainer);
  123. add_child(bake_hbox);
  124. button_bake = memnew(Button);
  125. button_bake->set_flat(true);
  126. bake_hbox->add_child(button_bake);
  127. button_bake->set_toggle_mode(true);
  128. button_bake->set_text(TTR("Bake NavigationPolygon"));
  129. button_bake->set_tooltip_text(TTR("Bakes the NavigationPolygon by first parsing the scene for source geometry and then creating the navigation polygon vertices and polygons."));
  130. button_bake->connect(SceneStringName(pressed), callable_mp(this, &NavigationPolygonEditor::_bake_pressed));
  131. button_reset = memnew(Button);
  132. button_reset->set_flat(true);
  133. bake_hbox->add_child(button_reset);
  134. button_reset->set_text(TTR("Clear NavigationPolygon"));
  135. button_reset->set_tooltip_text(TTR("Clears the internal NavigationPolygon outlines, vertices and polygons."));
  136. button_reset->connect(SceneStringName(pressed), callable_mp(this, &NavigationPolygonEditor::_clear_pressed));
  137. bake_info = memnew(Label);
  138. bake_hbox->add_child(bake_info);
  139. rebake_timer = memnew(Timer);
  140. add_child(rebake_timer);
  141. rebake_timer->set_one_shot(true);
  142. _rebake_timer_delay = EDITOR_GET("editors/polygon_editor/auto_bake_delay");
  143. if (_rebake_timer_delay >= 0.0) {
  144. rebake_timer->set_wait_time(_rebake_timer_delay);
  145. }
  146. rebake_timer->connect("timeout", callable_mp(this, &NavigationPolygonEditor::_rebake_timer_timeout));
  147. err_dialog = memnew(AcceptDialog);
  148. add_child(err_dialog);
  149. node = nullptr;
  150. }
  151. void NavigationPolygonEditor::_notification(int p_what) {
  152. switch (p_what) {
  153. case NOTIFICATION_ENTER_TREE: {
  154. button_bake->set_button_icon(get_editor_theme_icon(SNAME("Bake")));
  155. button_reset->set_button_icon(get_editor_theme_icon(SNAME("Reload")));
  156. } break;
  157. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  158. if (rebake_timer) {
  159. _rebake_timer_delay = EDITOR_GET("editors/polygon_editor/auto_bake_delay");
  160. if (_rebake_timer_delay >= 0.0) {
  161. rebake_timer->set_wait_time(_rebake_timer_delay);
  162. }
  163. }
  164. } break;
  165. }
  166. }
  167. void NavigationPolygonEditor::_bake_pressed() {
  168. if (rebake_timer) {
  169. rebake_timer->stop();
  170. }
  171. button_bake->set_pressed(false);
  172. ERR_FAIL_NULL(node);
  173. Ref<NavigationPolygon> navigation_polygon = node->get_navigation_polygon();
  174. if (navigation_polygon.is_null()) {
  175. err_dialog->set_text(TTR("A NavigationPolygon resource must be set or created for this node to work."));
  176. err_dialog->popup_centered();
  177. return;
  178. }
  179. node->bake_navigation_polygon(true);
  180. node->queue_redraw();
  181. }
  182. void NavigationPolygonEditor::_clear_pressed() {
  183. if (rebake_timer) {
  184. rebake_timer->stop();
  185. }
  186. if (node) {
  187. if (node->get_navigation_polygon().is_valid()) {
  188. node->get_navigation_polygon()->clear();
  189. // Needed to update all the region internals.
  190. node->set_navigation_polygon(node->get_navigation_polygon());
  191. }
  192. }
  193. button_bake->set_pressed(false);
  194. bake_info->set_text("");
  195. if (node) {
  196. node->queue_redraw();
  197. }
  198. }
  199. void NavigationPolygonEditor::_update_polygon_editing_state() {
  200. if (!_get_node()) {
  201. return;
  202. }
  203. if (node != nullptr && node->get_navigation_polygon().is_valid()) {
  204. bake_hbox->show();
  205. } else {
  206. bake_hbox->hide();
  207. }
  208. }
  209. void NavigationPolygonEditor::_rebake_timer_timeout() {
  210. if (!node) {
  211. return;
  212. }
  213. Ref<NavigationPolygon> navigation_polygon = node->get_navigation_polygon();
  214. if (!navigation_polygon.is_valid()) {
  215. return;
  216. }
  217. node->bake_navigation_polygon(true);
  218. node->queue_redraw();
  219. }
  220. NavigationPolygonEditorPlugin::NavigationPolygonEditorPlugin() :
  221. AbstractPolygon2DEditorPlugin(memnew(NavigationPolygonEditor), "NavigationRegion2D") {
  222. }