navigation_mesh_editor_plugin.cpp 6.7 KB

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