Path: blob/master/editor/settings/editor_folding.cpp
10277 views
/**************************************************************************/1/* editor_folding.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "editor_folding.h"3132#include "core/io/config_file.h"33#include "core/io/file_access.h"34#include "editor/file_system/editor_paths.h"35#include "editor/inspector/editor_inspector.h"3637Vector<String> EditorFolding::_get_unfolds(const Object *p_object) {38Vector<String> sections;39sections.resize(p_object->editor_get_section_folding().size());40if (sections.size()) {41String *w = sections.ptrw();42int idx = 0;43for (const String &E : p_object->editor_get_section_folding()) {44w[idx++] = E;45}46}4748return sections;49}5051void EditorFolding::save_resource_folding(const Ref<Resource> &p_resource, const String &p_path) {52Ref<ConfigFile> config;53config.instantiate();54Vector<String> unfolds = _get_unfolds(p_resource.ptr());55config->set_value("folding", "sections_unfolded", unfolds);5657String file = p_path.get_file() + "-folding-" + p_path.md5_text() + ".cfg";58file = EditorPaths::get_singleton()->get_project_settings_dir().path_join(file);59config->save(file);60}6162void EditorFolding::_set_unfolds(Object *p_object, const Vector<String> &p_unfolds) {63int uc = p_unfolds.size();64const String *r = p_unfolds.ptr();65p_object->editor_clear_section_folding();66for (int i = 0; i < uc; i++) {67p_object->editor_set_section_unfold(r[i], true, true);68}69}7071void EditorFolding::load_resource_folding(Ref<Resource> p_resource, const String &p_path) {72Ref<ConfigFile> config;73config.instantiate();7475String file = p_path.get_file() + "-folding-" + p_path.md5_text() + ".cfg";76file = EditorPaths::get_singleton()->get_project_settings_dir().path_join(file);7778if (config->load(file) != OK) {79return;80}8182Vector<String> unfolds;8384if (config->has_section_key("folding", "sections_unfolded")) {85unfolds = config->get_value("folding", "sections_unfolded");86}87_set_unfolds(p_resource.ptr(), unfolds);88}8990void EditorFolding::_fill_folds(const Node *p_root, const Node *p_node, Array &p_folds, Array &resource_folds, Array &nodes_folded, HashSet<Ref<Resource>> &resources) {91if (p_root != p_node) {92if (!p_node->get_owner()) {93return; //not owned, bye94}95if (p_node->get_owner() != p_root && !p_root->is_editable_instance(p_node->get_owner())) {96return;97}98}99100if (p_node->is_displayed_folded()) {101nodes_folded.push_back(p_root->get_path_to(p_node));102}103Vector<String> unfolds = _get_unfolds(p_node);104105if (unfolds.size()) {106p_folds.push_back(p_root->get_path_to(p_node));107p_folds.push_back(unfolds);108}109110List<PropertyInfo> plist;111p_node->get_property_list(&plist);112for (const PropertyInfo &E : plist) {113if (E.usage & PROPERTY_USAGE_EDITOR) {114if (E.type == Variant::OBJECT) {115Ref<Resource> res = p_node->get(E.name);116if (res.is_valid() && !resources.has(res) && !res->get_path().is_empty() && !res->get_path().is_resource_file()) {117Vector<String> res_unfolds = _get_unfolds(res.ptr());118resource_folds.push_back(res->get_path());119resource_folds.push_back(res_unfolds);120resources.insert(res);121}122}123}124}125126for (int i = 0; i < p_node->get_child_count(); i++) {127_fill_folds(p_root, p_node->get_child(i), p_folds, resource_folds, nodes_folded, resources);128}129}130131void EditorFolding::save_scene_folding(const Node *p_scene, const String &p_path) {132ERR_FAIL_NULL(p_scene);133134Ref<FileAccess> file_check = FileAccess::create(FileAccess::ACCESS_RESOURCES);135if (!file_check->file_exists(p_path)) { //This can happen when creating scene from FilesystemDock. It has path, but no file.136return;137}138139Ref<ConfigFile> config;140config.instantiate();141142Array unfolds, res_unfolds;143HashSet<Ref<Resource>> resources;144Array nodes_folded;145_fill_folds(p_scene, p_scene, unfolds, res_unfolds, nodes_folded, resources);146147config->set_value("folding", "node_unfolds", unfolds);148config->set_value("folding", "resource_unfolds", res_unfolds);149config->set_value("folding", "nodes_folded", nodes_folded);150151String file = p_path.get_file() + "-folding-" + p_path.md5_text() + ".cfg";152file = EditorPaths::get_singleton()->get_project_settings_dir().path_join(file);153config->save(file);154}155156void EditorFolding::load_scene_folding(Node *p_scene, const String &p_path) {157Ref<ConfigFile> config;158config.instantiate();159160String path = EditorPaths::get_singleton()->get_project_settings_dir();161String file = p_path.get_file() + "-folding-" + p_path.md5_text() + ".cfg";162file = EditorPaths::get_singleton()->get_project_settings_dir().path_join(file);163164if (config->load(file) != OK) {165return;166}167168Array unfolds;169if (config->has_section_key("folding", "node_unfolds")) {170unfolds = config->get_value("folding", "node_unfolds");171}172Array res_unfolds;173if (config->has_section_key("folding", "resource_unfolds")) {174res_unfolds = config->get_value("folding", "resource_unfolds");175}176Array nodes_folded;177if (config->has_section_key("folding", "nodes_folded")) {178nodes_folded = config->get_value("folding", "nodes_folded");179}180181ERR_FAIL_COND(unfolds.size() & 1);182ERR_FAIL_COND(res_unfolds.size() & 1);183184for (int i = 0; i < unfolds.size(); i += 2) {185NodePath path2 = unfolds[i];186Vector<String> un = unfolds[i + 1];187Node *node = p_scene->get_node_or_null(path2);188if (!node) {189continue;190}191_set_unfolds(node, un);192}193194for (int i = 0; i < res_unfolds.size(); i += 2) {195String path2 = res_unfolds[i];196Ref<Resource> res = ResourceCache::get_ref(path2);197if (res.is_null()) {198continue;199}200201Vector<String> unfolds2 = res_unfolds[i + 1];202_set_unfolds(res.ptr(), unfolds2);203}204205for (int i = 0; i < nodes_folded.size(); i++) {206NodePath fold_path = nodes_folded[i];207if (p_scene->has_node(fold_path)) {208Node *node = p_scene->get_node(fold_path);209node->set_display_folded(true);210}211}212}213214bool EditorFolding::has_folding_data(const String &p_path) {215String file = p_path.get_file() + "-folding-" + p_path.md5_text() + ".cfg";216file = EditorPaths::get_singleton()->get_project_settings_dir().path_join(file);217return FileAccess::exists(file);218}219220void EditorFolding::_do_object_unfolds(Object *p_object, HashSet<Ref<Resource>> &resources) {221List<PropertyInfo> plist;222p_object->get_property_list(&plist);223String group_base;224String group;225226HashSet<String> unfold_group;227228for (const PropertyInfo &E : plist) {229if (E.usage & PROPERTY_USAGE_CATEGORY) {230group = "";231group_base = "";232}233if (E.usage & PROPERTY_USAGE_GROUP) {234group = E.name;235group_base = E.hint_string;236if (group_base.ends_with("_")) {237group_base = group_base.substr(0, group_base.length() - 1);238}239}240241//can unfold242if (E.usage & PROPERTY_USAGE_EDITOR) {243if (!group.is_empty()) { //group244if (group_base.is_empty() || E.name.begins_with(group_base)) {245bool can_revert = EditorPropertyRevert::can_property_revert(p_object, E.name);246if (can_revert) {247unfold_group.insert(group);248}249}250} else { //path251int last = E.name.rfind_char('/');252if (last != -1) {253bool can_revert = EditorPropertyRevert::can_property_revert(p_object, E.name);254if (can_revert) {255unfold_group.insert(E.name.substr(0, last));256}257}258}259260if (E.type == Variant::OBJECT) {261Ref<Resource> res = p_object->get(E.name);262if (res.is_valid() && !resources.has(res) && !res->get_path().is_empty() && !res->get_path().is_resource_file()) {263resources.insert(res);264_do_object_unfolds(res.ptr(), resources);265}266}267}268}269270for (const String &E : unfold_group) {271p_object->editor_set_section_unfold(E, true);272}273}274275void EditorFolding::_do_node_unfolds(Node *p_root, Node *p_node, HashSet<Ref<Resource>> &resources) {276if (p_root != p_node) {277if (!p_node->get_owner()) {278return; //not owned, bye279}280if (p_node->get_owner() != p_root && !p_root->is_editable_instance(p_node)) {281return;282}283}284285_do_object_unfolds(p_node, resources);286287for (int i = 0; i < p_node->get_child_count(); i++) {288_do_node_unfolds(p_root, p_node->get_child(i), resources);289}290}291292void EditorFolding::unfold_scene(Node *p_scene) {293HashSet<Ref<Resource>> resources;294_do_node_unfolds(p_scene, p_scene, resources);295}296297298