diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2017-11-01 08:25:03 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-01 08:25:03 +0100 |
commit | 58889102b6fe3a562e4f5e0cb045def9ff2b1f53 (patch) | |
tree | 396926b4a20b832008a64814726e5841f89f5b10 /editor | |
parent | fc6ef93cc84c415519f05e38ecd3d91a9fe196f1 (diff) | |
parent | 0438f4875d0ef2a327c9d7d24dc04fb3e28a9193 (diff) |
Merge pull request #12538 from akien-mga/editorsettings
Code quality refactoring in EditorSettings
Diffstat (limited to 'editor')
-rw-r--r-- | editor/editor_settings.cpp | 992 | ||||
-rw-r--r-- | editor/editor_settings.h | 91 |
2 files changed, 539 insertions, 544 deletions
diff --git a/editor/editor_settings.cpp b/editor/editor_settings.cpp index 78cc215421..bc91fcdf04 100644 --- a/editor/editor_settings.cpp +++ b/editor/editor_settings.cpp @@ -47,12 +47,11 @@ #include "scene/main/scene_tree.h" #include "scene/main/viewport.h" -Ref<EditorSettings> EditorSettings::singleton = NULL; +// PRIVATE METHODS -EditorSettings *EditorSettings::get_singleton() { +Ref<EditorSettings> EditorSettings::singleton = NULL; - return singleton.ptr(); -} +// Properties bool EditorSettings::_set(const StringName &p_name, const Variant &p_value, bool p_emit_signal) { @@ -195,347 +194,26 @@ void EditorSettings::_get_property_list(List<PropertyInfo> *p_list) const { p_list->push_back(PropertyInfo(Variant::ARRAY, "shortcuts", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR)); //do not edit } -void EditorSettings::set_setting(const String &p_setting, const Variant &p_value) { - _THREAD_SAFE_METHOD_ - set(p_setting, p_value); -} - -Variant EditorSettings::get_setting(const String &p_setting) const { - _THREAD_SAFE_METHOD_ - return get(p_setting); -} - -bool EditorSettings::has_setting(String p_var) const { - - _THREAD_SAFE_METHOD_ - - return props.has(p_var); -} - -void EditorSettings::erase(String p_var) { - - _THREAD_SAFE_METHOD_ - - props.erase(p_var); -} - -void EditorSettings::raise_order(const String &p_name) { - _THREAD_SAFE_METHOD_ - - ERR_FAIL_COND(!props.has(p_name)); - props[p_name].order = ++last_order; -} - -Variant _EDITOR_DEF(const String &p_var, const Variant &p_default) { - - if (EditorSettings::get_singleton()->has_setting(p_var)) - return EditorSettings::get_singleton()->get(p_var); - EditorSettings::get_singleton()->set(p_var, p_default); - EditorSettings::get_singleton()->set_initial_value(p_var, p_default); - - return p_default; -} - -Variant _EDITOR_GET(const String &p_var) { - - ERR_FAIL_COND_V(!EditorSettings::get_singleton()->has_setting(p_var), Variant()) - return EditorSettings::get_singleton()->get(p_var); -} - -static Dictionary _get_builtin_script_templates() { - Dictionary templates; - - //No Comments - templates["no_comments.gd"] = - "extends %BASE%\n" - "\n" - "func _ready():\n" - "%TS%pass\n"; - - //Empty - templates["empty.gd"] = - "extends %BASE%" - "\n" - "\n"; - - return templates; -} - -static void _create_script_templates(const String &p_path) { - - Dictionary templates = _get_builtin_script_templates(); - List<Variant> keys; - templates.get_key_list(&keys); - FileAccess *file = FileAccess::create(FileAccess::ACCESS_FILESYSTEM); - - DirAccess *dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); - dir->change_dir(p_path); - for (int i = 0; i < keys.size(); i++) { - if (!dir->file_exists(keys[i])) { - Error err = file->reopen(p_path.plus_file((String)keys[i]), FileAccess::WRITE); - ERR_FAIL_COND(err != OK); - file->store_string(templates[keys[i]]); - file->close(); - } - } - - memdelete(dir); - memdelete(file); -} - -void EditorSettings::create() { - - if (singleton.ptr()) - return; //pointless - - DirAccess *dir = NULL; - Variant meta; - - String config_path; - String config_dir; - Ref<ConfigFile> extra_config = memnew(ConfigFile); - - String exe_path = OS::get_singleton()->get_executable_path().get_base_dir(); - DirAccess *d = DirAccess::create_for_path(exe_path); - bool self_contained = false; - - if (d->file_exists(exe_path + "/._sc_")) { - self_contained = true; - extra_config->load(exe_path + "/._sc_"); - } else if (d->file_exists(exe_path + "/_sc_")) { - self_contained = true; - extra_config->load(exe_path + "/_sc_"); - } - memdelete(d); - - if (self_contained) { - // editor is self contained - config_path = exe_path; - config_dir = "editor_data"; - } else { - - if (OS::get_singleton()->has_environment("APPDATA")) { - // Most likely under windows, save here - config_path = OS::get_singleton()->get_environment("APPDATA"); - config_dir = String(_MKSTR(VERSION_SHORT_NAME)).capitalize(); - } else if (OS::get_singleton()->has_environment("HOME")) { - - config_path = OS::get_singleton()->get_environment("HOME"); - config_dir = "." + String(_MKSTR(VERSION_SHORT_NAME)).to_lower(); - } - }; - - ClassDB::register_class<EditorSettings>(); //otherwise it can't be unserialized - String config_file_path; - - if (config_path != "") { - - dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); - if (dir->change_dir(config_path) != OK) { - ERR_PRINT("Cannot find path for config directory!"); - memdelete(dir); - goto fail; - } - - if (dir->change_dir(config_dir) != OK) { - dir->make_dir(config_dir); - if (dir->change_dir(config_dir) != OK) { - ERR_PRINT("Cannot create config directory!"); - memdelete(dir); - goto fail; - } - } - - if (dir->change_dir("templates") != OK) { - dir->make_dir("templates"); - } else { - - dir->change_dir(".."); - } - - if (dir->change_dir("text_editor_themes") != OK) { - dir->make_dir("text_editor_themes"); - } else { - dir->change_dir(".."); - } - - if (dir->change_dir("script_templates") != OK) { - dir->make_dir("script_templates"); - } else { - dir->change_dir(".."); - } - _create_script_templates(dir->get_current_dir() + "/script_templates"); - - if (dir->change_dir("tmp") != OK) { - dir->make_dir("tmp"); - } else { - - dir->change_dir(".."); - } - - if (dir->change_dir("config") != OK) { - dir->make_dir("config"); - } else { - - dir->change_dir(".."); - } - - dir->change_dir("config"); - - String pcp = ProjectSettings::get_singleton()->get_resource_path(); - if (pcp.ends_with("/")) - pcp = config_path.substr(0, pcp.size() - 1); - pcp = pcp.get_file() + "-" + pcp.md5_text(); - - if (dir->change_dir(pcp)) { - dir->make_dir(pcp); - } else { - dir->change_dir(".."); - } - - dir->change_dir(".."); - - // path at least is validated, so validate config file - - String config_file_name = "editor_settings-" + String(_MKSTR(VERSION_MAJOR)) + ".tres"; - config_file_path = config_path + "/" + config_dir + "/" + config_file_name; - - String open_path = config_file_path; - - if (!dir->file_exists(config_file_name)) { - - goto fail; - } - - memdelete(dir); - - singleton = ResourceLoader::load(open_path, "EditorSettings"); - - if (singleton.is_null()) { - WARN_PRINT("Could not open config file."); - goto fail; - } - - singleton->save_changed_setting = true; - singleton->config_file_path = config_file_path; - singleton->project_config_path = pcp; - singleton->settings_path = config_path + "/" + config_dir; - - if (OS::get_singleton()->is_stdout_verbose()) { - - print_line("EditorSettings: Load OK!"); - } - - singleton->setup_language(); - singleton->setup_network(); - singleton->load_favorites(); - singleton->list_text_editor_themes(); - - return; - } - -fail: - - // patch init projects - if (extra_config->has_section("init_projects")) { - Vector<String> list = extra_config->get_value("init_projects", "list"); - for (int i = 0; i < list.size(); i++) { - - list[i] = exe_path + "/" + list[i]; - }; - extra_config->set_value("init_projects", "list", list); - }; - - singleton = Ref<EditorSettings>(memnew(EditorSettings)); - singleton->save_changed_setting = true; - singleton->config_file_path = config_file_path; - singleton->settings_path = config_path + "/" + config_dir; - singleton->_load_defaults(extra_config); - singleton->setup_language(); - singleton->setup_network(); - singleton->list_text_editor_themes(); -} - -String EditorSettings::get_settings_path() const { - - return settings_path; -} - -void EditorSettings::setup_language() { - - String lang = get("interface/editor/editor_language"); - if (lang == "en") - return; //none to do - - for (int i = 0; i < translations.size(); i++) { - if (translations[i]->get_locale() == lang) { - TranslationServer::get_singleton()->set_tool_translation(translations[i]); - break; - } - } -} - -void EditorSettings::setup_network() { - - List<IP_Address> local_ip; - IP::get_singleton()->get_local_addresses(&local_ip); - String lip = "127.0.0.1"; - String hint; - String current = has_setting("network/debug/remote_host") ? get("network/debug/remote_host") : ""; - int port = has_setting("network/debug/remote_port") ? (int)get("network/debug/remote_port") : 6007; - - for (List<IP_Address>::Element *E = local_ip.front(); E; E = E->next()) { - - String ip = E->get(); - - // link-local IPv6 addresses don't work, skipping them - if (ip.begins_with("fe80:0:0:0:")) // fe80::/64 - continue; - if (ip == current) - lip = current; //so it saves - if (hint != "") - hint += ","; - hint += ip; - } - - set("network/debug/remote_host", lip); - add_property_hint(PropertyInfo(Variant::STRING, "network/debug/remote_host", PROPERTY_HINT_ENUM, hint)); - - set("network/debug/remote_port", port); - add_property_hint(PropertyInfo(Variant::INT, "network/debug/remote_port", PROPERTY_HINT_RANGE, "1,65535,1")); -} - -void EditorSettings::save() { - - //_THREAD_SAFE_METHOD_ - - if (!singleton.ptr()) - return; +void EditorSettings::_add_property_info_bind(const Dictionary &p_info) { - if (singleton->config_file_path == "") { - ERR_PRINT("Cannot save EditorSettings config, no valid path"); - return; - } + ERR_FAIL_COND(!p_info.has("name")); + ERR_FAIL_COND(!p_info.has("type")); - Error err = ResourceSaver::save(singleton->config_file_path, singleton); + PropertyInfo pinfo; + pinfo.name = p_info["name"]; + ERR_FAIL_COND(!props.has(pinfo.name)); + pinfo.type = Variant::Type(p_info["type"].operator int()); + ERR_FAIL_INDEX(pinfo.type, Variant::VARIANT_MAX); - if (err != OK) { - ERR_PRINT("Can't Save!"); - return; - } + if (p_info.has("hint")) + pinfo.hint = PropertyHint(p_info["hint"].operator int()); + if (p_info.has("hint_string")) + pinfo.hint_string = p_info["hint_string"]; - if (OS::get_singleton()->is_stdout_verbose()) { - print_line("EditorSettings Save OK!"); - } + add_property_hint(pinfo); } -void EditorSettings::destroy() { - - if (!singleton.ptr()) - return; - save(); - singleton = Ref<EditorSettings>(); -} +// Default configs void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) { @@ -857,41 +535,418 @@ void EditorSettings::_load_default_text_editor_theme() { _initial_set("text_editor/highlighting/search_result_border_color", Color(0.1, 0.45, 0.1, 1)); } -void EditorSettings::notify_changes() { +bool EditorSettings::_save_text_editor_theme(String p_file) { + String theme_section = "color_theme"; + Ref<ConfigFile> cf = memnew(ConfigFile); // hex is better? + cf->set_value(theme_section, "background_color", ((Color)get("text_editor/highlighting/background_color")).to_html()); + cf->set_value(theme_section, "completion_background_color", ((Color)get("text_editor/highlighting/completion_background_color")).to_html()); + cf->set_value(theme_section, "completion_selected_color", ((Color)get("text_editor/highlighting/completion_selected_color")).to_html()); + cf->set_value(theme_section, "completion_existing_color", ((Color)get("text_editor/highlighting/completion_existing_color")).to_html()); + cf->set_value(theme_section, "completion_scroll_color", ((Color)get("text_editor/highlighting/completion_scroll_color")).to_html()); + cf->set_value(theme_section, "completion_font_color", ((Color)get("text_editor/highlighting/completion_font_color")).to_html()); + cf->set_value(theme_section, "caret_color", ((Color)get("text_editor/highlighting/caret_color")).to_html()); + cf->set_value(theme_section, "caret_background_color", ((Color)get("text_editor/highlighting/caret_background_color")).to_html()); + cf->set_value(theme_section, "line_number_color", ((Color)get("text_editor/highlighting/line_number_color")).to_html()); + cf->set_value(theme_section, "text_color", ((Color)get("text_editor/highlighting/text_color")).to_html()); + cf->set_value(theme_section, "text_selected_color", ((Color)get("text_editor/highlighting/text_selected_color")).to_html()); + cf->set_value(theme_section, "keyword_color", ((Color)get("text_editor/highlighting/keyword_color")).to_html()); + cf->set_value(theme_section, "base_type_color", ((Color)get("text_editor/highlighting/base_type_color")).to_html()); + cf->set_value(theme_section, "engine_type_color", ((Color)get("text_editor/highlighting/engine_type_color")).to_html()); + cf->set_value(theme_section, "function_color", ((Color)get("text_editor/highlighting/function_color")).to_html()); + cf->set_value(theme_section, "member_variable_color", ((Color)get("text_editor/highlighting/member_variable_color")).to_html()); + cf->set_value(theme_section, "comment_color", ((Color)get("text_editor/highlighting/comment_color")).to_html()); + cf->set_value(theme_section, "string_color", ((Color)get("text_editor/highlighting/string_color")).to_html()); + cf->set_value(theme_section, "number_color", ((Color)get("text_editor/highlighting/number_color")).to_html()); + cf->set_value(theme_section, "symbol_color", ((Color)get("text_editor/highlighting/symbol_color")).to_html()); + cf->set_value(theme_section, "selection_color", ((Color)get("text_editor/highlighting/selection_color")).to_html()); + cf->set_value(theme_section, "brace_mismatch_color", ((Color)get("text_editor/highlighting/brace_mismatch_color")).to_html()); + cf->set_value(theme_section, "current_line_color", ((Color)get("text_editor/highlighting/current_line_color")).to_html()); + cf->set_value(theme_section, "line_length_guideline_color", ((Color)get("text_editor/highlighting/line_length_guideline_color")).to_html()); + cf->set_value(theme_section, "mark_color", ((Color)get("text_editor/highlighting/mark_color")).to_html()); + cf->set_value(theme_section, "breakpoint_color", ((Color)get("text_editor/highlighting/breakpoint_color")).to_html()); + cf->set_value(theme_section, "word_highlighted_color", ((Color)get("text_editor/highlighting/word_highlighted_color")).to_html()); + cf->set_value(theme_section, "search_result_color", ((Color)get("text_editor/highlighting/search_result_color")).to_html()); + cf->set_value(theme_section, "search_result_border_color", ((Color)get("text_editor/highlighting/search_result_border_color")).to_html()); - _THREAD_SAFE_METHOD_ + Error err = cf->save(p_file); - SceneTree *sml = Object::cast_to<SceneTree>(OS::get_singleton()->get_main_loop()); + if (err == OK) { + return true; + } + return false; +} + +static Dictionary _get_builtin_script_templates() { + Dictionary templates; + + //No Comments + templates["no_comments.gd"] = + "extends %BASE%\n" + "\n" + "func _ready():\n" + "%TS%pass\n"; + + //Empty + templates["empty.gd"] = + "extends %BASE%" + "\n" + "\n"; + + return templates; +} + +static void _create_script_templates(const String &p_path) { + + Dictionary templates = _get_builtin_script_templates(); + List<Variant> keys; + templates.get_key_list(&keys); + FileAccess *file = FileAccess::create(FileAccess::ACCESS_FILESYSTEM); + + DirAccess *dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + dir->change_dir(p_path); + for (int i = 0; i < keys.size(); i++) { + if (!dir->file_exists(keys[i])) { + Error err = file->reopen(p_path.plus_file((String)keys[i]), FileAccess::WRITE); + ERR_FAIL_COND(err != OK); + file->store_string(templates[keys[i]]); + file->close(); + } + } + + memdelete(dir); + memdelete(file); +} + +// PUBLIC METHODS + +EditorSettings *EditorSettings::get_singleton() { + + return singleton.ptr(); +} + +void EditorSettings::create() { + + if (singleton.ptr()) + return; //pointless + + DirAccess *dir = NULL; + Variant meta; + + String config_path; + String config_dir; + Ref<ConfigFile> extra_config = memnew(ConfigFile); + + String exe_path = OS::get_singleton()->get_executable_path().get_base_dir(); + DirAccess *d = DirAccess::create_for_path(exe_path); + bool self_contained = false; + + if (d->file_exists(exe_path + "/._sc_")) { + self_contained = true; + extra_config->load(exe_path + "/._sc_"); + } else if (d->file_exists(exe_path + "/_sc_")) { + self_contained = true; + extra_config->load(exe_path + "/_sc_"); + } + memdelete(d); + + if (self_contained) { + // editor is self contained + config_path = exe_path; + config_dir = "editor_data"; + } else { + + if (OS::get_singleton()->has_environment("APPDATA")) { + // Most likely under windows, save here + config_path = OS::get_singleton()->get_environment("APPDATA"); + config_dir = String(_MKSTR(VERSION_SHORT_NAME)).capitalize(); + } else if (OS::get_singleton()->has_environment("HOME")) { + + config_path = OS::get_singleton()->get_environment("HOME"); + config_dir = "." + String(_MKSTR(VERSION_SHORT_NAME)).to_lower(); + } + }; + + ClassDB::register_class<EditorSettings>(); //otherwise it can't be unserialized + String config_file_path; + + if (config_path != "") { + + dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + if (dir->change_dir(config_path) != OK) { + ERR_PRINT("Cannot find path for config directory!"); + memdelete(dir); + goto fail; + } + + if (dir->change_dir(config_dir) != OK) { + dir->make_dir(config_dir); + if (dir->change_dir(config_dir) != OK) { + ERR_PRINT("Cannot create config directory!"); + memdelete(dir); + goto fail; + } + } + + if (dir->change_dir("templates") != OK) { + dir->make_dir("templates"); + } else { + + dir->change_dir(".."); + } + + if (dir->change_dir("text_editor_themes") != OK) { + dir->make_dir("text_editor_themes"); + } else { + dir->change_dir(".."); + } + + if (dir->change_dir("script_templates") != OK) { + dir->make_dir("script_templates"); + } else { + dir->change_dir(".."); + } + _create_script_templates(dir->get_current_dir() + "/script_templates"); + + if (dir->change_dir("tmp") != OK) { + dir->make_dir("tmp"); + } else { + + dir->change_dir(".."); + } + + if (dir->change_dir("config") != OK) { + dir->make_dir("config"); + } else { + + dir->change_dir(".."); + } + + dir->change_dir("config"); + + String pcp = ProjectSettings::get_singleton()->get_resource_path(); + if (pcp.ends_with("/")) + pcp = config_path.substr(0, pcp.size() - 1); + pcp = pcp.get_file() + "-" + pcp.md5_text(); + + if (dir->change_dir(pcp)) { + dir->make_dir(pcp); + } else { + dir->change_dir(".."); + } + + dir->change_dir(".."); + + // path at least is validated, so validate config file + + String config_file_name = "editor_settings-" + String(_MKSTR(VERSION_MAJOR)) + ".tres"; + config_file_path = config_path + "/" + config_dir + "/" + config_file_name; + + String open_path = config_file_path; + + if (!dir->file_exists(config_file_name)) { + + goto fail; + } + + memdelete(dir); + + singleton = ResourceLoader::load(open_path, "EditorSettings"); + + if (singleton.is_null()) { + WARN_PRINT("Could not open config file."); + goto fail; + } + + singleton->save_changed_setting = true; + singleton->config_file_path = config_file_path; + singleton->project_config_path = pcp; + singleton->settings_path = config_path + "/" + config_dir; + + if (OS::get_singleton()->is_stdout_verbose()) { + + print_line("EditorSettings: Load OK!"); + } + + singleton->setup_language(); + singleton->setup_network(); + singleton->load_favorites(); + singleton->list_text_editor_themes(); - if (!sml) { return; } - Node *root = sml->get_root()->get_child(0); +fail: - if (!root) { + // patch init projects + if (extra_config->has_section("init_projects")) { + Vector<String> list = extra_config->get_value("init_projects", "list"); + for (int i = 0; i < list.size(); i++) { + + list[i] = exe_path + "/" + list[i]; + }; + extra_config->set_value("init_projects", "list", list); + }; + + singleton = Ref<EditorSettings>(memnew(EditorSettings)); + singleton->save_changed_setting = true; + singleton->config_file_path = config_file_path; + singleton->settings_path = config_path + "/" + config_dir; + singleton->_load_defaults(extra_config); + singleton->setup_language(); + singleton->setup_network(); + singleton->list_text_editor_themes(); +} + +void EditorSettings::setup_language() { + + String lang = get("interface/editor/editor_language"); + if (lang == "en") + return; //none to do + + for (int i = 0; i < translations.size(); i++) { + if (translations[i]->get_locale() == lang) { + TranslationServer::get_singleton()->set_tool_translation(translations[i]); + break; + } + } +} + +void EditorSettings::setup_network() { + + List<IP_Address> local_ip; + IP::get_singleton()->get_local_addresses(&local_ip); + String lip = "127.0.0.1"; + String hint; + String current = has_setting("network/debug/remote_host") ? get("network/debug/remote_host") : ""; + int port = has_setting("network/debug/remote_port") ? (int)get("network/debug/remote_port") : 6007; + + for (List<IP_Address>::Element *E = local_ip.front(); E; E = E->next()) { + + String ip = E->get(); + + // link-local IPv6 addresses don't work, skipping them + if (ip.begins_with("fe80:0:0:0:")) // fe80::/64 + continue; + if (ip == current) + lip = current; //so it saves + if (hint != "") + hint += ","; + hint += ip; + } + + set("network/debug/remote_host", lip); + add_property_hint(PropertyInfo(Variant::STRING, "network/debug/remote_host", PROPERTY_HINT_ENUM, hint)); + + set("network/debug/remote_port", port); + add_property_hint(PropertyInfo(Variant::INT, "network/debug/remote_port", PROPERTY_HINT_RANGE, "1,65535,1")); +} + +void EditorSettings::save() { + + //_THREAD_SAFE_METHOD_ + + if (!singleton.ptr()) + return; + + if (singleton->config_file_path == "") { + ERR_PRINT("Cannot save EditorSettings config, no valid path"); return; } - root->propagate_notification(NOTIFICATION_EDITOR_SETTINGS_CHANGED); + + Error err = ResourceSaver::save(singleton->config_file_path, singleton); + + if (err != OK) { + ERR_PRINT("Can't Save!"); + return; + } + + if (OS::get_singleton()->is_stdout_verbose()) { + print_line("EditorSettings Save OK!"); + } } -void EditorSettings::_add_property_info_bind(const Dictionary &p_info) { +void EditorSettings::destroy() { - ERR_FAIL_COND(!p_info.has("name")); - ERR_FAIL_COND(!p_info.has("type")); + if (!singleton.ptr()) + return; + save(); + singleton = Ref<EditorSettings>(); +} - PropertyInfo pinfo; - pinfo.name = p_info["name"]; - ERR_FAIL_COND(!props.has(pinfo.name)); - pinfo.type = Variant::Type(p_info["type"].operator int()); - ERR_FAIL_INDEX(pinfo.type, Variant::VARIANT_MAX); +void EditorSettings::set_optimize_save(bool p_optimize) { - if (p_info.has("hint")) - pinfo.hint = PropertyHint(p_info["hint"].operator int()); - if (p_info.has("hint_string")) - pinfo.hint_string = p_info["hint_string"]; + optimize_save = p_optimize; +} - add_property_hint(pinfo); +// Properties + +void EditorSettings::set_setting(const String &p_setting, const Variant &p_value) { + _THREAD_SAFE_METHOD_ + set(p_setting, p_value); +} + +Variant EditorSettings::get_setting(const String &p_setting) const { + _THREAD_SAFE_METHOD_ + return get(p_setting); +} + +bool EditorSettings::has_setting(const String &p_setting) const { + + _THREAD_SAFE_METHOD_ + + return props.has(p_setting); +} + +void EditorSettings::erase(const String &p_setting) { + + _THREAD_SAFE_METHOD_ + + props.erase(p_setting); +} + +void EditorSettings::raise_order(const String &p_setting) { + _THREAD_SAFE_METHOD_ + + ERR_FAIL_COND(!props.has(p_setting)); + props[p_setting].order = ++last_order; +} + +void EditorSettings::set_initial_value(const StringName &p_setting, const Variant &p_value) { + + ERR_FAIL_COND(!props.has(p_setting)); + props[p_setting].initial = p_value; +} + +Variant _EDITOR_DEF(const String &p_setting, const Variant &p_default) { + + if (EditorSettings::get_singleton()->has_setting(p_setting)) + return EditorSettings::get_singleton()->get(p_setting); + EditorSettings::get_singleton()->set(p_setting, p_default); + EditorSettings::get_singleton()->set_initial_value(p_setting, p_default); + + return p_default; +} + +Variant _EDITOR_GET(const String &p_setting) { + + ERR_FAIL_COND_V(!EditorSettings::get_singleton()->has_setting(p_setting), Variant()) + return EditorSettings::get_singleton()->get(p_setting); +} + +bool EditorSettings::property_can_revert(const String &p_setting) { + + if (!props.has(p_setting)) + return false; + + return props[p_setting].initial != props[p_setting].variant; +} + +Variant EditorSettings::property_get_revert(const String &p_setting) { + + if (!props.has(p_setting)) + return Variant(); + + return props[p_setting].initial; } void EditorSettings::add_property_hint(const PropertyInfo &p_hint) { @@ -901,6 +956,36 @@ void EditorSettings::add_property_hint(const PropertyInfo &p_hint) { hints[p_hint.name] = p_hint; } +// Settings paths and saved metadata + +String EditorSettings::get_settings_path() const { + + return settings_path; +} + +String EditorSettings::get_project_settings_path() const { + + return get_settings_path().plus_file("config").plus_file(project_config_path); +} + +void EditorSettings::set_project_metadata(const String &p_section, const String &p_key, Variant p_data) { + Ref<ConfigFile> cf = memnew(ConfigFile); + String path = get_project_settings_path().plus_file("project_metadata.cfg"); + cf->load(path); + cf->set_value(p_section, p_key, p_data); + cf->save(path); +} + +Variant EditorSettings::get_project_metadata(const String &p_section, const String &p_key, Variant p_default) { + Ref<ConfigFile> cf = memnew(ConfigFile); + String path = get_project_settings_path().plus_file("project_metadata.cfg"); + Error err = cf->load(path); + if (err != OK) { + return p_default; + } + return cf->get_value(p_section, p_key, p_default); +} + void EditorSettings::set_favorite_dirs(const Vector<String> &p_favorites_dirs) { favorite_dirs = p_favorites_dirs; @@ -933,11 +1018,6 @@ Vector<String> EditorSettings::get_recent_dirs() const { return recent_dirs; } -String EditorSettings::get_project_settings_path() const { - - return get_settings_path().plus_file("config").plus_file(project_config_path); -} - void EditorSettings::load_favorites() { FileAccess *f = FileAccess::open(get_project_settings_path().plus_file("favorite_dirs"), FileAccess::READ); @@ -963,7 +1043,7 @@ void EditorSettings::load_favorites() { void EditorSettings::list_text_editor_themes() { String themes = "Adaptive,Default"; - DirAccess *d = DirAccess::open(settings_path + "/text_editor_themes"); + DirAccess *d = DirAccess::open(get_settings_path().plus_file("text_editor_themes")); if (d) { d->list_dir_begin(); String file = d->get_next(); @@ -985,7 +1065,7 @@ void EditorSettings::load_text_editor_theme() { return; } - String theme_path = get_settings_path() + "/text_editor_themes/" + get("text_editor/theme/color_theme") + ".tet"; + String theme_path = get_settings_path().plus_file("text_editor_themes").plus_file((String)get("text_editor/theme/color_theme") + ".tet"); Ref<ConfigFile> cf = memnew(ConfigFile); Error err = cf->load(theme_path); @@ -1023,9 +1103,9 @@ bool EditorSettings::import_text_editor_theme(String p_file) { return false; } - DirAccess *d = DirAccess::open(settings_path + "/text_editor_themes"); + DirAccess *d = DirAccess::open(get_settings_path().plus_file("text_editor_themes")); if (d) { - d->copy(p_file, settings_path + "/text_editor_themes/" + p_file.get_file()); + d->copy(p_file, get_settings_path().plus_file("text_editor_themes").plus_file(p_file.get_file())); memdelete(d); return true; } @@ -1040,7 +1120,7 @@ bool EditorSettings::save_text_editor_theme() { if (p_file.get_file().to_lower() == "default" || p_file.get_file().to_lower() == "adaptive") { return false; } - String theme_path = get_settings_path() + "/text_editor_themes/" + p_file + ".tet"; + String theme_path = get_settings_path().plus_file("text_editor_themes").plus_file(p_file + ".tet"); return _save_text_editor_theme(theme_path); } @@ -1058,7 +1138,7 @@ bool EditorSettings::save_text_editor_theme_as(String p_file) { list_text_editor_themes(); String theme_name = p_file.substr(0, p_file.length() - 4).get_file(); - if (p_file.get_base_dir() == get_settings_path() + "/text_editor_themes") { + if (p_file.get_base_dir() == get_settings_path().plus_file("text_editor_themes")) { _initial_set("text_editor/theme/color_theme", theme_name); load_text_editor_theme(); } @@ -1070,7 +1150,7 @@ bool EditorSettings::save_text_editor_theme_as(String p_file) { Vector<String> EditorSettings::get_script_templates(const String &p_extension) { Vector<String> templates; - DirAccess *d = DirAccess::open(settings_path + "/script_templates"); + DirAccess *d = DirAccess::open(get_settings_path().plus_file("script_templates")); if (d) { d->list_dir_begin(); String file = d->get_next(); @@ -1086,46 +1166,7 @@ Vector<String> EditorSettings::get_script_templates(const String &p_extension) { return templates; } -bool EditorSettings::_save_text_editor_theme(String p_file) { - String theme_section = "color_theme"; - Ref<ConfigFile> cf = memnew(ConfigFile); // hex is better? - cf->set_value(theme_section, "background_color", ((Color)get("text_editor/highlighting/background_color")).to_html()); - cf->set_value(theme_section, "completion_background_color", ((Color)get("text_editor/highlighting/completion_background_color")).to_html()); - cf->set_value(theme_section, "completion_selected_color", ((Color)get("text_editor/highlighting/completion_selected_color")).to_html()); - cf->set_value(theme_section, "completion_existing_color", ((Color)get("text_editor/highlighting/completion_existing_color")).to_html()); - cf->set_value(theme_section, "completion_scroll_color", ((Color)get("text_editor/highlighting/completion_scroll_color")).to_html()); - cf->set_value(theme_section, "completion_font_color", ((Color)get("text_editor/highlighting/completion_font_color")).to_html()); - cf->set_value(theme_section, "caret_color", ((Color)get("text_editor/highlighting/caret_color")).to_html()); - cf->set_value(theme_section, "caret_background_color", ((Color)get("text_editor/highlighting/caret_background_color")).to_html()); - cf->set_value(theme_section, "line_number_color", ((Color)get("text_editor/highlighting/line_number_color")).to_html()); - cf->set_value(theme_section, "text_color", ((Color)get("text_editor/highlighting/text_color")).to_html()); - cf->set_value(theme_section, "text_selected_color", ((Color)get("text_editor/highlighting/text_selected_color")).to_html()); - cf->set_value(theme_section, "keyword_color", ((Color)get("text_editor/highlighting/keyword_color")).to_html()); - cf->set_value(theme_section, "base_type_color", ((Color)get("text_editor/highlighting/base_type_color")).to_html()); - cf->set_value(theme_section, "engine_type_color", ((Color)get("text_editor/highlighting/engine_type_color")).to_html()); - cf->set_value(theme_section, "function_color", ((Color)get("text_editor/highlighting/function_color")).to_html()); - cf->set_value(theme_section, "member_variable_color", ((Color)get("text_editor/highlighting/member_variable_color")).to_html()); - cf->set_value(theme_section, "comment_color", ((Color)get("text_editor/highlighting/comment_color")).to_html()); - cf->set_value(theme_section, "string_color", ((Color)get("text_editor/highlighting/string_color")).to_html()); - cf->set_value(theme_section, "number_color", ((Color)get("text_editor/highlighting/number_color")).to_html()); - cf->set_value(theme_section, "symbol_color", ((Color)get("text_editor/highlighting/symbol_color")).to_html()); - cf->set_value(theme_section, "selection_color", ((Color)get("text_editor/highlighting/selection_color")).to_html()); - cf->set_value(theme_section, "brace_mismatch_color", ((Color)get("text_editor/highlighting/brace_mismatch_color")).to_html()); - cf->set_value(theme_section, "current_line_color", ((Color)get("text_editor/highlighting/current_line_color")).to_html()); - cf->set_value(theme_section, "line_length_guideline_color", ((Color)get("text_editor/highlighting/line_length_guideline_color")).to_html()); - cf->set_value(theme_section, "mark_color", ((Color)get("text_editor/highlighting/mark_color")).to_html()); - cf->set_value(theme_section, "breakpoint_color", ((Color)get("text_editor/highlighting/breakpoint_color")).to_html()); - cf->set_value(theme_section, "word_highlighted_color", ((Color)get("text_editor/highlighting/word_highlighted_color")).to_html()); - cf->set_value(theme_section, "search_result_color", ((Color)get("text_editor/highlighting/search_result_color")).to_html()); - cf->set_value(theme_section, "search_result_border_color", ((Color)get("text_editor/highlighting/search_result_border_color")).to_html()); - - Error err = cf->save(p_file); - - if (err == OK) { - return true; - } - return false; -} +// Shortcuts void EditorSettings::add_shortcut(const String &p_name, Ref<ShortCut> &p_shortcut) { @@ -1160,49 +1201,64 @@ void EditorSettings::get_shortcut_list(List<String> *r_shortcuts) { } } -void EditorSettings::set_optimize_save(bool p_optimize) { +Ref<ShortCut> ED_GET_SHORTCUT(const String &p_path) { - optimize_save = p_optimize; + Ref<ShortCut> sc = EditorSettings::get_singleton()->get_shortcut(p_path); + if (!sc.is_valid()) { + ERR_EXPLAIN("Used ED_GET_SHORTCUT with invalid shortcut: " + p_path); + ERR_FAIL_COND_V(!sc.is_valid(), sc); + } + + return sc; } -Variant EditorSettings::get_project_metadata(const String &p_section, const String &p_key, Variant p_default) { - Ref<ConfigFile> cf = memnew(ConfigFile); - String path = get_project_settings_path().plus_file("project_metadata.cfg"); - Error err = cf->load(path); - if (err != OK) { - return p_default; +Ref<ShortCut> ED_SHORTCUT(const String &p_path, const String &p_name, uint32_t p_keycode) { + + Ref<InputEventKey> ie; + if (p_keycode) { + ie.instance(); + + ie->set_unicode(p_keycode & KEY_CODE_MASK); + ie->set_scancode(p_keycode & KEY_CODE_MASK); + ie->set_shift(bool(p_keycode & KEY_MASK_SHIFT)); + ie->set_alt(bool(p_keycode & KEY_MASK_ALT)); + ie->set_control(bool(p_keycode & KEY_MASK_CTRL)); + ie->set_metakey(bool(p_keycode & KEY_MASK_META)); } - return cf->get_value(p_section, p_key, p_default); -} -void EditorSettings::set_project_metadata(const String &p_section, const String &p_key, Variant p_data) { - Ref<ConfigFile> cf = memnew(ConfigFile); - String path = get_project_settings_path().plus_file("project_metadata.cfg"); - cf->load(path); - cf->set_value(p_section, p_key, p_data); - cf->save(path); -} + Ref<ShortCut> sc = EditorSettings::get_singleton()->get_shortcut(p_path); + if (sc.is_valid()) { -bool EditorSettings::property_can_revert(const String &p_name) { + sc->set_name(p_name); //keep name (the ones that come from disk have no name) + sc->set_meta("original", ie); //to compare against changes + return sc; + } - if (!props.has(p_name)) - return false; + sc.instance(); + sc->set_name(p_name); + sc->set_shortcut(ie); + sc->set_meta("original", ie); //to compare against changes + EditorSettings::get_singleton()->add_shortcut(p_path, sc); - return props[p_name].initial != props[p_name].variant; + return sc; } -Variant EditorSettings::property_get_revert(const String &p_name) { +void EditorSettings::notify_changes() { - if (!props.has(p_name)) - return Variant(); + _THREAD_SAFE_METHOD_ - return props[p_name].initial; -} + SceneTree *sml = Object::cast_to<SceneTree>(OS::get_singleton()->get_main_loop()); + + if (!sml) { + return; + } -void EditorSettings::set_initial_value(const StringName &p_name, const Variant &p_value) { + Node *root = sml->get_root()->get_child(0); - ERR_FAIL_COND(!props.has(p_name)); - props[p_name].initial = p_value; + if (!root) { + return; + } + root->propagate_notification(NOTIFICATION_EDITOR_SETTINGS_CHANGED); } void EditorSettings::_bind_methods() { @@ -1210,24 +1266,20 @@ void EditorSettings::_bind_methods() { ClassDB::bind_method(D_METHOD("has_setting", "name"), &EditorSettings::has_setting); ClassDB::bind_method(D_METHOD("set_setting", "name", "value"), &EditorSettings::set_setting); ClassDB::bind_method(D_METHOD("get_setting", "name"), &EditorSettings::get_setting); - ClassDB::bind_method(D_METHOD("erase", "property"), &EditorSettings::erase); + ClassDB::bind_method(D_METHOD("set_initial_value", "name", "value"), &EditorSettings::set_initial_value); + ClassDB::bind_method(D_METHOD("property_can_revert", "name"), &EditorSettings::property_can_revert); + ClassDB::bind_method(D_METHOD("property_get_revert", "name"), &EditorSettings::property_get_revert); + ClassDB::bind_method(D_METHOD("add_property_info", "info"), &EditorSettings::_add_property_info_bind); + ClassDB::bind_method(D_METHOD("get_settings_path"), &EditorSettings::get_settings_path); ClassDB::bind_method(D_METHOD("get_project_settings_path"), &EditorSettings::get_project_settings_path); - ClassDB::bind_method(D_METHOD("add_property_info", "info"), &EditorSettings::_add_property_info_bind); - ClassDB::bind_method(D_METHOD("set_favorite_dirs", "dirs"), &EditorSettings::set_favorite_dirs); ClassDB::bind_method(D_METHOD("get_favorite_dirs"), &EditorSettings::get_favorite_dirs); - ClassDB::bind_method(D_METHOD("set_recent_dirs", "dirs"), &EditorSettings::set_recent_dirs); ClassDB::bind_method(D_METHOD("get_recent_dirs"), &EditorSettings::get_recent_dirs); - ClassDB::bind_method(D_METHOD("property_can_revert", "name"), &EditorSettings::property_can_revert); - ClassDB::bind_method(D_METHOD("property_get_revert", "name"), &EditorSettings::property_get_revert); - - ClassDB::bind_method(D_METHOD("set_initial_value", "name", "value"), &EditorSettings::set_initial_value); - ADD_SIGNAL(MethodInfo("settings_changed")); } @@ -1263,45 +1315,3 @@ EditorSettings::EditorSettings() { EditorSettings::~EditorSettings() { } - -Ref<ShortCut> ED_GET_SHORTCUT(const String &p_path) { - - Ref<ShortCut> sc = EditorSettings::get_singleton()->get_shortcut(p_path); - if (!sc.is_valid()) { - ERR_EXPLAIN("Used ED_GET_SHORTCUT with invalid shortcut: " + p_path); - ERR_FAIL_COND_V(!sc.is_valid(), sc); - } - - return sc; -} - -Ref<ShortCut> ED_SHORTCUT(const String &p_path, const String &p_name, uint32_t p_keycode) { - - Ref<InputEventKey> ie; - if (p_keycode) { - ie.instance(); - - ie->set_unicode(p_keycode & KEY_CODE_MASK); - ie->set_scancode(p_keycode & KEY_CODE_MASK); - ie->set_shift(bool(p_keycode & KEY_MASK_SHIFT)); - ie->set_alt(bool(p_keycode & KEY_MASK_ALT)); - ie->set_control(bool(p_keycode & KEY_MASK_CTRL)); - ie->set_metakey(bool(p_keycode & KEY_MASK_META)); - } - - Ref<ShortCut> sc = EditorSettings::get_singleton()->get_shortcut(p_path); - if (sc.is_valid()) { - - sc->set_name(p_name); //keep name (the ones that come from disk have no name) - sc->set_meta("original", ie); //to compare against changes - return sc; - } - - sc.instance(); - sc->set_name(p_name); - sc->set_shortcut(ie); - sc->set_meta("original", ie); //to compare against changes - EditorSettings::get_singleton()->add_shortcut(p_path, sc); - - return sc; -} diff --git a/editor/editor_settings.h b/editor/editor_settings.h index c5d2670650..a74be6494a 100644 --- a/editor/editor_settings.h +++ b/editor/editor_settings.h @@ -80,44 +80,37 @@ private: } }; + static Ref<EditorSettings> singleton; + HashMap<String, PropertyInfo> hints; - int last_order; HashMap<String, VariantContainer> props; - String resource_path; - - bool _set(const StringName &p_name, const Variant &p_value, bool p_emit_signal = true); - bool _get(const StringName &p_name, Variant &r_ret) const; - void _get_property_list(List<PropertyInfo> *p_list) const; - - void _initial_set(const StringName &p_name, const Variant &p_value); + int last_order; - static Ref<EditorSettings> singleton; + Ref<Resource> clipboard; + Vector<Ref<Translation> > translations; + Map<String, Ref<ShortCut> > shortcuts; + String resource_path; String config_file_path; String settings_path; + String project_config_path; - Ref<Resource> clipboard; + Vector<String> favorite_dirs; + Vector<String> recent_dirs; bool save_changed_setting; - bool optimize_save; //do not save stuff that came from config but was not set from engine + bool _set(const StringName &p_name, const Variant &p_value, bool p_emit_signal = true); + bool _get(const StringName &p_name, Variant &r_ret) const; + void _initial_set(const StringName &p_name, const Variant &p_value); + void _get_property_list(List<PropertyInfo> *p_list) const; + void _add_property_info_bind(const Dictionary &p_info); + void _load_defaults(Ref<ConfigFile> p_extra_config = NULL); void _load_default_text_editor_theme(); - bool _save_text_editor_theme(String p_file); - String project_config_path; - - Vector<String> favorite_dirs; - Vector<String> recent_dirs; - - Vector<Ref<Translation> > translations; - - Map<String, Ref<ShortCut> > shortcuts; - - void _add_property_info_bind(const Dictionary &p_info); - protected: static void _bind_methods(); @@ -126,41 +119,41 @@ public: NOTIFICATION_EDITOR_SETTINGS_CHANGED = 10000 }; - void set_manually(const StringName &p_name, const Variant &p_value, bool p_emit_signal = false) { - _set(p_name, p_value, p_emit_signal); - } - - void set_setting(const String &p_setting, const Variant &p_value); - Variant get_setting(const String &p_setting) const; - - bool has_setting(String p_var) const; static EditorSettings *get_singleton(); - void erase(String p_var); - String get_settings_path() const; - //String get_global_settings_path() const; - String get_project_settings_path() const; + static void create(); void setup_language(); void setup_network(); - - void raise_order(const String &p_name); - static void create(); static void save(); static void destroy(); + void set_optimize_save(bool p_optimize); - void notify_changes(); + void set_setting(const String &p_setting, const Variant &p_value); + Variant get_setting(const String &p_setting) const; + bool has_setting(const String &p_setting) const; + void erase(const String &p_setting); + void raise_order(const String &p_setting); + void set_initial_value(const StringName &p_setting, const Variant &p_value); + void set_manually(const StringName &p_setting, const Variant &p_value, bool p_emit_signal = false) { + _set(p_setting, p_value, p_emit_signal); + } + bool property_can_revert(const String &p_setting); + Variant property_get_revert(const String &p_setting); + void add_property_hint(const PropertyInfo &p_hint); void set_resource_clipboard(const Ref<Resource> &p_resource) { clipboard = p_resource; } Ref<Resource> get_resource_clipboard() const { return clipboard; } - void add_property_hint(const PropertyInfo &p_hint); + String get_settings_path() const; + String get_project_settings_path() const; + + void set_project_metadata(const String &p_section, const String &p_key, Variant p_data); + Variant get_project_metadata(const String &p_section, const String &p_key, Variant p_default); void set_favorite_dirs(const Vector<String> &p_favorites_dirs); Vector<String> get_favorite_dirs() const; - void set_recent_dirs(const Vector<String> &p_recent_dirs); Vector<String> get_recent_dirs() const; - void load_favorites(); void list_text_editor_themes(); @@ -176,15 +169,7 @@ public: Ref<ShortCut> get_shortcut(const String &p_name) const; void get_shortcut_list(List<String> *r_shortcuts); - void set_optimize_save(bool p_optimize); - - Variant get_project_metadata(const String &p_section, const String &p_key, Variant p_default); - void set_project_metadata(const String &p_section, const String &p_key, Variant p_data); - - bool property_can_revert(const String &p_name); - Variant property_get_revert(const String &p_name); - - void set_initial_value(const StringName &p_name, const Variant &p_value); + void notify_changes(); EditorSettings(); ~EditorSettings(); @@ -193,10 +178,10 @@ public: //not a macro any longer #define EDITOR_DEF(m_var, m_val) _EDITOR_DEF(m_var, Variant(m_val)) -Variant _EDITOR_DEF(const String &p_var, const Variant &p_default); +Variant _EDITOR_DEF(const String &p_setting, const Variant &p_default); #define EDITOR_GET(m_var) _EDITOR_GET(m_var) -Variant _EDITOR_GET(const String &p_var); +Variant _EDITOR_GET(const String &p_setting); #define ED_IS_SHORTCUT(p_name, p_ev) (EditorSettings::get_singleton()->is_shortcut(p_name, p_ev)) Ref<ShortCut> ED_SHORTCUT(const String &p_path, const String &p_name, uint32_t p_keycode = 0); |