editor_settings.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. /*************************************************************************/
  2. /* editor_settings.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "os/dir_access.h"
  32. #include "os/file_access.h"
  33. #include "os/os.h"
  34. #include "editor_node.h"
  35. #include "globals.h"
  36. #include "io/compression.h"
  37. #include "io/config_file.h"
  38. #include "io/file_access_memory.h"
  39. #include "io/resource_loader.h"
  40. #include "io/resource_saver.h"
  41. #include "io/translation_loader_po.h"
  42. #include "os/keyboard.h"
  43. #include "os/os.h"
  44. #include "scene/main/node.h"
  45. #include "scene/main/scene_main_loop.h"
  46. #include "scene/main/viewport.h"
  47. #include "translations.gen.h"
  48. #include "version.h"
  49. Ref<EditorSettings> EditorSettings::singleton = NULL;
  50. EditorSettings *EditorSettings::get_singleton() {
  51. return singleton.ptr();
  52. }
  53. bool EditorSettings::_set(const StringName &p_name, const Variant &p_value) {
  54. _THREAD_SAFE_METHOD_
  55. if (p_name.operator String() == "shortcuts") {
  56. Array arr = p_value;
  57. ERR_FAIL_COND_V(arr.size() && arr.size() & 1, true);
  58. for (int i = 0; i < arr.size(); i += 2) {
  59. String name = arr[i];
  60. InputEvent shortcut = arr[i + 1];
  61. Ref<ShortCut> sc;
  62. sc.instance();
  63. sc->set_shortcut(shortcut);
  64. add_shortcut(name, sc);
  65. }
  66. return true;
  67. }
  68. if (p_value.get_type() == Variant::NIL)
  69. props.erase(p_name);
  70. else {
  71. if (props.has(p_name))
  72. props[p_name].variant = p_value;
  73. else
  74. props[p_name] = VariantContainer(p_value, last_order++);
  75. if (save_changed_setting) {
  76. props[p_name].save = true;
  77. }
  78. }
  79. emit_signal("settings_changed");
  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. InputEvent original = sc->get_meta("original");
  93. if (sc->is_shortcut(original) || (original.type == InputEvent::NONE && sc->get_shortcut().type == InputEvent::NONE))
  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. return false;
  105. r_ret = v->variant;
  106. return true;
  107. }
  108. struct _EVCSort {
  109. String name;
  110. Variant::Type type;
  111. int order;
  112. bool save;
  113. bool operator<(const _EVCSort &p_vcs) const { return order < p_vcs.order; }
  114. };
  115. void EditorSettings::_get_property_list(List<PropertyInfo> *p_list) const {
  116. _THREAD_SAFE_METHOD_
  117. const String *k = NULL;
  118. Set<_EVCSort> vclist;
  119. while ((k = props.next(k))) {
  120. const VariantContainer *v = props.getptr(*k);
  121. if (v->hide_from_editor)
  122. continue;
  123. _EVCSort vc;
  124. vc.name = *k;
  125. vc.order = v->order;
  126. vc.type = v->variant.get_type();
  127. vc.save = v->save;
  128. vclist.insert(vc);
  129. }
  130. for (Set<_EVCSort>::Element *E = vclist.front(); E; E = E->next()) {
  131. int pinfo = 0;
  132. if (E->get().save || !optimize_save) {
  133. pinfo |= PROPERTY_USAGE_STORAGE;
  134. }
  135. if (!E->get().name.begins_with("_") && !E->get().name.begins_with("projects/")) {
  136. pinfo |= PROPERTY_USAGE_EDITOR;
  137. } else {
  138. pinfo |= PROPERTY_USAGE_STORAGE; //hiddens must always be saved
  139. }
  140. PropertyInfo pi(E->get().type, E->get().name);
  141. pi.usage = pinfo;
  142. if (hints.has(E->get().name))
  143. pi = hints[E->get().name];
  144. p_list->push_back(pi);
  145. }
  146. p_list->push_back(PropertyInfo(Variant::ARRAY, "shortcuts", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR)); //do not edit
  147. }
  148. bool EditorSettings::has(String p_var) const {
  149. _THREAD_SAFE_METHOD_
  150. return props.has(p_var);
  151. }
  152. void EditorSettings::erase(String p_var) {
  153. _THREAD_SAFE_METHOD_
  154. props.erase(p_var);
  155. }
  156. void EditorSettings::raise_order(const String &p_name) {
  157. _THREAD_SAFE_METHOD_
  158. ERR_FAIL_COND(!props.has(p_name));
  159. props[p_name].order = ++last_order;
  160. }
  161. Variant _EDITOR_DEF(const String &p_var, const Variant &p_default) {
  162. if (EditorSettings::get_singleton()->has(p_var))
  163. return EditorSettings::get_singleton()->get(p_var);
  164. EditorSettings::get_singleton()->set(p_var, p_default);
  165. return p_default;
  166. }
  167. void EditorSettings::create() {
  168. if (singleton.ptr())
  169. return; //pointless
  170. DirAccess *dir = NULL;
  171. Variant meta;
  172. String config_path;
  173. String config_dir;
  174. //String config_file="editor_settings.xml";
  175. Ref<ConfigFile> extra_config = memnew(ConfigFile);
  176. String exe_path = OS::get_singleton()->get_executable_path().get_base_dir();
  177. DirAccess *d = DirAccess::create_for_path(exe_path);
  178. bool self_contained = false;
  179. if (d->file_exists(exe_path + "/._sc_")) {
  180. self_contained = true;
  181. extra_config->load(exe_path + "/._sc_");
  182. } else if (d->file_exists(exe_path + "/_sc_")) {
  183. self_contained = true;
  184. extra_config->load(exe_path + "/_sc_");
  185. }
  186. if (self_contained) {
  187. // editor is self contained
  188. config_path = exe_path;
  189. config_dir = "editor_data";
  190. } else {
  191. if (OS::get_singleton()->has_environment("APPDATA")) {
  192. // Most likely under windows, save here
  193. config_path = OS::get_singleton()->get_environment("APPDATA");
  194. config_dir = String(_MKSTR(VERSION_SHORT_NAME)).capitalize();
  195. } else if (OS::get_singleton()->has_environment("HOME")) {
  196. config_path = OS::get_singleton()->get_environment("HOME");
  197. config_dir = "." + String(_MKSTR(VERSION_SHORT_NAME)).to_lower();
  198. }
  199. };
  200. ObjectTypeDB::register_type<EditorSettings>(); //otherwise it can't be unserialized
  201. String config_file_path;
  202. if (config_path != "") {
  203. dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  204. if (dir->change_dir(config_path) != OK) {
  205. ERR_PRINT("Cannot find path for config directory!");
  206. memdelete(dir);
  207. goto fail;
  208. }
  209. if (dir->change_dir(config_dir) != OK) {
  210. dir->make_dir(config_dir);
  211. if (dir->change_dir(config_dir) != OK) {
  212. ERR_PRINT("Cannot create config directory!");
  213. memdelete(dir);
  214. goto fail;
  215. }
  216. }
  217. if (dir->change_dir("templates") != OK) {
  218. dir->make_dir("templates");
  219. } else {
  220. dir->change_dir("..");
  221. }
  222. if (dir->change_dir("text_editor_themes") != OK) {
  223. dir->make_dir("text_editor_themes");
  224. } else {
  225. dir->change_dir("..");
  226. }
  227. if (dir->change_dir("tmp") != OK) {
  228. dir->make_dir("tmp");
  229. } else {
  230. dir->change_dir("..");
  231. }
  232. if (dir->change_dir("config") != OK) {
  233. dir->make_dir("config");
  234. } else {
  235. dir->change_dir("..");
  236. }
  237. dir->change_dir("config");
  238. String pcp = Globals::get_singleton()->get_resource_path();
  239. if (pcp.ends_with("/"))
  240. pcp = config_path.substr(0, pcp.size() - 1);
  241. pcp = pcp.get_file() + "-" + pcp.md5_text();
  242. if (dir->change_dir(pcp)) {
  243. dir->make_dir(pcp);
  244. } else {
  245. dir->change_dir("..");
  246. }
  247. dir->change_dir("..");
  248. // path at least is validated, so validate config file
  249. config_file_path = config_path + "/" + config_dir + "/editor_settings.tres";
  250. String open_path = config_file_path;
  251. if (!dir->file_exists("editor_settings.tres")) {
  252. open_path = config_path + "/" + config_dir + "/editor_settings.xml";
  253. if (!dir->file_exists("editor_settings.xml")) {
  254. memdelete(dir);
  255. WARN_PRINT("Config file does not exist, creating.");
  256. goto fail;
  257. }
  258. }
  259. memdelete(dir);
  260. singleton = ResourceLoader::load(open_path, "EditorSettings");
  261. if (singleton.is_null()) {
  262. WARN_PRINT("Could not open config file.");
  263. goto fail;
  264. }
  265. singleton->save_changed_setting = true;
  266. singleton->config_file_path = config_file_path;
  267. singleton->project_config_path = pcp;
  268. singleton->settings_path = config_path + "/" + config_dir;
  269. if (OS::get_singleton()->is_stdout_verbose()) {
  270. print_line("EditorSettings: Load OK!");
  271. }
  272. singleton->setup_language();
  273. singleton->setup_network();
  274. singleton->load_favorites();
  275. singleton->list_text_editor_themes();
  276. return;
  277. }
  278. fail:
  279. // patch init projects
  280. if (extra_config->has_section("init_projects")) {
  281. Vector<String> list = extra_config->get_value("init_projects", "list");
  282. for (int i = 0; i < list.size(); i++) {
  283. list[i] = exe_path + "/" + list[i];
  284. };
  285. extra_config->set_value("init_projects", "list", list);
  286. };
  287. singleton = Ref<EditorSettings>(memnew(EditorSettings));
  288. singleton->save_changed_setting = true;
  289. singleton->config_file_path = config_file_path;
  290. singleton->settings_path = config_path + "/" + config_dir;
  291. singleton->_load_defaults(extra_config);
  292. singleton->setup_language();
  293. singleton->setup_network();
  294. singleton->list_text_editor_themes();
  295. }
  296. String EditorSettings::get_settings_path() const {
  297. return settings_path;
  298. }
  299. void EditorSettings::setup_language() {
  300. String lang = get("global/editor_language");
  301. if (lang == "en")
  302. return; //none to do
  303. for (int i = 0; i < translations.size(); i++) {
  304. if (translations[i]->get_locale() == lang) {
  305. TranslationServer::get_singleton()->set_tool_translation(translations[i]);
  306. break;
  307. }
  308. }
  309. }
  310. void EditorSettings::setup_network() {
  311. List<IP_Address> local_ip;
  312. IP::get_singleton()->get_local_addresses(&local_ip);
  313. String lip = "127.0.0.1";
  314. String hint;
  315. String current = has("network/debug_host") ? get("network/debug_host") : "";
  316. int port = has("network/debug_port") ? (int)get("network/debug_port") : 6096;
  317. for (List<IP_Address>::Element *E = local_ip.front(); E; E = E->next()) {
  318. String ip = E->get();
  319. // link-local IPv6 addresses don't work, skipping them
  320. if (ip.begins_with("fe80:0:0:0:")) // fe80::/64
  321. continue;
  322. if (ip == current)
  323. lip = current; //so it saves
  324. if (hint != "")
  325. hint += ",";
  326. hint += ip;
  327. }
  328. set("network/debug_host", lip);
  329. add_property_hint(PropertyInfo(Variant::STRING, "network/debug_host", PROPERTY_HINT_ENUM, hint));
  330. set("network/debug_port", port);
  331. add_property_hint(PropertyInfo(Variant::INT, "network/debug_port", PROPERTY_HINT_RANGE, "1,65535,1"));
  332. }
  333. void EditorSettings::save() {
  334. //_THREAD_SAFE_METHOD_
  335. if (!singleton.ptr())
  336. return;
  337. if (singleton->config_file_path == "") {
  338. ERR_PRINT("Cannot save EditorSettings config, no valid path");
  339. return;
  340. }
  341. Error err = ResourceSaver::save(singleton->config_file_path, singleton);
  342. if (err != OK) {
  343. ERR_PRINT("Can't Save!");
  344. return;
  345. }
  346. if (OS::get_singleton()->is_stdout_verbose()) {
  347. print_line("EditorSettings Save OK!");
  348. }
  349. }
  350. void EditorSettings::destroy() {
  351. if (!singleton.ptr())
  352. return;
  353. save();
  354. singleton = Ref<EditorSettings>();
  355. }
  356. void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) {
  357. _THREAD_SAFE_METHOD_
  358. {
  359. String lang_hint = "en";
  360. String host_lang = OS::get_singleton()->get_locale();
  361. String best;
  362. for (int i = 0; i < translations.size(); i++) {
  363. String locale = translations[i]->get_locale();
  364. lang_hint += ",";
  365. lang_hint += locale;
  366. if (host_lang == locale) {
  367. best = locale;
  368. }
  369. if (best == String() && host_lang.begins_with(locale)) {
  370. best = locale;
  371. }
  372. }
  373. if (best == String()) {
  374. best = "en";
  375. }
  376. set("global/editor_language", best);
  377. hints["global/editor_language"] = PropertyInfo(Variant::STRING, "global/editor_language", PROPERTY_HINT_ENUM, lang_hint, PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED);
  378. }
  379. set("global/hidpi_mode", 0);
  380. hints["global/hidpi_mode"] = PropertyInfo(Variant::INT, "global/hidpi_mode", PROPERTY_HINT_ENUM, "Auto,LoDPI,HiDPI", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED);
  381. set("global/show_script_in_scene_tabs", false);
  382. set("global/font_size", 14);
  383. hints["global/font_size"] = PropertyInfo(Variant::INT, "global/font_size", PROPERTY_HINT_RANGE, "10,40,1", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED);
  384. set("global/source_font_size", 14);
  385. hints["global/source_font_size"] = PropertyInfo(Variant::INT, "global/source_font_size", PROPERTY_HINT_RANGE, "8,96,1", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED);
  386. set("global/custom_font", "");
  387. hints["global/custom_font"] = PropertyInfo(Variant::STRING, "global/custom_font", PROPERTY_HINT_GLOBAL_FILE, "*.fnt", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED);
  388. set("global/custom_theme", "");
  389. hints["global/custom_theme"] = PropertyInfo(Variant::STRING, "global/custom_theme", PROPERTY_HINT_GLOBAL_FILE, "*.res,*.tres,*.theme", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED);
  390. set("global/autoscan_project_path", "");
  391. hints["global/autoscan_project_path"] = PropertyInfo(Variant::STRING, "global/autoscan_project_path", PROPERTY_HINT_GLOBAL_DIR);
  392. set("global/default_project_path", "");
  393. hints["global/default_project_path"] = PropertyInfo(Variant::STRING, "global/default_project_path", PROPERTY_HINT_GLOBAL_DIR);
  394. set("global/default_project_export_path", "");
  395. hints["global/default_project_export_path"] = PropertyInfo(Variant::STRING, "global/default_project_export_path", PROPERTY_HINT_GLOBAL_DIR);
  396. set("global/show_script_in_scene_tabs", false);
  397. set("text_editor/color_theme", "Default");
  398. hints["text_editor/color_theme"] = PropertyInfo(Variant::STRING, "text_editor/color_theme", PROPERTY_HINT_ENUM, "Default");
  399. _load_default_text_editor_theme();
  400. set("text_editor/syntax_highlighting", true);
  401. set("text_editor/highlight_all_occurrences", true);
  402. set("text_editor/scroll_past_end_of_file", false);
  403. set("text_editor/tab_size", 4);
  404. hints["text_editor/tab_size"] = PropertyInfo(Variant::INT, "text_editor/tab_size", PROPERTY_HINT_RANGE, "1, 64, 1"); // size of 0 crashes.
  405. set("text_editor/draw_tabs", true);
  406. set("text_editor/line_numbers_zero_padded", true);
  407. set("text_editor/show_line_numbers", true);
  408. set("text_editor/show_breakpoint_gutter", true);
  409. set("text_editor/show_line_length_guideline", false);
  410. set("text_editor/line_length_guideline_column", 80);
  411. hints["text_editor/line_length_guideline_column"] = PropertyInfo(Variant::INT, "text_editor/line_length_guideline_column", PROPERTY_HINT_RANGE, "20, 160, 10");
  412. set("text_editor/show_members_overview", true);
  413. set("text_editor/trim_trailing_whitespace_on_save", false);
  414. set("text_editor/idle_parse_delay", 2);
  415. set("text_editor/create_signal_callbacks", true);
  416. set("text_editor/autosave_interval_secs", 0);
  417. set("text_editor/block_caret", false);
  418. set("text_editor/caret_blink", false);
  419. set("text_editor/caret_blink_speed", 0.65);
  420. hints["text_editor/caret_blink_speed"] = PropertyInfo(Variant::REAL, "text_editor/caret_blink_speed", PROPERTY_HINT_RANGE, "0.1, 10, 0.1");
  421. set("text_editor/font", "");
  422. hints["text_editor/font"] = PropertyInfo(Variant::STRING, "text_editor/font", PROPERTY_HINT_GLOBAL_FILE, "*.fnt");
  423. set("text_editor/auto_brace_complete", false);
  424. set("text_editor/restore_scripts_on_load", true);
  425. set("scenetree_editor/duplicate_node_name_num_separator", 0);
  426. hints["scenetree_editor/duplicate_node_name_num_separator"] = PropertyInfo(Variant::INT, "scenetree_editor/duplicate_node_name_num_separator", PROPERTY_HINT_ENUM, "None,Space,Underscore,Dash");
  427. //set("scenetree_editor/display_old_action_buttons",false);
  428. set("scenetree_editor/start_create_dialog_fully_expanded", false);
  429. set("scenetree_editor/draw_relationship_lines", false);
  430. set("scenetree_editor/relationship_line_color", Color::html("464646"));
  431. set("grid_map/pick_distance", 5000.0);
  432. set("3d_editor/grid_color", Color(0, 1, 0, 0.2));
  433. hints["3d_editor/grid_color"] = PropertyInfo(Variant::COLOR, "3d_editor/grid_color", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED);
  434. set("3d_editor/default_fov", 55.0);
  435. set("3d_editor/default_z_near", 0.1);
  436. set("3d_editor/default_z_far", 500.0);
  437. set("3d_editor/navigation_scheme", 0);
  438. hints["3d_editor/navigation_scheme"] = PropertyInfo(Variant::INT, "3d_editor/navigation_scheme", PROPERTY_HINT_ENUM, "Godot,Maya,Modo");
  439. set("3d_editor/zoom_style", 0);
  440. hints["3d_editor/zoom_style"] = PropertyInfo(Variant::INT, "3d_editor/zoom_style", PROPERTY_HINT_ENUM, "Vertical, Horizontal");
  441. set("3d_editor/orbit_modifier", 0);
  442. hints["3d_editor/orbit_modifier"] = PropertyInfo(Variant::INT, "3d_editor/orbit_modifier", PROPERTY_HINT_ENUM, "None,Shift,Alt,Meta,Ctrl");
  443. set("3d_editor/pan_modifier", 1);
  444. hints["3d_editor/pan_modifier"] = PropertyInfo(Variant::INT, "3d_editor/pan_modifier", PROPERTY_HINT_ENUM, "None,Shift,Alt,Meta,Ctrl");
  445. set("3d_editor/zoom_modifier", 4);
  446. hints["3d_editor/zoom_modifier"] = PropertyInfo(Variant::INT, "3d_editor/zoom_modifier", PROPERTY_HINT_ENUM, "None,Shift,Alt,Meta,Ctrl");
  447. set("3d_editor/emulate_numpad", false);
  448. set("3d_editor/emulate_3_button_mouse", false);
  449. set("3d_editor/warped_mouse_panning", true);
  450. set("2d_editor/bone_width", 5);
  451. set("2d_editor/bone_color1", Color(1.0, 1.0, 1.0, 0.9));
  452. set("2d_editor/bone_color2", Color(0.75, 0.75, 0.75, 0.9));
  453. set("2d_editor/bone_selected_color", Color(0.9, 0.45, 0.45, 0.9));
  454. set("2d_editor/bone_ik_color", Color(0.9, 0.9, 0.45, 0.9));
  455. set("2d_editor/keep_margins_when_changing_anchors", false);
  456. set("2d_editor/warped_mouse_panning", true);
  457. set("2d_editor/scroll_to_pan", false);
  458. set("2d_editor/pan_speed", 20);
  459. set("game_window_placement/rect", 1);
  460. hints["game_window_placement/rect"] = PropertyInfo(Variant::INT, "game_window_placement/rect", PROPERTY_HINT_ENUM, "Top Left,Centered,Custom Position,Force Maximized,Force Fullscreen");
  461. String screen_hints = TTR("Default (Same as Editor)");
  462. for (int i = 0; i < OS::get_singleton()->get_screen_count(); i++) {
  463. screen_hints += ",Monitor " + itos(i + 1);
  464. }
  465. set("game_window_placement/rect_custom_position", Vector2());
  466. set("game_window_placement/screen", 0);
  467. hints["game_window_placement/screen"] = PropertyInfo(Variant::INT, "game_window_placement/screen", PROPERTY_HINT_ENUM, screen_hints);
  468. set("on_save/compress_binary_resources", true);
  469. set("on_save/save_modified_external_resources", true);
  470. //set("on_save/save_paths_as_relative",false);
  471. //set("on_save/save_paths_without_extension",false);
  472. set("text_editor/create_signal_callbacks", true);
  473. set("file_dialog/show_hidden_files", false);
  474. set("file_dialog/display_mode", 0);
  475. hints["file_dialog/display_mode"] = PropertyInfo(Variant::INT, "file_dialog/display_mode", PROPERTY_HINT_ENUM, "Thumbnails,List");
  476. set("file_dialog/thumbnail_size", 64);
  477. hints["file_dialog/thumbnail_size"] = PropertyInfo(Variant::INT, "file_dialog/thumbnail_size", PROPERTY_HINT_RANGE, "32,128,16");
  478. set("filesystem_dock/display_mode", 0);
  479. hints["filesystem_dock/display_mode"] = PropertyInfo(Variant::INT, "filesystem_dock/display_mode", PROPERTY_HINT_ENUM, "Thumbnails,List");
  480. set("filesystem_dock/thumbnail_size", 64);
  481. hints["filesystem_dock/thumbnail_size"] = PropertyInfo(Variant::INT, "filesystem_dock/thumbnail_size", PROPERTY_HINT_RANGE, "32,128,16");
  482. set("animation/autorename_animation_tracks", true);
  483. set("animation/confirm_insert_track", true);
  484. set("property_editor/texture_preview_width", 48);
  485. set("property_editor/auto_refresh_interval", 0.3);
  486. set("help/doc_path", "");
  487. set("import/ask_save_before_reimport", false);
  488. set("import/pvrtc_texture_tool", "");
  489. #ifdef WINDOWS_ENABLED
  490. hints["import/pvrtc_texture_tool"] = PropertyInfo(Variant::STRING, "import/pvrtc_texture_tool", PROPERTY_HINT_GLOBAL_FILE, "*.exe");
  491. #else
  492. hints["import/pvrtc_texture_tool"] = PropertyInfo(Variant::STRING, "import/pvrtc_texture_tool", PROPERTY_HINT_GLOBAL_FILE, "");
  493. #endif
  494. // TODO: Rename to "import/pvrtc_fast_conversion" to match other names?
  495. set("PVRTC/fast_conversion", false);
  496. set("run/auto_save_before_running", true);
  497. set("resources/save_compressed_resources", true);
  498. set("resources/auto_reload_modified_images", true);
  499. set("run/always_close_output_on_stop", false);
  500. set("import/automatic_reimport_on_sources_changed", true);
  501. if (p_extra_config.is_valid()) {
  502. if (p_extra_config->has_section("init_projects") && p_extra_config->has_section_key("init_projects", "list")) {
  503. Vector<String> list = p_extra_config->get_value("init_projects", "list");
  504. for (int i = 0; i < list.size(); i++) {
  505. String name = list[i].replace("/", "::");
  506. set("projects/" + name, list[i]);
  507. };
  508. };
  509. if (p_extra_config->has_section("presets")) {
  510. List<String> keys;
  511. p_extra_config->get_section_keys("presets", &keys);
  512. for (List<String>::Element *E = keys.front(); E; E = E->next()) {
  513. String key = E->get();
  514. Variant val = p_extra_config->get_value("presets", key);
  515. set(key, val);
  516. };
  517. };
  518. };
  519. }
  520. void EditorSettings::_load_default_text_editor_theme() {
  521. set("text_editor/background_color", Color::html("3b000000"));
  522. set("text_editor/completion_background_color", Color::html("2C2A32"));
  523. set("text_editor/completion_selected_color", Color::html("434244"));
  524. set("text_editor/completion_existing_color", Color::html("21dfdfdf"));
  525. set("text_editor/completion_scroll_color", Color::html("ffffff"));
  526. set("text_editor/completion_font_color", Color::html("aaaaaa"));
  527. set("text_editor/caret_color", Color::html("aaaaaa"));
  528. set("text_editor/caret_background_color", Color::html("000000"));
  529. set("text_editor/line_number_color", Color::html("66aaaaaa"));
  530. set("text_editor/text_color", Color::html("aaaaaa"));
  531. set("text_editor/text_selected_color", Color::html("000000"));
  532. set("text_editor/keyword_color", Color::html("ffffb3"));
  533. set("text_editor/base_type_color", Color::html("a4ffd4"));
  534. set("text_editor/engine_type_color", Color::html("83d3ff"));
  535. set("text_editor/function_color", Color::html("66a2ce"));
  536. set("text_editor/member_variable_color", Color::html("e64e59"));
  537. set("text_editor/comment_color", Color::html("676767"));
  538. set("text_editor/string_color", Color::html("ef6ebe"));
  539. set("text_editor/number_color", Color::html("EB9532"));
  540. set("text_editor/symbol_color", Color::html("badfff"));
  541. set("text_editor/selection_color", Color::html("6ca9c2"));
  542. set("text_editor/brace_mismatch_color", Color(1, 0.2, 0.2));
  543. set("text_editor/current_line_color", Color(0.3, 0.5, 0.8, 0.15));
  544. set("text_editor/line_length_guideline_color", Color(0.3, 0.5, 0.8, 0.1));
  545. set("text_editor/mark_color", Color(1.0, 0.4, 0.4, 0.4));
  546. set("text_editor/breakpoint_color", Color(0.8, 0.8, 0.4, 0.2));
  547. set("text_editor/word_highlighted_color", Color(0.8, 0.9, 0.9, 0.15));
  548. set("text_editor/search_result_color", Color(0.05, 0.25, 0.05, 1));
  549. set("text_editor/search_result_border_color", Color(0.1, 0.45, 0.1, 1));
  550. }
  551. void EditorSettings::notify_changes() {
  552. _THREAD_SAFE_METHOD_
  553. SceneTree *sml = NULL;
  554. if (OS::get_singleton()->get_main_loop())
  555. sml = OS::get_singleton()->get_main_loop()->cast_to<SceneTree>();
  556. if (!sml) {
  557. return;
  558. }
  559. Node *root = sml->get_root()->get_child(0);
  560. if (!root) {
  561. return;
  562. }
  563. root->propagate_notification(NOTIFICATION_EDITOR_SETTINGS_CHANGED);
  564. }
  565. void EditorSettings::_add_property_info_bind(const Dictionary &p_info) {
  566. ERR_FAIL_COND(!p_info.has("name"));
  567. ERR_FAIL_COND(!p_info.has("type"));
  568. PropertyInfo pinfo;
  569. pinfo.name = p_info["name"];
  570. ERR_FAIL_COND(!props.has(pinfo.name));
  571. pinfo.type = Variant::Type(p_info["type"].operator int());
  572. ERR_FAIL_INDEX(pinfo.type, Variant::VARIANT_MAX);
  573. if (p_info.has("hint"))
  574. pinfo.hint = PropertyHint(p_info["hint"].operator int());
  575. if (p_info.has("hint_string"))
  576. pinfo.hint_string = p_info["hint_string"];
  577. add_property_hint(pinfo);
  578. }
  579. void EditorSettings::add_property_hint(const PropertyInfo &p_hint) {
  580. _THREAD_SAFE_METHOD_
  581. hints[p_hint.name] = p_hint;
  582. }
  583. void EditorSettings::set_favorite_dirs(const Vector<String> &p_favorites) {
  584. favorite_dirs = p_favorites;
  585. FileAccess *f = FileAccess::open(get_project_settings_path().plus_file("favorite_dirs"), FileAccess::WRITE);
  586. if (f) {
  587. for (int i = 0; i < favorite_dirs.size(); i++)
  588. f->store_line(favorite_dirs[i]);
  589. memdelete(f);
  590. }
  591. }
  592. Vector<String> EditorSettings::get_favorite_dirs() const {
  593. return favorite_dirs;
  594. }
  595. void EditorSettings::set_recent_dirs(const Vector<String> &p_recent) {
  596. recent_dirs = p_recent;
  597. FileAccess *f = FileAccess::open(get_project_settings_path().plus_file("recent_dirs"), FileAccess::WRITE);
  598. if (f) {
  599. for (int i = 0; i < recent_dirs.size(); i++)
  600. f->store_line(recent_dirs[i]);
  601. memdelete(f);
  602. }
  603. }
  604. Vector<String> EditorSettings::get_recent_dirs() const {
  605. return recent_dirs;
  606. }
  607. String EditorSettings::get_project_settings_path() const {
  608. return get_settings_path().plus_file("config").plus_file(project_config_path);
  609. }
  610. void EditorSettings::load_favorites() {
  611. FileAccess *f = FileAccess::open(get_project_settings_path().plus_file("favorite_dirs"), FileAccess::READ);
  612. if (f) {
  613. String line = f->get_line().strip_edges();
  614. while (line != "") {
  615. favorite_dirs.push_back(line);
  616. line = f->get_line().strip_edges();
  617. }
  618. memdelete(f);
  619. }
  620. f = FileAccess::open(get_project_settings_path().plus_file("recent_dirs"), FileAccess::READ);
  621. if (f) {
  622. String line = f->get_line().strip_edges();
  623. while (line != "") {
  624. recent_dirs.push_back(line);
  625. line = f->get_line().strip_edges();
  626. }
  627. memdelete(f);
  628. }
  629. }
  630. void EditorSettings::list_text_editor_themes() {
  631. String themes = "Default";
  632. DirAccess *d = DirAccess::open(settings_path + "/text_editor_themes");
  633. if (d) {
  634. d->list_dir_begin();
  635. String file = d->get_next();
  636. while (file != String()) {
  637. if (file.extension() == "tet" && file.basename().to_lower() != "default") {
  638. themes += "," + file.basename();
  639. }
  640. file = d->get_next();
  641. }
  642. d->list_dir_end();
  643. memdelete(d);
  644. }
  645. add_property_hint(PropertyInfo(Variant::STRING, "text_editor/color_theme", PROPERTY_HINT_ENUM, themes));
  646. }
  647. void EditorSettings::load_text_editor_theme() {
  648. if (get("text_editor/color_theme") == "Default") {
  649. _load_default_text_editor_theme(); // sorry for "Settings changed" console spam
  650. return;
  651. }
  652. String theme_path = get_settings_path() + "/text_editor_themes/" + get("text_editor/color_theme") + ".tet";
  653. Ref<ConfigFile> cf = memnew(ConfigFile);
  654. Error err = cf->load(theme_path);
  655. if (err != OK) {
  656. return;
  657. }
  658. List<String> keys;
  659. cf->get_section_keys("color_theme", &keys);
  660. for (List<String>::Element *E = keys.front(); E; E = E->next()) {
  661. String key = E->get();
  662. String val = cf->get_value("color_theme", key);
  663. // don't load if it's not already there!
  664. if (has("text_editor/" + key)) {
  665. // make sure it is actually a color
  666. if (val.is_valid_html_color() && key.find("color") >= 0) {
  667. props["text_editor/" + key].variant = Color::html(val); // change manually to prevent "Settings changed" console spam
  668. }
  669. }
  670. }
  671. emit_signal("settings_changed");
  672. // if it doesn't load just use what is currently loaded
  673. }
  674. bool EditorSettings::import_text_editor_theme(String p_file) {
  675. if (!p_file.ends_with(".tet")) {
  676. return false;
  677. } else {
  678. if (p_file.get_file().to_lower() == "default.tet") {
  679. return false;
  680. }
  681. DirAccess *d = DirAccess::open(settings_path + "/text_editor_themes");
  682. if (d) {
  683. d->copy(p_file, settings_path + "/text_editor_themes/" + p_file.get_file());
  684. memdelete(d);
  685. return true;
  686. }
  687. }
  688. return false;
  689. }
  690. bool EditorSettings::save_text_editor_theme() {
  691. String p_file = get("text_editor/color_theme");
  692. if (p_file.get_file().to_lower() == "default") {
  693. return false;
  694. }
  695. String theme_path = get_settings_path() + "/text_editor_themes/" + p_file + ".tet";
  696. return _save_text_editor_theme(theme_path);
  697. }
  698. bool EditorSettings::save_text_editor_theme_as(String p_file) {
  699. if (!p_file.ends_with(".tet")) {
  700. p_file += ".tet";
  701. }
  702. if (p_file.get_file().to_lower() == "default.tet") {
  703. return false;
  704. }
  705. if (_save_text_editor_theme(p_file)) {
  706. // switch to theme is saved in the theme directory
  707. list_text_editor_themes();
  708. String theme_name = p_file.substr(0, p_file.length() - 4).get_file();
  709. if (p_file.get_base_dir() == get_settings_path() + "/text_editor_themes") {
  710. set("text_editor/color_theme", theme_name);
  711. load_text_editor_theme();
  712. }
  713. return true;
  714. }
  715. return false;
  716. }
  717. bool EditorSettings::_save_text_editor_theme(String p_file) {
  718. String theme_section = "color_theme";
  719. Ref<ConfigFile> cf = memnew(ConfigFile); // hex is better?
  720. cf->set_value(theme_section, "background_color", ((Color)get("text_editor/background_color")).to_html());
  721. cf->set_value(theme_section, "completion_background_color", ((Color)get("text_editor/completion_background_color")).to_html());
  722. cf->set_value(theme_section, "completion_selected_color", ((Color)get("text_editor/completion_selected_color")).to_html());
  723. cf->set_value(theme_section, "completion_existing_color", ((Color)get("text_editor/completion_existing_color")).to_html());
  724. cf->set_value(theme_section, "completion_scroll_color", ((Color)get("text_editor/completion_scroll_color")).to_html());
  725. cf->set_value(theme_section, "completion_font_color", ((Color)get("text_editor/completion_font_color")).to_html());
  726. cf->set_value(theme_section, "caret_color", ((Color)get("text_editor/caret_color")).to_html());
  727. cf->set_value(theme_section, "caret_background_color", ((Color)get("text_editor/caret_background_color")).to_html());
  728. cf->set_value(theme_section, "line_number_color", ((Color)get("text_editor/line_number_color")).to_html());
  729. cf->set_value(theme_section, "text_color", ((Color)get("text_editor/text_color")).to_html());
  730. cf->set_value(theme_section, "text_selected_color", ((Color)get("text_editor/text_selected_color")).to_html());
  731. cf->set_value(theme_section, "keyword_color", ((Color)get("text_editor/keyword_color")).to_html());
  732. cf->set_value(theme_section, "base_type_color", ((Color)get("text_editor/base_type_color")).to_html());
  733. cf->set_value(theme_section, "engine_type_color", ((Color)get("text_editor/engine_type_color")).to_html());
  734. cf->set_value(theme_section, "function_color", ((Color)get("text_editor/function_color")).to_html());
  735. cf->set_value(theme_section, "member_variable_color", ((Color)get("text_editor/member_variable_color")).to_html());
  736. cf->set_value(theme_section, "comment_color", ((Color)get("text_editor/comment_color")).to_html());
  737. cf->set_value(theme_section, "string_color", ((Color)get("text_editor/string_color")).to_html());
  738. cf->set_value(theme_section, "number_color", ((Color)get("text_editor/number_color")).to_html());
  739. cf->set_value(theme_section, "symbol_color", ((Color)get("text_editor/symbol_color")).to_html());
  740. cf->set_value(theme_section, "selection_color", ((Color)get("text_editor/selection_color")).to_html());
  741. cf->set_value(theme_section, "brace_mismatch_color", ((Color)get("text_editor/brace_mismatch_color")).to_html());
  742. cf->set_value(theme_section, "current_line_color", ((Color)get("text_editor/current_line_color")).to_html());
  743. cf->set_value(theme_section, "line_length_guideline_color", ((Color)get("text_editor/line_length_guideline_color")).to_html());
  744. cf->set_value(theme_section, "mark_color", ((Color)get("text_editor/mark_color")).to_html());
  745. cf->set_value(theme_section, "breakpoint_color", ((Color)get("text_editor/breakpoint_color")).to_html());
  746. cf->set_value(theme_section, "word_highlighted_color", ((Color)get("text_editor/word_highlighted_color")).to_html());
  747. cf->set_value(theme_section, "search_result_color", ((Color)get("text_editor/search_result_color")).to_html());
  748. cf->set_value(theme_section, "search_result_border_color", ((Color)get("text_editor/search_result_border_color")).to_html());
  749. Error err = cf->save(p_file);
  750. if (err == OK) {
  751. return true;
  752. }
  753. return false;
  754. }
  755. void EditorSettings::add_shortcut(const String &p_name, Ref<ShortCut> &p_shortcut) {
  756. shortcuts[p_name] = p_shortcut;
  757. }
  758. bool EditorSettings::is_shortcut(const String &p_name, const InputEvent &p_event) const {
  759. const Map<String, Ref<ShortCut> >::Element *E = shortcuts.find(p_name);
  760. if (!E) {
  761. ERR_EXPLAIN("Unknown Shortcut: " + p_name);
  762. ERR_FAIL_V(false);
  763. }
  764. return E->get()->is_shortcut(p_event);
  765. }
  766. Ref<ShortCut> EditorSettings::get_shortcut(const String &p_name) const {
  767. const Map<String, Ref<ShortCut> >::Element *E = shortcuts.find(p_name);
  768. if (!E)
  769. return Ref<ShortCut>();
  770. return E->get();
  771. }
  772. void EditorSettings::get_shortcut_list(List<String> *r_shortcuts) {
  773. for (const Map<String, Ref<ShortCut> >::Element *E = shortcuts.front(); E; E = E->next()) {
  774. r_shortcuts->push_back(E->key());
  775. }
  776. }
  777. void EditorSettings::set_optimize_save(bool p_optimize) {
  778. optimize_save = p_optimize;
  779. }
  780. void EditorSettings::_bind_methods() {
  781. ObjectTypeDB::bind_method(_MD("erase", "property"), &EditorSettings::erase);
  782. ObjectTypeDB::bind_method(_MD("get_settings_path"), &EditorSettings::get_settings_path);
  783. ObjectTypeDB::bind_method(_MD("get_project_settings_path"), &EditorSettings::get_project_settings_path);
  784. ObjectTypeDB::bind_method(_MD("add_property_info", "info"), &EditorSettings::_add_property_info_bind);
  785. ObjectTypeDB::bind_method(_MD("set_favorite_dirs", "dirs"), &EditorSettings::set_favorite_dirs);
  786. ObjectTypeDB::bind_method(_MD("get_favorite_dirs"), &EditorSettings::get_favorite_dirs);
  787. ObjectTypeDB::bind_method(_MD("set_recent_dirs", "dirs"), &EditorSettings::set_recent_dirs);
  788. ObjectTypeDB::bind_method(_MD("get_recent_dirs"), &EditorSettings::get_recent_dirs);
  789. ADD_SIGNAL(MethodInfo("settings_changed"));
  790. }
  791. EditorSettings::EditorSettings() {
  792. //singleton=this;
  793. last_order = 0;
  794. optimize_save = true;
  795. save_changed_setting = true;
  796. EditorTranslationList *etl = _editor_translations;
  797. while (etl->data) {
  798. Vector<uint8_t> data;
  799. data.resize(etl->uncomp_size);
  800. Compression::decompress(data.ptr(), etl->uncomp_size, etl->data, etl->comp_size, Compression::MODE_DEFLATE);
  801. FileAccessMemory *fa = memnew(FileAccessMemory);
  802. fa->open_custom(data.ptr(), data.size());
  803. Ref<Translation> tr = TranslationLoaderPO::load_translation(fa, NULL, "translation_" + String(etl->lang));
  804. if (tr.is_valid()) {
  805. tr->set_locale(etl->lang);
  806. translations.push_back(tr);
  807. }
  808. etl++;
  809. }
  810. _load_defaults();
  811. }
  812. EditorSettings::~EditorSettings() {
  813. // singleton=NULL;
  814. }
  815. Ref<ShortCut> ED_GET_SHORTCUT(const String &p_path) {
  816. Ref<ShortCut> sc = EditorSettings::get_singleton()->get_shortcut(p_path);
  817. if (!sc.is_valid()) {
  818. ERR_EXPLAIN("Used ED_GET_SHORTCUT with invalid shortcut: " + p_path);
  819. ERR_FAIL_COND_V(!sc.is_valid(), sc);
  820. }
  821. return sc;
  822. }
  823. Ref<ShortCut> ED_SHORTCUT(const String &p_path, const String &p_name, uint32_t p_keycode) {
  824. InputEvent ie;
  825. if (p_keycode) {
  826. ie.type = InputEvent::KEY;
  827. ie.key.unicode = p_keycode & KEY_CODE_MASK;
  828. ie.key.scancode = p_keycode & KEY_CODE_MASK;
  829. ie.key.mod.shift = bool(p_keycode & KEY_MASK_SHIFT);
  830. ie.key.mod.alt = bool(p_keycode & KEY_MASK_ALT);
  831. ie.key.mod.control = bool(p_keycode & KEY_MASK_CTRL);
  832. ie.key.mod.meta = bool(p_keycode & KEY_MASK_META);
  833. }
  834. Ref<ShortCut> sc = EditorSettings::get_singleton()->get_shortcut(p_path);
  835. if (sc.is_valid()) {
  836. sc->set_name(p_name); //keep name (the ones that come from disk have no name)
  837. sc->set_meta("original", ie); //to compare against changes
  838. return sc;
  839. }
  840. sc.instance();
  841. sc->set_name(p_name);
  842. sc->set_shortcut(ie);
  843. sc->set_meta("original", ie); //to compare against changes
  844. EditorSettings::get_singleton()->add_shortcut(p_path, sc);
  845. return sc;
  846. }