navigation_mesh_editor_plugin.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 "scene/3d/mesh_instance_3d.h"
  36. #include "scene/3d/navigation_region_3d.h"
  37. #include "scene/gui/box_container.h"
  38. #include "scene/gui/button.h"
  39. #include "scene/gui/dialogs.h"
  40. #include "scene/gui/label.h"
  41. #include "scene/resources/navigation_mesh_source_geometry_data_3d.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_icon(get_theme_icon(SNAME("Bake"), SNAME("EditorIcons")));
  52. button_reset->set_icon(get_theme_icon(SNAME("Reload"), SNAME("EditorIcons")));
  53. } break;
  54. }
  55. }
  56. void NavigationMeshEditor::_bake_pressed() {
  57. button_bake->set_pressed(false);
  58. ERR_FAIL_COND(!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(false);
  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. void NavigationMeshEditor::_bind_methods() {
  113. }
  114. NavigationMeshEditor::NavigationMeshEditor() {
  115. bake_hbox = memnew(HBoxContainer);
  116. button_bake = memnew(Button);
  117. button_bake->set_flat(true);
  118. bake_hbox->add_child(button_bake);
  119. button_bake->set_toggle_mode(true);
  120. button_bake->set_text(TTR("Bake NavigationMesh"));
  121. 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."));
  122. button_bake->connect("pressed", callable_mp(this, &NavigationMeshEditor::_bake_pressed));
  123. button_reset = memnew(Button);
  124. button_reset->set_flat(true);
  125. bake_hbox->add_child(button_reset);
  126. button_reset->set_text(TTR("Clear NavigationMesh"));
  127. button_reset->set_tooltip_text(TTR("Clears the internal NavigationMesh vertices and polygons."));
  128. button_reset->connect("pressed", callable_mp(this, &NavigationMeshEditor::_clear_pressed));
  129. bake_info = memnew(Label);
  130. bake_hbox->add_child(bake_info);
  131. err_dialog = memnew(AcceptDialog);
  132. add_child(err_dialog);
  133. node = nullptr;
  134. }
  135. NavigationMeshEditor::~NavigationMeshEditor() {
  136. }
  137. void NavigationMeshEditorPlugin::edit(Object *p_object) {
  138. navigation_mesh_editor->edit(Object::cast_to<NavigationRegion3D>(p_object));
  139. }
  140. bool NavigationMeshEditorPlugin::handles(Object *p_object) const {
  141. return p_object->is_class("NavigationRegion3D");
  142. }
  143. void NavigationMeshEditorPlugin::make_visible(bool p_visible) {
  144. if (p_visible) {
  145. navigation_mesh_editor->show();
  146. navigation_mesh_editor->bake_hbox->show();
  147. } else {
  148. navigation_mesh_editor->hide();
  149. navigation_mesh_editor->bake_hbox->hide();
  150. navigation_mesh_editor->edit(nullptr);
  151. }
  152. }
  153. NavigationMeshEditorPlugin::NavigationMeshEditorPlugin() {
  154. navigation_mesh_editor = memnew(NavigationMeshEditor);
  155. EditorNode::get_singleton()->get_main_screen_control()->add_child(navigation_mesh_editor);
  156. add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, navigation_mesh_editor->bake_hbox);
  157. navigation_mesh_editor->hide();
  158. navigation_mesh_editor->bake_hbox->hide();
  159. }
  160. NavigationMeshEditorPlugin::~NavigationMeshEditorPlugin() {
  161. }
  162. #endif // TOOLS_ENABLED