editor_settings.cpp 50 KB

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