editor_folding.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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().type == Variant::OBJECT) {
  97. RES res = p_node->get(E->get().name);
  98. if (res.is_valid() && !resources.has(res) && res->get_path() != String() && !res->get_path().is_resource_file()) {
  99. PoolVector<String> res_unfolds = _get_unfolds(res.ptr());
  100. resource_folds.push_back(res->get_path());
  101. resource_folds.push_back(res_unfolds);
  102. resources.insert(res);
  103. }
  104. }
  105. }
  106. for (int i = 0; i < p_node->get_child_count(); i++) {
  107. _fill_folds(p_root, p_node->get_child(i), p_folds, resource_folds, resources);
  108. }
  109. }
  110. void EditorFolding::save_scene_folding(const Node *p_scene, const String &p_path) {
  111. Ref<ConfigFile> config;
  112. config.instance();
  113. Array unfolds, res_unfolds;
  114. Set<RES> resources;
  115. _fill_folds(p_scene, p_scene, unfolds, res_unfolds, resources);
  116. config->set_value("folding", "node_unfolds", unfolds);
  117. config->set_value("folding", "resource_unfolds", res_unfolds);
  118. String path = EditorSettings::get_singleton()->get_project_settings_dir();
  119. String file = p_path.get_file() + "-folding-" + p_path.md5_text() + ".cfg";
  120. file = EditorSettings::get_singleton()->get_project_settings_dir().plus_file(file);
  121. config->save(file);
  122. }
  123. void EditorFolding::load_scene_folding(Node *p_scene, const String &p_path) {
  124. Ref<ConfigFile> config;
  125. config.instance();
  126. String path = EditorSettings::get_singleton()->get_project_settings_dir();
  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. if (config->load(file) != OK) {
  130. return;
  131. }
  132. Array unfolds;
  133. if (config->has_section_key("folding", "node_unfolds")) {
  134. unfolds = config->get_value("folding", "node_unfolds");
  135. }
  136. Array res_unfolds;
  137. if (config->has_section_key("folding", "resource_unfolds")) {
  138. res_unfolds = config->get_value("folding", "resource_unfolds");
  139. }
  140. ERR_FAIL_COND(unfolds.size() & 1);
  141. ERR_FAIL_COND(res_unfolds.size() & 1);
  142. for (int i = 0; i < unfolds.size(); i += 2) {
  143. NodePath path = unfolds[i];
  144. PoolVector<String> un = unfolds[i + 1];
  145. Node *node = p_scene->get_node(path);
  146. if (!node) {
  147. continue;
  148. }
  149. _set_unfolds(node, un);
  150. }
  151. for (int i = 0; i < res_unfolds.size(); i += 2) {
  152. String path = res_unfolds[i];
  153. RES res;
  154. if (ResourceCache::has(path)) {
  155. res = RES(ResourceCache::get(path));
  156. }
  157. if (res.is_null()) {
  158. continue;
  159. }
  160. PoolVector<String> unfolds = res_unfolds[i + 1];
  161. _set_unfolds(res.ptr(), unfolds);
  162. }
  163. }
  164. bool EditorFolding::has_folding_data(const String &p_path) {
  165. String file = p_path.get_file() + "-folding-" + p_path.md5_text() + ".cfg";
  166. file = EditorSettings::get_singleton()->get_project_settings_dir().plus_file(file);
  167. return FileAccess::exists(file);
  168. }
  169. void EditorFolding::_do_object_unfolds(Object *p_object, Set<RES> &resources) {
  170. List<PropertyInfo> plist;
  171. p_object->get_property_list(&plist);
  172. String group_base;
  173. String group;
  174. Set<String> unfold_group;
  175. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  176. if (E->get().usage & PROPERTY_USAGE_CATEGORY) {
  177. group = "";
  178. group_base = "";
  179. }
  180. if (E->get().usage & PROPERTY_USAGE_GROUP) {
  181. group = E->get().name;
  182. group_base = E->get().hint_string;
  183. if (group_base.ends_with("_")) {
  184. group_base = group_base.substr(0, group_base.length() - 1);
  185. }
  186. }
  187. //can unfold
  188. if (E->get().usage & PROPERTY_USAGE_EDITOR) {
  189. if (group != "") { //group
  190. if (group_base == String() || E->get().name.begins_with(group_base)) {
  191. bool can_revert = EditorPropertyRevert::can_property_revert(p_object, E->get().name);
  192. if (can_revert) {
  193. unfold_group.insert(group);
  194. }
  195. }
  196. } else { //path
  197. int last = E->get().name.find_last("/");
  198. if (last != -1) {
  199. bool can_revert = EditorPropertyRevert::can_property_revert(p_object, E->get().name);
  200. if (can_revert) {
  201. unfold_group.insert(E->get().name.substr(0, last));
  202. }
  203. }
  204. }
  205. }
  206. if (E->get().type == Variant::OBJECT) {
  207. RES res = p_object->get(E->get().name);
  208. if (res.is_valid() && !resources.has(res) && res->get_path() != String() && !res->get_path().is_resource_file()) {
  209. resources.insert(res);
  210. _do_object_unfolds(res.ptr(), resources);
  211. }
  212. }
  213. }
  214. for (Set<String>::Element *E = unfold_group.front(); E; E = E->next()) {
  215. p_object->editor_set_section_unfold(E->get(), true);
  216. }
  217. }
  218. void EditorFolding::_do_node_unfolds(Node *p_root, Node *p_node, Set<RES> &resources) {
  219. if (p_root != p_node) {
  220. if (!p_node->get_owner()) {
  221. return; //not owned, bye
  222. }
  223. if (p_node->get_owner() != p_root && !p_root->is_editable_instance(p_node)) {
  224. return;
  225. }
  226. }
  227. _do_object_unfolds(p_node, resources);
  228. for (int i = 0; i < p_node->get_child_count(); i++) {
  229. _do_node_unfolds(p_root, p_node->get_child(i), resources);
  230. }
  231. }
  232. void EditorFolding::unfold_scene(Node *p_scene) {
  233. Set<RES> resources;
  234. _do_node_unfolds(p_scene, p_scene, resources);
  235. }
  236. EditorFolding::EditorFolding() {
  237. }