navigation_mesh_editor_plugin.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #ifdef TOOLS_ENABLED
  31. #include "navigation_mesh_editor_plugin.h"
  32. #include "core/io/marshalls.h"
  33. #include "core/io/resource_saver.h"
  34. #include "navigation_mesh_generator.h"
  35. #include "scene/3d/mesh_instance.h"
  36. #include "scene/gui/box_container.h"
  37. void NavigationMeshEditor::_node_removed(Node *p_node) {
  38. if (p_node == node) {
  39. node = nullptr;
  40. hide();
  41. }
  42. }
  43. void NavigationMeshEditor::_notification(int p_option) {
  44. if (p_option == NOTIFICATION_ENTER_TREE) {
  45. button_bake->set_icon(get_icon("Bake", "EditorIcons"));
  46. button_reset->set_icon(get_icon("Reload", "EditorIcons"));
  47. }
  48. }
  49. void NavigationMeshEditor::_bake_pressed() {
  50. button_bake->set_pressed(false);
  51. ERR_FAIL_COND(!node);
  52. if (!node->get_navigation_mesh().is_valid()) {
  53. err_dialog->set_text(TTR("A NavigationMesh resource must be set or created for this node to work."));
  54. err_dialog->popup_centered_minsize();
  55. return;
  56. }
  57. NavigationMeshGenerator::get_singleton()->clear(node->get_navigation_mesh());
  58. NavigationMeshGenerator::get_singleton()->bake(node->get_navigation_mesh(), node);
  59. node->update_gizmo();
  60. }
  61. void NavigationMeshEditor::_clear_pressed() {
  62. if (node) {
  63. NavigationMeshGenerator::get_singleton()->clear(node->get_navigation_mesh());
  64. }
  65. button_bake->set_pressed(false);
  66. bake_info->set_text("");
  67. if (node) {
  68. node->update_gizmo();
  69. }
  70. }
  71. void NavigationMeshEditor::edit(NavigationMeshInstance *p_nav_mesh_instance) {
  72. if (p_nav_mesh_instance == nullptr || node == p_nav_mesh_instance) {
  73. return;
  74. }
  75. node = p_nav_mesh_instance;
  76. }
  77. void NavigationMeshEditor::_bind_methods() {
  78. ClassDB::bind_method("_bake_pressed", &NavigationMeshEditor::_bake_pressed);
  79. ClassDB::bind_method("_clear_pressed", &NavigationMeshEditor::_clear_pressed);
  80. }
  81. NavigationMeshEditor::NavigationMeshEditor() {
  82. bake_hbox = memnew(HBoxContainer);
  83. button_bake = memnew(ToolButton);
  84. bake_hbox->add_child(button_bake);
  85. button_bake->set_toggle_mode(true);
  86. button_bake->set_text(TTR("Bake NavMesh"));
  87. button_bake->connect("pressed", this, "_bake_pressed");
  88. button_reset = memnew(ToolButton);
  89. bake_hbox->add_child(button_reset);
  90. // No button text, we only use a revert icon which is set when entering the tree.
  91. button_reset->set_tooltip(TTR("Clear the navigation mesh."));
  92. button_reset->connect("pressed", this, "_clear_pressed");
  93. bake_info = memnew(Label);
  94. bake_hbox->add_child(bake_info);
  95. err_dialog = memnew(AcceptDialog);
  96. add_child(err_dialog);
  97. node = nullptr;
  98. }
  99. NavigationMeshEditor::~NavigationMeshEditor() {
  100. }
  101. void NavigationMeshEditorPlugin::edit(Object *p_object) {
  102. navigation_mesh_editor->edit(Object::cast_to<NavigationMeshInstance>(p_object));
  103. }
  104. bool NavigationMeshEditorPlugin::handles(Object *p_object) const {
  105. return p_object->is_class("NavigationMeshInstance");
  106. }
  107. void NavigationMeshEditorPlugin::make_visible(bool p_visible) {
  108. if (p_visible) {
  109. navigation_mesh_editor->show();
  110. navigation_mesh_editor->bake_hbox->show();
  111. } else {
  112. navigation_mesh_editor->hide();
  113. navigation_mesh_editor->bake_hbox->hide();
  114. navigation_mesh_editor->edit(nullptr);
  115. }
  116. }
  117. NavigationMeshEditorPlugin::NavigationMeshEditorPlugin(EditorNode *p_node) {
  118. editor = p_node;
  119. navigation_mesh_editor = memnew(NavigationMeshEditor);
  120. editor->get_viewport()->add_child(navigation_mesh_editor);
  121. add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, navigation_mesh_editor->bake_hbox);
  122. navigation_mesh_editor->hide();
  123. navigation_mesh_editor->bake_hbox->hide();
  124. }
  125. NavigationMeshEditorPlugin::~NavigationMeshEditorPlugin() {
  126. }
  127. #endif