From 3fae26277a0e355441190f7b5325969808b89ecb Mon Sep 17 00:00:00 2001 From: smix8 <52464204+smix8@users.noreply.github.com> Date: Sun, 2 Jun 2024 14:04:25 +0200 Subject: [PATCH] Add confirm dialog for render mesh to navigation mesh conversion Adds confirm dialog for render mesh to navigation mesh conversion that lists all the requirement for the pathfinding. --- .../mesh_instance_3d_editor_plugin.cpp | 67 +++++++++++++------ .../plugins/mesh_instance_3d_editor_plugin.h | 3 + 2 files changed, 48 insertions(+), 22 deletions(-) diff --git a/editor/plugins/mesh_instance_3d_editor_plugin.cpp b/editor/plugins/mesh_instance_3d_editor_plugin.cpp index 6eae3b975a8..b934ac298bc 100644 --- a/editor/plugins/mesh_instance_3d_editor_plugin.cpp +++ b/editor/plugins/mesh_instance_3d_editor_plugin.cpp @@ -214,28 +214,7 @@ void MeshInstance3DEditor::_menu_option(int p_option) { } break; case MENU_OPTION_CREATE_NAVMESH: { - Ref nmesh = memnew(NavigationMesh); - - if (nmesh.is_null()) { - return; - } - - nmesh->create_from_mesh(mesh); - NavigationRegion3D *nmi = memnew(NavigationRegion3D); - nmi->set_navigation_mesh(nmesh); - - Node *owner = get_tree()->get_edited_scene_root(); - - EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); - ur->create_action(TTR("Create Navigation Mesh")); - - ur->add_do_method(node, "add_child", nmi, true); - ur->add_do_method(nmi, "set_owner", owner); - ur->add_do_method(Node3DEditor::get_singleton(), SceneStringName(_request_gizmo), nmi); - - ur->add_do_reference(nmi); - ur->add_undo_method(node, "remove_child", nmi); - ur->commit_action(); + navigation_mesh_dialog->popup_centered(Vector2(200, 90)); } break; case MENU_OPTION_CREATE_OUTLINE_MESH: { @@ -472,6 +451,36 @@ void MeshInstance3DEditor::_debug_uv_draw() { debug_uv->draw_multiline(uv_lines, get_theme_color(SNAME("mono_color"), EditorStringName(Editor)) * Color(1, 1, 1, 0.5)); } +void MeshInstance3DEditor::_create_navigation_mesh() { + Ref mesh = node->get_mesh(); + if (mesh.is_null()) { + return; + } + + Ref nmesh = memnew(NavigationMesh); + + if (nmesh.is_null()) { + return; + } + + nmesh->create_from_mesh(mesh); + NavigationRegion3D *nmi = memnew(NavigationRegion3D); + nmi->set_navigation_mesh(nmesh); + + Node *owner = get_tree()->get_edited_scene_root(); + + EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); + ur->create_action(TTR("Create Navigation Mesh")); + + ur->add_do_method(node, "add_child", nmi, true); + ur->add_do_method(nmi, "set_owner", owner); + ur->add_do_method(Node3DEditor::get_singleton(), SceneStringName(_request_gizmo), nmi); + + ur->add_do_reference(nmi); + ur->add_undo_method(node, "remove_child", nmi); + ur->commit_action(); +} + void MeshInstance3DEditor::_create_outline_mesh() { Ref mesh = node->get_mesh(); if (mesh.is_null()) { @@ -608,6 +617,20 @@ MeshInstance3DEditor::MeshInstance3DEditor() { debug_uv->set_custom_minimum_size(Size2(600, 600) * EDSCALE); debug_uv->connect(SceneStringName(draw), callable_mp(this, &MeshInstance3DEditor::_debug_uv_draw)); debug_uv_dialog->add_child(debug_uv); + + navigation_mesh_dialog = memnew(ConfirmationDialog); + navigation_mesh_dialog->set_title(TTR("Create NavigationMesh")); + navigation_mesh_dialog->set_ok_button_text(TTR("Create")); + + VBoxContainer *navigation_mesh_dialog_vbc = memnew(VBoxContainer); + navigation_mesh_dialog->add_child(navigation_mesh_dialog_vbc); + + Label *navigation_mesh_l = memnew(Label); + navigation_mesh_l->set_text(TTR("Before converting a rendering mesh to a navigation mesh, please verify:\n\n- The mesh is two-dimensional.\n- The mesh has no surface overlap.\n- The mesh has no self-intersection.\n- The mesh surfaces have indices.\n\nIf the mesh does not fulfill these requirements, the pathfinding will be broken.")); + navigation_mesh_dialog_vbc->add_child(navigation_mesh_l); + + add_child(navigation_mesh_dialog); + navigation_mesh_dialog->connect("confirmed", callable_mp(this, &MeshInstance3DEditor::_create_navigation_mesh)); } void MeshInstance3DEditorPlugin::edit(Object *p_object) { diff --git a/editor/plugins/mesh_instance_3d_editor_plugin.h b/editor/plugins/mesh_instance_3d_editor_plugin.h index 20c151fb926..c982df9c5fa 100644 --- a/editor/plugins/mesh_instance_3d_editor_plugin.h +++ b/editor/plugins/mesh_instance_3d_editor_plugin.h @@ -82,10 +82,13 @@ class MeshInstance3DEditor : public Control { Control *debug_uv = nullptr; Vector uv_lines; + ConfirmationDialog *navigation_mesh_dialog = nullptr; + void _create_collision_shape(); Vector> create_shape_from_mesh(Ref p_mesh, int p_option, bool p_verbose); void _menu_option(int p_option); void _create_outline_mesh(); + void _create_navigation_mesh(); void _create_uv_lines(int p_layer); friend class MeshInstance3DEditorPlugin;