navigation_mesh_editor_plugin.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "../navigation_mesh_generator.h"
  33. #include "core/io/marshalls.h"
  34. #include "core/io/resource_saver.h"
  35. #include "editor/editor_node.h"
  36. #include "scene/3d/mesh_instance_3d.h"
  37. #include "scene/gui/box_container.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_icon(get_theme_icon(SNAME("Bake"), SNAME("EditorIcons")));
  48. button_reset->set_icon(get_theme_icon(SNAME("Reload"), SNAME("EditorIcons")));
  49. } break;
  50. }
  51. }
  52. void NavigationMeshEditor::_bake_pressed() {
  53. button_bake->set_pressed(false);
  54. ERR_FAIL_COND(!node);
  55. if (!node->get_navigation_mesh().is_valid()) {
  56. err_dialog->set_text(TTR("A NavigationMesh resource must be set or created for this node to work."));
  57. err_dialog->popup_centered();
  58. return;
  59. }
  60. NavigationMeshGenerator::get_singleton()->clear(node->get_navigation_mesh());
  61. NavigationMeshGenerator::get_singleton()->bake(node->get_navigation_mesh(), node);
  62. node->update_gizmos();
  63. }
  64. void NavigationMeshEditor::_clear_pressed() {
  65. if (node) {
  66. NavigationMeshGenerator::get_singleton()->clear(node->get_navigation_mesh());
  67. }
  68. button_bake->set_pressed(false);
  69. bake_info->set_text("");
  70. if (node) {
  71. node->update_gizmos();
  72. }
  73. }
  74. void NavigationMeshEditor::edit(NavigationRegion3D *p_nav_region) {
  75. if (p_nav_region == nullptr || node == p_nav_region) {
  76. return;
  77. }
  78. node = p_nav_region;
  79. }
  80. void NavigationMeshEditor::_bind_methods() {
  81. }
  82. NavigationMeshEditor::NavigationMeshEditor() {
  83. bake_hbox = memnew(HBoxContainer);
  84. button_bake = memnew(Button);
  85. button_bake->set_flat(true);
  86. bake_hbox->add_child(button_bake);
  87. button_bake->set_toggle_mode(true);
  88. button_bake->set_text(TTR("Bake NavMesh"));
  89. button_bake->connect("pressed", callable_mp(this, &NavigationMeshEditor::_bake_pressed));
  90. button_reset = memnew(Button);
  91. button_reset->set_flat(true);
  92. bake_hbox->add_child(button_reset);
  93. // No button text, we only use a revert icon which is set when entering the tree.
  94. button_reset->set_tooltip(TTR("Clear the navigation mesh."));
  95. button_reset->connect("pressed", callable_mp(this, &NavigationMeshEditor::_clear_pressed));
  96. bake_info = memnew(Label);
  97. bake_hbox->add_child(bake_info);
  98. err_dialog = memnew(AcceptDialog);
  99. add_child(err_dialog);
  100. node = nullptr;
  101. }
  102. NavigationMeshEditor::~NavigationMeshEditor() {
  103. }
  104. void NavigationMeshEditorPlugin::edit(Object *p_object) {
  105. navigation_mesh_editor->edit(Object::cast_to<NavigationRegion3D>(p_object));
  106. }
  107. bool NavigationMeshEditorPlugin::handles(Object *p_object) const {
  108. return p_object->is_class("NavigationRegion3D");
  109. }
  110. void NavigationMeshEditorPlugin::make_visible(bool p_visible) {
  111. if (p_visible) {
  112. navigation_mesh_editor->show();
  113. navigation_mesh_editor->bake_hbox->show();
  114. } else {
  115. navigation_mesh_editor->hide();
  116. navigation_mesh_editor->bake_hbox->hide();
  117. navigation_mesh_editor->edit(nullptr);
  118. }
  119. }
  120. NavigationMeshEditorPlugin::NavigationMeshEditorPlugin() {
  121. navigation_mesh_editor = memnew(NavigationMeshEditor);
  122. EditorNode::get_singleton()->get_main_control()->add_child(navigation_mesh_editor);
  123. add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, navigation_mesh_editor->bake_hbox);
  124. navigation_mesh_editor->hide();
  125. navigation_mesh_editor->bake_hbox->hide();
  126. }
  127. NavigationMeshEditorPlugin::~NavigationMeshEditorPlugin() {
  128. }
  129. #endif // TOOLS_ENABLED