summaryrefslogtreecommitdiff
path: root/editor/editor_sectioned_inspector.cpp
blob: cbca3e9dcd3c9146a1ecb9a10ebcffc42a9e631d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*************************************************************************/
/*  editor_sectioned_inspector.cpp                                       */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2022 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 "editor_sectioned_inspector.h"

#include "editor/editor_property_name_processor.h"
#include "editor/editor_scale.h"
#include "editor/editor_settings.h"

static bool _property_path_matches(const String &p_property_path, const String &p_filter, EditorPropertyNameProcessor::Style p_style) {
	if (p_property_path.findn(p_filter) != -1) {
		return true;
	}

	const Vector<String> sections = p_property_path.split("/");
	for (int i = 0; i < sections.size(); i++) {
		if (p_filter.is_subsequence_ofn(EditorPropertyNameProcessor::get_singleton()->process_name(sections[i], p_style))) {
			return true;
		}
	}
	return false;
}

class SectionedInspectorFilter : public Object {
	GDCLASS(SectionedInspectorFilter, Object);

	Object *edited = nullptr;
	String section;
	bool allow_sub = false;

	bool _set(const StringName &p_name, const Variant &p_value) {
		if (!edited) {
			return false;
		}

		String name = p_name;
		if (!section.is_empty()) {
			name = section + "/" + name;
		}

		bool valid;
		edited->set(name, p_value, &valid);
		return valid;
	}

	bool _get(const StringName &p_name, Variant &r_ret) const {
		if (!edited) {
			return false;
		}

		String name = p_name;
		if (!section.is_empty()) {
			name = section + "/" + name;
		}

		bool valid = false;

		r_ret = edited->get(name, &valid);
		return valid;
	}
	void _get_property_list(List<PropertyInfo> *p_list) const {
		if (!edited) {
			return;
		}

		List<PropertyInfo> pinfo;
		edited->get_property_list(&pinfo);
		for (PropertyInfo &pi : pinfo) {
			int sp = pi.name.find("/");

			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
				continue;
			}

			if (sp == -1) {
				pi.name = "global/" + pi.name;
			}

			if (pi.name.begins_with(section + "/")) {
				pi.name = pi.name.replace_first(section + "/", "");
				if (!allow_sub && pi.name.contains("/")) {
					continue;
				}
				p_list->push_back(pi);
			}
		}
	}

	bool property_can_revert(const String &p_name) {
		return edited->property_can_revert(section + "/" + p_name);
	}

	Variant property_get_revert(const String &p_name) {
		return edited->property_get_revert(section + "/" + p_name);
	}

protected:
	static void _bind_methods() {
		ClassDB::bind_method("property_can_revert", &SectionedInspectorFilter::property_can_revert);
		ClassDB::bind_method("property_get_revert", &SectionedInspectorFilter::property_get_revert);
	}

public:
	void set_section(const String &p_section, bool p_allow_sub) {
		section = p_section;
		allow_sub = p_allow_sub;
		notify_property_list_changed();
	}

	void set_edited(Object *p_edited) {
		edited = p_edited;
		notify_property_list_changed();
	}
};

void SectionedInspector::_bind_methods() {
	ClassDB::bind_method("update_category_list", &SectionedInspector::update_category_list);
}

void SectionedInspector::_section_selected() {
	if (!sections->get_selected()) {
		return;
	}

	selected_category = sections->get_selected()->get_metadata(0);
	filter->set_section(selected_category, sections->get_selected()->get_first_child() == nullptr);
	inspector->set_property_prefix(selected_category + "/");
}

void SectionedInspector::set_current_section(const String &p_section) {
	if (section_map.has(p_section)) {
		section_map[p_section]->select(0);
	}
}

String SectionedInspector::get_current_section() const {
	if (sections->get_selected()) {
		return sections->get_selected()->get_metadata(0);
	} else {
		return "";
	}
}

String SectionedInspector::get_full_item_path(const String &p_item) {
	String base = get_current_section();

	if (!base.is_empty()) {
		return base + "/" + p_item;
	} else {
		return p_item;
	}
}

void SectionedInspector::edit(Object *p_object) {
	if (!p_object) {
		obj = ObjectID();
		sections->clear();

		filter->set_edited(nullptr);
		inspector->edit(nullptr);

		return;
	}

	ObjectID id = p_object->get_instance_id();

	inspector->set_object_class(p_object->get_class());

	if (obj != id) {
		obj = id;
		update_category_list();

		filter->set_edited(p_object);
		inspector->edit(filter);

		TreeItem *first_item = sections->get_root();
		if (first_item) {
			while (first_item->get_first_child()) {
				first_item = first_item->get_first_child();
			}

			first_item->select(0);
			selected_category = first_item->get_metadata(0);
		}
	} else {
		update_category_list();
	}
}

void SectionedInspector::update_category_list() {
	sections->clear();

	Object *o = ObjectDB::get_instance(obj);

	if (!o) {
		return;
	}

	List<PropertyInfo> pinfo;
	o->get_property_list(&pinfo);

	section_map.clear();

	TreeItem *root = sections->create_item();
	section_map[""] = root;

	String filter;
	if (search_box) {
		filter = search_box->get_text();
	}

	const EditorPropertyNameProcessor::Style name_style = EditorPropertyNameProcessor::get_settings_style();
	const EditorPropertyNameProcessor::Style tooltip_style = EditorPropertyNameProcessor::get_tooltip_style(name_style);

	for (PropertyInfo &pi : pinfo) {
		if (pi.usage & PROPERTY_USAGE_CATEGORY) {
			continue;
		} else if (!(pi.usage & PROPERTY_USAGE_EDITOR) || (restrict_to_basic && !(pi.usage & PROPERTY_USAGE_EDITOR_BASIC_SETTING))) {
			continue;
		}

		if (pi.name.contains(":") || pi.name == "script" || pi.name == "resource_name" || pi.name == "resource_path" || pi.name == "resource_local_to_scene" || pi.name.begins_with("_global_script")) {
			continue;
		}

		if (!filter.is_empty() && !_property_path_matches(pi.name, filter, name_style)) {
			continue;
		}

		int sp = pi.name.find("/");
		if (sp == -1) {
			pi.name = "global/" + pi.name;
		}

		Vector<String> sectionarr = pi.name.split("/");
		String metasection;

		int sc = MIN(2, sectionarr.size() - 1);

		for (int i = 0; i < sc; i++) {
			TreeItem *parent = section_map[metasection];
			//parent->set_custom_bg_color(0, get_theme_color(SNAME("prop_subsection"), SNAME("Editor")));
			parent->set_custom_font(0, get_theme_font(SNAME("bold"), SNAME("EditorFonts")));

			if (i > 0) {
				metasection += "/" + sectionarr[i];
			} else {
				metasection = sectionarr[i];
			}

			if (!section_map.has(metasection)) {
				TreeItem *ms = sections->create_item(parent);
				section_map[metasection] = ms;

				const String text = EditorPropertyNameProcessor::get_singleton()->process_name(sectionarr[i], name_style);
				const String tooltip = EditorPropertyNameProcessor::get_singleton()->process_name(sectionarr[i], tooltip_style);

				ms->set_text(0, text);
				ms->set_tooltip(0, tooltip);
				ms->set_metadata(0, metasection);
				ms->set_selectable(0, false);
			}

			if (i == sc - 1) {
				//if it has children, make selectable
				section_map[metasection]->set_selectable(0, true);
			}
		}
	}

	if (section_map.has(selected_category)) {
		section_map[selected_category]->select(0);
	}

	inspector->update_tree();
}

void SectionedInspector::register_search_box(LineEdit *p_box) {
	search_box = p_box;
	inspector->register_text_enter(p_box);
	search_box->connect("text_changed", callable_mp(this, &SectionedInspector::_search_changed));
}

void SectionedInspector::_search_changed(const String &p_what) {
	update_category_list();
}

void SectionedInspector::_notification(int p_what) {
	switch (p_what) {
		case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
			inspector->set_property_name_style(EditorPropertyNameProcessor::get_settings_style());
		} break;
	}
}

EditorInspector *SectionedInspector::get_inspector() {
	return inspector;
}

void SectionedInspector::set_restrict_to_basic_settings(bool p_restrict) {
	restrict_to_basic = p_restrict;
	update_category_list();
	inspector->set_restrict_to_basic_settings(p_restrict);
}

SectionedInspector::SectionedInspector() :
		sections(memnew(Tree)),
		filter(memnew(SectionedInspectorFilter)),
		inspector(memnew(EditorInspector)) {
	add_theme_constant_override("autohide", 1); // Fixes the dragger always showing up

	VBoxContainer *left_vb = memnew(VBoxContainer);
	left_vb->set_custom_minimum_size(Size2(190, 0) * EDSCALE);
	add_child(left_vb);

	sections->set_v_size_flags(SIZE_EXPAND_FILL);
	sections->set_hide_root(true);

	left_vb->add_child(sections, true);

	VBoxContainer *right_vb = memnew(VBoxContainer);
	right_vb->set_custom_minimum_size(Size2(300, 0) * EDSCALE);
	right_vb->set_h_size_flags(SIZE_EXPAND_FILL);
	add_child(right_vb);

	inspector->set_v_size_flags(SIZE_EXPAND_FILL);
	right_vb->add_child(inspector, true);
	inspector->set_use_doc_hints(true);
	inspector->set_property_name_style(EditorPropertyNameProcessor::get_settings_style());

	sections->connect("cell_selected", callable_mp(this, &SectionedInspector::_section_selected));
}

SectionedInspector::~SectionedInspector() {
	memdelete(filter);
}