1/**************************************************************************/
2/* editor_folding.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_folding.h"
32
33#include "core/io/config_file.h"
34#include "core/io/file_access.h"
35#include "editor/editor_inspector.h"
36#include "editor/editor_paths.h"
37
38Vector<String> EditorFolding::_get_unfolds(const Object *p_object) {
39 Vector<String> sections;
40 sections.resize(p_object->editor_get_section_folding().size());
41 if (sections.size()) {
42 String *w = sections.ptrw();
43 int idx = 0;
44 for (const String &E : p_object->editor_get_section_folding()) {
45 w[idx++] = E;
46 }
47 }
48
49 return sections;
50}
51
52void EditorFolding::save_resource_folding(const Ref<Resource> &p_resource, const String &p_path) {
53 Ref<ConfigFile> config;
54 config.instantiate();
55 Vector<String> unfolds = _get_unfolds(p_resource.ptr());
56 config->set_value("folding", "sections_unfolded", unfolds);
57
58 String file = p_path.get_file() + "-folding-" + p_path.md5_text() + ".cfg";
59 file = EditorPaths::get_singleton()->get_project_settings_dir().path_join(file);
60 config->save(file);
61}
62
63void EditorFolding::_set_unfolds(Object *p_object, const Vector<String> &p_unfolds) {
64 int uc = p_unfolds.size();
65 const String *r = p_unfolds.ptr();
66 p_object->editor_clear_section_folding();
67 for (int i = 0; i < uc; i++) {
68 p_object->editor_set_section_unfold(r[i], true);
69 }
70}
71
72void EditorFolding::load_resource_folding(Ref<Resource> p_resource, const String &p_path) {
73 Ref<ConfigFile> config;
74 config.instantiate();
75
76 String file = p_path.get_file() + "-folding-" + p_path.md5_text() + ".cfg";
77 file = EditorPaths::get_singleton()->get_project_settings_dir().path_join(file);
78
79 if (config->load(file) != OK) {
80 return;
81 }
82
83 Vector<String> unfolds;
84
85 if (config->has_section_key("folding", "sections_unfolded")) {
86 unfolds = config->get_value("folding", "sections_unfolded");
87 }
88 _set_unfolds(p_resource.ptr(), unfolds);
89}
90
91void EditorFolding::_fill_folds(const Node *p_root, const Node *p_node, Array &p_folds, Array &resource_folds, Array &nodes_folded, HashSet<Ref<Resource>> &resources) {
92 if (p_root != p_node) {
93 if (!p_node->get_owner()) {
94 return; //not owned, bye
95 }
96 if (p_node->get_owner() != p_root && !p_root->is_editable_instance(p_node)) {
97 return;
98 }
99 }
100
101 if (p_node->is_displayed_folded()) {
102 nodes_folded.push_back(p_root->get_path_to(p_node));
103 }
104 Vector<String> unfolds = _get_unfolds(p_node);
105
106 if (unfolds.size()) {
107 p_folds.push_back(p_root->get_path_to(p_node));
108 p_folds.push_back(unfolds);
109 }
110
111 List<PropertyInfo> plist;
112 p_node->get_property_list(&plist);
113 for (const PropertyInfo &E : plist) {
114 if (E.usage & PROPERTY_USAGE_EDITOR) {
115 if (E.type == Variant::OBJECT) {
116 Ref<Resource> res = p_node->get(E.name);
117 if (res.is_valid() && !resources.has(res) && !res->get_path().is_empty() && !res->get_path().is_resource_file()) {
118 Vector<String> res_unfolds = _get_unfolds(res.ptr());
119 resource_folds.push_back(res->get_path());
120 resource_folds.push_back(res_unfolds);
121 resources.insert(res);
122 }
123 }
124 }
125 }
126
127 for (int i = 0; i < p_node->get_child_count(); i++) {
128 _fill_folds(p_root, p_node->get_child(i), p_folds, resource_folds, nodes_folded, resources);
129 }
130}
131
132void EditorFolding::save_scene_folding(const Node *p_scene, const String &p_path) {
133 ERR_FAIL_NULL(p_scene);
134
135 Ref<FileAccess> file_check = FileAccess::create(FileAccess::ACCESS_RESOURCES);
136 if (!file_check->file_exists(p_path)) { //This can happen when creating scene from FilesystemDock. It has path, but no file.
137 return;
138 }
139
140 Ref<ConfigFile> config;
141 config.instantiate();
142
143 Array unfolds, res_unfolds;
144 HashSet<Ref<Resource>> resources;
145 Array nodes_folded;
146 _fill_folds(p_scene, p_scene, unfolds, res_unfolds, nodes_folded, resources);
147
148 config->set_value("folding", "node_unfolds", unfolds);
149 config->set_value("folding", "resource_unfolds", res_unfolds);
150 config->set_value("folding", "nodes_folded", nodes_folded);
151
152 String file = p_path.get_file() + "-folding-" + p_path.md5_text() + ".cfg";
153 file = EditorPaths::get_singleton()->get_project_settings_dir().path_join(file);
154 config->save(file);
155}
156
157void EditorFolding::load_scene_folding(Node *p_scene, const String &p_path) {
158 Ref<ConfigFile> config;
159 config.instantiate();
160
161 String path = EditorPaths::get_singleton()->get_project_settings_dir();
162 String file = p_path.get_file() + "-folding-" + p_path.md5_text() + ".cfg";
163 file = EditorPaths::get_singleton()->get_project_settings_dir().path_join(file);
164
165 if (config->load(file) != OK) {
166 return;
167 }
168
169 Array unfolds;
170 if (config->has_section_key("folding", "node_unfolds")) {
171 unfolds = config->get_value("folding", "node_unfolds");
172 }
173 Array res_unfolds;
174 if (config->has_section_key("folding", "resource_unfolds")) {
175 res_unfolds = config->get_value("folding", "resource_unfolds");
176 }
177 Array nodes_folded;
178 if (config->has_section_key("folding", "nodes_folded")) {
179 nodes_folded = config->get_value("folding", "nodes_folded");
180 }
181
182 ERR_FAIL_COND(unfolds.size() & 1);
183 ERR_FAIL_COND(res_unfolds.size() & 1);
184
185 for (int i = 0; i < unfolds.size(); i += 2) {
186 NodePath path2 = unfolds[i];
187 Vector<String> un = unfolds[i + 1];
188 Node *node = p_scene->get_node_or_null(path2);
189 if (!node) {
190 continue;
191 }
192 _set_unfolds(node, un);
193 }
194
195 for (int i = 0; i < res_unfolds.size(); i += 2) {
196 String path2 = res_unfolds[i];
197 Ref<Resource> res = ResourceCache::get_ref(path2);
198 if (res.is_null()) {
199 continue;
200 }
201
202 Vector<String> unfolds2 = res_unfolds[i + 1];
203 _set_unfolds(res.ptr(), unfolds2);
204 }
205
206 for (int i = 0; i < nodes_folded.size(); i++) {
207 NodePath fold_path = nodes_folded[i];
208 if (p_scene->has_node(fold_path)) {
209 Node *node = p_scene->get_node(fold_path);
210 node->set_display_folded(true);
211 }
212 }
213}
214
215bool EditorFolding::has_folding_data(const String &p_path) {
216 String file = p_path.get_file() + "-folding-" + p_path.md5_text() + ".cfg";
217 file = EditorPaths::get_singleton()->get_project_settings_dir().path_join(file);
218 return FileAccess::exists(file);
219}
220
221void EditorFolding::_do_object_unfolds(Object *p_object, HashSet<Ref<Resource>> &resources) {
222 List<PropertyInfo> plist;
223 p_object->get_property_list(&plist);
224 String group_base;
225 String group;
226
227 HashSet<String> unfold_group;
228
229 for (const PropertyInfo &E : plist) {
230 if (E.usage & PROPERTY_USAGE_CATEGORY) {
231 group = "";
232 group_base = "";
233 }
234 if (E.usage & PROPERTY_USAGE_GROUP) {
235 group = E.name;
236 group_base = E.hint_string;
237 if (group_base.ends_with("_")) {
238 group_base = group_base.substr(0, group_base.length() - 1);
239 }
240 }
241
242 //can unfold
243 if (E.usage & PROPERTY_USAGE_EDITOR) {
244 if (!group.is_empty()) { //group
245 if (group_base.is_empty() || E.name.begins_with(group_base)) {
246 bool can_revert = EditorPropertyRevert::can_property_revert(p_object, E.name);
247 if (can_revert) {
248 unfold_group.insert(group);
249 }
250 }
251 } else { //path
252 int last = E.name.rfind("/");
253 if (last != -1) {
254 bool can_revert = EditorPropertyRevert::can_property_revert(p_object, E.name);
255 if (can_revert) {
256 unfold_group.insert(E.name.substr(0, last));
257 }
258 }
259 }
260
261 if (E.type == Variant::OBJECT) {
262 Ref<Resource> res = p_object->get(E.name);
263 if (res.is_valid() && !resources.has(res) && !res->get_path().is_empty() && !res->get_path().is_resource_file()) {
264 resources.insert(res);
265 _do_object_unfolds(res.ptr(), resources);
266 }
267 }
268 }
269 }
270
271 for (const String &E : unfold_group) {
272 p_object->editor_set_section_unfold(E, true);
273 }
274}
275
276void EditorFolding::_do_node_unfolds(Node *p_root, Node *p_node, HashSet<Ref<Resource>> &resources) {
277 if (p_root != p_node) {
278 if (!p_node->get_owner()) {
279 return; //not owned, bye
280 }
281 if (p_node->get_owner() != p_root && !p_root->is_editable_instance(p_node)) {
282 return;
283 }
284 }
285
286 _do_object_unfolds(p_node, resources);
287
288 for (int i = 0; i < p_node->get_child_count(); i++) {
289 _do_node_unfolds(p_root, p_node->get_child(i), resources);
290 }
291}
292
293void EditorFolding::unfold_scene(Node *p_scene) {
294 HashSet<Ref<Resource>> resources;
295 _do_node_unfolds(p_scene, p_scene, resources);
296}
297
298EditorFolding::EditorFolding() {
299}
300