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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #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 = NULL;
  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. button_bake->set_pressed(false);
  65. bake_info->set_text("");
  66. if (node) {
  67. node->update_gizmo();
  68. }
  69. }
  70. void NavigationMeshEditor::edit(NavigationRegion *p_nav_region) {
  71. if (p_nav_region == NULL || node == p_nav_region) {
  72. return;
  73. }
  74. node = p_nav_region;
  75. }
  76. void NavigationMeshEditor::_bind_methods() {
  77. }
  78. NavigationMeshEditor::NavigationMeshEditor() {
  79. bake_hbox = memnew(HBoxContainer);
  80. button_bake = memnew(ToolButton);
  81. bake_hbox->add_child(button_bake);
  82. button_bake->set_toggle_mode(true);
  83. button_bake->set_text(TTR("Bake NavMesh"));
  84. button_bake->connect("pressed", callable_mp(this, &NavigationMeshEditor::_bake_pressed));
  85. button_reset = memnew(ToolButton);
  86. bake_hbox->add_child(button_reset);
  87. // No button text, we only use a revert icon which is set when entering the tree.
  88. button_reset->set_tooltip(TTR("Clear the navigation mesh."));
  89. button_reset->connect("pressed", callable_mp(this, &NavigationMeshEditor::_clear_pressed));
  90. bake_info = memnew(Label);
  91. bake_hbox->add_child(bake_info);
  92. err_dialog = memnew(AcceptDialog);
  93. add_child(err_dialog);
  94. node = NULL;
  95. }
  96. NavigationMeshEditor::~NavigationMeshEditor() {
  97. }
  98. void NavigationMeshEditorPlugin::edit(Object *p_object) {
  99. navigation_mesh_editor->edit(Object::cast_to<NavigationRegion>(p_object));
  100. }
  101. bool NavigationMeshEditorPlugin::handles(Object *p_object) const {
  102. return p_object->is_class("NavigationRegion");
  103. }
  104. void NavigationMeshEditorPlugin::make_visible(bool p_visible) {
  105. if (p_visible) {
  106. navigation_mesh_editor->show();
  107. navigation_mesh_editor->bake_hbox->show();
  108. } else {
  109. navigation_mesh_editor->hide();
  110. navigation_mesh_editor->bake_hbox->hide();
  111. navigation_mesh_editor->edit(NULL);
  112. }
  113. }
  114. NavigationMeshEditorPlugin::NavigationMeshEditorPlugin(EditorNode *p_node) {
  115. editor = p_node;
  116. navigation_mesh_editor = memnew(NavigationMeshEditor);
  117. editor->get_viewport()->add_child(navigation_mesh_editor);
  118. add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, navigation_mesh_editor->bake_hbox);
  119. navigation_mesh_editor->hide();
  120. navigation_mesh_editor->bake_hbox->hide();
  121. }
  122. NavigationMeshEditorPlugin::~NavigationMeshEditorPlugin() {
  123. }
  124. #endif