editor_settings.cpp 54 KB

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