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