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) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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. Vector<String> EditorFolding::_get_unfolds(const Object *p_object) {
  35. Vector<String> sections;
  36. sections.resize(p_object->editor_get_section_folding().size());
  37. if (sections.size()) {
  38. String *w = sections.ptrw();
  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. Vector<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 Vector<String> &p_unfolds) {
  56. int uc = p_unfolds.size();
  57. const String *r = p_unfolds.ptr();
  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. Vector<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. Vector<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. Vector<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. Ref<ConfigFile> config;
  119. config.instance();
  120. Array unfolds, res_unfolds;
  121. Set<RES> resources;
  122. Array nodes_folded;
  123. _fill_folds(p_scene, p_scene, unfolds, res_unfolds, nodes_folded, resources);
  124. config->set_value("folding", "node_unfolds", unfolds);
  125. config->set_value("folding", "resource_unfolds", res_unfolds);
  126. config->set_value("folding", "nodes_folded", nodes_folded);
  127. String file = p_path.get_file() + "-folding-" + p_path.md5_text() + ".cfg";
  128. file = EditorSettings::get_singleton()->get_project_settings_dir().plus_file(file);
  129. config->save(file);
  130. }
  131. void EditorFolding::load_scene_folding(Node *p_scene, const String &p_path) {
  132. Ref<ConfigFile> config;
  133. config.instance();
  134. String path = EditorSettings::get_singleton()->get_project_settings_dir();
  135. String file = p_path.get_file() + "-folding-" + p_path.md5_text() + ".cfg";
  136. file = EditorSettings::get_singleton()->get_project_settings_dir().plus_file(file);
  137. if (config->load(file) != OK) {
  138. return;
  139. }
  140. Array unfolds;
  141. if (config->has_section_key("folding", "node_unfolds")) {
  142. unfolds = config->get_value("folding", "node_unfolds");
  143. }
  144. Array res_unfolds;
  145. if (config->has_section_key("folding", "resource_unfolds")) {
  146. res_unfolds = config->get_value("folding", "resource_unfolds");
  147. }
  148. Array nodes_folded;
  149. if (config->has_section_key("folding", "nodes_folded")) {
  150. nodes_folded = config->get_value("folding", "nodes_folded");
  151. }
  152. ERR_FAIL_COND(unfolds.size() & 1);
  153. ERR_FAIL_COND(res_unfolds.size() & 1);
  154. for (int i = 0; i < unfolds.size(); i += 2) {
  155. NodePath path2 = unfolds[i];
  156. Vector<String> un = unfolds[i + 1];
  157. Node *node = p_scene->get_node_or_null(path2);
  158. if (!node) {
  159. continue;
  160. }
  161. _set_unfolds(node, un);
  162. }
  163. for (int i = 0; i < res_unfolds.size(); i += 2) {
  164. String path2 = res_unfolds[i];
  165. RES res;
  166. if (ResourceCache::has(path2)) {
  167. res = RES(ResourceCache::get(path2));
  168. }
  169. if (res.is_null()) {
  170. continue;
  171. }
  172. Vector<String> unfolds2 = res_unfolds[i + 1];
  173. _set_unfolds(res.ptr(), unfolds2);
  174. }
  175. for (int i = 0; i < nodes_folded.size(); i++) {
  176. NodePath fold_path = nodes_folded[i];
  177. if (p_scene->has_node(fold_path)) {
  178. Node *node = p_scene->get_node(fold_path);
  179. node->set_display_folded(true);
  180. }
  181. }
  182. }
  183. bool EditorFolding::has_folding_data(const String &p_path) {
  184. String file = p_path.get_file() + "-folding-" + p_path.md5_text() + ".cfg";
  185. file = EditorSettings::get_singleton()->get_project_settings_dir().plus_file(file);
  186. return FileAccess::exists(file);
  187. }
  188. void EditorFolding::_do_object_unfolds(Object *p_object, Set<RES> &resources) {
  189. List<PropertyInfo> plist;
  190. p_object->get_property_list(&plist);
  191. String group_base;
  192. String group;
  193. Set<String> unfold_group;
  194. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  195. if (E->get().usage & PROPERTY_USAGE_CATEGORY) {
  196. group = "";
  197. group_base = "";
  198. }
  199. if (E->get().usage & PROPERTY_USAGE_GROUP) {
  200. group = E->get().name;
  201. group_base = E->get().hint_string;
  202. if (group_base.ends_with("_")) {
  203. group_base = group_base.substr(0, group_base.length() - 1);
  204. }
  205. }
  206. //can unfold
  207. if (E->get().usage & PROPERTY_USAGE_EDITOR) {
  208. if (group != "") { //group
  209. if (group_base == String() || E->get().name.begins_with(group_base)) {
  210. bool can_revert = EditorPropertyRevert::can_property_revert(p_object, E->get().name);
  211. if (can_revert) {
  212. unfold_group.insert(group);
  213. }
  214. }
  215. } else { //path
  216. int last = E->get().name.find_last("/");
  217. if (last != -1) {
  218. bool can_revert = EditorPropertyRevert::can_property_revert(p_object, E->get().name);
  219. if (can_revert) {
  220. unfold_group.insert(E->get().name.substr(0, last));
  221. }
  222. }
  223. }
  224. }
  225. if (E->get().type == Variant::OBJECT) {
  226. RES res = p_object->get(E->get().name);
  227. if (res.is_valid() && !resources.has(res) && res->get_path() != String() && !res->get_path().is_resource_file()) {
  228. resources.insert(res);
  229. _do_object_unfolds(res.ptr(), resources);
  230. }
  231. }
  232. }
  233. for (Set<String>::Element *E = unfold_group.front(); E; E = E->next()) {
  234. p_object->editor_set_section_unfold(E->get(), true);
  235. }
  236. }
  237. void EditorFolding::_do_node_unfolds(Node *p_root, Node *p_node, Set<RES> &resources) {
  238. if (p_root != p_node) {
  239. if (!p_node->get_owner()) {
  240. return; //not owned, bye
  241. }
  242. if (p_node->get_owner() != p_root && !p_root->is_editable_instance(p_node)) {
  243. return;
  244. }
  245. }
  246. _do_object_unfolds(p_node, resources);
  247. for (int i = 0; i < p_node->get_child_count(); i++) {
  248. _do_node_unfolds(p_root, p_node->get_child(i), resources);
  249. }
  250. }
  251. void EditorFolding::unfold_scene(Node *p_scene) {
  252. Set<RES> resources;
  253. _do_node_unfolds(p_scene, p_scene, resources);
  254. }
  255. EditorFolding::EditorFolding() {
  256. }