Cleaned up and enabled the new navigation mesh generator editor plugins.

This commit is contained in:
Relintai 2023-06-05 21:58:00 +02:00
parent 2296bf693a
commit 03944c456c
6 changed files with 81 additions and 46 deletions

View File

@ -42,8 +42,8 @@ module_obj = []
env_navigation_mesh_generator.add_source_files(module_obj, "*.cpp")
#if env.editor_build:
# env_navigation_mesh_generator.add_source_files(module_obj, "editor/*.cpp")
if env["tools"]:
env_navigation_mesh_generator.add_source_files(module_obj, "editor/*.cpp")
env.modules_sources += module_obj

View File

@ -35,12 +35,14 @@
#include "core/io/marshalls.h"
#include "core/io/resource_saver.h"
#include "editor/editor_node.h"
#include "scene/3d/mesh_instance_3d.h"
#include "scene/3d/navigation_region_3d.h"
#include "scene/3d/mesh_instance.h"
#include "scene/3d/navigation_mesh_instance.h"
#include "scene/gui/box_container.h"
#include "scene/gui/button.h"
#include "scene/gui/dialogs.h"
#include "scene/gui/label.h"
#include "scene/resources/navigation_mesh.h"
#include "scene/resources/navigation_mesh_source_geometry_data_3d.h"
#include "servers/navigation/navigation_mesh_generator.h"
void NavigationMeshEditor::_node_removed(Node *p_node) {
@ -54,8 +56,8 @@ void NavigationMeshEditor::_node_removed(Node *p_node) {
void NavigationMeshEditor::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
button_bake->set_icon(get_theme_icon(SNAME("Bake"), SNAME("EditorIcons")));
button_reset->set_icon(get_theme_icon(SNAME("Reload"), SNAME("EditorIcons")));
button_bake->set_icon(get_theme_icon("Bake", "EditorIcons"));
button_reset->set_icon(get_theme_icon("Reload", "EditorIcons"));
} break;
}
}
@ -77,7 +79,7 @@ void NavigationMeshEditor::_bake_pressed() {
if (srpos != -1) {
String base = path.substr(0, srpos);
if (ResourceLoader::get_resource_type(base) == "PackedScene") {
if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) {
if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_filename() != base) {
err_dialog->set_text(TTR("Cannot generate navigation mesh because it does not belong to the edited scene. Make it unique first."));
err_dialog->popup_centered();
return;
@ -118,7 +120,7 @@ void NavigationMeshEditor::_clear_pressed() {
}
}
void NavigationMeshEditor::edit(NavigationRegion3D *p_nav_region) {
void NavigationMeshEditor::edit(NavigationMeshInstance *p_nav_region) {
if (p_nav_region == nullptr || node == p_nav_region) {
return;
}
@ -127,6 +129,8 @@ void NavigationMeshEditor::edit(NavigationRegion3D *p_nav_region) {
}
void NavigationMeshEditor::_bind_methods() {
ClassDB::bind_method(D_METHOD("_bake_pressed"), &NavigationMeshEditor::_bake_pressed);
ClassDB::bind_method(D_METHOD("_clear_pressed"), &NavigationMeshEditor::_clear_pressed);
}
NavigationMeshEditor::NavigationMeshEditor() {
@ -137,15 +141,15 @@ NavigationMeshEditor::NavigationMeshEditor() {
bake_hbox->add_child(button_bake);
button_bake->set_toggle_mode(true);
button_bake->set_text(TTR("Bake NavigationMesh"));
button_bake->set_tooltip_text(TTR("Bakes the NavigationMesh by first parsing the scene for source geometry and then creating the navigation mesh vertices and indices."));
button_bake->connect("pressed", callable_mp(this, &NavigationMeshEditor::_bake_pressed));
button_bake->set_tooltip(TTR("Bakes the NavigationMesh by first parsing the scene for source geometry and then creating the navigation mesh vertices and indices."));
button_bake->connect("pressed", this, "_bake_pressed");
button_reset = memnew(Button);
button_reset->set_flat(true);
bake_hbox->add_child(button_reset);
button_reset->set_text(TTR("Clear NavigationMesh"));
button_reset->set_tooltip_text(TTR("Clears the internal NavigationMesh vertices and indices."));
button_reset->connect("pressed", callable_mp(this, &NavigationMeshEditor::_clear_pressed));
button_reset->set_tooltip(TTR("Clears the internal NavigationMesh vertices and indices."));
button_reset->connect("pressed", this, "_clear_pressed");
bake_info = memnew(Label);
bake_hbox->add_child(bake_info);
@ -159,11 +163,11 @@ NavigationMeshEditor::~NavigationMeshEditor() {
}
void NavigationMeshEditorPlugin::edit(Object *p_object) {
navigation_mesh_editor->edit(Object::cast_to<NavigationRegion3D>(p_object));
navigation_mesh_editor->edit(Object::cast_to<NavigationMeshInstance>(p_object));
}
bool NavigationMeshEditorPlugin::handles(Object *p_object) const {
return p_object->is_class("NavigationRegion3D");
return p_object->is_class("NavigationMeshInstance");
}
void NavigationMeshEditorPlugin::make_visible(bool p_visible) {
@ -177,9 +181,10 @@ void NavigationMeshEditorPlugin::make_visible(bool p_visible) {
}
}
NavigationMeshEditorPlugin::NavigationMeshEditorPlugin() {
NavigationMeshEditorPlugin::NavigationMeshEditorPlugin(EditorNode *p_node) {
navigation_mesh_editor = memnew(NavigationMeshEditor);
EditorNode::get_singleton()->get_main_screen_control()->add_child(navigation_mesh_editor);
EditorNode::get_singleton()->get_viewport()->add_child(navigation_mesh_editor);
add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, navigation_mesh_editor->bake_hbox);
navigation_mesh_editor->hide();
navigation_mesh_editor->bake_hbox->hide();
@ -188,4 +193,7 @@ NavigationMeshEditorPlugin::NavigationMeshEditorPlugin() {
NavigationMeshEditorPlugin::~NavigationMeshEditorPlugin() {
}
void NavigationMeshEditorPlugin::_bind_methods() {
}
#endif // TOOLS_ENABLED

View File

@ -39,7 +39,7 @@ class AcceptDialog;
class Button;
class HBoxContainer;
class Label;
class NavigationRegion3D;
class NavigationMeshInstance;
class NavigationMeshEditor : public Control {
friend class NavigationMeshEditorPlugin;
@ -53,7 +53,7 @@ class NavigationMeshEditor : public Control {
Button *button_reset = nullptr;
Label *bake_info = nullptr;
NavigationRegion3D *node = nullptr;
NavigationMeshInstance *node = nullptr;
void _bake_pressed();
void _clear_pressed();
@ -64,7 +64,7 @@ protected:
void _notification(int p_what);
public:
void edit(NavigationRegion3D *p_nav_region);
void edit(NavigationMeshInstance *p_nav_region);
NavigationMeshEditor();
~NavigationMeshEditor();
};
@ -81,8 +81,11 @@ public:
virtual bool handles(Object *p_object) const;
virtual void make_visible(bool p_visible);
NavigationMeshEditorPlugin();
NavigationMeshEditorPlugin(EditorNode *p_node);
~NavigationMeshEditorPlugin();
protected:
static void _bind_methods();
};
#endif // TOOLS_ENABLED

View File

@ -32,14 +32,16 @@
#include "core/io/marshalls.h"
#include "core/io/resource_saver.h"
#include "core/object/undo_redo.h"
#include "editor/editor_node.h"
#include "editor/editor_undo_redo_manager.h"
#include "scene/2d/mesh_instance_2d.h"
#include "scene/gui/box_container.h"
#include "scene/gui/button.h"
#include "scene/gui/dialogs.h"
#include "scene/gui/label.h"
#include "scene/gui/option_button.h"
#include "scene/resources/navigation_mesh_source_geometry_data_2d.h"
#include "scene/resources/navigation_polygon.h"
#include "servers/navigation/navigation_mesh_generator.h"
Ref<NavigationPolygon> NavigationPolygonEditor::_ensure_navpoly() const {
@ -84,23 +86,26 @@ void NavigationPolygonEditor::_set_polygon(int p_idx, const Variant &p_polygon)
void NavigationPolygonEditor::_action_add_polygon(const Variant &p_polygon) {
Ref<NavigationPolygon> navpoly = _ensure_navpoly();
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
undo_redo->add_do_method(navpoly.ptr(), "add_outline", p_polygon);
undo_redo->add_undo_method(navpoly.ptr(), "remove_outline", navpoly->get_outline_count());
undo_redo->commit_action();
}
void NavigationPolygonEditor::_action_remove_polygon(int p_idx) {
Ref<NavigationPolygon> navpoly = _ensure_navpoly();
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
undo_redo->add_do_method(navpoly.ptr(), "remove_outline", p_idx);
undo_redo->add_undo_method(navpoly.ptr(), "add_outline_at_index", navpoly->get_outline(p_idx), p_idx);
undo_redo->commit_action();
}
void NavigationPolygonEditor::_action_set_polygon(int p_idx, const Variant &p_previous, const Variant &p_polygon) {
Ref<NavigationPolygon> navpoly = _ensure_navpoly();
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
undo_redo->add_do_method(navpoly.ptr(), "set_outline", p_idx, p_polygon);
undo_redo->add_undo_method(navpoly.ptr(), "set_outline", p_idx, p_previous);
undo_redo->commit_action();
}
bool NavigationPolygonEditor::_has_resource() const {
@ -112,16 +117,19 @@ void NavigationPolygonEditor::_create_resource() {
return;
}
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
undo_redo->create_action(TTR("Create Navigation Polygon"));
undo_redo->add_do_method(node, "set_navigation_polygon", Ref<NavigationPolygon>(memnew(NavigationPolygon)));
undo_redo->add_undo_method(node, "set_navigation_polygon", Variant(Ref<RefCounted>()));
undo_redo->add_undo_method(node, "set_navigation_polygon", Variant(Ref<NavigationPolygon>()));
undo_redo->commit_action();
_menu_option(MODE_CREATE);
}
NavigationPolygonEditor::NavigationPolygonEditor() {
NavigationPolygonEditor::NavigationPolygonEditor(EditorNode *p_editor, bool p_wip_destructive) :
AbstractPolygon2DEditor(p_editor, p_wip_destructive) {
bake_hbox = memnew(HBoxContainer);
add_child(bake_hbox);
@ -130,15 +138,15 @@ NavigationPolygonEditor::NavigationPolygonEditor() {
bake_hbox->add_child(button_bake);
button_bake->set_toggle_mode(true);
button_bake->set_text(TTR("Bake NavigationPolygon"));
button_bake->set_tooltip_text(TTR("Bakes the NavigationPolygon by first parsing the scene for source geometry and then creating the navigation polygon vertices and polygons."));
button_bake->connect("pressed", callable_mp(this, &NavigationPolygonEditor::_bake_pressed));
button_bake->set_tooltip(TTR("Bakes the NavigationPolygon by first parsing the scene for source geometry and then creating the navigation polygon vertices and polygons."));
button_bake->connect("pressed", this, "_bake_pressed");
button_reset = memnew(Button);
button_reset->set_flat(true);
bake_hbox->add_child(button_reset);
button_reset->set_text(TTR("Clear NavigationPolygon"));
button_reset->set_tooltip_text(TTR("Clears the internal NavigationPolygon outlines, vertices and polygons."));
button_reset->connect("pressed", callable_mp(this, &NavigationPolygonEditor::_clear_pressed));
button_reset->set_tooltip(TTR("Clears the internal NavigationPolygon outlines, vertices and polygons."));
button_reset->connect("pressed", this, "_clear_pressed");
bake_info = memnew(Label);
bake_hbox->add_child(bake_info);
@ -148,15 +156,23 @@ NavigationPolygonEditor::NavigationPolygonEditor() {
node = nullptr;
}
NavigationPolygonEditor::~NavigationPolygonEditor() {
}
void NavigationPolygonEditor::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
button_bake->set_icon(get_theme_icon(SNAME("Bake"), SNAME("EditorIcons")));
button_reset->set_icon(get_theme_icon(SNAME("Reload"), SNAME("EditorIcons")));
button_bake->set_icon(get_theme_icon("Bake", "EditorIcons"));
button_reset->set_icon(get_theme_icon("Reload", "EditorIcons"));
} break;
}
}
void NavigationPolygonEditor::_bind_methods() {
ClassDB::bind_method(D_METHOD("_bake_pressed"), &NavigationPolygonEditor::_bake_pressed);
ClassDB::bind_method(D_METHOD("_clear_pressed"), &NavigationPolygonEditor::_clear_pressed);
}
void NavigationPolygonEditor::_bake_pressed() {
button_bake->set_pressed(false);
@ -169,12 +185,12 @@ void NavigationPolygonEditor::_bake_pressed() {
}
navigation_polygon->clear_polygons();
navigation_polygon->set_vertices(Vector<Vector2>());
navigation_polygon->set_vertices(PoolVector<Vector2>());
Ref<NavigationMeshSourceGeometryData2D> source_geometry_data = NavigationMeshGenerator::get_singleton()->parse_2d_source_geometry_data(navigation_polygon, node);
NavigationMeshGenerator::get_singleton()->bake_2d_from_source_geometry_data(navigation_polygon, source_geometry_data);
node->queue_redraw();
node->update();
}
void NavigationPolygonEditor::_clear_pressed() {
@ -189,7 +205,7 @@ void NavigationPolygonEditor::_clear_pressed() {
bake_info->set_text("");
if (node) {
node->queue_redraw();
node->update();
}
}
@ -205,6 +221,12 @@ void NavigationPolygonEditor::_update_polygon_editing_state() {
}
}
NavigationPolygonEditorPlugin::NavigationPolygonEditorPlugin() :
AbstractPolygon2DEditorPlugin(memnew(NavigationPolygonEditor), "NavigationPolygonInstance") {
NavigationPolygonEditorPlugin::NavigationPolygonEditorPlugin(EditorNode *p_node) :
AbstractPolygon2DEditorPlugin(p_node, memnew(NavigationPolygonEditor(p_node)), "NavigationPolygonInstance") {
}
NavigationPolygonEditorPlugin::~NavigationPolygonEditorPlugin() {
}
void NavigationPolygonEditorPlugin::_bind_methods() {
}

View File

@ -63,6 +63,7 @@ class NavigationPolygonEditor : public AbstractPolygon2DEditor {
protected:
void _notification(int p_what);
static void _bind_methods();
virtual Node2D *_get_node() const;
virtual void _set_node(Node *p_polygon);
@ -79,16 +80,19 @@ protected:
virtual void _create_resource();
public:
NavigationPolygonEditor();
NavigationPolygonEditor(EditorNode *p_editor, bool p_wip_destructive = true);
~NavigationPolygonEditor();
};
class NavigationPolygonEditorPlugin : public AbstractPolygon2DEditorPlugin {
GDCLASS(NavigationPolygonEditorPlugin, AbstractPolygon2DEditorPlugin);
NavigationPolygonEditor *navigation_polygon_editor = nullptr;
public:
NavigationPolygonEditorPlugin();
NavigationPolygonEditorPlugin(EditorNode *p_node);
~NavigationPolygonEditorPlugin();
protected:
static void _bind_methods();
};
#endif // NAVIGATION_POLYGON_EDITOR_PLUGIN_H

View File

@ -35,12 +35,10 @@
#include "pandemonium_navigation_mesh_generator.h"
/*
#ifdef TOOLS_ENABLED
#include "editor/navigation_mesh_editor_plugin.h"
#include "editor/navigation_polygon_editor_plugin.h"
#endif // TOOLS_ENABLED
*/
NavigationMeshGenerator *new_navigation_mesh_generator_server() {
return memnew(PandemoniumNavigationMeshGenerator);
@ -54,8 +52,8 @@ void register_navigation_mesh_generator_types(ModuleRegistrationLevel p_level) {
#ifdef TOOLS_ENABLED
if (p_level == MODULE_REGISTRATION_LEVEL_EDITOR) {
//EditorPlugins::add_by_type<NavigationMeshEditorPlugin>();
//EditorPlugins::add_by_type<NavigationPolygonEditorPlugin>();
EditorPlugins::add_by_type<NavigationMeshEditorPlugin>();
EditorPlugins::add_by_type<NavigationPolygonEditorPlugin>();
}
#endif // TOOLS_ENABLED
}