From 7249d2555bae1220b75044ec4582dee3dcb861c2 Mon Sep 17 00:00:00 2001 From: Eric M Date: Thu, 10 Sep 2020 22:45:10 +1000 Subject: [PATCH] Added confirmation dialog for making subresources unique. --- editor/inspector_dock.cpp | 130 ++++++++++++++++++++++++++++++-------- editor/inspector_dock.h | 8 ++- 2 files changed, 109 insertions(+), 29 deletions(-) diff --git a/editor/inspector_dock.cpp b/editor/inspector_dock.cpp index 778046f45c..04ddf3552b 100644 --- a/editor/inspector_dock.cpp +++ b/editor/inspector_dock.cpp @@ -30,11 +30,22 @@ #include "inspector_dock.h" -#include "editor/editor_node.h" -#include "editor/editor_settings.h" +#include "editor/editor_scale.h" #include "editor/plugins/animation_player_editor_plugin.h" void InspectorDock::_menu_option(int p_option) { + _menu_option_confirm(p_option, false); +} + +void InspectorDock::_menu_confirm_current() { + _menu_option_confirm(current_option, true); +} + +void InspectorDock::_menu_option_confirm(int p_option, bool p_confirmed) { + if (!p_confirmed) { + current_option = p_option; + } + switch (p_option) { case EXPAND_ALL: { _menu_expandall(); @@ -82,40 +93,82 @@ void InspectorDock::_menu_option(int p_option) { } break; case OBJECT_UNIQUE_RESOURCES: { - editor_data->apply_changes_in_editors(); - if (current) { - List props; - current->get_property_list(&props); - Map duplicates; - for (const PropertyInfo &E : props) { - if (!(E.usage & PROPERTY_USAGE_STORAGE)) { - continue; + if (!p_confirmed) { + Vector resource_propnames; + + if (current) { + List props; + current->get_property_list(&props); + + for (List::Element *E = props.front(); E; E = E->next()) { + if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) { + continue; + } + + Variant v = current->get(E->get().name); + REF ref = v; + RES res = ref; + if (v.is_ref() && ref.is_valid() && res.is_valid()) { + // Valid resource which would be duplicated if action is confirmed. + resource_propnames.append(E->get().name); + } + } + } + + if (resource_propnames.size()) { + unique_resources_list_tree->clear(); + TreeItem *root = unique_resources_list_tree->create_item(); + + for (int i = 0; i < resource_propnames.size(); i++) { + String propname = resource_propnames[i].replace("/", " / "); + + TreeItem *ti = unique_resources_list_tree->create_item(root); + ti->set_text(0, bool(EDITOR_GET("interface/inspector/capitalize_properties")) ? propname.capitalize() : propname); } - Variant v = current->get(E.name); - if (v.is_ref()) { - REF ref = v; - if (ref.is_valid()) { - RES res = ref; - if (res.is_valid()) { - if (!duplicates.has(res)) { - duplicates[res] = res->duplicate(); - } - res = duplicates[res]; + unique_resources_confirmation->popup_centered(); + } else { + unique_resources_confirmation->set_text(TTR("This object has no resources.")); + current_option = -1; + unique_resources_confirmation->popup_centered(); + } + } else { + editor_data->apply_changes_in_editors(); - current->set(E.name, res); - editor->get_inspector()->update_property(E.name); + if (current) { + List props; + current->get_property_list(&props); + Map duplicates; + for (const PropertyInfo &prop_info : props) { + if (!(prop_info.usage & PROPERTY_USAGE_STORAGE)) { + continue; + } + + Variant v = current->get(prop_info.name); + if (v.is_ref()) { + REF ref = v; + if (ref.is_valid()) { + RES res = ref; + if (res.is_valid()) { + if (!duplicates.has(res)) { + duplicates[res] = res->duplicate(); + } + res = duplicates[res]; + + current->set(prop_info.name, res); + editor->get_inspector()->update_property(prop_info.name); + } } } } } + + editor_data->get_undo_redo().clear_history(); + + editor->get_editor_plugins_over()->edit(nullptr); + editor->get_editor_plugins_over()->edit(current); } - editor_data->get_undo_redo().clear_history(); - - editor->get_editor_plugins_over()->edit(nullptr); - editor->get_editor_plugins_over()->edit(current); - } break; default: { @@ -619,6 +672,29 @@ InspectorDock::InspectorDock(EditorNode *p_editor, EditorData &p_editor_data) { warning->hide(); warning->connect("pressed", callable_mp(this, &InspectorDock::_warning_pressed)); + unique_resources_confirmation = memnew(ConfirmationDialog); + add_child(unique_resources_confirmation); + + VBoxContainer *container = memnew(VBoxContainer); + unique_resources_confirmation->add_child(container); + + Label *top_label = memnew(Label); + top_label->set_text(TTR("The following resources will be duplicated and embedded within this resource/object.")); + container->add_child(top_label); + + unique_resources_list_tree = memnew(Tree); + unique_resources_list_tree->set_hide_root(true); + unique_resources_list_tree->set_columns(1); + unique_resources_list_tree->set_column_title(0, TTR("Property")); + unique_resources_list_tree->set_custom_minimum_size(Size2(0, 200 * EDSCALE)); + container->add_child(unique_resources_list_tree); + + Label *bottom_label = memnew(Label); + bottom_label->set_text(TTR("This cannot be undone. Are you sure?")); + container->add_child(bottom_label); + + unique_resources_confirmation->connect("confirmed", callable_mp(this, &InspectorDock::_menu_confirm_current)); + warning_dialog = memnew(AcceptDialog); editor->get_gui_base()->add_child(warning_dialog); diff --git a/editor/inspector_dock.h b/editor/inspector_dock.h index 6615845b66..5bf6a34617 100644 --- a/editor/inspector_dock.h +++ b/editor/inspector_dock.h @@ -40,8 +40,6 @@ #include "scene/gui/box_container.h" #include "scene/gui/button.h" #include "scene/gui/control.h" -#include "scene/gui/label.h" -#include "scene/gui/popup_menu.h" class EditorNode; @@ -92,7 +90,13 @@ class InspectorDock : public VBoxContainer { Button *warning; AcceptDialog *warning_dialog; + int current_option = -1; + ConfirmationDialog *unique_resources_confirmation; + Tree *unique_resources_list_tree; + void _menu_option(int p_option); + void _menu_confirm_current(); + void _menu_option_confirm(int p_option, bool p_confirmed); void _new_resource(); void _load_resource(const String &p_type = "");