navigation_mesh_editor_plugin.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**************************************************************************/
  2. /* navigation_mesh_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_mesh_editor_plugin.h"
  31. #ifdef TOOLS_ENABLED
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_string_names.h"
  34. #include "scene/3d/navigation_region_3d.h"
  35. #include "scene/gui/box_container.h"
  36. #include "scene/gui/button.h"
  37. #include "scene/gui/dialogs.h"
  38. #include "scene/gui/label.h"
  39. void NavigationMeshEditor::_node_removed(Node *p_node) {
  40. if (p_node == node) {
  41. node = nullptr;
  42. hide();
  43. }
  44. }
  45. void NavigationMeshEditor::_notification(int p_what) {
  46. switch (p_what) {
  47. case NOTIFICATION_ENTER_TREE: {
  48. button_bake->set_button_icon(get_theme_icon(SNAME("Bake"), EditorStringName(EditorIcons)));
  49. button_reset->set_button_icon(get_theme_icon(SNAME("Reload"), EditorStringName(EditorIcons)));
  50. } break;
  51. }
  52. }
  53. void NavigationMeshEditor::_bake_pressed() {
  54. button_bake->set_pressed(false);
  55. ERR_FAIL_NULL(node);
  56. Ref<NavigationMesh> navmesh = node->get_navigation_mesh();
  57. if (navmesh.is_null()) {
  58. err_dialog->set_text(TTR("A NavigationMesh resource must be set or created for this node to work."));
  59. err_dialog->popup_centered();
  60. return;
  61. }
  62. String path = navmesh->get_path();
  63. if (!path.is_resource_file()) {
  64. int srpos = path.find("::");
  65. if (srpos != -1) {
  66. String base = path.substr(0, srpos);
  67. if (ResourceLoader::get_resource_type(base) == "PackedScene") {
  68. if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) {
  69. err_dialog->set_text(TTR("Cannot generate navigation mesh because it does not belong to the edited scene. Make it unique first."));
  70. err_dialog->popup_centered();
  71. return;
  72. }
  73. } else {
  74. if (FileAccess::exists(base + ".import")) {
  75. err_dialog->set_text(TTR("Cannot generate navigation mesh because it belongs to a resource which was imported."));
  76. err_dialog->popup_centered();
  77. return;
  78. }
  79. }
  80. }
  81. } else {
  82. if (FileAccess::exists(path + ".import")) {
  83. err_dialog->set_text(TTR("Cannot generate navigation mesh because the resource was imported from another type."));
  84. err_dialog->popup_centered();
  85. return;
  86. }
  87. }
  88. node->bake_navigation_mesh(true);
  89. node->update_gizmos();
  90. }
  91. void NavigationMeshEditor::_clear_pressed() {
  92. if (node) {
  93. if (node->get_navigation_mesh().is_valid()) {
  94. node->get_navigation_mesh()->clear();
  95. }
  96. }
  97. button_bake->set_pressed(false);
  98. bake_info->set_text("");
  99. if (node) {
  100. node->update_gizmos();
  101. }
  102. }
  103. void NavigationMeshEditor::edit(NavigationRegion3D *p_nav_region) {
  104. if (p_nav_region == nullptr || node == p_nav_region) {
  105. return;
  106. }
  107. node = p_nav_region;
  108. }
  109. NavigationMeshEditor::NavigationMeshEditor() {
  110. bake_hbox = memnew(HBoxContainer);
  111. button_bake = memnew(Button);
  112. button_bake->set_theme_type_variation(SceneStringName(FlatButton));
  113. bake_hbox->add_child(button_bake);
  114. button_bake->set_toggle_mode(true);
  115. button_bake->set_text(TTR("Bake NavigationMesh"));
  116. button_bake->set_tooltip_text(TTR("Bakes the NavigationMesh by first parsing the scene for source geometry and then creating the navigation mesh vertices and polygons."));
  117. button_bake->connect(SceneStringName(pressed), callable_mp(this, &NavigationMeshEditor::_bake_pressed));
  118. button_reset = memnew(Button);
  119. button_reset->set_theme_type_variation(SceneStringName(FlatButton));
  120. bake_hbox->add_child(button_reset);
  121. button_reset->set_text(TTR("Clear NavigationMesh"));
  122. button_reset->set_tooltip_text(TTR("Clears the internal NavigationMesh vertices and polygons."));
  123. button_reset->connect(SceneStringName(pressed), callable_mp(this, &NavigationMeshEditor::_clear_pressed));
  124. bake_info = memnew(Label);
  125. bake_hbox->add_child(bake_info);
  126. err_dialog = memnew(AcceptDialog);
  127. add_child(err_dialog);
  128. node = nullptr;
  129. }
  130. NavigationMeshEditor::~NavigationMeshEditor() {
  131. }
  132. void NavigationMeshEditorPlugin::edit(Object *p_object) {
  133. navigation_mesh_editor->edit(Object::cast_to<NavigationRegion3D>(p_object));
  134. }
  135. bool NavigationMeshEditorPlugin::handles(Object *p_object) const {
  136. return p_object->is_class("NavigationRegion3D");
  137. }
  138. void NavigationMeshEditorPlugin::make_visible(bool p_visible) {
  139. if (p_visible) {
  140. navigation_mesh_editor->show();
  141. navigation_mesh_editor->bake_hbox->show();
  142. } else {
  143. navigation_mesh_editor->hide();
  144. navigation_mesh_editor->bake_hbox->hide();
  145. navigation_mesh_editor->edit(nullptr);
  146. }
  147. }
  148. NavigationMeshEditorPlugin::NavigationMeshEditorPlugin() {
  149. navigation_mesh_editor = memnew(NavigationMeshEditor);
  150. EditorNode::get_singleton()->get_gui_base()->add_child(navigation_mesh_editor);
  151. add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, navigation_mesh_editor->bake_hbox);
  152. navigation_mesh_editor->hide();
  153. navigation_mesh_editor->bake_hbox->hide();
  154. }
  155. NavigationMeshEditorPlugin::~NavigationMeshEditorPlugin() {
  156. }
  157. #endif // TOOLS_ENABLED