editor_settings.cpp 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  1. /*************************************************************************/
  2. /* editor_settings.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_settings.h"
  31. #include "core/io/compression.h"
  32. #include "core/io/config_file.h"
  33. #include "core/io/file_access_memory.h"
  34. #include "core/io/resource_loader.h"
  35. #include "core/io/resource_saver.h"
  36. #include "core/io/translation_loader_po.h"
  37. #include "core/os/dir_access.h"
  38. #include "core/os/file_access.h"
  39. #include "core/os/keyboard.h"
  40. #include "core/os/os.h"
  41. #include "core/project_settings.h"
  42. #include "core/version.h"
  43. #include "editor/editor_node.h"
  44. #include "editor/translations.gen.h"
  45. #include "scene/main/node.h"
  46. #include "scene/main/scene_tree.h"
  47. #include "scene/main/viewport.h"
  48. // PRIVATE METHODS
  49. Ref<EditorSettings> EditorSettings::singleton = NULL;
  50. // Properties
  51. bool EditorSettings::_set(const StringName &p_name, const Variant &p_value) {
  52. _THREAD_SAFE_METHOD_
  53. bool changed = _set_only(p_name, p_value);
  54. if (changed) {
  55. emit_signal("settings_changed");
  56. }
  57. return true;
  58. }
  59. bool EditorSettings::_set_only(const StringName &p_name, const Variant &p_value) {
  60. _THREAD_SAFE_METHOD_
  61. if (p_name.operator String() == "shortcuts") {
  62. Array arr = p_value;
  63. ERR_FAIL_COND_V(arr.size() && arr.size() & 1, true);
  64. for (int i = 0; i < arr.size(); i += 2) {
  65. String name = arr[i];
  66. Ref<InputEvent> shortcut = arr[i + 1];
  67. Ref<ShortCut> sc;
  68. sc.instance();
  69. sc->set_shortcut(shortcut);
  70. add_shortcut(name, sc);
  71. }
  72. return false;
  73. }
  74. bool changed = false;
  75. if (p_value.get_type() == Variant::NIL) {
  76. if (props.has(p_name)) {
  77. props.erase(p_name);
  78. changed = true;
  79. }
  80. } else {
  81. if (props.has(p_name)) {
  82. if (p_value != props[p_name].variant) {
  83. props[p_name].variant = p_value;
  84. changed = true;
  85. }
  86. } else {
  87. props[p_name] = VariantContainer(p_value, last_order++);
  88. changed = true;
  89. }
  90. if (save_changed_setting) {
  91. if (props[p_name].save != true) {
  92. props[p_name].save = true;
  93. changed = true;
  94. }
  95. }
  96. }
  97. return changed;
  98. }
  99. bool EditorSettings::_get(const StringName &p_name, Variant &r_ret) const {
  100. _THREAD_SAFE_METHOD_
  101. if (p_name.operator String() == "shortcuts") {
  102. Array arr;
  103. for (const Map<String, Ref<ShortCut> >::Element *E = shortcuts.front(); E; E = E->next()) {
  104. Ref<ShortCut> sc = E->get();
  105. if (optimize_save) {
  106. if (!sc->has_meta("original")) {
  107. continue; //this came from settings but is not any longer used
  108. }
  109. Ref<InputEvent> original = sc->get_meta("original");
  110. if (sc->is_shortcut(original) || (original.is_null() && sc->get_shortcut().is_null()))
  111. continue; //not changed from default, don't save
  112. }
  113. arr.push_back(E->key());
  114. arr.push_back(sc->get_shortcut());
  115. }
  116. r_ret = arr;
  117. return true;
  118. }
  119. const VariantContainer *v = props.getptr(p_name);
  120. if (!v) {
  121. print_line("EditorSettings::_get - Warning, not found: " + String(p_name));
  122. return false;
  123. }
  124. r_ret = v->variant;
  125. return true;
  126. }
  127. void EditorSettings::_initial_set(const StringName &p_name, const Variant &p_value) {
  128. set(p_name, p_value);
  129. props[p_name].initial = p_value;
  130. props[p_name].has_default_value = true;
  131. }
  132. struct _EVCSort {
  133. String name;
  134. Variant::Type type;
  135. int order;
  136. bool save;
  137. bool operator<(const _EVCSort &p_vcs) const { return order < p_vcs.order; }
  138. };
  139. void EditorSettings::_get_property_list(List<PropertyInfo> *p_list) const {
  140. _THREAD_SAFE_METHOD_
  141. const String *k = NULL;
  142. Set<_EVCSort> vclist;
  143. while ((k = props.next(k))) {
  144. const VariantContainer *v = props.getptr(*k);
  145. if (v->hide_from_editor)
  146. continue;
  147. _EVCSort vc;
  148. vc.name = *k;
  149. vc.order = v->order;
  150. vc.type = v->variant.get_type();
  151. vc.save = v->save;
  152. vclist.insert(vc);
  153. }
  154. for (Set<_EVCSort>::Element *E = vclist.front(); E; E = E->next()) {
  155. int pinfo = 0;
  156. if (E->get().save || !optimize_save) {
  157. pinfo |= PROPERTY_USAGE_STORAGE;
  158. }
  159. if (!E->get().name.begins_with("_") && !E->get().name.begins_with("projects/")) {
  160. pinfo |= PROPERTY_USAGE_EDITOR;
  161. } else {
  162. pinfo |= PROPERTY_USAGE_STORAGE; //hiddens must always be saved
  163. }
  164. PropertyInfo pi(E->get().type, E->get().name);
  165. pi.usage = pinfo;
  166. if (hints.has(E->get().name))
  167. pi = hints[E->get().name];
  168. p_list->push_back(pi);
  169. }
  170. p_list->push_back(PropertyInfo(Variant::ARRAY, "shortcuts", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL)); //do not edit
  171. }
  172. void EditorSettings::_add_property_info_bind(const Dictionary &p_info) {
  173. ERR_FAIL_COND(!p_info.has("name"));
  174. ERR_FAIL_COND(!p_info.has("type"));
  175. PropertyInfo pinfo;
  176. pinfo.name = p_info["name"];
  177. ERR_FAIL_COND(!props.has(pinfo.name));
  178. pinfo.type = Variant::Type(p_info["type"].operator int());
  179. ERR_FAIL_INDEX(pinfo.type, Variant::VARIANT_MAX);
  180. if (p_info.has("hint"))
  181. pinfo.hint = PropertyHint(p_info["hint"].operator int());
  182. if (p_info.has("hint_string"))
  183. pinfo.hint_string = p_info["hint_string"];
  184. add_property_hint(pinfo);
  185. }
  186. // Default configs
  187. bool EditorSettings::has_default_value(const String &p_setting) const {
  188. _THREAD_SAFE_METHOD_
  189. if (!props.has(p_setting))
  190. return false;
  191. return props[p_setting].has_default_value;
  192. }
  193. void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) {
  194. _THREAD_SAFE_METHOD_
  195. {
  196. String lang_hint = "en";
  197. String host_lang = OS::get_singleton()->get_locale();
  198. host_lang = TranslationServer::standardize_locale(host_lang);
  199. String best;
  200. for (int i = 0; i < translations.size(); i++) {
  201. String locale = translations[i]->get_locale();
  202. lang_hint += ",";
  203. lang_hint += locale;
  204. if (host_lang == locale) {
  205. best = locale;
  206. }
  207. if (best == String() && host_lang.begins_with(locale)) {
  208. best = locale;
  209. }
  210. }
  211. if (best == String()) {
  212. best = "en";
  213. }
  214. _initial_set("interface/editor/editor_language", best);
  215. hints["interface/editor/editor_language"] = PropertyInfo(Variant::STRING, "interface/editor/editor_language", PROPERTY_HINT_ENUM, lang_hint, PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED);
  216. }
  217. _initial_set("interface/editor/hidpi_mode", 0);
  218. hints["interface/editor/hidpi_mode"] = PropertyInfo(Variant::INT, "interface/editor/hidpi_mode", PROPERTY_HINT_ENUM, "Auto,VeryLoDPI,LoDPI,MidDPI,HiDPI", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED);
  219. _initial_set("interface/scene_tabs/show_script_button", false);
  220. _initial_set("interface/editor/main_font_size", 14);
  221. hints["interface/editor/main_font_size"] = PropertyInfo(Variant::INT, "interface/editor/main_font_size", PROPERTY_HINT_RANGE, "10,40,1", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED);
  222. _initial_set("interface/editor/code_font_size", 14);
  223. hints["interface/editor/code_font_size"] = PropertyInfo(Variant::INT, "interface/editor/code_font_size", PROPERTY_HINT_RANGE, "8,96,1", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED);
  224. _initial_set("interface/editor/main_font_hinting", 2);
  225. hints["interface/editor/main_font_hinting"] = PropertyInfo(Variant::INT, "interface/editor/main_font_hinting", PROPERTY_HINT_ENUM, "None,Light,Normal", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED);
  226. _initial_set("interface/editor/code_font_hinting", 2);
  227. hints["interface/editor/code_font_hinting"] = PropertyInfo(Variant::INT, "interface/editor/code_font_hinting", PROPERTY_HINT_ENUM, "None,Light,Normal", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED);
  228. _initial_set("interface/editor/main_font", "");
  229. hints["interface/editor/main_font"] = PropertyInfo(Variant::STRING, "interface/editor/main_font", PROPERTY_HINT_GLOBAL_FILE, "*.ttf,*.otf", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED);
  230. _initial_set("interface/editor/code_font", "");
  231. hints["interface/editor/code_font"] = PropertyInfo(Variant::STRING, "interface/editor/code_font", PROPERTY_HINT_GLOBAL_FILE, "*.ttf,*.otf", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED);
  232. _initial_set("interface/editor/dim_editor_on_dialog_popup", true);
  233. _initial_set("interface/editor/dim_amount", 0.6f);
  234. hints["interface/editor/dim_amount"] = PropertyInfo(Variant::REAL, "interface/editor/dim_amount", PROPERTY_HINT_RANGE, "0,1,0.01", PROPERTY_USAGE_DEFAULT);
  235. _initial_set("interface/editor/dim_transition_time", 0.08f);
  236. hints["interface/editor/dim_transition_time"] = PropertyInfo(Variant::REAL, "interface/editor/dim_transition_time", PROPERTY_HINT_RANGE, "0,1,0.001", PROPERTY_USAGE_DEFAULT);
  237. _initial_set("interface/editor/separate_distraction_mode", false);
  238. _initial_set("interface/editor/save_each_scene_on_quit", true); // Regression
  239. _initial_set("interface/editor/quit_confirmation", true);
  240. _initial_set("interface/theme/preset", 0);
  241. hints["interface/theme/preset"] = PropertyInfo(Variant::INT, "interface/theme/preset", PROPERTY_HINT_ENUM, "Default,Grey,Godot 2,Arc,Light,Alien,Custom", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED);
  242. _initial_set("interface/theme/icon_and_font_color", 0);
  243. hints["interface/theme/icon_and_font_color"] = PropertyInfo(Variant::INT, "interface/theme/icon_and_font_color", PROPERTY_HINT_ENUM, "Auto,Dark,Light", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED);
  244. _initial_set("interface/theme/base_color", Color::html("#323b4f"));
  245. hints["interface/theme/accent_color"] = PropertyInfo(Variant::COLOR, "interface/theme/accent_color", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED);
  246. _initial_set("interface/theme/accent_color", Color::html("#699ce8"));
  247. hints["interface/theme/base_color"] = PropertyInfo(Variant::COLOR, "interface/theme/base_color", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED);
  248. _initial_set("interface/theme/contrast", 0.25);
  249. hints["interface/theme/contrast"] = PropertyInfo(Variant::REAL, "interface/theme/contrast", PROPERTY_HINT_RANGE, "0.01, 1, 0.01");
  250. _initial_set("interface/theme/highlight_tabs", false);
  251. _initial_set("interface/theme/border_size", 1);
  252. _initial_set("interface/theme/use_graph_node_headers", false);
  253. hints["interface/theme/border_size"] = PropertyInfo(Variant::INT, "interface/theme/border_size", PROPERTY_HINT_RANGE, "0,2,1", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED);
  254. _initial_set("interface/theme/additional_spacing", 0);
  255. hints["interface/theme/additional_spacing"] = PropertyInfo(Variant::REAL, "interface/theme/additional_spacing", PROPERTY_HINT_RANGE, "0,5,0.1", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED);
  256. _initial_set("interface/theme/custom_theme", "");
  257. hints["interface/theme/custom_theme"] = PropertyInfo(Variant::STRING, "interface/theme/custom_theme", PROPERTY_HINT_GLOBAL_FILE, "*.res,*.tres,*.theme", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED);
  258. _initial_set("interface/scene_tabs/show_extension", false);
  259. _initial_set("interface/scene_tabs/show_thumbnail_on_hover", true);
  260. _initial_set("interface/scene_tabs/resize_if_many_tabs", true);
  261. _initial_set("interface/scene_tabs/minimum_width", 50);
  262. hints["interface/scene_tabs/minimum_width"] = PropertyInfo(Variant::INT, "interface/scene_tabs/minimum_width", PROPERTY_HINT_RANGE, "50,500,1", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED);
  263. _initial_set("filesystem/directories/autoscan_project_path", "");
  264. hints["filesystem/directories/autoscan_project_path"] = PropertyInfo(Variant::STRING, "filesystem/directories/autoscan_project_path", PROPERTY_HINT_GLOBAL_DIR);
  265. _initial_set("filesystem/directories/default_project_path", OS::get_singleton()->has_environment("HOME") ? OS::get_singleton()->get_environment("HOME") : OS::get_singleton()->get_system_dir(OS::SYSTEM_DIR_DOCUMENTS));
  266. hints["filesystem/directories/default_project_path"] = PropertyInfo(Variant::STRING, "filesystem/directories/default_project_path", PROPERTY_HINT_GLOBAL_DIR);
  267. _initial_set("filesystem/directories/default_project_export_path", "");
  268. hints["filesystem/directories/default_project_export_path"] = PropertyInfo(Variant::STRING, "filesystem/directories/default_project_export_path", PROPERTY_HINT_GLOBAL_DIR);
  269. _initial_set("interface/scene_tabs/show_script_button", false);
  270. _initial_set("text_editor/theme/color_theme", "Adaptive");
  271. hints["text_editor/theme/color_theme"] = PropertyInfo(Variant::STRING, "text_editor/theme/color_theme", PROPERTY_HINT_ENUM, "Adaptive,Default,Custom");
  272. _initial_set("text_editor/theme/line_spacing", 4);
  273. _load_default_text_editor_theme();
  274. _initial_set("text_editor/highlighting/syntax_highlighting", true);
  275. _initial_set("text_editor/highlighting/highlight_all_occurrences", true);
  276. _initial_set("text_editor/highlighting/highlight_current_line", true);
  277. _initial_set("text_editor/cursor/scroll_past_end_of_file", false);
  278. _initial_set("text_editor/indent/type", 0);
  279. hints["text_editor/indent/type"] = PropertyInfo(Variant::INT, "text_editor/indent/type", PROPERTY_HINT_ENUM, "Tabs,Spaces");
  280. _initial_set("text_editor/indent/size", 4);
  281. hints["text_editor/indent/size"] = PropertyInfo(Variant::INT, "text_editor/indent/size", PROPERTY_HINT_RANGE, "1, 64, 1"); // size of 0 crashes.
  282. _initial_set("text_editor/indent/auto_indent", true);
  283. _initial_set("text_editor/indent/convert_indent_on_save", false);
  284. _initial_set("text_editor/indent/draw_tabs", true);
  285. _initial_set("text_editor/line_numbers/show_line_numbers", true);
  286. _initial_set("text_editor/line_numbers/line_numbers_zero_padded", false);
  287. _initial_set("text_editor/line_numbers/show_breakpoint_gutter", true);
  288. _initial_set("text_editor/line_numbers/code_folding", true);
  289. _initial_set("text_editor/line_numbers/show_line_length_guideline", false);
  290. _initial_set("text_editor/line_numbers/line_length_guideline_column", 80);
  291. hints["text_editor/line_numbers/line_length_guideline_column"] = PropertyInfo(Variant::INT, "text_editor/line_numbers/line_length_guideline_column", PROPERTY_HINT_RANGE, "20, 160, 1");
  292. _initial_set("text_editor/open_scripts/smooth_scrolling", true);
  293. _initial_set("text_editor/open_scripts/v_scroll_speed", 80);
  294. _initial_set("text_editor/open_scripts/show_members_overview", true);
  295. _initial_set("text_editor/files/trim_trailing_whitespace_on_save", false);
  296. _initial_set("text_editor/completion/idle_parse_delay", 2);
  297. _initial_set("text_editor/tools/create_signal_callbacks", true);
  298. _initial_set("text_editor/files/autosave_interval_secs", 0);
  299. _initial_set("text_editor/cursor/block_caret", false);
  300. _initial_set("text_editor/cursor/caret_blink", true);
  301. _initial_set("text_editor/cursor/caret_blink_speed", 0.65);
  302. hints["text_editor/cursor/caret_blink_speed"] = PropertyInfo(Variant::REAL, "text_editor/cursor/caret_blink_speed", PROPERTY_HINT_RANGE, "0.1, 10, 0.01");
  303. _initial_set("text_editor/cursor/right_click_moves_caret", true);
  304. _initial_set("text_editor/completion/auto_brace_complete", false);
  305. _initial_set("text_editor/completion/put_callhint_tooltip_below_current_line", true);
  306. _initial_set("text_editor/completion/callhint_tooltip_offset", Vector2());
  307. _initial_set("text_editor/files/restore_scripts_on_load", true);
  308. _initial_set("text_editor/completion/complete_file_paths", true);
  309. _initial_set("text_editor/files/maximum_recent_files", 20);
  310. hints["text_editor/files/maximum_recent_files"] = PropertyInfo(Variant::INT, "text_editor/files/maximum_recent_files", PROPERTY_HINT_RANGE, "1, 200, 1");
  311. _initial_set("docks/scene_tree/start_create_dialog_fully_expanded", false);
  312. _initial_set("docks/scene_tree/draw_relationship_lines", false);
  313. _initial_set("docks/scene_tree/relationship_line_color", Color::html("464646"));
  314. _initial_set("editors/grid_map/pick_distance", 5000.0);
  315. _initial_set("editors/3d/grid_color", Color::html("808080"));
  316. hints["editors/3d/grid_color"] = PropertyInfo(Variant::COLOR, "editors/3d/grid_color", PROPERTY_HINT_COLOR_NO_ALPHA, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED);
  317. _initial_set("editors/3d/default_fov", 70.0);
  318. _initial_set("editors/3d/default_z_near", 0.05);
  319. _initial_set("editors/3d/default_z_far", 500.0);
  320. // navigation
  321. _initial_set("editors/3d/navigation/navigation_scheme", 0);
  322. _initial_set("editors/3d/navigation/invert_y_axis", false);
  323. hints["editors/3d/navigation/navigation_scheme"] = PropertyInfo(Variant::INT, "editors/3d/navigation/navigation_scheme", PROPERTY_HINT_ENUM, "Godot,Maya,Modo");
  324. _initial_set("editors/3d/navigation/zoom_style", 0);
  325. hints["editors/3d/navigation/zoom_style"] = PropertyInfo(Variant::INT, "editors/3d/navigation/zoom_style", PROPERTY_HINT_ENUM, "Vertical, Horizontal");
  326. _initial_set("editors/3d/navigation/emulate_3_button_mouse", false);
  327. _initial_set("editors/3d/navigation/orbit_modifier", 0);
  328. hints["editors/3d/navigation/orbit_modifier"] = PropertyInfo(Variant::INT, "editors/3d/navigation/orbit_modifier", PROPERTY_HINT_ENUM, "None,Shift,Alt,Meta,Ctrl");
  329. _initial_set("editors/3d/navigation/pan_modifier", 1);
  330. hints["editors/3d/navigation/pan_modifier"] = PropertyInfo(Variant::INT, "editors/3d/navigation/pan_modifier", PROPERTY_HINT_ENUM, "None,Shift,Alt,Meta,Ctrl");
  331. _initial_set("editors/3d/navigation/zoom_modifier", 4);
  332. hints["editors/3d/navigation/zoom_modifier"] = PropertyInfo(Variant::INT, "editors/3d/navigation/zoom_modifier", PROPERTY_HINT_ENUM, "None,Shift,Alt,Meta,Ctrl");
  333. // _initial_set("editors/3d/navigation/emulate_numpad", false); not used at the moment
  334. _initial_set("editors/3d/navigation/warped_mouse_panning", true);
  335. // navigation feel
  336. _initial_set("editors/3d/navigation_feel/orbit_sensitivity", 0.4);
  337. hints["editors/3d/navigation_feel/orbit_sensitivity"] = PropertyInfo(Variant::REAL, "editors/3d/navigation_feel/orbit_sensitivity", PROPERTY_HINT_RANGE, "0.0, 2, 0.01");
  338. _initial_set("editors/3d/navigation_feel/orbit_inertia", 0.05);
  339. hints["editors/3d/navigation_feel/orbit_inertia"] = PropertyInfo(Variant::REAL, "editors/3d/navigation_feel/orbit_inertia", PROPERTY_HINT_RANGE, "0.0, 1, 0.01");
  340. _initial_set("editors/3d/navigation_feel/translation_inertia", 0.15);
  341. hints["editors/3d/navigation_feel/translation_inertia"] = PropertyInfo(Variant::REAL, "editors/3d/navigation_feel/translation_inertia", PROPERTY_HINT_RANGE, "0.0, 1, 0.01");
  342. _initial_set("editors/3d/navigation_feel/zoom_inertia", 0.075);
  343. hints["editors/3d/navigation_feel/zoom_inertia"] = PropertyInfo(Variant::REAL, "editors/3d/navigation_feel/zoom_inertia", PROPERTY_HINT_RANGE, "0.0, 1, 0.01");
  344. _initial_set("editors/3d/navigation_feel/manipulation_orbit_inertia", 0.075);
  345. hints["editors/3d/navigation_feel/manipulation_orbit_inertia"] = PropertyInfo(Variant::REAL, "editors/3d/navigation_feel/manipulation_orbit_inertia", PROPERTY_HINT_RANGE, "0.0, 1, 0.01");
  346. _initial_set("editors/3d/navigation_feel/manipulation_translation_inertia", 0.075);
  347. hints["editors/3d/navigation_feel/manipulation_translation_inertia"] = PropertyInfo(Variant::REAL, "editors/3d/navigation_feel/manipulation_translation_inertia", PROPERTY_HINT_RANGE, "0.0, 1, 0.01");
  348. // freelook
  349. _initial_set("editors/3d/freelook/freelook_inertia", 0.1);
  350. hints["editors/3d/freelook/freelook_inertia"] = PropertyInfo(Variant::REAL, "editors/3d/freelook/freelook_inertia", PROPERTY_HINT_RANGE, "0.0, 1, 0.01");
  351. _initial_set("editors/3d/freelook/freelook_base_speed", 5.0);
  352. hints["editors/3d/freelook/freelook_base_speed"] = PropertyInfo(Variant::REAL, "editors/3d/freelook/freelook_base_speed", PROPERTY_HINT_RANGE, "0.0, 10, 0.01");
  353. _initial_set("editors/3d/freelook/freelook_activation_modifier", 0);
  354. hints["editors/3d/freelook/freelook_activation_modifier"] = PropertyInfo(Variant::INT, "editors/3d/freelook/freelook_activation_modifier", PROPERTY_HINT_ENUM, "None,Shift,Alt,Meta,Ctrl");
  355. _initial_set("editors/3d/freelook/freelook_modifier_speed_factor", 3.0);
  356. hints["editors/3d/freelook/freelook_modifier_speed_factor"] = PropertyInfo(Variant::REAL, "editors/3d/freelook/freelook_modifier_speed_factor", PROPERTY_HINT_RANGE, "0.0, 10.0, 0.1");
  357. _initial_set("editors/3d/freelook/freelook_speed_zoom_link", false);
  358. _initial_set("editors/2d/guides_color", Color(0.6, 0.0, 0.8));
  359. _initial_set("editors/2d/bone_width", 5);
  360. _initial_set("editors/2d/bone_color1", Color(1.0, 1.0, 1.0, 0.9));
  361. _initial_set("editors/2d/bone_color2", Color(0.75, 0.75, 0.75, 0.9));
  362. _initial_set("editors/2d/bone_selected_color", Color(0.9, 0.45, 0.45, 0.9));
  363. _initial_set("editors/2d/bone_ik_color", Color(0.9, 0.9, 0.45, 0.9));
  364. _initial_set("editors/2d/keep_margins_when_changing_anchors", false);
  365. _initial_set("editors/2d/warped_mouse_panning", true);
  366. _initial_set("editors/2d/simple_spacebar_panning", false);
  367. _initial_set("editors/2d/scroll_to_pan", false);
  368. _initial_set("editors/2d/pan_speed", 20);
  369. _initial_set("editors/poly_editor/point_grab_radius", 8);
  370. _initial_set("editors/poly_editor/show_previous_outline", true);
  371. _initial_set("run/window_placement/rect", 1);
  372. hints["run/window_placement/rect"] = PropertyInfo(Variant::INT, "run/window_placement/rect", PROPERTY_HINT_ENUM, "Top Left,Centered,Custom Position,Force Maximized,Force Fullscreen");
  373. String screen_hints = TTR("Default (Same as Editor)");
  374. for (int i = 0; i < OS::get_singleton()->get_screen_count(); i++) {
  375. screen_hints += ",Monitor " + itos(i + 1);
  376. }
  377. _initial_set("run/window_placement/rect_custom_position", Vector2());
  378. _initial_set("run/window_placement/screen", 0);
  379. hints["run/window_placement/screen"] = PropertyInfo(Variant::INT, "run/window_placement/screen", PROPERTY_HINT_ENUM, screen_hints);
  380. _initial_set("filesystem/on_save/compress_binary_resources", true);
  381. _initial_set("filesystem/on_save/save_modified_external_resources", true);
  382. _initial_set("text_editor/tools/create_signal_callbacks", true);
  383. _initial_set("filesystem/file_dialog/show_hidden_files", false);
  384. _initial_set("filesystem/file_dialog/display_mode", 0);
  385. hints["filesystem/file_dialog/display_mode"] = PropertyInfo(Variant::INT, "filesystem/file_dialog/display_mode", PROPERTY_HINT_ENUM, "Thumbnails,List");
  386. _initial_set("filesystem/file_dialog/thumbnail_size", 64);
  387. hints["filesystem/file_dialog/thumbnail_size"] = PropertyInfo(Variant::INT, "filesystem/file_dialog/thumbnail_size", PROPERTY_HINT_RANGE, "32,128,16");
  388. _initial_set("docks/filesystem/display_mode", 0);
  389. hints["docks/filesystem/display_mode"] = PropertyInfo(Variant::INT, "docks/filesystem/display_mode", PROPERTY_HINT_ENUM, "Thumbnails,List");
  390. _initial_set("docks/filesystem/thumbnail_size", 64);
  391. hints["docks/filesystem/thumbnail_size"] = PropertyInfo(Variant::INT, "docks/filesystem/thumbnail_size", PROPERTY_HINT_RANGE, "32,128,16");
  392. _initial_set("docks/filesystem/display_mode", 0);
  393. hints["docks/filesystem/display_mode"] = PropertyInfo(Variant::INT, "docks/filesystem/display_mode", PROPERTY_HINT_ENUM, "Thumbnails,List");
  394. _initial_set("docks/filesystem/always_show_folders", true);
  395. _initial_set("editors/animation/autorename_animation_tracks", true);
  396. _initial_set("editors/animation/confirm_insert_track", true);
  397. _initial_set("editors/animation/onion_layers_past_color", Color(1, 0, 0));
  398. _initial_set("editors/animation/onion_layers_future_color", Color(0, 1, 0));
  399. _initial_set("docks/property_editor/texture_preview_width", 48);
  400. _initial_set("docks/property_editor/auto_refresh_interval", 0.3);
  401. _initial_set("text_editor/help/doc_path", "");
  402. _initial_set("text_editor/help/show_help_index", true);
  403. _initial_set("filesystem/import/ask_save_before_reimport", false);
  404. _initial_set("filesystem/import/pvrtc_texture_tool", "");
  405. #ifdef WINDOWS_ENABLED
  406. hints["filesystem/import/pvrtc_texture_tool"] = PropertyInfo(Variant::STRING, "filesystem/import/pvrtc_texture_tool", PROPERTY_HINT_GLOBAL_FILE, "*.exe");
  407. #else
  408. hints["filesystem/import/pvrtc_texture_tool"] = PropertyInfo(Variant::STRING, "filesystem/import/pvrtc_texture_tool", PROPERTY_HINT_GLOBAL_FILE, "");
  409. #endif
  410. _initial_set("filesystem/import/pvrtc_fast_conversion", false);
  411. _initial_set("run/auto_save/save_before_running", true);
  412. // Output
  413. hints["run/output/font_size"] = PropertyInfo(Variant::INT, "run/output/font_size", PROPERTY_HINT_RANGE, "8,96,1", PROPERTY_USAGE_DEFAULT);
  414. _initial_set("run/output/always_clear_output_on_play", true);
  415. _initial_set("run/output/always_open_output_on_play", true);
  416. _initial_set("run/output/always_close_output_on_stop", false);
  417. _initial_set("filesystem/resources/save_compressed_resources", true);
  418. _initial_set("filesystem/resources/auto_reload_modified_images", true);
  419. _initial_set("filesystem/import/automatic_reimport_on_sources_changed", true);
  420. _initial_set("filesystem/on_save/safe_save_on_backup_then_rename", true);
  421. if (p_extra_config.is_valid()) {
  422. if (p_extra_config->has_section("init_projects") && p_extra_config->has_section_key("init_projects", "list")) {
  423. Vector<String> list = p_extra_config->get_value("init_projects", "list");
  424. for (int i = 0; i < list.size(); i++) {
  425. String name = list[i].replace("/", "::");
  426. set("projects/" + name, list[i]);
  427. };
  428. };
  429. if (p_extra_config->has_section("presets")) {
  430. List<String> keys;
  431. p_extra_config->get_section_keys("presets", &keys);
  432. for (List<String>::Element *E = keys.front(); E; E = E->next()) {
  433. String key = E->get();
  434. Variant val = p_extra_config->get_value("presets", key);
  435. set(key, val);
  436. };
  437. };
  438. };
  439. }
  440. void EditorSettings::_load_default_text_editor_theme() {
  441. _initial_set("text_editor/highlighting/background_color", Color::html("3b000000"));
  442. _initial_set("text_editor/highlighting/completion_background_color", Color::html("2C2A32"));
  443. _initial_set("text_editor/highlighting/completion_selected_color", Color::html("434244"));
  444. _initial_set("text_editor/highlighting/completion_existing_color", Color::html("21dfdfdf"));
  445. _initial_set("text_editor/highlighting/completion_scroll_color", Color::html("ffffff"));
  446. _initial_set("text_editor/highlighting/completion_font_color", Color::html("aaaaaa"));
  447. _initial_set("text_editor/highlighting/caret_color", Color::html("aaaaaa"));
  448. _initial_set("text_editor/highlighting/caret_background_color", Color::html("000000"));
  449. _initial_set("text_editor/highlighting/line_number_color", Color::html("66aaaaaa"));
  450. _initial_set("text_editor/highlighting/text_color", Color::html("aaaaaa"));
  451. _initial_set("text_editor/highlighting/text_selected_color", Color::html("000000"));
  452. _initial_set("text_editor/highlighting/keyword_color", Color::html("ffffb3"));
  453. _initial_set("text_editor/highlighting/base_type_color", Color::html("a4ffd4"));
  454. _initial_set("text_editor/highlighting/engine_type_color", Color::html("83d3ff"));
  455. _initial_set("text_editor/highlighting/function_color", Color::html("66a2ce"));
  456. _initial_set("text_editor/highlighting/member_variable_color", Color::html("e64e59"));
  457. _initial_set("text_editor/highlighting/comment_color", Color::html("676767"));
  458. _initial_set("text_editor/highlighting/string_color", Color::html("ef6ebe"));
  459. _initial_set("text_editor/highlighting/number_color", Color::html("EB9532"));
  460. _initial_set("text_editor/highlighting/symbol_color", Color::html("badfff"));
  461. _initial_set("text_editor/highlighting/selection_color", Color::html("6ca9c2"));
  462. _initial_set("text_editor/highlighting/brace_mismatch_color", Color(1, 0.2, 0.2));
  463. _initial_set("text_editor/highlighting/current_line_color", Color(0.3, 0.5, 0.8, 0.15));
  464. _initial_set("text_editor/highlighting/line_length_guideline_color", Color(0.3, 0.5, 0.8, 0.1));
  465. _initial_set("text_editor/highlighting/mark_color", Color(1.0, 0.4, 0.4, 0.4));
  466. _initial_set("text_editor/highlighting/breakpoint_color", Color(0.8, 0.8, 0.4, 0.2));
  467. _initial_set("text_editor/highlighting/code_folding_color", Color(0.8, 0.8, 0.8, 0.8));
  468. _initial_set("text_editor/highlighting/word_highlighted_color", Color(0.8, 0.9, 0.9, 0.15));
  469. _initial_set("text_editor/highlighting/search_result_color", Color(0.05, 0.25, 0.05, 1));
  470. _initial_set("text_editor/highlighting/search_result_border_color", Color(0.1, 0.45, 0.1, 1));
  471. }
  472. bool EditorSettings::_save_text_editor_theme(String p_file) {
  473. String theme_section = "color_theme";
  474. Ref<ConfigFile> cf = memnew(ConfigFile); // hex is better?
  475. cf->set_value(theme_section, "background_color", ((Color)get("text_editor/highlighting/background_color")).to_html());
  476. cf->set_value(theme_section, "completion_background_color", ((Color)get("text_editor/highlighting/completion_background_color")).to_html());
  477. cf->set_value(theme_section, "completion_selected_color", ((Color)get("text_editor/highlighting/completion_selected_color")).to_html());
  478. cf->set_value(theme_section, "completion_existing_color", ((Color)get("text_editor/highlighting/completion_existing_color")).to_html());
  479. cf->set_value(theme_section, "completion_scroll_color", ((Color)get("text_editor/highlighting/completion_scroll_color")).to_html());
  480. cf->set_value(theme_section, "completion_font_color", ((Color)get("text_editor/highlighting/completion_font_color")).to_html());
  481. cf->set_value(theme_section, "caret_color", ((Color)get("text_editor/highlighting/caret_color")).to_html());
  482. cf->set_value(theme_section, "caret_background_color", ((Color)get("text_editor/highlighting/caret_background_color")).to_html());
  483. cf->set_value(theme_section, "line_number_color", ((Color)get("text_editor/highlighting/line_number_color")).to_html());
  484. cf->set_value(theme_section, "text_color", ((Color)get("text_editor/highlighting/text_color")).to_html());
  485. cf->set_value(theme_section, "text_selected_color", ((Color)get("text_editor/highlighting/text_selected_color")).to_html());
  486. cf->set_value(theme_section, "keyword_color", ((Color)get("text_editor/highlighting/keyword_color")).to_html());
  487. cf->set_value(theme_section, "base_type_color", ((Color)get("text_editor/highlighting/base_type_color")).to_html());
  488. cf->set_value(theme_section, "engine_type_color", ((Color)get("text_editor/highlighting/engine_type_color")).to_html());
  489. cf->set_value(theme_section, "function_color", ((Color)get("text_editor/highlighting/function_color")).to_html());
  490. cf->set_value(theme_section, "member_variable_color", ((Color)get("text_editor/highlighting/member_variable_color")).to_html());
  491. cf->set_value(theme_section, "comment_color", ((Color)get("text_editor/highlighting/comment_color")).to_html());
  492. cf->set_value(theme_section, "string_color", ((Color)get("text_editor/highlighting/string_color")).to_html());
  493. cf->set_value(theme_section, "number_color", ((Color)get("text_editor/highlighting/number_color")).to_html());
  494. cf->set_value(theme_section, "symbol_color", ((Color)get("text_editor/highlighting/symbol_color")).to_html());
  495. cf->set_value(theme_section, "selection_color", ((Color)get("text_editor/highlighting/selection_color")).to_html());
  496. cf->set_value(theme_section, "brace_mismatch_color", ((Color)get("text_editor/highlighting/brace_mismatch_color")).to_html());
  497. cf->set_value(theme_section, "current_line_color", ((Color)get("text_editor/highlighting/current_line_color")).to_html());
  498. cf->set_value(theme_section, "line_length_guideline_color", ((Color)get("text_editor/highlighting/line_length_guideline_color")).to_html());
  499. cf->set_value(theme_section, "mark_color", ((Color)get("text_editor/highlighting/mark_color")).to_html());
  500. cf->set_value(theme_section, "breakpoint_color", ((Color)get("text_editor/highlighting/breakpoint_color")).to_html());
  501. cf->set_value(theme_section, "code_folding_color", ((Color)get("text_editor/highlighting/code_folding_color")).to_html());
  502. cf->set_value(theme_section, "word_highlighted_color", ((Color)get("text_editor/highlighting/word_highlighted_color")).to_html());
  503. cf->set_value(theme_section, "search_result_color", ((Color)get("text_editor/highlighting/search_result_color")).to_html());
  504. cf->set_value(theme_section, "search_result_border_color", ((Color)get("text_editor/highlighting/search_result_border_color")).to_html());
  505. Error err = cf->save(p_file);
  506. if (err == OK) {
  507. return true;
  508. }
  509. return false;
  510. }
  511. static Dictionary _get_builtin_script_templates() {
  512. Dictionary templates;
  513. //No Comments
  514. templates["no_comments.gd"] =
  515. "extends %BASE%\n"
  516. "\n"
  517. "func _ready():\n"
  518. "%TS%pass\n";
  519. //Empty
  520. templates["empty.gd"] =
  521. "extends %BASE%"
  522. "\n"
  523. "\n";
  524. return templates;
  525. }
  526. static void _create_script_templates(const String &p_path) {
  527. Dictionary templates = _get_builtin_script_templates();
  528. List<Variant> keys;
  529. templates.get_key_list(&keys);
  530. FileAccess *file = FileAccess::create(FileAccess::ACCESS_FILESYSTEM);
  531. DirAccess *dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  532. dir->change_dir(p_path);
  533. for (int i = 0; i < keys.size(); i++) {
  534. if (!dir->file_exists(keys[i])) {
  535. Error err = file->reopen(p_path.plus_file((String)keys[i]), FileAccess::WRITE);
  536. ERR_FAIL_COND(err != OK);
  537. file->store_string(templates[keys[i]]);
  538. file->close();
  539. }
  540. }
  541. memdelete(dir);
  542. memdelete(file);
  543. }
  544. // PUBLIC METHODS
  545. EditorSettings *EditorSettings::get_singleton() {
  546. return singleton.ptr();
  547. }
  548. void EditorSettings::create() {
  549. if (singleton.ptr())
  550. return; //pointless
  551. DirAccess *dir = NULL;
  552. String data_path;
  553. String data_dir;
  554. String config_path;
  555. String config_dir;
  556. String cache_path;
  557. String cache_dir;
  558. Ref<ConfigFile> extra_config = memnew(ConfigFile);
  559. String exe_path = OS::get_singleton()->get_executable_path().get_base_dir();
  560. DirAccess *d = DirAccess::create_for_path(exe_path);
  561. bool self_contained = false;
  562. if (d->file_exists(exe_path + "/._sc_")) {
  563. self_contained = true;
  564. extra_config->load(exe_path + "/._sc_");
  565. } else if (d->file_exists(exe_path + "/_sc_")) {
  566. self_contained = true;
  567. extra_config->load(exe_path + "/_sc_");
  568. }
  569. memdelete(d);
  570. if (self_contained) {
  571. // editor is self contained, all in same folder
  572. data_path = exe_path;
  573. data_dir = data_path.plus_file("editor_data");
  574. config_path = exe_path;
  575. config_dir = data_dir;
  576. cache_path = exe_path;
  577. cache_dir = data_dir.plus_file("cache");
  578. } else {
  579. // Typically XDG_DATA_HOME or %APPDATA%
  580. data_path = OS::get_singleton()->get_data_path();
  581. data_dir = data_path.plus_file(OS::get_singleton()->get_godot_dir_name());
  582. // Can be different from data_path e.g. on Linux or macOS
  583. config_path = OS::get_singleton()->get_config_path();
  584. config_dir = config_path.plus_file(OS::get_singleton()->get_godot_dir_name());
  585. // Can be different from above paths, otherwise a subfolder of data_dir
  586. cache_path = OS::get_singleton()->get_cache_path();
  587. if (cache_path == data_path) {
  588. cache_dir = data_dir.plus_file("cache");
  589. } else {
  590. cache_dir = cache_path.plus_file(OS::get_singleton()->get_godot_dir_name());
  591. }
  592. }
  593. ClassDB::register_class<EditorSettings>(); //otherwise it can't be unserialized
  594. String config_file_path;
  595. if (data_path != "" && config_path != "" && cache_path != "") {
  596. // Validate/create data dir and subdirectories
  597. dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  598. if (dir->change_dir(data_dir) != OK) {
  599. dir->make_dir_recursive(data_dir);
  600. if (dir->change_dir(data_dir) != OK) {
  601. ERR_PRINT("Cannot create data directory!");
  602. memdelete(dir);
  603. goto fail;
  604. }
  605. }
  606. if (dir->change_dir("templates") != OK) {
  607. dir->make_dir("templates");
  608. } else {
  609. dir->change_dir("..");
  610. }
  611. // Validate/create cache dir
  612. if (dir->change_dir(cache_dir) != OK) {
  613. dir->make_dir_recursive(cache_dir);
  614. if (dir->change_dir(cache_dir) != OK) {
  615. ERR_PRINT("Cannot create cache directory!");
  616. memdelete(dir);
  617. goto fail;
  618. }
  619. }
  620. // Validate/create config dir and subdirectories
  621. if (dir->change_dir(config_dir) != OK) {
  622. dir->make_dir_recursive(config_dir);
  623. if (dir->change_dir(config_dir) != OK) {
  624. ERR_PRINT("Cannot create config directory!");
  625. memdelete(dir);
  626. goto fail;
  627. }
  628. }
  629. if (dir->change_dir("text_editor_themes") != OK) {
  630. dir->make_dir("text_editor_themes");
  631. } else {
  632. dir->change_dir("..");
  633. }
  634. if (dir->change_dir("script_templates") != OK) {
  635. dir->make_dir("script_templates");
  636. } else {
  637. dir->change_dir("..");
  638. }
  639. _create_script_templates(dir->get_current_dir().plus_file("script_templates"));
  640. if (dir->change_dir("projects") != OK) {
  641. dir->make_dir("projects");
  642. } else {
  643. dir->change_dir("..");
  644. }
  645. // Validate/create project-specific config dir
  646. dir->change_dir("projects");
  647. String project_config_dir = ProjectSettings::get_singleton()->get_resource_path();
  648. if (project_config_dir.ends_with("/"))
  649. project_config_dir = config_path.substr(0, project_config_dir.size() - 1);
  650. project_config_dir = project_config_dir.get_file() + "-" + project_config_dir.md5_text();
  651. if (dir->change_dir(project_config_dir) != OK) {
  652. dir->make_dir(project_config_dir);
  653. } else {
  654. dir->change_dir("..");
  655. }
  656. dir->change_dir("..");
  657. // Validate editor config file
  658. String config_file_name = "editor_settings-" + itos(VERSION_MAJOR) + ".tres";
  659. config_file_path = config_dir.plus_file(config_file_name);
  660. if (!dir->file_exists(config_file_name)) {
  661. goto fail;
  662. }
  663. memdelete(dir);
  664. singleton = ResourceLoader::load(config_file_path, "EditorSettings");
  665. if (singleton.is_null()) {
  666. WARN_PRINT("Could not open config file.");
  667. goto fail;
  668. }
  669. singleton->save_changed_setting = true;
  670. singleton->config_file_path = config_file_path;
  671. singleton->project_config_dir = project_config_dir;
  672. singleton->settings_dir = config_dir;
  673. singleton->data_dir = data_dir;
  674. singleton->cache_dir = cache_dir;
  675. if (OS::get_singleton()->is_stdout_verbose()) {
  676. print_line("EditorSettings: Load OK!");
  677. }
  678. singleton->setup_language();
  679. singleton->setup_network();
  680. singleton->load_favorites();
  681. singleton->list_text_editor_themes();
  682. return;
  683. }
  684. fail:
  685. // patch init projects
  686. if (extra_config->has_section("init_projects")) {
  687. Vector<String> list = extra_config->get_value("init_projects", "list");
  688. for (int i = 0; i < list.size(); i++) {
  689. list[i] = exe_path + "/" + list[i];
  690. };
  691. extra_config->set_value("init_projects", "list", list);
  692. };
  693. singleton = Ref<EditorSettings>(memnew(EditorSettings));
  694. singleton->save_changed_setting = true;
  695. singleton->config_file_path = config_file_path;
  696. singleton->settings_dir = config_dir;
  697. singleton->data_dir = data_dir;
  698. singleton->cache_dir = cache_dir;
  699. singleton->_load_defaults(extra_config);
  700. singleton->setup_language();
  701. singleton->setup_network();
  702. singleton->list_text_editor_themes();
  703. }
  704. void EditorSettings::setup_language() {
  705. String lang = get("interface/editor/editor_language");
  706. if (lang == "en")
  707. return; //none to do
  708. for (int i = 0; i < translations.size(); i++) {
  709. if (translations[i]->get_locale() == lang) {
  710. TranslationServer::get_singleton()->set_tool_translation(translations[i]);
  711. break;
  712. }
  713. }
  714. }
  715. void EditorSettings::setup_network() {
  716. List<IP_Address> local_ip;
  717. IP::get_singleton()->get_local_addresses(&local_ip);
  718. String lip = "127.0.0.1";
  719. String hint;
  720. String current = has_setting("network/debug/remote_host") ? get("network/debug/remote_host") : "";
  721. int port = has_setting("network/debug/remote_port") ? (int)get("network/debug/remote_port") : 6007;
  722. for (List<IP_Address>::Element *E = local_ip.front(); E; E = E->next()) {
  723. String ip = E->get();
  724. // link-local IPv6 addresses don't work, skipping them
  725. if (ip.begins_with("fe80:0:0:0:")) // fe80::/64
  726. continue;
  727. if (ip == current)
  728. lip = current; //so it saves
  729. if (hint != "")
  730. hint += ",";
  731. hint += ip;
  732. }
  733. _initial_set("network/debug/remote_host", lip);
  734. add_property_hint(PropertyInfo(Variant::STRING, "network/debug/remote_host", PROPERTY_HINT_ENUM, hint));
  735. _initial_set("network/debug/remote_port", port);
  736. add_property_hint(PropertyInfo(Variant::INT, "network/debug/remote_port", PROPERTY_HINT_RANGE, "1,65535,1"));
  737. }
  738. void EditorSettings::save() {
  739. //_THREAD_SAFE_METHOD_
  740. if (!singleton.ptr())
  741. return;
  742. if (singleton->config_file_path == "") {
  743. ERR_PRINT("Cannot save EditorSettings config, no valid path");
  744. return;
  745. }
  746. Error err = ResourceSaver::save(singleton->config_file_path, singleton);
  747. if (err != OK) {
  748. ERR_PRINTS("Error saving editor settings to " + singleton->config_file_path);
  749. } else if (OS::get_singleton()->is_stdout_verbose()) {
  750. print_line("EditorSettings Save OK!");
  751. }
  752. }
  753. void EditorSettings::destroy() {
  754. if (!singleton.ptr())
  755. return;
  756. save();
  757. singleton = Ref<EditorSettings>();
  758. }
  759. void EditorSettings::set_optimize_save(bool p_optimize) {
  760. optimize_save = p_optimize;
  761. }
  762. // Properties
  763. void EditorSettings::set_setting(const String &p_setting, const Variant &p_value) {
  764. _THREAD_SAFE_METHOD_
  765. set(p_setting, p_value);
  766. }
  767. Variant EditorSettings::get_setting(const String &p_setting) const {
  768. _THREAD_SAFE_METHOD_
  769. return get(p_setting);
  770. }
  771. bool EditorSettings::has_setting(const String &p_setting) const {
  772. _THREAD_SAFE_METHOD_
  773. return props.has(p_setting);
  774. }
  775. void EditorSettings::erase(const String &p_setting) {
  776. _THREAD_SAFE_METHOD_
  777. props.erase(p_setting);
  778. }
  779. void EditorSettings::raise_order(const String &p_setting) {
  780. _THREAD_SAFE_METHOD_
  781. ERR_FAIL_COND(!props.has(p_setting));
  782. props[p_setting].order = ++last_order;
  783. }
  784. void EditorSettings::set_initial_value(const StringName &p_setting, const Variant &p_value, bool p_update_current) {
  785. _THREAD_SAFE_METHOD_
  786. if (!props.has(p_setting))
  787. return;
  788. props[p_setting].initial = p_value;
  789. props[p_setting].has_default_value = true;
  790. if (p_update_current) {
  791. set(p_setting, p_value);
  792. }
  793. }
  794. Variant _EDITOR_DEF(const String &p_setting, const Variant &p_default) {
  795. Variant ret = p_default;
  796. if (EditorSettings::get_singleton()->has_setting(p_setting))
  797. ret = EditorSettings::get_singleton()->get(p_setting);
  798. else
  799. EditorSettings::get_singleton()->set_manually(p_setting, p_default);
  800. if (!EditorSettings::get_singleton()->has_default_value(p_setting))
  801. EditorSettings::get_singleton()->set_initial_value(p_setting, p_default);
  802. return ret;
  803. }
  804. Variant _EDITOR_GET(const String &p_setting) {
  805. ERR_FAIL_COND_V(!EditorSettings::get_singleton()->has_setting(p_setting), Variant())
  806. return EditorSettings::get_singleton()->get(p_setting);
  807. }
  808. bool EditorSettings::property_can_revert(const String &p_setting) {
  809. if (!props.has(p_setting))
  810. return false;
  811. if (!props[p_setting].has_default_value)
  812. return false;
  813. return props[p_setting].initial != props[p_setting].variant;
  814. }
  815. Variant EditorSettings::property_get_revert(const String &p_setting) {
  816. if (!props.has(p_setting) || !props[p_setting].has_default_value)
  817. return Variant();
  818. return props[p_setting].initial;
  819. }
  820. void EditorSettings::add_property_hint(const PropertyInfo &p_hint) {
  821. _THREAD_SAFE_METHOD_
  822. hints[p_hint.name] = p_hint;
  823. }
  824. // Data directories
  825. String EditorSettings::get_data_dir() const {
  826. return data_dir;
  827. }
  828. String EditorSettings::get_templates_dir() const {
  829. return get_data_dir().plus_file("templates");
  830. }
  831. // Config directories
  832. String EditorSettings::get_settings_dir() const {
  833. return settings_dir;
  834. }
  835. String EditorSettings::get_project_settings_dir() const {
  836. return get_settings_dir().plus_file("projects").plus_file(project_config_dir);
  837. }
  838. String EditorSettings::get_text_editor_themes_dir() const {
  839. return get_settings_dir().plus_file("text_editor_themes");
  840. }
  841. String EditorSettings::get_script_templates_dir() const {
  842. return get_settings_dir().plus_file("script_templates");
  843. }
  844. // Cache directory
  845. String EditorSettings::get_cache_dir() const {
  846. return cache_dir;
  847. }
  848. // Metadata
  849. void EditorSettings::set_project_metadata(const String &p_section, const String &p_key, Variant p_data) {
  850. Ref<ConfigFile> cf = memnew(ConfigFile);
  851. String path = get_project_settings_dir().plus_file("project_metadata.cfg");
  852. cf->load(path);
  853. cf->set_value(p_section, p_key, p_data);
  854. cf->save(path);
  855. }
  856. Variant EditorSettings::get_project_metadata(const String &p_section, const String &p_key, Variant p_default) {
  857. Ref<ConfigFile> cf = memnew(ConfigFile);
  858. String path = get_project_settings_dir().plus_file("project_metadata.cfg");
  859. Error err = cf->load(path);
  860. if (err != OK) {
  861. return p_default;
  862. }
  863. return cf->get_value(p_section, p_key, p_default);
  864. }
  865. void EditorSettings::set_favorite_dirs(const Vector<String> &p_favorites_dirs) {
  866. favorite_dirs = p_favorites_dirs;
  867. FileAccess *f = FileAccess::open(get_project_settings_dir().plus_file("favorite_dirs"), FileAccess::WRITE);
  868. if (f) {
  869. for (int i = 0; i < favorite_dirs.size(); i++)
  870. f->store_line(favorite_dirs[i]);
  871. memdelete(f);
  872. }
  873. }
  874. Vector<String> EditorSettings::get_favorite_dirs() const {
  875. return favorite_dirs;
  876. }
  877. void EditorSettings::set_recent_dirs(const Vector<String> &p_recent_dirs) {
  878. recent_dirs = p_recent_dirs;
  879. FileAccess *f = FileAccess::open(get_project_settings_dir().plus_file("recent_dirs"), FileAccess::WRITE);
  880. if (f) {
  881. for (int i = 0; i < recent_dirs.size(); i++)
  882. f->store_line(recent_dirs[i]);
  883. memdelete(f);
  884. }
  885. }
  886. Vector<String> EditorSettings::get_recent_dirs() const {
  887. return recent_dirs;
  888. }
  889. void EditorSettings::load_favorites() {
  890. FileAccess *f = FileAccess::open(get_project_settings_dir().plus_file("favorite_dirs"), FileAccess::READ);
  891. if (f) {
  892. String line = f->get_line().strip_edges();
  893. while (line != "") {
  894. favorite_dirs.push_back(line);
  895. line = f->get_line().strip_edges();
  896. }
  897. memdelete(f);
  898. }
  899. f = FileAccess::open(get_project_settings_dir().plus_file("recent_dirs"), FileAccess::READ);
  900. if (f) {
  901. String line = f->get_line().strip_edges();
  902. while (line != "") {
  903. recent_dirs.push_back(line);
  904. line = f->get_line().strip_edges();
  905. }
  906. memdelete(f);
  907. }
  908. }
  909. void EditorSettings::list_text_editor_themes() {
  910. String themes = "Adaptive,Default,Custom";
  911. DirAccess *d = DirAccess::open(get_text_editor_themes_dir());
  912. if (d) {
  913. List<String> custom_themes;
  914. d->list_dir_begin();
  915. String file = d->get_next();
  916. while (file != String()) {
  917. if (file.get_extension() == "tet" && file.get_basename().to_lower() != "default" && file.get_basename().to_lower() != "adaptive" && file.get_basename().to_lower() != "custom") {
  918. custom_themes.push_back(file.get_basename());
  919. }
  920. file = d->get_next();
  921. }
  922. d->list_dir_end();
  923. memdelete(d);
  924. custom_themes.sort();
  925. for (List<String>::Element *E = custom_themes.front(); E; E = E->next()) {
  926. themes += "," + E->get();
  927. }
  928. }
  929. add_property_hint(PropertyInfo(Variant::STRING, "text_editor/theme/color_theme", PROPERTY_HINT_ENUM, themes));
  930. }
  931. void EditorSettings::load_text_editor_theme() {
  932. if (get("text_editor/theme/color_theme") == "Default" || get("text_editor/theme/color_theme") == "Adaptive" || get("text_editor/theme/color_theme") == "Custom") {
  933. if (get("text_editor/theme/color_theme") == "Default") {
  934. _load_default_text_editor_theme();
  935. }
  936. return; // sorry for "Settings changed" console spam
  937. }
  938. String theme_path = get_text_editor_themes_dir().plus_file((String)get("text_editor/theme/color_theme") + ".tet");
  939. Ref<ConfigFile> cf = memnew(ConfigFile);
  940. Error err = cf->load(theme_path);
  941. if (err != OK) {
  942. return;
  943. }
  944. List<String> keys;
  945. cf->get_section_keys("color_theme", &keys);
  946. for (List<String>::Element *E = keys.front(); E; E = E->next()) {
  947. String key = E->get();
  948. String val = cf->get_value("color_theme", key);
  949. // don't load if it's not already there!
  950. if (has_setting("text_editor/highlighting/" + key)) {
  951. // make sure it is actually a color
  952. if (val.is_valid_html_color() && key.find("color") >= 0) {
  953. props["text_editor/highlighting/" + key].variant = Color::html(val); // change manually to prevent "Settings changed" console spam
  954. }
  955. }
  956. }
  957. emit_signal("settings_changed");
  958. // if it doesn't load just use what is currently loaded
  959. }
  960. bool EditorSettings::import_text_editor_theme(String p_file) {
  961. if (!p_file.ends_with(".tet")) {
  962. return false;
  963. } else {
  964. if (p_file.get_file().to_lower() == "default.tet") {
  965. return false;
  966. }
  967. DirAccess *d = DirAccess::open(get_text_editor_themes_dir());
  968. if (d) {
  969. d->copy(p_file, get_text_editor_themes_dir().plus_file(p_file.get_file()));
  970. memdelete(d);
  971. return true;
  972. }
  973. }
  974. return false;
  975. }
  976. bool EditorSettings::save_text_editor_theme() {
  977. String p_file = get("text_editor/theme/color_theme");
  978. if (p_file.get_file().to_lower() == "default" || p_file.get_file().to_lower() == "adaptive" || p_file.get_file().to_lower() == "custom") {
  979. return false;
  980. }
  981. String theme_path = get_text_editor_themes_dir().plus_file(p_file + ".tet");
  982. return _save_text_editor_theme(theme_path);
  983. }
  984. bool EditorSettings::save_text_editor_theme_as(String p_file) {
  985. if (!p_file.ends_with(".tet")) {
  986. p_file += ".tet";
  987. }
  988. if (p_file.get_file().to_lower() == "default.tet" || p_file.get_file().to_lower() == "adaptive.tet" || p_file.get_file().to_lower() == "custom.tet") {
  989. return false;
  990. }
  991. if (_save_text_editor_theme(p_file)) {
  992. // switch to theme is saved in the theme directory
  993. list_text_editor_themes();
  994. String theme_name = p_file.substr(0, p_file.length() - 4).get_file();
  995. if (p_file.get_base_dir() == get_text_editor_themes_dir()) {
  996. _initial_set("text_editor/theme/color_theme", theme_name);
  997. load_text_editor_theme();
  998. }
  999. return true;
  1000. }
  1001. return false;
  1002. }
  1003. Vector<String> EditorSettings::get_script_templates(const String &p_extension) {
  1004. Vector<String> templates;
  1005. DirAccess *d = DirAccess::open(get_script_templates_dir());
  1006. if (d) {
  1007. d->list_dir_begin();
  1008. String file = d->get_next();
  1009. while (file != String()) {
  1010. if (file.get_extension() == p_extension) {
  1011. templates.push_back(file.get_basename());
  1012. }
  1013. file = d->get_next();
  1014. }
  1015. d->list_dir_end();
  1016. memdelete(d);
  1017. }
  1018. return templates;
  1019. }
  1020. String EditorSettings::get_editor_layouts_config() const {
  1021. return get_settings_dir().plus_file("editor_layouts.cfg");
  1022. }
  1023. // Shortcuts
  1024. void EditorSettings::add_shortcut(const String &p_name, Ref<ShortCut> &p_shortcut) {
  1025. shortcuts[p_name] = p_shortcut;
  1026. }
  1027. bool EditorSettings::is_shortcut(const String &p_name, const Ref<InputEvent> &p_event) const {
  1028. const Map<String, Ref<ShortCut> >::Element *E = shortcuts.find(p_name);
  1029. if (!E) {
  1030. ERR_EXPLAIN("Unknown Shortcut: " + p_name);
  1031. ERR_FAIL_V(false);
  1032. }
  1033. return E->get()->is_shortcut(p_event);
  1034. }
  1035. Ref<ShortCut> EditorSettings::get_shortcut(const String &p_name) const {
  1036. const Map<String, Ref<ShortCut> >::Element *E = shortcuts.find(p_name);
  1037. if (!E)
  1038. return Ref<ShortCut>();
  1039. return E->get();
  1040. }
  1041. void EditorSettings::get_shortcut_list(List<String> *r_shortcuts) {
  1042. for (const Map<String, Ref<ShortCut> >::Element *E = shortcuts.front(); E; E = E->next()) {
  1043. r_shortcuts->push_back(E->key());
  1044. }
  1045. }
  1046. Ref<ShortCut> ED_GET_SHORTCUT(const String &p_path) {
  1047. Ref<ShortCut> sc = EditorSettings::get_singleton()->get_shortcut(p_path);
  1048. if (!sc.is_valid()) {
  1049. ERR_EXPLAIN("Used ED_GET_SHORTCUT with invalid shortcut: " + p_path);
  1050. ERR_FAIL_COND_V(!sc.is_valid(), sc);
  1051. }
  1052. return sc;
  1053. }
  1054. struct ShortCutMapping {
  1055. const char *path;
  1056. uint32_t keycode;
  1057. };
  1058. Ref<ShortCut> ED_SHORTCUT(const String &p_path, const String &p_name, uint32_t p_keycode) {
  1059. #ifdef OSX_ENABLED
  1060. // Use Cmd+Backspace as a general replacement for Delete shortcuts on macOS
  1061. if (p_keycode == KEY_DELETE) {
  1062. p_keycode = KEY_MASK_CMD | KEY_BACKSPACE;
  1063. }
  1064. #endif
  1065. Ref<InputEventKey> ie;
  1066. if (p_keycode) {
  1067. ie.instance();
  1068. ie->set_unicode(p_keycode & KEY_CODE_MASK);
  1069. ie->set_scancode(p_keycode & KEY_CODE_MASK);
  1070. ie->set_shift(bool(p_keycode & KEY_MASK_SHIFT));
  1071. ie->set_alt(bool(p_keycode & KEY_MASK_ALT));
  1072. ie->set_control(bool(p_keycode & KEY_MASK_CTRL));
  1073. ie->set_metakey(bool(p_keycode & KEY_MASK_META));
  1074. }
  1075. Ref<ShortCut> sc = EditorSettings::get_singleton()->get_shortcut(p_path);
  1076. if (sc.is_valid()) {
  1077. sc->set_name(p_name); //keep name (the ones that come from disk have no name)
  1078. sc->set_meta("original", ie); //to compare against changes
  1079. return sc;
  1080. }
  1081. sc.instance();
  1082. sc->set_name(p_name);
  1083. sc->set_shortcut(ie);
  1084. sc->set_meta("original", ie); //to compare against changes
  1085. EditorSettings::get_singleton()->add_shortcut(p_path, sc);
  1086. return sc;
  1087. }
  1088. void EditorSettings::notify_changes() {
  1089. _THREAD_SAFE_METHOD_
  1090. SceneTree *sml = Object::cast_to<SceneTree>(OS::get_singleton()->get_main_loop());
  1091. if (!sml) {
  1092. return;
  1093. }
  1094. Node *root = sml->get_root()->get_child(0);
  1095. if (!root) {
  1096. return;
  1097. }
  1098. root->propagate_notification(NOTIFICATION_EDITOR_SETTINGS_CHANGED);
  1099. }
  1100. void EditorSettings::_bind_methods() {
  1101. ClassDB::bind_method(D_METHOD("has_setting", "name"), &EditorSettings::has_setting);
  1102. ClassDB::bind_method(D_METHOD("set_setting", "name", "value"), &EditorSettings::set_setting);
  1103. ClassDB::bind_method(D_METHOD("get_setting", "name"), &EditorSettings::get_setting);
  1104. ClassDB::bind_method(D_METHOD("erase", "property"), &EditorSettings::erase);
  1105. ClassDB::bind_method(D_METHOD("set_initial_value", "name", "value", "update_current"), &EditorSettings::set_initial_value);
  1106. ClassDB::bind_method(D_METHOD("property_can_revert", "name"), &EditorSettings::property_can_revert);
  1107. ClassDB::bind_method(D_METHOD("property_get_revert", "name"), &EditorSettings::property_get_revert);
  1108. ClassDB::bind_method(D_METHOD("add_property_info", "info"), &EditorSettings::_add_property_info_bind);
  1109. ClassDB::bind_method(D_METHOD("get_settings_dir"), &EditorSettings::get_settings_dir);
  1110. ClassDB::bind_method(D_METHOD("get_project_settings_dir"), &EditorSettings::get_project_settings_dir);
  1111. ClassDB::bind_method(D_METHOD("set_favorite_dirs", "dirs"), &EditorSettings::set_favorite_dirs);
  1112. ClassDB::bind_method(D_METHOD("get_favorite_dirs"), &EditorSettings::get_favorite_dirs);
  1113. ClassDB::bind_method(D_METHOD("set_recent_dirs", "dirs"), &EditorSettings::set_recent_dirs);
  1114. ClassDB::bind_method(D_METHOD("get_recent_dirs"), &EditorSettings::get_recent_dirs);
  1115. ADD_SIGNAL(MethodInfo("settings_changed"));
  1116. }
  1117. EditorSettings::EditorSettings() {
  1118. last_order = 0;
  1119. optimize_save = true;
  1120. save_changed_setting = true;
  1121. EditorTranslationList *etl = _editor_translations;
  1122. while (etl->data) {
  1123. Vector<uint8_t> data;
  1124. data.resize(etl->uncomp_size);
  1125. Compression::decompress(data.ptrw(), etl->uncomp_size, etl->data, etl->comp_size, Compression::MODE_DEFLATE);
  1126. FileAccessMemory *fa = memnew(FileAccessMemory);
  1127. fa->open_custom(data.ptr(), data.size());
  1128. Ref<Translation> tr = TranslationLoaderPO::load_translation(fa, NULL, "translation_" + String(etl->lang));
  1129. if (tr.is_valid()) {
  1130. tr->set_locale(etl->lang);
  1131. translations.push_back(tr);
  1132. }
  1133. etl++;
  1134. }
  1135. _load_defaults();
  1136. }
  1137. EditorSettings::~EditorSettings() {
  1138. }