navigation_mesh_editor_plugin.cpp 6.9 KB

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