editor_folding.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*************************************************************************/
  2. /* editor_folding.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. 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 path = EditorSettings::get_singleton()->get_project_settings_dir();
  52. String file = p_path.get_file() + "-folding-" + p_path.md5_text() + ".cfg";
  53. file = EditorSettings::get_singleton()->get_project_settings_dir().plus_file(file);
  54. config->save(file);
  55. }
  56. void EditorFolding::_set_unfolds(Object *p_object, const PoolVector<String> &p_unfolds) {
  57. int uc = p_unfolds.size();
  58. PoolVector<String>::Read r = p_unfolds.read();
  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(RES p_resource, const String &p_path) {
  65. Ref<ConfigFile> config;
  66. config.instance();
  67. String path = EditorSettings::get_singleton()->get_project_settings_dir();
  68. String file = p_path.get_file() + "-folding-" + p_path.md5_text() + ".cfg";
  69. file = EditorSettings::get_singleton()->get_project_settings_dir().plus_file(file);
  70. if (config->load(file) != OK) {
  71. return;
  72. }
  73. PoolVector<String> unfolds;
  74. if (config->has_section_key("folding", "sections_unfolded")) {
  75. unfolds = config->get_value("folding", "sections_unfolded");
  76. }
  77. _set_unfolds(p_resource.ptr(), unfolds);
  78. }
  79. void EditorFolding::_fill_folds(const Node *p_root, const Node *p_node, Array &p_folds, Array &resource_folds, Set<RES> &resources) {
  80. if (p_root != p_node) {
  81. if (!p_node->get_owner()) {
  82. return; //not owned, bye
  83. }
  84. if (p_node->get_owner() != p_root && !p_root->is_editable_instance(p_node)) {
  85. return;
  86. }
  87. }
  88. PoolVector<String> unfolds = _get_unfolds(p_node);
  89. if (unfolds.size()) {
  90. p_folds.push_back(p_root->get_path_to(p_node));
  91. p_folds.push_back(unfolds);
  92. }
  93. List<PropertyInfo> plist;
  94. p_node->get_property_list(&plist);
  95. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  96. if (E->get().usage & PROPERTY_USAGE_EDITOR) {
  97. if (E->get().type == Variant::OBJECT) {
  98. RES res = p_node->get(E->get().name);
  99. if (res.is_valid() && !resources.has(res) && res->get_path() != String() && !res->get_path().is_resource_file()) {
  100. PoolVector<String> res_unfolds = _get_unfolds(res.ptr());
  101. resource_folds.push_back(res->get_path());
  102. resource_folds.push_back(res_unfolds);
  103. resources.insert(res);
  104. }
  105. }
  106. }
  107. }
  108. for (int i = 0; i < p_node->get_child_count(); i++) {
  109. _fill_folds(p_root, p_node->get_child(i), p_folds, resource_folds, resources);
  110. }
  111. }
  112. void EditorFolding::save_scene_folding(const Node *p_scene, const String &p_path) {
  113. Ref<ConfigFile> config;
  114. config.instance();
  115. Array unfolds, res_unfolds;
  116. Set<RES> resources;
  117. _fill_folds(p_scene, p_scene, unfolds, res_unfolds, resources);
  118. config->set_value("folding", "node_unfolds", unfolds);
  119. config->set_value("folding", "resource_unfolds", res_unfolds);
  120. String path = EditorSettings::get_singleton()->get_project_settings_dir();
  121. String file = p_path.get_file() + "-folding-" + p_path.md5_text() + ".cfg";
  122. file = EditorSettings::get_singleton()->get_project_settings_dir().plus_file(file);
  123. config->save(file);
  124. }
  125. void EditorFolding::load_scene_folding(Node *p_scene, const String &p_path) {
  126. Ref<ConfigFile> config;
  127. config.instance();
  128. String path = EditorSettings::get_singleton()->get_project_settings_dir();
  129. String file = p_path.get_file() + "-folding-" + p_path.md5_text() + ".cfg";
  130. file = EditorSettings::get_singleton()->get_project_settings_dir().plus_file(file);
  131. if (config->load(file) != OK) {
  132. return;
  133. }
  134. Array unfolds;
  135. if (config->has_section_key("folding", "node_unfolds")) {
  136. unfolds = config->get_value("folding", "node_unfolds");
  137. }
  138. Array res_unfolds;
  139. if (config->has_section_key("folding", "resource_unfolds")) {
  140. res_unfolds = config->get_value("folding", "resource_unfolds");
  141. }
  142. ERR_FAIL_COND(unfolds.size() & 1);
  143. ERR_FAIL_COND(res_unfolds.size() & 1);
  144. for (int i = 0; i < unfolds.size(); i += 2) {
  145. NodePath path2 = unfolds[i];
  146. PoolVector<String> un = unfolds[i + 1];
  147. Node *node = p_scene->get_node_or_null(path2);
  148. if (!node) {
  149. continue;
  150. }
  151. _set_unfolds(node, un);
  152. }
  153. for (int i = 0; i < res_unfolds.size(); i += 2) {
  154. String path2 = res_unfolds[i];
  155. RES res;
  156. if (ResourceCache::has(path2)) {
  157. res = RES(ResourceCache::get(path2));
  158. }
  159. if (res.is_null()) {
  160. continue;
  161. }
  162. PoolVector<String> unfolds2 = res_unfolds[i + 1];
  163. _set_unfolds(res.ptr(), unfolds2);
  164. }
  165. }
  166. bool EditorFolding::has_folding_data(const String &p_path) {
  167. String file = p_path.get_file() + "-folding-" + p_path.md5_text() + ".cfg";
  168. file = EditorSettings::get_singleton()->get_project_settings_dir().plus_file(file);
  169. return FileAccess::exists(file);
  170. }
  171. void EditorFolding::_do_object_unfolds(Object *p_object, Set<RES> &resources) {
  172. List<PropertyInfo> plist;
  173. p_object->get_property_list(&plist);
  174. String group_base;
  175. String group;
  176. Set<String> unfold_group;
  177. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  178. if (E->get().usage & PROPERTY_USAGE_CATEGORY) {
  179. group = "";
  180. group_base = "";
  181. }
  182. if (E->get().usage & PROPERTY_USAGE_GROUP) {
  183. group = E->get().name;
  184. group_base = E->get().hint_string;
  185. if (group_base.ends_with("_")) {
  186. group_base = group_base.substr(0, group_base.length() - 1);
  187. }
  188. }
  189. //can unfold
  190. if (E->get().usage & PROPERTY_USAGE_EDITOR) {
  191. if (group != "") { //group
  192. if (group_base == String() || E->get().name.begins_with(group_base)) {
  193. bool can_revert = EditorPropertyRevert::can_property_revert(p_object, E->get().name);
  194. if (can_revert) {
  195. unfold_group.insert(group);
  196. }
  197. }
  198. } else { //path
  199. int last = E->get().name.find_last("/");
  200. if (last != -1) {
  201. bool can_revert = EditorPropertyRevert::can_property_revert(p_object, E->get().name);
  202. if (can_revert) {
  203. unfold_group.insert(E->get().name.substr(0, last));
  204. }
  205. }
  206. }
  207. }
  208. if (E->get().type == Variant::OBJECT) {
  209. RES res = p_object->get(E->get().name);
  210. if (res.is_valid() && !resources.has(res) && res->get_path() != String() && !res->get_path().is_resource_file()) {
  211. resources.insert(res);
  212. _do_object_unfolds(res.ptr(), resources);
  213. }
  214. }
  215. }
  216. for (Set<String>::Element *E = unfold_group.front(); E; E = E->next()) {
  217. p_object->editor_set_section_unfold(E->get(), true);
  218. }
  219. }
  220. void EditorFolding::_do_node_unfolds(Node *p_root, Node *p_node, Set<RES> &resources) {
  221. if (p_root != p_node) {
  222. if (!p_node->get_owner()) {
  223. return; //not owned, bye
  224. }
  225. if (p_node->get_owner() != p_root && !p_root->is_editable_instance(p_node)) {
  226. return;
  227. }
  228. }
  229. _do_object_unfolds(p_node, resources);
  230. for (int i = 0; i < p_node->get_child_count(); i++) {
  231. _do_node_unfolds(p_root, p_node->get_child(i), resources);
  232. }
  233. }
  234. void EditorFolding::unfold_scene(Node *p_scene) {
  235. Set<RES> resources;
  236. _do_node_unfolds(p_scene, p_scene, resources);
  237. }
  238. EditorFolding::EditorFolding() {
  239. }