editor_folding.cpp 10 KB

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