diff options
Diffstat (limited to 'tools/editor/editor_settings.cpp')
-rw-r--r-- | tools/editor/editor_settings.cpp | 444 |
1 files changed, 420 insertions, 24 deletions
diff --git a/tools/editor/editor_settings.cpp b/tools/editor/editor_settings.cpp index 6f1e90936a..bf01e02330 100644 --- a/tools/editor/editor_settings.cpp +++ b/tools/editor/editor_settings.cpp @@ -41,6 +41,11 @@ #include "io/config_file.h" #include "editor_node.h" #include "globals.h" +#include "translations.h" +#include "io/file_access_memory.h" +#include "io/translation_loader_po.h" +#include "io/compression.h" +#include "os/keyboard.h" Ref<EditorSettings> EditorSettings::singleton=NULL; @@ -53,6 +58,26 @@ EditorSettings *EditorSettings::get_singleton() { bool EditorSettings::_set(const StringName& p_name, const Variant& p_value) { _THREAD_SAFE_METHOD_ + + if (p_name.operator String()=="shortcuts") { + + Array arr=p_value; + ERR_FAIL_COND_V(arr.size() && arr.size()&1,true); + print_line("shortcuts: "+Variant(arr).get_construct_string()); + for(int i=0;i<arr.size();i+=2) { + + String name = arr[i]; + InputEvent shortcut = arr[i+1]; + + Ref<ShortCut> sc; + sc.instance(); + sc->set_shortcut(shortcut); + add_shortcut(name,sc); + } + + return true; + } + if (p_value.get_type()==Variant::NIL) props.erase(p_name); else { @@ -70,6 +95,25 @@ bool EditorSettings::_get(const StringName& p_name,Variant &r_ret) const { _THREAD_SAFE_METHOD_ + if (p_name.operator String()=="shortcuts") { + + Array arr; + for (const Map<String,Ref<ShortCut> >::Element *E=shortcuts.front();E;E=E->next()) { + + Ref<ShortCut> sc=E->get(); + if (!sc->has_meta("original")) + continue; //this came from settings but is not any longer used + + InputEvent original = sc->get_meta("original"); + if (sc->is_shortcut(original) || (original.type==InputEvent::NONE && sc->get_shortcut().type==InputEvent::NONE)) + continue; //not changed from default, don't save + arr.push_back(E->key()); + arr.push_back(sc->get_shortcut()); + } + r_ret=arr; + return true; + } + const VariantContainer *v=props.getptr(p_name); if (!v) return false; @@ -122,6 +166,8 @@ void EditorSettings::_get_property_list(List<PropertyInfo> *p_list) const { p_list->push_back( pi ); } + + p_list->push_back(PropertyInfo(Variant::ARRAY,"shortcuts",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR)); //do not edit } bool EditorSettings::has(String p_var) const { @@ -169,7 +215,7 @@ void EditorSettings::create() { String config_path; String config_dir; - String config_file="editor_settings.xml"; + //String config_file="editor_settings.xml"; Ref<ConfigFile> extra_config = memnew(ConfigFile); String exe_path = OS::get_singleton()->get_executable_path().get_base_dir(); @@ -221,6 +267,12 @@ void EditorSettings::create() { 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("tmp")!=OK) { dir->make_dir("tmp"); } else { @@ -253,17 +305,26 @@ void EditorSettings::create() { // path at least is validated, so validate config file - config_file_path = config_path+"/"+config_dir+"/"+config_file; + config_file_path = config_path+"/"+config_dir+"/editor_settings.tres"; - if (!dir->file_exists(config_file)) { - memdelete(dir); - WARN_PRINT("Config file does not exist, creating.") - goto fail; + String open_path = config_file_path; + + if (!dir->file_exists("editor_settings.tres")) { + + open_path = config_path+"/"+config_dir+"/editor_settings.xml"; + + if (!dir->file_exists("editor_settings.xml")) { + + memdelete(dir); + WARN_PRINT("Config file does not exist, creating."); + goto fail; + } } memdelete(dir); - singleton = ResourceLoader::load(config_file_path,"EditorSettings"); + singleton = ResourceLoader::load(open_path,"EditorSettings"); + if (singleton.is_null()) { WARN_PRINT("Could not open config file."); goto fail; @@ -278,8 +339,10 @@ void EditorSettings::create() { print_line("EditorSettings: Load OK!"); } + singleton->setup_language(); singleton->setup_network(); singleton->load_favorites(); + singleton->list_text_editor_themes(); return; @@ -302,8 +365,10 @@ void EditorSettings::create() { singleton = Ref<EditorSettings>( memnew( EditorSettings ) ); singleton->config_file_path=config_file_path; singleton->settings_path=config_path+"/"+config_dir; - singleton->_load_defaults(extra_config); + singleton->_load_defaults(extra_config); + singleton->setup_language(); singleton->setup_network(); + singleton->list_text_editor_themes(); } @@ -315,6 +380,23 @@ String EditorSettings::get_settings_path() const { +void EditorSettings::setup_language() { + + String lang = get("global/editor_language"); + print_line("LANG IS "+lang); + if (lang=="en") + return; //none to do + + for(int i=0;i<translations.size();i++) { + print_line("TESTING "+translations[i]->get_locale()); + if (translations[i]->get_locale()==lang) { + print_line("ok translation"); + TranslationServer::get_singleton()->set_tool_translation(translations[i]); + break; + } + } +} + void EditorSettings::setup_network() { List<IP_Address> local_ip; @@ -382,8 +464,44 @@ void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) { _THREAD_SAFE_METHOD_ - set("global/font",""); - hints["global/font"]=PropertyInfo(Variant::STRING,"global/font",PROPERTY_HINT_GLOBAL_FILE,"*.fnt"); + + { + String lang_hint="en"; + String host_lang = OS::get_singleton()->get_locale(); + + String best; + + for(int i=0;i<translations.size();i++) { + String locale = translations[i]->get_locale(); + lang_hint+=","; + lang_hint+=locale; + + if (host_lang==locale) { + best=locale; + } + + if (best==String() && host_lang.begins_with(locale)) { + best=locale; + } + } + + if (best==String()) { + best="en"; + } + + set("global/editor_language",best); + hints["global/editor_language"]=PropertyInfo(Variant::STRING,"global/editor_language",PROPERTY_HINT_ENUM,lang_hint,PROPERTY_USAGE_DEFAULT|PROPERTY_USAGE_RESTART_IF_CHANGED); + } + + set("global/hidpi_mode",0); + hints["global/hidpi_mode"]=PropertyInfo(Variant::INT,"global/hidpi_mode",PROPERTY_HINT_ENUM,"Auto,LoDPI,HiDPI",PROPERTY_USAGE_DEFAULT|PROPERTY_USAGE_RESTART_IF_CHANGED); + set("global/show_script_in_scene_tabs",false); + set("global/font_size",14); + hints["global/font_size"]=PropertyInfo(Variant::INT,"global/font_size",PROPERTY_HINT_RANGE,"10,40,1",PROPERTY_USAGE_DEFAULT|PROPERTY_USAGE_RESTART_IF_CHANGED); + set("global/source_font_size",14); + hints["global/source_font_size"]=PropertyInfo(Variant::INT,"global/source_font_size",PROPERTY_HINT_RANGE,"10,40,1",PROPERTY_USAGE_DEFAULT|PROPERTY_USAGE_RESTART_IF_CHANGED); + set("global/custom_font",""); + hints["global/custom_font"]=PropertyInfo(Variant::STRING,"global/custom_font",PROPERTY_HINT_GLOBAL_FILE,"*.fnt",PROPERTY_USAGE_DEFAULT|PROPERTY_USAGE_RESTART_IF_CHANGED); set("global/autoscan_project_path",""); hints["global/autoscan_project_path"]=PropertyInfo(Variant::STRING,"global/autoscan_project_path",PROPERTY_HINT_GLOBAL_DIR); set("global/default_project_path",""); @@ -391,19 +509,14 @@ void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) { set("global/default_project_export_path",""); hints["global/default_project_export_path"]=PropertyInfo(Variant::STRING,"global/default_project_export_path",PROPERTY_HINT_GLOBAL_DIR); set("global/show_script_in_scene_tabs",false); - set("text_editor/background_color",Color::html("3b000000")); - set("text_editor/text_color",Color::html("aaaaaa")); - set("text_editor/text_selected_color",Color::html("000000")); - set("text_editor/keyword_color",Color::html("ffffb3")); - set("text_editor/base_type_color",Color::html("a4ffd4")); - set("text_editor/engine_type_color",Color::html("83d3ff")); - set("text_editor/comment_color",Color::html("983d1b")); - set("text_editor/string_color",Color::html("ef6ebe")); - set("text_editor/symbol_color",Color::html("badfff")); - set("text_editor/selection_color",Color::html("7b5dbe")); - set("text_editor/brace_mismatch_color",Color(1,0.2,0.2)); - set("text_editor/current_line_color",Color(0.3,0.5,0.8,0.15)); - set("text_editor/word_highlighted_color",Color(0.8,0.9,0.9,0.15)); + + + set("text_editor/color_theme","Default"); + hints["text_editor/color_theme"]=PropertyInfo(Variant::STRING,"text_editor/color_theme",PROPERTY_HINT_ENUM,"Default"); + + _load_default_text_editor_theme(); + + set("text_editor/syntax_highlighting", true); set("text_editor/highlight_all_occurrences", true); set("text_editor/scroll_past_end_of_file", false); @@ -412,10 +525,18 @@ void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) { hints["text_editor/tab_size"]=PropertyInfo(Variant::INT,"text_editor/tab_size",PROPERTY_HINT_RANGE,"1, 64, 1"); // size of 0 crashes. set("text_editor/draw_tabs", true); + set("text_editor/show_line_numbers", true); + set("text_editor/show_breakpoint_gutter", true); + + set("text_editor/trim_trailing_whitespace_on_save", false); set("text_editor/idle_parse_delay",2); set("text_editor/create_signal_callbacks",true); set("text_editor/autosave_interval_secs",0); + set("text_editor/caret_blink", false); + set("text_editor/caret_blink_speed", 0.65); + hints["text_editor/caret_blink_speed"]=PropertyInfo(Variant::REAL,"text_editor/caret_blink_speed",PROPERTY_HINT_RANGE,"0.1, 10, 0.1"); + set("text_editor/font",""); hints["text_editor/font"]=PropertyInfo(Variant::STRING,"text_editor/font",PROPERTY_HINT_GLOBAL_FILE,"*.fnt"); set("text_editor/auto_brace_complete", false); @@ -424,6 +545,8 @@ void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) { set("scenetree_editor/duplicate_node_name_num_separator",0); hints["scenetree_editor/duplicate_node_name_num_separator"]=PropertyInfo(Variant::INT,"scenetree_editor/duplicate_node_name_num_separator",PROPERTY_HINT_ENUM, "None,Space,Underscore,Dash"); + //set("scenetree_editor/display_old_action_buttons",false); + set("scenetree_editor/start_create_dialog_fully_expanded",false); set("gridmap_editor/pick_distance", 5000.0); @@ -442,6 +565,7 @@ void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) { set("3d_editor/zoom_modifier",4); hints["3d_editor/zoom_modifier"]=PropertyInfo(Variant::INT,"3d_editor/zoom_modifier",PROPERTY_HINT_ENUM,"None,Shift,Alt,Meta,Ctrl"); set("3d_editor/emulate_numpad",false); + set("3d_editor/trackpad_hint", false); set("2d_editor/bone_width",5); set("2d_editor/bone_color1",Color(1.0,1.0,1.0,0.9)); @@ -449,9 +573,11 @@ void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) { set("2d_editor/bone_selected_color",Color(0.9,0.45,0.45,0.9)); set("2d_editor/bone_ik_color",Color(0.9,0.9,0.45,0.9)); + set("2d_editor/keep_margins_when_changing_anchors", false); + set("game_window_placement/rect",0); hints["game_window_placement/rect"]=PropertyInfo(Variant::INT,"game_window_placement/rect",PROPERTY_HINT_ENUM,"Default,Centered,Custom Position,Force Maximized,Force Full Screen"); - String screen_hints="Default (Same as Editor)"; + String screen_hints=TTR("Default (Same as Editor)"); for(int i=0;i<OS::get_singleton()->get_screen_count();i++) { screen_hints+=",Monitor "+itos(i+1); } @@ -487,6 +613,7 @@ void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) { #else hints["import/pvrtc_texture_tool"]=PropertyInfo(Variant::STRING,"import/pvrtc_texture_tool",PROPERTY_HINT_GLOBAL_FILE,""); #endif + // TODO: Rename to "import/pvrtc_fast_conversion" to match other names? set("PVRTC/fast_conversion",false); @@ -494,6 +621,8 @@ void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) { set("resources/save_compressed_resources",true); set("resources/auto_reload_modified_images",true); + set("import/automatic_reimport_on_sources_changed",true); + if (p_extra_config.is_valid()) { if (p_extra_config->has_section("init_projects") && p_extra_config->has_section_key("init_projects", "list")) { @@ -520,6 +649,34 @@ void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) { }; }; + + + +} + +void EditorSettings::_load_default_text_editor_theme() { + set("text_editor/background_color",Color::html("3b000000")); + set("text_editor/caret_color",Color::html("aaaaaa")); + set("text_editor/line_number_color",Color::html("66aaaaaa")); + set("text_editor/text_color",Color::html("aaaaaa")); + set("text_editor/text_selected_color",Color::html("000000")); + set("text_editor/keyword_color",Color::html("ffffb3")); + set("text_editor/base_type_color",Color::html("a4ffd4")); + set("text_editor/engine_type_color",Color::html("83d3ff")); + set("text_editor/function_color",Color::html("66a2ce")); + set("text_editor/member_variable_color",Color::html("e64e59")); + set("text_editor/comment_color",Color::html("676767")); + set("text_editor/string_color",Color::html("ef6ebe")); + set("text_editor/number_color",Color::html("EB9532")); + set("text_editor/symbol_color",Color::html("badfff")); + set("text_editor/selection_color",Color::html("7b5dbe")); + set("text_editor/brace_mismatch_color",Color(1,0.2,0.2)); + set("text_editor/current_line_color",Color(0.3,0.5,0.8,0.15)); + set("text_editor/mark_color", Color(1.0,0.4,0.4,0.4)); + set("text_editor/breakpoint_color", Color(0.8,0.8,0.4,0.2)); + set("text_editor/word_highlighted_color",Color(0.8,0.9,0.9,0.15)); + set("text_editor/search_result_color",Color(0.05,0.25,0.05,1)); + set("text_editor/search_result_border_color",Color(0.1,0.45,0.1,1)); } void EditorSettings::notify_changes() { @@ -622,6 +779,180 @@ void EditorSettings::load_favorites() { } +void EditorSettings::list_text_editor_themes() { + String themes="Default"; + DirAccess *d = DirAccess::open(settings_path + "/text_editor_themes"); + if (d) { + d->list_dir_begin(); + String file = d->get_next(); + while(file != String()) { + if (file.extension() == "tet" && file.basename().to_lower() != "default") { + themes += "," + file.basename(); + } + file = d->get_next(); + } + d->list_dir_end(); + memdelete(d); + } + add_property_hint(PropertyInfo(Variant::STRING,"text_editor/color_theme",PROPERTY_HINT_ENUM,themes)); +} + +void EditorSettings::load_text_editor_theme() { + if (get("text_editor/color_theme") == "Default") { + _load_default_text_editor_theme(); // sorry for "Settings changed" console spam + return; + } + + String theme_path = get_settings_path() + "/text_editor_themes/" + get("text_editor/color_theme") + ".tet"; + + Ref<ConfigFile> cf = memnew( ConfigFile ); + Error err = cf->load(theme_path); + + if (err != OK) { + return; + } + + List<String> keys; + cf->get_section_keys("color_theme", &keys); + + for(List<String>::Element *E=keys.front();E;E=E->next()) { + String key = E->get(); + String val = cf->get_value("color_theme", key); + + // don't load if it's not already there! + if (has("text_editor/" + key)) { + + // make sure it is actually a color + if (val.is_valid_html_color() && key.find("color") >= 0) { + props["text_editor/"+key].variant = Color::html(val); // change manually to prevent "Settings changed" console spam + } + } + } + emit_signal("settings_changed"); + // if it doesn't load just use what is currently loaded +} + +bool EditorSettings::import_text_editor_theme(String p_file) { + + if (!p_file.ends_with(".tet")) { + return false; + } else { + if (p_file.get_file().to_lower() == "default.tet") { + return false; + } + + DirAccess *d = DirAccess::open(settings_path + "/text_editor_themes"); + if (d) { + d->copy(p_file, settings_path + "/text_editor_themes/" + p_file.get_file()); + memdelete(d); + return true; + } + } + return false; +} + +bool EditorSettings::save_text_editor_theme() { + + String p_file = get("text_editor/color_theme"); + + if (p_file.get_file().to_lower() == "default") { + return false; + } + String theme_path = get_settings_path() + "/text_editor_themes/" + p_file + ".tet"; + return _save_text_editor_theme(theme_path); +} + +bool EditorSettings::save_text_editor_theme_as(String p_file) { + if (!p_file.ends_with(".tet")) { + p_file += ".tet"; + } + + if (p_file.get_file().to_lower() == "default.tet") { + return false; + } + if(_save_text_editor_theme(p_file)) { + + // switch to theme is saved in the theme directory + 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") { + set("text_editor/color_theme", theme_name); + load_text_editor_theme(); + } + return true; + } + return false; +} + +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/background_color")).to_html()); + cf->set_value(theme_section, "caret_color", ((Color)get("text_editor/caret_color")).to_html()); + cf->set_value(theme_section, "line_number_color", ((Color)get("text_editor/line_number_color")).to_html()); + cf->set_value(theme_section, "text_color", ((Color)get("text_editor/text_color")).to_html()); + cf->set_value(theme_section, "text_selected_color", ((Color)get("text_editor/text_selected_color")).to_html()); + cf->set_value(theme_section, "keyword_color", ((Color)get("text_editor/keyword_color")).to_html()); + cf->set_value(theme_section, "base_type_color", ((Color)get("text_editor/base_type_color")).to_html()); + cf->set_value(theme_section, "engine_type_color", ((Color)get("text_editor/engine_type_color")).to_html()); + cf->set_value(theme_section, "function_color", ((Color)get("text_editor/function_color")).to_html()); + cf->set_value(theme_section, "member_variable_color", ((Color)get("text_editor/member_variable_color")).to_html()); + cf->set_value(theme_section, "comment_color", ((Color)get("text_editor/comment_color")).to_html()); + cf->set_value(theme_section, "string_color", ((Color)get("text_editor/string_color")).to_html()); + cf->set_value(theme_section, "number_color", ((Color)get("text_editor/number_color")).to_html()); + cf->set_value(theme_section, "symbol_color", ((Color)get("text_editor/symbol_color")).to_html()); + cf->set_value(theme_section, "selection_color", ((Color)get("text_editor/selection_color")).to_html()); + cf->set_value(theme_section, "brace_mismatch_color", ((Color)get("text_editor/brace_mismatch_color")).to_html()); + cf->set_value(theme_section, "current_line_color", ((Color)get("text_editor/current_line_color")).to_html()); + cf->set_value(theme_section, "mark_color", ((Color)get("text_editor/mark_color")).to_html()); + cf->set_value(theme_section, "breakpoint_color", ((Color)get("text_editor/breakpoint_color")).to_html()); + cf->set_value(theme_section, "word_highlighted_color", ((Color)get("text_editor/word_highlighted_color")).to_html()); + cf->set_value(theme_section, "search_result_color", ((Color)get("text_editor/search_result_color")).to_html()); + cf->set_value(theme_section, "search_result_border_color", ((Color)get("text_editor/search_result_border_color")).to_html()); + Error err = cf->save(p_file); + + if (err == OK) { + return true; + } + return false; +} + + +void EditorSettings::add_shortcut(const String& p_name, Ref<ShortCut> &p_shortcut) { + + shortcuts[p_name]=p_shortcut; +} + +bool EditorSettings::is_shortcut(const String&p_name, const InputEvent &p_event) const{ + + const Map<String,Ref<ShortCut> >::Element *E=shortcuts.find(p_name); + if (!E) { + ERR_EXPLAIN("Unknown Shortcut: "+p_name); + ERR_FAIL_V(false); + } + + return E->get()->is_shortcut(p_event); + +} + +Ref<ShortCut> EditorSettings::get_shortcut(const String&p_name) const{ + + const Map<String,Ref<ShortCut> >::Element *E=shortcuts.find(p_name); + if (!E) + return Ref<ShortCut>(); + + return E->get(); +} + +void EditorSettings::get_shortcut_list(List<String> *r_shortcuts) { + + for (const Map<String,Ref<ShortCut> >::Element*E=shortcuts.front();E;E=E->next()) { + + r_shortcuts->push_back(E->key()); + } +} + void EditorSettings::_bind_methods() { @@ -644,7 +975,32 @@ EditorSettings::EditorSettings() { //singleton=this; last_order=0; + + EditorTranslationList *etl=_editor_translations; + + while(etl->data) { + + Vector<uint8_t> data; + data.resize(etl->uncomp_size); + Compression::decompress(data.ptr(),etl->uncomp_size,etl->data,etl->comp_size,Compression::MODE_DEFLATE); + + FileAccessMemory *fa = memnew (FileAccessMemory); + fa->open_custom(data.ptr(),data.size()); + + Ref<Translation> tr = TranslationLoaderPO::load_translation(fa,NULL,"translation_"+String(etl->lang)); + + if (tr.is_valid()) { + tr->set_locale(etl->lang); + translations.push_back(tr); + } + + etl++; + + } + _load_defaults(); + + } @@ -653,4 +1009,44 @@ EditorSettings::~EditorSettings() { // singleton=NULL; } +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) { + + InputEvent ie; + if (p_keycode) { + ie.type=InputEvent::KEY; + ie.key.unicode=p_keycode&KEY_CODE_MASK; + ie.key.scancode=p_keycode&KEY_CODE_MASK; + ie.key.mod.shift=bool(p_keycode&KEY_MASK_SHIFT); + ie.key.mod.alt=bool(p_keycode&KEY_MASK_ALT); + ie.key.mod.control=bool(p_keycode&KEY_MASK_CTRL); + ie.key.mod.meta=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; +} |