From 3220e9c59255caeabbd79ffb9da7328a624a5351 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 20 Aug 2022 01:33:02 +0200 Subject: [PATCH] Now in order to edit MeshDataInstance a new edit mode has to be togged from the top bar. While this mode is enabled the edited MeshDatainstace can't be deselected. Also now Mesh Data Editor's sidebar will only be visible in this mode, along with the editor gizmo. --- modules/mesh_data_resource/editor/mdi_ed.cpp | 43 +++++++++++++++++++ modules/mesh_data_resource/editor/mdi_ed.h | 10 +++++ .../editor/mdi_ed_plugin.cpp | 25 +++++++---- .../mesh_data_resource/editor/mdi_ed_plugin.h | 5 +-- .../mesh_data_resource/editor/mdi_gizmo.cpp | 19 ++++++-- modules/mesh_data_resource/editor/mdi_gizmo.h | 4 ++ 6 files changed, 90 insertions(+), 16 deletions(-) diff --git a/modules/mesh_data_resource/editor/mdi_ed.cpp b/modules/mesh_data_resource/editor/mdi_ed.cpp index bd3727df4..ab4ac310b 100644 --- a/modules/mesh_data_resource/editor/mdi_ed.cpp +++ b/modules/mesh_data_resource/editor/mdi_ed.cpp @@ -27,6 +27,7 @@ SOFTWARE. #include "./uv_editor/mdr_uv_rect_editor.h" #include "./uv_editor/mdr_uv_rect_editor_popup.h" #include "core/os/keyboard.h" +#include "editor/plugins/spatial_editor_plugin.h" #include "mdi_ed_plugin.h" #include "scene/gui/box_container.h" #include "scene/gui/button.h" @@ -34,6 +35,21 @@ SOFTWARE. #include "scene/gui/scroll_container.h" #include "scene/gui/separator.h" +bool MDIEd::is_editing() { + return _editing; +} + +void MDIEd::make_visible(const bool visible) { + _edit_mode_button->set_visible(visible); + + if (!visible) { + hide(); + } else { + set_visible(_editing); + _plugin->set_gizmo_visible(_editing); + } +} + void MDIEd::set_plugin(MDIEdPlugin *plugin) { _plugin = plugin; uv_editor->set_plugin(plugin); @@ -871,11 +887,36 @@ MDIEd::MDIEd() { uv_editor->set_resizable(true); uv_editor->hide(); popups_node->add_child(uv_editor); + + _edit_mode_button = memnew(Button); + _edit_mode_button->set_flat(true); + _edit_mode_button->set_toggle_mode(true); + _edit_mode_button->set_focus_mode(BaseButton::FOCUS_NONE); + _edit_mode_button->set_text("Edit"); + _edit_mode_button->set_tooltip(TTR("Edit MeshDataResource.")); + _edit_mode_button->connect("toggled", this, "_edit_mode_toggled"); + SpatialEditor::get_singleton()->add_control_to_menu_panel(_edit_mode_button); + + _editing = false; } MDIEd::~MDIEd() { } +void MDIEd::_notification(int p_what) { + switch (p_what) { + case NOTIFICATION_ENTER_TREE: { + _edit_mode_button->set_icon(get_theme_icon("MeshInstance", "EditorIcons")); + } break; + } +} + +void MDIEd::_edit_mode_toggled(const bool pressed) { + _editing = pressed; + + make_visible(_editing); +} + void MDIEd::_bind_methods() { ClassDB::bind_method(D_METHOD("on_edit_mode_translate_toggled"), &MDIEd::on_edit_mode_translate_toggled); ClassDB::bind_method(D_METHOD("on_edit_mode_rotate_toggled"), &MDIEd::on_edit_mode_rotate_toggled); @@ -936,4 +977,6 @@ void MDIEd::_bind_methods() { ClassDB::bind_method(D_METHOD("on_add_box_pressed"), &MDIEd::on_add_box_pressed); ClassDB::bind_method(D_METHOD("on_add_triangle_pressed"), &MDIEd::on_add_triangle_pressed); ClassDB::bind_method(D_METHOD("on_add_quad_pressed"), &MDIEd::on_add_quad_pressed); + + ClassDB::bind_method(D_METHOD("_edit_mode_toggled"), &MDIEd::_edit_mode_toggled); } diff --git a/modules/mesh_data_resource/editor/mdi_ed.h b/modules/mesh_data_resource/editor/mdi_ed.h index a72abaaea..98d02e81e 100644 --- a/modules/mesh_data_resource/editor/mdi_ed.h +++ b/modules/mesh_data_resource/editor/mdi_ed.h @@ -38,6 +38,9 @@ class MDIEd : public PanelContainer { GDCLASS(MDIEd, PanelContainer); public: + bool is_editing(); + void make_visible(const bool visible); + void set_plugin(MDIEdPlugin *plugin); void set_mesh_data_resource(const Ref &a); void set_mesh_data_instance(MeshDataInstance *a); @@ -147,8 +150,15 @@ public: Button *_selection_mode_edge_button; Button *_selection_mode_face_button; + Button *_edit_mode_button; + protected: + void _notification(int p_what); static void _bind_methods(); + + void _edit_mode_toggled(const bool pressed); + + bool _editing; }; #endif diff --git a/modules/mesh_data_resource/editor/mdi_ed_plugin.cpp b/modules/mesh_data_resource/editor/mdi_ed_plugin.cpp index 46e20c5d0..17d84c99a 100644 --- a/modules/mesh_data_resource/editor/mdi_ed_plugin.cpp +++ b/modules/mesh_data_resource/editor/mdi_ed_plugin.cpp @@ -54,18 +54,20 @@ void MDIEdPlugin::edit(Object *object) { current_mesh_data_instance = mdi; } void MDIEdPlugin::make_visible(bool visible) { - if (visible) { - mdi_ed_gui->show(); - } - //else - //mdi_ed_gui.hide() - //figure out how to hide it when something else gets selected, don't hide on unselect + mdi_ed_gui->make_visible(visible); } String MDIEdPlugin::get_name() const { return "MeshDataResourceEditor"; } +void MDIEdPlugin::set_gizmo_visible(const bool visible) { + if (current_mesh_data_instance) { + Ref g = get_gizmo_from(current_mesh_data_instance); + g->set_visible(visible); + } +} + void MDIEdPlugin::set_translate() { if (current_mesh_data_instance) { Ref g = get_gizmo_from(current_mesh_data_instance); @@ -137,9 +139,14 @@ EditorPlugin::AfterGUIInput MDIEdPlugin::forward_spatial_gui_input(Camera *camer } if (current_mesh_data_instance) { - Ref g = get_gizmo_from(current_mesh_data_instance); - if (g.is_valid()) { - return g->forward_spatial_gui_input(camera, p_event); + if (mdi_ed_gui->is_editing()) { + Ref g = get_gizmo_from(current_mesh_data_instance); + + if (g.is_valid()) { + return g->forward_spatial_gui_input(camera, p_event); + } + + return EditorPlugin::AFTER_GUI_INPUT_NO_DESELECT; } } diff --git a/modules/mesh_data_resource/editor/mdi_ed_plugin.h b/modules/mesh_data_resource/editor/mdi_ed_plugin.h index 4cd21a6ea..dab14fb66 100644 --- a/modules/mesh_data_resource/editor/mdi_ed_plugin.h +++ b/modules/mesh_data_resource/editor/mdi_ed_plugin.h @@ -27,9 +27,6 @@ SOFTWARE. #include "core/input/input_event.h" -//TODO: -//Remove gizmo registration, it's not needed anymore - class Camera; class MDIGizmo; class MeshDataResource; @@ -47,6 +44,8 @@ public: String get_name() const; + void set_gizmo_visible(const bool visible); + void set_translate(); void set_scale(); void set_rotate(); diff --git a/modules/mesh_data_resource/editor/mdi_gizmo.cpp b/modules/mesh_data_resource/editor/mdi_gizmo.cpp index 4852b593e..30c8c01e3 100644 --- a/modules/mesh_data_resource/editor/mdi_gizmo.cpp +++ b/modules/mesh_data_resource/editor/mdi_gizmo.cpp @@ -32,6 +32,11 @@ SOFTWARE. #include "modules/mesh_utils/mesh_utils.h" #include "scene/3d/camera.h" +void MDIGizmo::set_visible(const bool visible) { + _visible = visible; + redraw(); +} + void MDIGizmo::setup() { MeshDataInstance *mdi = Object::cast_to(get_spatial_node()); @@ -151,6 +156,10 @@ void MDIGizmo::set_handle(int index, bool secondary, Camera *camera, const Point void MDIGizmo::redraw() { clear(); + if (!_visible) { + return; + } + if (!_mdr.is_valid()) { return; } @@ -606,7 +615,7 @@ EditorPlugin::AfterGUIInput MDIGizmo::forward_spatial_gui_input(Camera *camera, // Dont consume the event here, because the handles will get stuck // to the mouse pointer if we return true - return EditorPlugin::AFTER_GUI_INPUT_PASS; + return EditorPlugin::AFTER_GUI_INPUT_NO_DESELECT; } if (!event_button->is_pressed()) { @@ -625,13 +634,13 @@ EditorPlugin::AfterGUIInput MDIGizmo::forward_spatial_gui_input(Camera *camera, if (selection_click(camera, event)) { return EditorPlugin::AFTER_GUI_INPUT_STOP; } else { - return EditorPlugin::AFTER_GUI_INPUT_PASS; + return EditorPlugin::AFTER_GUI_INPUT_NO_DESELECT; } } else { selection_drag(camera, event_button); // Always return false here, so the drag rect thing disappears in the editor - return EditorPlugin::AFTER_GUI_INPUT_PASS; + return EditorPlugin::AFTER_GUI_INPUT_NO_DESELECT; } } else { // event is pressed @@ -641,7 +650,7 @@ EditorPlugin::AfterGUIInput MDIGizmo::forward_spatial_gui_input(Camera *camera, } } - return EditorPlugin::AFTER_GUI_INPUT_PASS; + return EditorPlugin::AFTER_GUI_INPUT_NO_DESELECT; } void MDIGizmo::add_to_all_selected(const Vector3 &ofs) { for (int i = 0; i < _selected_points.size(); ++i) { @@ -2204,6 +2213,8 @@ MDIGizmo::MDIGizmo() { _editor_plugin = nullptr; _undo_redo = nullptr; + + _visible = false; } MDIGizmo::~MDIGizmo() { diff --git a/modules/mesh_data_resource/editor/mdi_gizmo.h b/modules/mesh_data_resource/editor/mdi_gizmo.h index c3d9dfd76..3ea844904 100644 --- a/modules/mesh_data_resource/editor/mdi_gizmo.h +++ b/modules/mesh_data_resource/editor/mdi_gizmo.h @@ -74,6 +74,8 @@ public: HANDLE_SELECTION_TYPE_ALL = 2, }; + void set_visible(const bool visible); + void setup(); void set_editor_plugin(EditorPlugin *editor_plugin); @@ -234,6 +236,8 @@ public: EditorPlugin *_editor_plugin; UndoRedo *_undo_redo; + bool _visible; + protected: static void _bind_methods(); };