navigation_mesh_editor_plugin.cpp 5.5 KB

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