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