Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/import/import_defaults_editor.cpp
20837 views
1
/**************************************************************************/
2
/* import_defaults_editor.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "import_defaults_editor.h"
32
33
#include "core/config/project_settings.h"
34
#include "core/io/resource_importer.h"
35
#include "editor/inspector/editor_inspector.h"
36
#include "editor/inspector/editor_sectioned_inspector.h"
37
#include "editor/settings/action_map_editor.h"
38
#include "scene/gui/center_container.h"
39
#include "scene/gui/label.h"
40
#include "scene/gui/margin_container.h"
41
42
class ImportDefaultsEditorSettings : public Object {
43
GDCLASS(ImportDefaultsEditorSettings, Object)
44
friend class ImportDefaultsEditor;
45
List<PropertyInfo> properties;
46
HashMap<StringName, Variant> values;
47
HashMap<StringName, Variant> default_values;
48
49
Ref<ResourceImporter> importer;
50
51
protected:
52
bool _set(const StringName &p_name, const Variant &p_value) {
53
if (values.has(p_name)) {
54
values[p_name] = p_value;
55
return true;
56
} else {
57
return false;
58
}
59
}
60
bool _get(const StringName &p_name, Variant &r_ret) const {
61
if (values.has(p_name)) {
62
r_ret = values[p_name];
63
return true;
64
} else {
65
r_ret = Variant();
66
return false;
67
}
68
}
69
void _get_property_list(List<PropertyInfo> *p_list) const {
70
if (importer.is_null()) {
71
return;
72
}
73
for (const PropertyInfo &E : properties) {
74
if (importer->get_option_visibility("", E.name, values)) {
75
p_list->push_back(E);
76
}
77
}
78
}
79
};
80
81
void ImportDefaultsEditor::_notification(int p_what) {
82
switch (p_what) {
83
case NOTIFICATION_PREDELETE: {
84
inspector->edit(nullptr);
85
} break;
86
}
87
}
88
89
void ImportDefaultsEditor::_reset() {
90
if (settings->importer.is_valid()) {
91
settings->values = settings->default_values;
92
settings->notify_property_list_changed();
93
}
94
}
95
96
void ImportDefaultsEditor::_save() {
97
if (settings->importer.is_valid()) {
98
Dictionary modified;
99
100
for (const KeyValue<StringName, Variant> &E : settings->values) {
101
if (E.value != settings->default_values[E.key]) {
102
modified[E.key] = E.value;
103
}
104
}
105
106
if (modified.size()) {
107
ProjectSettings::get_singleton()->set("importer_defaults/" + settings->importer->get_importer_name(), modified);
108
} else {
109
ProjectSettings::get_singleton()->set("importer_defaults/" + settings->importer->get_importer_name(), Variant());
110
}
111
ProjectSettings::get_singleton()->save();
112
}
113
}
114
115
void ImportDefaultsEditor::_update_importer() {
116
List<Ref<ResourceImporter>> importer_list;
117
ResourceFormatImporter::get_singleton()->get_importers(&importer_list);
118
Ref<ResourceImporter> importer;
119
for (const Ref<ResourceImporter> &E : importer_list) {
120
if (E->get_visible_name() == importers->get_item_text(importers->get_selected())) {
121
importer = E;
122
break;
123
}
124
}
125
126
settings->properties.clear();
127
settings->values.clear();
128
settings->importer = importer;
129
130
if (importer.is_valid()) {
131
List<ResourceImporter::ImportOption> options;
132
importer->get_import_options("", &options);
133
Dictionary d;
134
if (ProjectSettings::get_singleton()->has_setting("importer_defaults/" + importer->get_importer_name())) {
135
d = GLOBAL_GET("importer_defaults/" + importer->get_importer_name());
136
}
137
138
for (const ResourceImporter::ImportOption &E : options) {
139
settings->properties.push_back(E.option);
140
if (d.has(E.option.name)) {
141
settings->values[E.option.name] = d[E.option.name];
142
} else {
143
settings->values[E.option.name] = E.default_value;
144
}
145
settings->default_values[E.option.name] = E.default_value;
146
}
147
148
save_defaults->set_disabled(false);
149
reset_defaults->set_disabled(false);
150
151
} else {
152
save_defaults->set_disabled(true);
153
reset_defaults->set_disabled(true);
154
}
155
156
settings->notify_property_list_changed();
157
158
// Set the importer class to fetch the correct class in the XML class reference.
159
// This allows tooltips to display when hovering properties.
160
inspector->set_object_class(importer->get_class_name());
161
inspector->edit(settings);
162
}
163
164
void ImportDefaultsEditor::_importer_selected(int p_index) {
165
_update_importer();
166
}
167
168
void ImportDefaultsEditor::clear() {
169
String last_selected;
170
171
if (importers->get_selected() >= 0) {
172
last_selected = importers->get_item_text(importers->get_selected());
173
}
174
175
importers->clear();
176
177
List<Ref<ResourceImporter>> importer_list;
178
ResourceFormatImporter::get_singleton()->get_importers(&importer_list);
179
Vector<String> names;
180
for (const Ref<ResourceImporter> &E : importer_list) {
181
String vn = E->get_visible_name();
182
names.push_back(vn);
183
}
184
names.sort();
185
186
// `last_selected.is_empty()` means it's the first time being called.
187
if (last_selected.is_empty() && !names.is_empty()) {
188
last_selected = names[0];
189
}
190
191
for (int i = 0; i < names.size(); i++) {
192
importers->add_item(names[i]);
193
194
if (names[i] == last_selected) {
195
importers->select(i);
196
_update_importer();
197
}
198
}
199
}
200
201
ImportDefaultsEditor::ImportDefaultsEditor() {
202
ProjectSettings::get_singleton()->add_hidden_prefix("importer_defaults/");
203
204
HBoxContainer *hb = memnew(HBoxContainer);
205
hb->add_child(memnew(Label(TTRC("Importer:"))));
206
importers = memnew(OptionButton);
207
hb->add_child(importers);
208
hb->add_spacer();
209
importers->connect(SceneStringName(item_selected), callable_mp(this, &ImportDefaultsEditor::_importer_selected));
210
reset_defaults = memnew(Button);
211
reset_defaults->set_text(TTRC("Reset to Defaults"));
212
reset_defaults->set_disabled(true);
213
reset_defaults->connect(SceneStringName(pressed), callable_mp(this, &ImportDefaultsEditor::_reset));
214
hb->add_child(reset_defaults);
215
add_child(hb);
216
217
MarginContainer *mc = memnew(MarginContainer);
218
mc->set_theme_type_variation("NoBorderHorizontal");
219
mc->set_v_size_flags(SIZE_EXPAND_FILL);
220
add_child(mc);
221
222
inspector = memnew(EditorInspector);
223
inspector->set_scroll_hint_mode(ScrollContainer::SCROLL_HINT_MODE_ALL);
224
// Make it possible to display tooltips stored in the XML class reference.
225
// The object name is set when the importer changes in `_update_importer()`.
226
inspector->set_use_doc_hints(true);
227
mc->add_child(inspector);
228
229
CenterContainer *cc = memnew(CenterContainer);
230
save_defaults = memnew(Button);
231
save_defaults->set_text(TTRC("Save"));
232
save_defaults->connect(SceneStringName(pressed), callable_mp(this, &ImportDefaultsEditor::_save));
233
cc->add_child(save_defaults);
234
add_child(cc);
235
236
settings = memnew(ImportDefaultsEditorSettings);
237
}
238
239
ImportDefaultsEditor::~ImportDefaultsEditor() {
240
memdelete(settings);
241
}
242
243