From b6fce5d75030e4333da8ddeb79224cb5d579a8c4 Mon Sep 17 00:00:00 2001 From: reduz Date: Tue, 23 Feb 2021 14:17:42 -0300 Subject: [PATCH] Added Import Defaults Editor in Project Settings -Change importer defaults in project settings. -Ability to change them or reset them. --- core/io/resource_importer.cpp | 6 + core/io/resource_importer.h | 1 + editor/import_defaults_editor.cpp | 194 +++++++++++++++++++++++++++++ editor/import_defaults_editor.h | 74 +++++++++++ editor/project_settings_editor.cpp | 6 + editor/project_settings_editor.h | 2 + 6 files changed, 283 insertions(+) create mode 100644 editor/import_defaults_editor.cpp create mode 100644 editor/import_defaults_editor.h diff --git a/core/io/resource_importer.cpp b/core/io/resource_importer.cpp index 3e460726f6a9..5ca0eb884a29 100644 --- a/core/io/resource_importer.cpp +++ b/core/io/resource_importer.cpp @@ -352,6 +352,12 @@ void ResourceFormatImporter::get_importers_for_extension(const String &p_extensi } } +void ResourceFormatImporter::get_importers(List> *r_importers) { + for (int i = 0; i < importers.size(); i++) { + r_importers->push_back(importers[i]); + } +} + Ref ResourceFormatImporter::get_importer_by_extension(const String &p_extension) const { Ref importer; float priority = 0; diff --git a/core/io/resource_importer.h b/core/io/resource_importer.h index bda8b74b73dc..91efec5534f3 100644 --- a/core/io/resource_importer.h +++ b/core/io/resource_importer.h @@ -82,6 +82,7 @@ class ResourceFormatImporter : public ResourceFormatLoader { Ref get_importer_by_name(const String &p_name) const; Ref get_importer_by_extension(const String &p_extension) const; void get_importers_for_extension(const String &p_extension, List> *r_importers); + void get_importers(List> *r_importers); bool are_import_settings_valid(const String &p_path) const; String get_import_settings_hash() const; diff --git a/editor/import_defaults_editor.cpp b/editor/import_defaults_editor.cpp new file mode 100644 index 000000000000..ad0841140348 --- /dev/null +++ b/editor/import_defaults_editor.cpp @@ -0,0 +1,194 @@ +/*************************************************************************/ +/* import_defaults_editor.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "import_defaults_editor.h" + +class ImportDefaultsEditorSettings : public Object { + GDCLASS(ImportDefaultsEditorSettings, Object) + friend class ImportDefaultsEditor; + List properties; + Map values; + Map default_values; + + Ref importer; + +protected: + bool _set(const StringName &p_name, const Variant &p_value) { + if (values.has(p_name)) { + values[p_name] = p_value; + return true; + } else { + return false; + } + } + bool _get(const StringName &p_name, Variant &r_ret) const { + if (values.has(p_name)) { + r_ret = values[p_name]; + return true; + } else { + r_ret = Variant(); + return false; + } + } + void _get_property_list(List *p_list) const { + if (importer.is_null()) { + return; + } + for (const List::Element *E = properties.front(); E; E = E->next()) { + if (importer->get_option_visibility(E->get().name, values)) { + p_list->push_back(E->get()); + } + } + } +}; + +void ImportDefaultsEditor::_reset() { + if (settings->importer.is_valid()) { + settings->values = settings->default_values; + settings->notify_property_list_changed(); + } +} +void ImportDefaultsEditor::_save() { + if (settings->importer.is_valid()) { + Dictionary modified; + + for (Map::Element *E = settings->values.front(); E; E = E->next()) { + if (E->get() != settings->default_values[E->key()]) { + modified[E->key()] = E->get(); + } + } + + if (modified.size()) { + ProjectSettings::get_singleton()->set("importer_defaults/" + settings->importer->get_importer_name(), modified); + } else { + ProjectSettings::get_singleton()->set("importer_defaults/" + settings->importer->get_importer_name(), Variant()); + } + + emit_signal("project_settings_changed"); + } +} + +void ImportDefaultsEditor::_update_importer() { + List> importer_list; + ResourceFormatImporter::get_singleton()->get_importers(&importer_list); + Ref importer; + for (List>::Element *E = importer_list.front(); E; E = E->next()) { + if (E->get()->get_visible_name() == importers->get_item_text(importers->get_selected())) { + importer = E->get(); + break; + } + } + + settings->properties.clear(); + settings->values.clear(); + settings->importer = importer; + + if (importer.is_valid()) { + List options; + importer->get_import_options(&options); + Dictionary d; + if (ProjectSettings::get_singleton()->has_setting("importer_defaults/" + importer->get_importer_name())) { + d = ProjectSettings::get_singleton()->get("importer_defaults/" + importer->get_importer_name()); + } + + for (List::Element *E = options.front(); E; E = E->next()) { + settings->properties.push_back(E->get().option); + if (d.has(E->get().option.name)) { + settings->values[E->get().option.name] = d[E->get().option.name]; + } else { + settings->values[E->get().option.name] = E->get().default_value; + } + settings->default_values[E->get().option.name] = E->get().default_value; + } + + save_defaults->set_disabled(false); + reset_defaults->set_disabled(false); + + } else { + save_defaults->set_disabled(true); + reset_defaults->set_disabled(true); + } + + settings->notify_property_list_changed(); + + inspector->edit(settings); +} +void ImportDefaultsEditor::_importer_selected(int p_index) { + _update_importer(); +} +void ImportDefaultsEditor::clear() { + importers->clear(); + importers->add_item("<" + TTR("Select Importer") + ">"); + List> importer_list; + ResourceFormatImporter::get_singleton()->get_importers(&importer_list); + Vector names; + for (List>::Element *E = importer_list.front(); E; E = E->next()) { + String vn = E->get()->get_visible_name(); + names.push_back(vn); + } + names.sort(); + + for (int i = 0; i < names.size(); i++) { + importers->add_item(names[i]); + } +} +void ImportDefaultsEditor::_bind_methods() { + ADD_SIGNAL(MethodInfo("project_settings_changed")); +} +ImportDefaultsEditor::ImportDefaultsEditor() { + HBoxContainer *hb = memnew(HBoxContainer); + hb->add_child(memnew(Label(TTR("Importer:")))); + importers = memnew(OptionButton); + hb->add_child(importers); + hb->add_spacer(); + importers->connect("item_selected", callable_mp(this, &ImportDefaultsEditor::_importer_selected)); + reset_defaults = memnew(Button); + reset_defaults->set_text(TTR("Reset to Defaults")); + reset_defaults->set_disabled(true); + reset_defaults->connect("pressed", callable_mp(this, &ImportDefaultsEditor::_reset)); + hb->add_child(reset_defaults); + add_child(hb); + inspector = memnew(EditorInspector); + add_child(inspector); + inspector->set_v_size_flags(SIZE_EXPAND_FILL); + CenterContainer *cc = memnew(CenterContainer); + save_defaults = memnew(Button); + save_defaults->set_text(TTR("Save")); + save_defaults->connect("pressed", callable_mp(this, &ImportDefaultsEditor::_save)); + cc->add_child(save_defaults); + add_child(cc); + + settings = memnew(ImportDefaultsEditorSettings); +} + +ImportDefaultsEditor::~ImportDefaultsEditor() { + inspector->edit(nullptr); + memdelete(settings); +} diff --git a/editor/import_defaults_editor.h b/editor/import_defaults_editor.h new file mode 100644 index 000000000000..ff85a25b00d7 --- /dev/null +++ b/editor/import_defaults_editor.h @@ -0,0 +1,74 @@ +/*************************************************************************/ +/* import_defaults_editor.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef IMPORT_DEFAULTS_EDITOR_H +#define IMPORT_DEFAULTS_EDITOR_H + +#include "core/object/undo_redo.h" +#include "editor/action_map_editor.h" +#include "editor/editor_data.h" +#include "editor/editor_plugin_settings.h" +#include "editor/editor_sectioned_inspector.h" +#include "editor/localization_editor.h" +#include "editor/shader_globals_editor.h" +#include "editor_autoload_settings.h" +#include "scene/gui/center_container.h" +#include "scene/gui/option_button.h" + +class ImportDefaultsEditorSettings; + +class ImportDefaultsEditor : public VBoxContainer { + GDCLASS(ImportDefaultsEditor, VBoxContainer) + + OptionButton *importers; + Button *save_defaults; + Button *reset_defaults; + + EditorInspector *inspector; + + ImportDefaultsEditorSettings *settings; + + void _update_importer(); + void _importer_selected(int p_index); + + void _reset(); + void _save(); + +protected: + static void _bind_methods(); + +public: + void clear(); + + ImportDefaultsEditor(); + ~ImportDefaultsEditor(); +}; + +#endif // IMPORT_DEFAULTS_EDITOR_H diff --git a/editor/project_settings_editor.cpp b/editor/project_settings_editor.cpp index 6e2cd727963b..d7d12903e09b 100644 --- a/editor/project_settings_editor.cpp +++ b/editor/project_settings_editor.cpp @@ -52,6 +52,7 @@ void ProjectSettingsEditor::popup_project_settings() { localization_editor->update_translations(); autoload_settings->update_autoload(); plugin_settings->update_plugins(); + import_defaults_editor->clear(); } void ProjectSettingsEditor::queue_save() { @@ -692,4 +693,9 @@ ProjectSettingsEditor::ProjectSettingsEditor(EditorData *p_data) { } inspector->set_restrict_to_basic_settings(!use_advanced); + + import_defaults_editor = memnew(ImportDefaultsEditor); + import_defaults_editor->set_name(TTR("Import Defaults")); + tab_container->add_child(import_defaults_editor); + import_defaults_editor->connect("project_settings_changed", callable_mp(this, &ProjectSettingsEditor::queue_save)); } diff --git a/editor/project_settings_editor.h b/editor/project_settings_editor.h index c28785bb2797..cde46ac4c4f4 100644 --- a/editor/project_settings_editor.h +++ b/editor/project_settings_editor.h @@ -36,6 +36,7 @@ #include "editor/editor_data.h" #include "editor/editor_plugin_settings.h" #include "editor/editor_sectioned_inspector.h" +#include "editor/import_defaults_editor.h" #include "editor/localization_editor.h" #include "editor/shader_globals_editor.h" #include "editor_autoload_settings.h" @@ -75,6 +76,7 @@ class ProjectSettingsEditor : public AcceptDialog { PanelContainer *restart_container; Button *restart_close_button; + ImportDefaultsEditor *import_defaults_editor; EditorData *data; UndoRedo *undo_redo;