Debian Codegraph @

editor/inspector/editor_sectioned_inspector.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* editor_sectioned_inspector.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 "editor_sectioned_inspector.h"
32
33 #include "editor/editor_string_names.h"
34 #include "editor/inspector/editor_inspector.h"
35 #include "editor/inspector/editor_property_name_processor.h"
36 #include "editor/themes/editor_scale.h"
37 #include "scene/gui/check_button.h"
38 #include "scene/gui/line_edit.h"
39 #include "scene/gui/tree.h"
40
41 static bool _property_path_matches(const String &p_property_path, const String &p_filter, EditorPropertyNameProcessor::Style p_style) {
42 if (p_property_path.containsn(p_filter)) {
43 return true;
44 }
45
46 const Vector<String> sections = p_property_path.split("/");
47 for (int i = 0; i < sections.size(); i++) {
48 if (p_filter.is_subsequence_ofn(EditorPropertyNameProcessor::get_singleton()->process_name(sections[i], p_style, p_property_path))) {
49 return true;
50 }
51 }
52 return false;
53 }
54
55 class SectionedInspectorFilter : public Object {
56 GDCLASS(SectionedInspectorFilter, Object);
57
58 Object *edited = nullptr;
59 String section;
60 bool allow_sub = false;
61
62 bool _set(const StringName &p_name, const Variant &p_value) {
63 if (!edited) {
64 return false;
65 }
66
67 String name = p_name;
68 if (!section.is_empty()) {
69 name = section + "/" + name;
70 }
71
72 bool valid;
73 edited->set(name, p_value, &valid);
74 return valid;
75 }
76
77 bool _get(const StringName &p_name, Variant &r_ret) const {
78 if (!edited) {
79 return false;
80 }
81
82 String name = p_name;
83 if (!section.is_empty()) {
84 name = section + "/" + name;
85 }
86
87 bool valid = false;
88
89 r_ret = edited->get(name, &valid);
90 return valid;
91 }
92 void _get_property_list(List<PropertyInfo> *p_list) const {
93 if (!edited) {
94 return;
95 }
96
97 List<PropertyInfo> pinfo;
98 edited->get_property_list(&pinfo);
99 for (PropertyInfo &pi : pinfo) {
100 int sp = pi.name.find_char('/');
101
102 if (pi.name == "resource_path" || pi.name == "resource_name" || pi.name == "resource_local_to_scene" || pi.name.begins_with("script/") || pi.name.begins_with("_global_script")) { //skip resource stuff
103 continue;
104 }
105
106 if (sp == -1) {
107 pi.name = "global/" + pi.name;
108 }
109
110 if (pi.name.begins_with(section + "/")) {
111 pi.name = pi.name.replace_first(section + "/", "");
112 if (!allow_sub && pi.name.contains_char('/')) {
113 continue;
114 }
115 p_list->push_back(pi);
116 }
117 }
118 }
119
120 bool _property_can_revert(const StringName &p_name) const {
121 return edited->property_can_revert(section + "/" + p_name);
122 }
123
124 bool _property_get_revert(const StringName &p_name, Variant &r_property) const {
125 r_property = edited->property_get_revert(section + "/" + p_name);
126 return true;
127 }
128
129 public:
130 void set_section(const String &p_section, bool p_allow_sub) {
131 section = p_section;
132 allow_sub = p_allow_sub;
133 notify_property_list_changed();
134 }
135
136 void set_edited(Object *p_edited) {
137 edited = p_edited;
138 notify_property_list_changed();
139 }
140 };
141
142 void SectionedInspector::_bind_methods() {
143 ClassDB::bind_method("update_category_list", &SectionedInspector::update_category_list);
144 ADD_SIGNAL(MethodInfo("category_changed", PropertyInfo(Variant::STRING, "new_category")));
145 }
146
147 void SectionedInspector::_section_selected() {
148 if (!sections->get_selected()) {
149 return;
150 }
151
152 selected_category = sections->get_selected()->get_metadata(0);
153 filter->set_section(selected_category, sections->get_selected()->get_first_child() == nullptr);
154 inspector->set_property_prefix(selected_category + "/");
155 inspector->set_scroll_offset(0);
156 emit_signal(SNAME("category_changed"), selected_category);
157 }
158
159 void SectionedInspector::set_current_section(const String &p_section) {
160 if (section_map.has(p_section)) {
161 TreeItem *item = section_map[p_section];
162 item->select(0);
163 sections->scroll_to_item(item);
164 }
165 }
166
167 String SectionedInspector::get_current_section() const {
168 if (sections->get_selected()) {
169 return sections->get_selected()->get_metadata(0);
170 } else {
171 return "";
172 }
173 }
174
175 String SectionedInspector::get_full_item_path(const String &p_item) {
176 String base = get_current_section();
177
178 if (!base.is_empty()) {
179 return base + "/" + p_item;
180 } else {
181 return p_item;
182 }
183 }
184
185 void SectionedInspector::edit(Object *p_object) {
186 if (!p_object) {
187 obj = ObjectID();
188 sections->clear();
189
190 filter->set_edited(nullptr);
191 inspector->edit(nullptr);
192
193 return;
194 }
195
196 ObjectID id = p_object->get_instance_id();
197
198 inspector->set_object_class(p_object->get_class());
199
200 if (obj != id) {
201 obj = id;
202 update_category_list();
203
204 filter->set_edited(p_object);
205 inspector->edit(filter);
206
207 TreeItem *first_item = sections->get_root();
208 if (first_item) {
209 while (first_item->get_first_child()) {
210 first_item = first_item->get_first_child();
211 }
212
213 first_item->select(0);
214 selected_category = first_item->get_metadata(0);
215 }
216 } else {
217 update_category_list();
218 }
219 }
220
221 void SectionedInspector::update_category_list() {
222 sections->clear();
223
224 Object *o = ObjectDB::get_instance(obj);
225
226 if (!o) {
227 return;
228 }
229
230 List<PropertyInfo> pinfo;
231 o->get_property_list(&pinfo);
232
233 section_map.clear();
234
235 TreeItem *root = sections->create_item();
236 section_map[""] = root;
237
238 String filter_text;
239 if (search_box) {
240 filter_text = search_box->get_text();
241 }
242
243 const EditorPropertyNameProcessor::Style name_style = EditorPropertyNameProcessor::get_settings_style();
244 const EditorPropertyNameProcessor::Style tooltip_style = EditorPropertyNameProcessor::get_tooltip_style(name_style);
245
246 for (PropertyInfo &pi : pinfo) {
247 if (pi.usage & PROPERTY_USAGE_CATEGORY) {
248 continue;
249 } else if (!(pi.usage & PROPERTY_USAGE_EDITOR) ||
250 (filter_text.is_empty() && restrict_to_basic && !(pi.usage & PROPERTY_USAGE_EDITOR_BASIC_SETTING))) {
251 continue;
252 }
253
254 if (pi.name.contains_char(':') || pi.name == "script" || pi.name == "resource_name" || pi.name == "resource_path" || pi.name == "resource_local_to_scene" || pi.name.begins_with("_global_script")) {
255 continue;
256 }
257
258 if (!filter_text.is_empty() && !_property_path_matches(pi.name, filter_text, name_style)) {
259 continue;
260 }
261
262 int sp = pi.name.find_char('/');
263 if (sp == -1) {
264 pi.name = "global/" + pi.name;
265 }
266
267 Vector<String> sectionarr = pi.name.split("/");
268 String metasection;
269
270 int sc = MIN(2, sectionarr.size() - 1);
271
272 for (int i = 0; i < sc; i++) {
273 TreeItem *parent = section_map[metasection];
274 //parent->set_custom_bg_color(0, get_theme_color(SNAME("prop_subsection"), EditorStringName(Editor)));
275 parent->set_custom_font(0, get_theme_font(SNAME("bold"), EditorStringName(EditorFonts)));
276
277 if (i > 0) {
278 metasection += "/" + sectionarr[i];
279 } else {
280 metasection = sectionarr[i];
281 }
282
283 if (!section_map.has(metasection)) {
284 TreeItem *ms = sections->create_item(parent);
285 section_map[metasection] = ms;
286
287 const String text = EditorPropertyNameProcessor::get_singleton()->process_name(sectionarr[i], name_style, pi.name);
288 const String tooltip = EditorPropertyNameProcessor::get_singleton()->process_name(sectionarr[i], tooltip_style, pi.name);
289
290 ms->set_text(0, text);
291 ms->set_tooltip_text(0, tooltip);
292 ms->set_metadata(0, metasection);
293 ms->set_selectable(0, false);
294 }
295
296 if (i == sc - 1) {
297 //if it has children, make selectable
298 section_map[metasection]->set_selectable(0, true);
299 }
300 }
301 }
302
303 if (section_map.has(selected_category)) {
304 section_map[selected_category]->select(0);
305 }
306
307 inspector->update_tree();
308 }
309
310 void SectionedInspector::register_search_box(LineEdit *p_box) {
311 search_box = p_box;
312 inspector->register_text_enter(p_box);
313 search_box->connect(SceneStringName(text_changed), callable_mp(this, &SectionedInspector::_search_changed));
314 }
315
316 void SectionedInspector::register_advanced_toggle(CheckButton *p_toggle) {
317 advanced_toggle = p_toggle;
318 advanced_toggle->connect(SceneStringName(toggled), callable_mp(this, &SectionedInspector::_advanced_toggled));
319 _advanced_toggled(advanced_toggle->is_pressed());
320 }
321
322 void SectionedInspector::_search_changed(const String &p_what) {
323 if (advanced_toggle) {
324 if (p_what.is_empty()) {
325 advanced_toggle->set_pressed_no_signal(!restrict_to_basic);
326 advanced_toggle->set_disabled(false);
327 advanced_toggle->set_tooltip_text(String());
328 } else {
329 advanced_toggle->set_pressed_no_signal(true);
330 advanced_toggle->set_disabled(true);
331 advanced_toggle->set_tooltip_text(TTRC("Advanced settings are always shown when searching."));
332 }
333 }
334 update_category_list();
335 }
336
337 void SectionedInspector::_advanced_toggled(bool p_toggled_on) {
338 restrict_to_basic = !p_toggled_on;
339 update_category_list();
340 inspector->set_restrict_to_basic_settings(restrict_to_basic);
341 }
342
343 void SectionedInspector::_notification(int p_notification) {
344 if (p_notification == NOTIFICATION_TRANSLATION_CHANGED) {
345 if (sections->get_root()) {
346 // Only update when initialized.
347 callable_mp(this, &SectionedInspector::update_category_list).call_deferred();
348 }
349 }
350 }
351
352 EditorInspector *SectionedInspector::get_inspector() {
353 return inspector;
354 }
355
356 SectionedInspector::SectionedInspector() :
357 sections(memnew(Tree)),
358 filter(memnew(SectionedInspectorFilter)),
359 inspector(memnew(EditorInspector)) {
360 add_theme_constant_override("autohide", 1); // Fixes the dragger always showing up
361
362 VBoxContainer *left_vb = memnew(VBoxContainer);
363 left_vb->set_custom_minimum_size(Size2(190, 0) * EDSCALE);
364 add_child(left_vb);
365
366 sections->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
367 sections->set_v_size_flags(SIZE_EXPAND_FILL);
368 sections->set_hide_root(true);
369 sections->set_theme_type_variation("TreeSecondary");
370
371 left_vb->add_child(sections, true);
372
373 VBoxContainer *right_vb = memnew(VBoxContainer);
374 right_vb->set_custom_minimum_size(Size2(300, 0) * EDSCALE);
375 right_vb->set_h_size_flags(SIZE_EXPAND_FILL);
376 add_child(right_vb);
377
378 inspector->set_v_size_flags(SIZE_EXPAND_FILL);
379 right_vb->add_child(inspector, true);
380 inspector->set_use_doc_hints(true);
381 inspector->set_theme_type_variation("TreeSecondary");
382
383 sections->connect("cell_selected", callable_mp(this, &SectionedInspector::_section_selected));
384 }
385
386 SectionedInspector::~SectionedInspector() {
387 memdelete(filter);
388 }