quick_settings_dialog.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /**************************************************************************/
  2. /* quick_settings_dialog.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "quick_settings_dialog.h"
  31. #include "core/string/translation_server.h"
  32. #include "editor/doc/editor_help.h"
  33. #include "editor/editor_string_names.h"
  34. #include "editor/inspector/editor_properties.h"
  35. #include "editor/settings/editor_settings.h"
  36. #include "editor/settings/editor_settings_dialog.h"
  37. #include "editor/themes/editor_scale.h"
  38. #include "scene/gui/box_container.h"
  39. #include "scene/gui/button.h"
  40. #include "scene/gui/label.h"
  41. #include "scene/gui/option_button.h"
  42. #include "scene/gui/panel_container.h"
  43. void QuickSettingsDialog::_fetch_setting_values() {
  44. #ifndef ANDROID_ENABLED
  45. editor_languages.clear();
  46. #endif
  47. editor_styles.clear();
  48. editor_themes.clear();
  49. editor_scales.clear();
  50. editor_network_modes.clear();
  51. editor_check_for_updates.clear();
  52. editor_directory_naming_conventions.clear();
  53. {
  54. List<PropertyInfo> editor_settings_properties;
  55. EditorSettings::get_singleton()->get_property_list(&editor_settings_properties);
  56. for (const PropertyInfo &pi : editor_settings_properties) {
  57. if (pi.name == "interface/editor/editor_language") {
  58. #ifndef ANDROID_ENABLED
  59. editor_languages = pi.hint_string.split(";", false);
  60. #endif
  61. } else if (pi.name == "interface/theme/style") {
  62. editor_styles = pi.hint_string.split(",");
  63. } else if (pi.name == "interface/theme/color_preset") {
  64. editor_themes = pi.hint_string.split(",");
  65. } else if (pi.name == "interface/editor/display_scale") {
  66. editor_scales = pi.hint_string.split(",");
  67. } else if (pi.name == "network/connection/network_mode") {
  68. editor_network_modes = pi.hint_string.split(",");
  69. } else if (pi.name == "network/connection/check_for_updates") {
  70. editor_check_for_updates = pi.hint_string.split(",");
  71. } else if (pi.name == "project_manager/directory_naming_convention") {
  72. editor_directory_naming_conventions = pi.hint_string.split(",");
  73. }
  74. }
  75. }
  76. }
  77. void QuickSettingsDialog::_update_current_values() {
  78. #ifndef ANDROID_ENABLED
  79. // Language options.
  80. {
  81. const String current_lang = EDITOR_GET("interface/editor/editor_language");
  82. for (int i = 0; i < editor_languages.size(); i++) {
  83. const String &lang_value = editor_languages[i].get_slicec('/', 0);
  84. if (current_lang == lang_value) {
  85. language_option_button->set_text(editor_languages[i].get_slicec('/', 1));
  86. language_option_button->select(i);
  87. break;
  88. }
  89. }
  90. }
  91. #endif
  92. // Style options.
  93. {
  94. const String current_style = EDITOR_GET("interface/theme/style");
  95. for (int i = 0; i < editor_styles.size(); i++) {
  96. const String &style_value = editor_styles[i];
  97. if (current_style == style_value) {
  98. style_option_button->select(i);
  99. }
  100. }
  101. }
  102. // Theme options.
  103. {
  104. const String current_theme = EDITOR_GET("interface/theme/color_preset");
  105. for (int i = 0; i < editor_themes.size(); i++) {
  106. const String &theme_value = editor_themes[i];
  107. if (current_theme == theme_value) {
  108. theme_option_button->set_text(current_theme);
  109. theme_option_button->select(i);
  110. theme_option_button->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  111. custom_theme_label->set_visible(current_theme == "Custom");
  112. }
  113. }
  114. }
  115. // Scale options.
  116. {
  117. const int current_scale = EDITOR_GET("interface/editor/display_scale");
  118. for (int i = 0; i < editor_scales.size(); i++) {
  119. const String &scale_value = editor_scales[i];
  120. if (current_scale == i) {
  121. scale_option_button->set_text(scale_value);
  122. scale_option_button->select(i);
  123. }
  124. }
  125. }
  126. // Network mode options.
  127. {
  128. const int current_network_mode = EDITOR_GET("network/connection/network_mode");
  129. for (int i = 0; i < editor_network_modes.size(); i++) {
  130. const String &network_mode_value = editor_network_modes[i];
  131. if (current_network_mode == i) {
  132. network_mode_option_button->set_text(network_mode_value);
  133. network_mode_option_button->select(i);
  134. }
  135. }
  136. }
  137. // Check for updates options.
  138. {
  139. const int current_update_mode = EDITOR_GET("network/connection/check_for_updates");
  140. for (int i = 0; i < editor_check_for_updates.size(); i++) {
  141. const String &check_for_update_value = editor_check_for_updates[i];
  142. if (current_update_mode == i) {
  143. check_for_update_button->set_text(check_for_update_value);
  144. check_for_update_button->select(i);
  145. // Disables Check for Updates selection if Network mode is set to Offline.
  146. check_for_update_button->set_disabled(!EDITOR_GET("network/connection/network_mode"));
  147. }
  148. }
  149. }
  150. // Project directory naming options.
  151. {
  152. const int current_directory_naming = EDITOR_GET("project_manager/directory_naming_convention");
  153. for (int i = 0; i < editor_directory_naming_conventions.size(); i++) {
  154. const String &directory_naming_value = editor_directory_naming_conventions[i];
  155. if (current_directory_naming == i) {
  156. directory_naming_convention_button->set_text(directory_naming_value);
  157. directory_naming_convention_button->select(i);
  158. }
  159. }
  160. }
  161. }
  162. void QuickSettingsDialog::_add_setting_control(const String &p_text, Control *p_control) {
  163. HBoxContainer *container = memnew(HBoxContainer);
  164. settings_list->add_child(container);
  165. Label *label = memnew(Label(p_text));
  166. label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  167. container->add_child(label);
  168. p_control->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  169. container->add_child(p_control);
  170. }
  171. #ifndef ANDROID_ENABLED
  172. void QuickSettingsDialog::_language_selected(int p_id) {
  173. const String selected_language = language_option_button->get_item_metadata(p_id);
  174. _set_setting_value("interface/editor/editor_language", selected_language);
  175. }
  176. #endif
  177. void QuickSettingsDialog::_style_selected(int p_id) {
  178. const String selected_style = style_option_button->get_item_text(p_id);
  179. _set_setting_value("interface/theme/style", selected_style);
  180. }
  181. void QuickSettingsDialog::_theme_selected(int p_id) {
  182. const String selected_theme = theme_option_button->get_item_text(p_id);
  183. _set_setting_value("interface/theme/color_preset", selected_theme);
  184. custom_theme_label->set_visible(selected_theme == "Custom");
  185. }
  186. void QuickSettingsDialog::_scale_selected(int p_id) {
  187. _set_setting_value("interface/editor/display_scale", p_id, true);
  188. }
  189. void QuickSettingsDialog::_network_mode_selected(int p_id) {
  190. _set_setting_value("network/connection/network_mode", p_id);
  191. // Disables Check for Updates selection if Network mode is set to Offline.
  192. check_for_update_button->set_disabled(!p_id);
  193. }
  194. void QuickSettingsDialog::_check_for_update_selected(int p_id) {
  195. _set_setting_value("network/connection/check_for_updates", p_id);
  196. }
  197. void QuickSettingsDialog::_directory_naming_convention_selected(int p_id) {
  198. _set_setting_value("project_manager/directory_naming_convention", p_id);
  199. }
  200. void QuickSettingsDialog::_set_setting_value(const String &p_setting, const Variant &p_value, bool p_restart_required) {
  201. EditorSettings::get_singleton()->set(p_setting, p_value);
  202. EditorSettings::get_singleton()->notify_changes();
  203. EditorSettings::get_singleton()->save();
  204. if (p_restart_required) {
  205. restart_required_label->show();
  206. if (!restart_required_button) {
  207. int ed_swap_cancel_ok = EDITOR_GET("interface/editor/accept_dialog_cancel_ok_buttons");
  208. if (ed_swap_cancel_ok == 0) {
  209. ed_swap_cancel_ok = DisplayServer::get_singleton()->get_swap_cancel_ok() ? 2 : 1;
  210. }
  211. restart_required_button = add_button(TTRC("Restart Now"), ed_swap_cancel_ok != 2);
  212. restart_required_button->connect(SceneStringName(pressed), callable_mp(this, &QuickSettingsDialog::_request_restart));
  213. }
  214. }
  215. }
  216. void QuickSettingsDialog::_show_full_settings() {
  217. if (!editor_settings_dialog) {
  218. EditorHelp::generate_doc();
  219. Ref<EditorInspectorDefaultPlugin> eidp;
  220. eidp.instantiate();
  221. EditorInspector::add_inspector_plugin(eidp);
  222. EditorPropertyNameProcessor *epnp = memnew(EditorPropertyNameProcessor);
  223. add_child(epnp);
  224. editor_settings_dialog = memnew(EditorSettingsDialog);
  225. get_parent()->add_child(editor_settings_dialog);
  226. editor_settings_dialog->connect("restart_requested", callable_mp(this, &QuickSettingsDialog::_request_restart));
  227. }
  228. hide();
  229. editor_settings_dialog->popup_edit_settings();
  230. }
  231. void QuickSettingsDialog::_request_restart() {
  232. emit_signal("restart_required");
  233. }
  234. void QuickSettingsDialog::update_size_limits(const Size2 &p_max_popup_size) {
  235. #ifndef ANDROID_ENABLED
  236. language_option_button->get_popup()->set_max_size(p_max_popup_size);
  237. #endif
  238. }
  239. void QuickSettingsDialog::_notification(int p_what) {
  240. switch (p_what) {
  241. case NOTIFICATION_THEME_CHANGED: {
  242. settings_list_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("quick_settings_panel"), SNAME("ProjectManager")));
  243. restart_required_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
  244. custom_theme_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("font_placeholder_color"), EditorStringName(Editor)));
  245. } break;
  246. case NOTIFICATION_VISIBILITY_CHANGED: {
  247. if (is_visible()) {
  248. _update_current_values();
  249. }
  250. } break;
  251. }
  252. }
  253. void QuickSettingsDialog::_bind_methods() {
  254. ADD_SIGNAL(MethodInfo("restart_required"));
  255. }
  256. QuickSettingsDialog::QuickSettingsDialog() {
  257. set_title(TTRC("Quick Settings"));
  258. set_ok_button_text(TTRC("Close"));
  259. VBoxContainer *main_vbox = memnew(VBoxContainer);
  260. add_child(main_vbox);
  261. main_vbox->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  262. // Settings grid.
  263. {
  264. _fetch_setting_values();
  265. settings_list_panel = memnew(PanelContainer);
  266. main_vbox->add_child(settings_list_panel);
  267. settings_list = memnew(VBoxContainer);
  268. settings_list_panel->add_child(settings_list);
  269. #ifndef ANDROID_ENABLED
  270. // Language options.
  271. {
  272. language_option_button = memnew(OptionButton);
  273. language_option_button->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  274. language_option_button->set_fit_to_longest_item(false);
  275. language_option_button->connect(SceneStringName(item_selected), callable_mp(this, &QuickSettingsDialog::_language_selected));
  276. for (int i = 0; i < editor_languages.size(); i++) {
  277. const String &lang_code = editor_languages[i].get_slicec('/', 0);
  278. const String &lang_name = editor_languages[i].get_slicec('/', 1);
  279. language_option_button->add_item(lang_name, i);
  280. language_option_button->set_item_metadata(i, lang_code);
  281. }
  282. _add_setting_control(TTRC("Language"), language_option_button);
  283. }
  284. #endif
  285. // Style options.
  286. {
  287. style_option_button = memnew(OptionButton);
  288. style_option_button->set_fit_to_longest_item(false);
  289. style_option_button->connect(SceneStringName(item_selected), callable_mp(this, &QuickSettingsDialog::_style_selected));
  290. for (int i = 0; i < editor_styles.size(); i++) {
  291. const String &style_value = editor_styles[i];
  292. style_option_button->add_item(style_value, i);
  293. }
  294. _add_setting_control(TTRC("Style"), style_option_button);
  295. }
  296. // Theme options.
  297. {
  298. theme_option_button = memnew(OptionButton);
  299. theme_option_button->set_fit_to_longest_item(false);
  300. theme_option_button->connect(SceneStringName(item_selected), callable_mp(this, &QuickSettingsDialog::_theme_selected));
  301. for (int i = 0; i < editor_themes.size(); i++) {
  302. const String &theme_value = editor_themes[i];
  303. theme_option_button->add_item(theme_value, i);
  304. }
  305. _add_setting_control(TTRC("Color Preset"), theme_option_button);
  306. custom_theme_label = memnew(Label(TTRC("Custom preset can be further configured in the editor.")));
  307. custom_theme_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  308. custom_theme_label->set_custom_minimum_size(Size2(220, 0) * EDSCALE);
  309. custom_theme_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD);
  310. custom_theme_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  311. custom_theme_label->set_stretch_ratio(2.0);
  312. custom_theme_label->hide();
  313. settings_list->add_child(custom_theme_label);
  314. }
  315. // Scale options.
  316. {
  317. scale_option_button = memnew(OptionButton);
  318. scale_option_button->set_fit_to_longest_item(false);
  319. scale_option_button->connect(SceneStringName(item_selected), callable_mp(this, &QuickSettingsDialog::_scale_selected));
  320. for (int i = 0; i < editor_scales.size(); i++) {
  321. const String &scale_value = editor_scales[i];
  322. scale_option_button->add_item(scale_value, i);
  323. }
  324. _add_setting_control(TTRC("Display Scale"), scale_option_button);
  325. }
  326. // Network mode options.
  327. {
  328. network_mode_option_button = memnew(OptionButton);
  329. network_mode_option_button->set_fit_to_longest_item(false);
  330. network_mode_option_button->connect(SceneStringName(item_selected), callable_mp(this, &QuickSettingsDialog::_network_mode_selected));
  331. for (int i = 0; i < editor_network_modes.size(); i++) {
  332. const String &network_mode_value = editor_network_modes[i];
  333. network_mode_option_button->add_item(network_mode_value, i);
  334. }
  335. _add_setting_control(TTRC("Network Mode"), network_mode_option_button);
  336. }
  337. // Check for updates options.
  338. {
  339. check_for_update_button = memnew(OptionButton);
  340. check_for_update_button->set_fit_to_longest_item(false);
  341. check_for_update_button->connect(SceneStringName(item_selected), callable_mp(this, &QuickSettingsDialog::_check_for_update_selected));
  342. for (int i = 0; i < editor_check_for_updates.size(); i++) {
  343. const String &check_for_update_value = editor_check_for_updates[i];
  344. check_for_update_button->add_item(check_for_update_value, i);
  345. }
  346. _add_setting_control(TTRC("Check for Updates"), check_for_update_button);
  347. }
  348. // Project directory naming options.
  349. {
  350. directory_naming_convention_button = memnew(OptionButton);
  351. directory_naming_convention_button->set_fit_to_longest_item(false);
  352. directory_naming_convention_button->connect(SceneStringName(item_selected), callable_mp(this, &QuickSettingsDialog::_directory_naming_convention_selected));
  353. for (int i = 0; i < editor_directory_naming_conventions.size(); i++) {
  354. const String &directory_naming_convention = editor_directory_naming_conventions[i];
  355. directory_naming_convention_button->add_item(directory_naming_convention, i);
  356. }
  357. _add_setting_control(TTRC("Directory Naming Convention"), directory_naming_convention_button);
  358. }
  359. _update_current_values();
  360. }
  361. // Full settings button.
  362. {
  363. Button *open_full_settings = memnew(Button);
  364. open_full_settings->set_text(TTRC("Edit All Settings"));
  365. open_full_settings->set_h_size_flags(Control::SIZE_SHRINK_END);
  366. settings_list->add_child(open_full_settings);
  367. open_full_settings->connect(SceneStringName(pressed), callable_mp(this, &QuickSettingsDialog::_show_full_settings));
  368. }
  369. // Restart required panel.
  370. {
  371. restart_required_label = memnew(Label(TTRC("Settings changed! The project manager must be restarted for changes to take effect.")));
  372. restart_required_label->set_custom_minimum_size(Size2(560, 0) * EDSCALE);
  373. restart_required_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD);
  374. restart_required_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  375. restart_required_label->hide();
  376. main_vbox->add_child(restart_required_label);
  377. }
  378. }