navigation_mesh_editor_plugin.cpp 5.5 KB

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