quick_settings_dialog.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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/editor_settings.h"
  33. #include "editor/editor_string_names.h"
  34. #include "editor/themes/editor_scale.h"
  35. #include "scene/gui/box_container.h"
  36. #include "scene/gui/button.h"
  37. #include "scene/gui/label.h"
  38. #include "scene/gui/option_button.h"
  39. #include "scene/gui/panel_container.h"
  40. void QuickSettingsDialog::_fetch_setting_values() {
  41. #ifndef ANDROID_ENABLED
  42. editor_languages.clear();
  43. #endif
  44. editor_themes.clear();
  45. editor_scales.clear();
  46. editor_network_modes.clear();
  47. editor_check_for_updates.clear();
  48. editor_directory_naming_conventions.clear();
  49. {
  50. List<PropertyInfo> editor_settings_properties;
  51. EditorSettings::get_singleton()->get_property_list(&editor_settings_properties);
  52. for (const PropertyInfo &pi : editor_settings_properties) {
  53. if (pi.name == "interface/editor/editor_language") {
  54. #ifndef ANDROID_ENABLED
  55. editor_languages = pi.hint_string.split(",");
  56. #endif
  57. } else if (pi.name == "interface/theme/preset") {
  58. editor_themes = pi.hint_string.split(",");
  59. } else if (pi.name == "interface/editor/display_scale") {
  60. editor_scales = pi.hint_string.split(",");
  61. } else if (pi.name == "network/connection/network_mode") {
  62. editor_network_modes = pi.hint_string.split(",");
  63. } else if (pi.name == "network/connection/check_for_updates") {
  64. editor_check_for_updates = pi.hint_string.split(",");
  65. } else if (pi.name == "project_manager/directory_naming_convention") {
  66. editor_directory_naming_conventions = pi.hint_string.split(",");
  67. }
  68. }
  69. }
  70. }
  71. void QuickSettingsDialog::_update_current_values() {
  72. #ifndef ANDROID_ENABLED
  73. // Language options.
  74. {
  75. const String current_lang = EDITOR_GET("interface/editor/editor_language");
  76. for (int i = 0; i < editor_languages.size(); i++) {
  77. const String &lang_value = editor_languages[i];
  78. if (current_lang == lang_value) {
  79. language_option_button->set_text(current_lang);
  80. language_option_button->select(i);
  81. }
  82. }
  83. }
  84. #endif
  85. // Theme options.
  86. {
  87. const String current_theme = EDITOR_GET("interface/theme/preset");
  88. for (int i = 0; i < editor_themes.size(); i++) {
  89. const String &theme_value = editor_themes[i];
  90. if (current_theme == theme_value) {
  91. theme_option_button->set_text(current_theme);
  92. theme_option_button->select(i);
  93. theme_option_button->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  94. custom_theme_label->set_visible(current_theme == "Custom");
  95. }
  96. }
  97. }
  98. // Scale options.
  99. {
  100. const int current_scale = EDITOR_GET("interface/editor/display_scale");
  101. for (int i = 0; i < editor_scales.size(); i++) {
  102. const String &scale_value = editor_scales[i];
  103. if (current_scale == i) {
  104. scale_option_button->set_text(scale_value);
  105. scale_option_button->select(i);
  106. }
  107. }
  108. }
  109. // Network mode options.
  110. {
  111. const int current_network_mode = EDITOR_GET("network/connection/network_mode");
  112. for (int i = 0; i < editor_network_modes.size(); i++) {
  113. const String &network_mode_value = editor_network_modes[i];
  114. if (current_network_mode == i) {
  115. network_mode_option_button->set_text(network_mode_value);
  116. network_mode_option_button->select(i);
  117. }
  118. }
  119. }
  120. // Check for updates options.
  121. {
  122. const int current_update_mode = EDITOR_GET("network/connection/check_for_updates");
  123. for (int i = 0; i < editor_check_for_updates.size(); i++) {
  124. const String &check_for_update_value = editor_check_for_updates[i];
  125. if (current_update_mode == i) {
  126. check_for_update_button->set_text(check_for_update_value);
  127. check_for_update_button->select(i);
  128. // Disables Check for Updates selection if Network mode is set to Offline.
  129. check_for_update_button->set_disabled(!EDITOR_GET("network/connection/network_mode"));
  130. }
  131. }
  132. }
  133. // Project directory naming options.
  134. {
  135. const int current_directory_naming = EDITOR_GET("project_manager/directory_naming_convention");
  136. for (int i = 0; i < editor_directory_naming_conventions.size(); i++) {
  137. const String &directory_naming_value = editor_directory_naming_conventions[i];
  138. if (current_directory_naming == i) {
  139. directory_naming_convention_button->set_text(directory_naming_value);
  140. directory_naming_convention_button->select(i);
  141. }
  142. }
  143. }
  144. }
  145. void QuickSettingsDialog::_add_setting_control(const String &p_text, Control *p_control) {
  146. HBoxContainer *container = memnew(HBoxContainer);
  147. settings_list->add_child(container);
  148. Label *label = memnew(Label(p_text));
  149. label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  150. container->add_child(label);
  151. p_control->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  152. p_control->set_stretch_ratio(2.0);
  153. container->add_child(p_control);
  154. }
  155. #ifndef ANDROID_ENABLED
  156. void QuickSettingsDialog::_language_selected(int p_id) {
  157. const String selected_language = language_option_button->get_item_metadata(p_id);
  158. _set_setting_value("interface/editor/editor_language", selected_language);
  159. }
  160. #endif
  161. void QuickSettingsDialog::_theme_selected(int p_id) {
  162. const String selected_theme = theme_option_button->get_item_text(p_id);
  163. _set_setting_value("interface/theme/preset", selected_theme);
  164. custom_theme_label->set_visible(selected_theme == "Custom");
  165. }
  166. void QuickSettingsDialog::_scale_selected(int p_id) {
  167. _set_setting_value("interface/editor/display_scale", p_id, true);
  168. }
  169. void QuickSettingsDialog::_network_mode_selected(int p_id) {
  170. _set_setting_value("network/connection/network_mode", p_id);
  171. // Disables Check for Updates selection if Network mode is set to Offline.
  172. check_for_update_button->set_disabled(!p_id);
  173. }
  174. void QuickSettingsDialog::_check_for_update_selected(int p_id) {
  175. _set_setting_value("network/connection/check_for_updates", p_id);
  176. }
  177. void QuickSettingsDialog::_directory_naming_convention_selected(int p_id) {
  178. _set_setting_value("project_manager/directory_naming_convention", p_id);
  179. }
  180. void QuickSettingsDialog::_set_setting_value(const String &p_setting, const Variant &p_value, bool p_restart_required) {
  181. EditorSettings::get_singleton()->set(p_setting, p_value);
  182. EditorSettings::get_singleton()->notify_changes();
  183. EditorSettings::get_singleton()->save();
  184. if (p_restart_required) {
  185. restart_required_label->show();
  186. if (!restart_required_button) {
  187. int ed_swap_cancel_ok = EDITOR_GET("interface/editor/accept_dialog_cancel_ok_buttons");
  188. if (ed_swap_cancel_ok == 0) {
  189. ed_swap_cancel_ok = DisplayServer::get_singleton()->get_swap_cancel_ok() ? 2 : 1;
  190. }
  191. restart_required_button = add_button(TTRC("Restart Now"), ed_swap_cancel_ok != 2);
  192. restart_required_button->connect(SceneStringName(pressed), callable_mp(this, &QuickSettingsDialog::_request_restart));
  193. }
  194. }
  195. }
  196. void QuickSettingsDialog::_request_restart() {
  197. emit_signal("restart_required");
  198. }
  199. void QuickSettingsDialog::update_size_limits(const Size2 &p_max_popup_size) {
  200. #ifndef ANDROID_ENABLED
  201. language_option_button->get_popup()->set_max_size(p_max_popup_size);
  202. #endif
  203. }
  204. void QuickSettingsDialog::_notification(int p_what) {
  205. switch (p_what) {
  206. case NOTIFICATION_THEME_CHANGED: {
  207. settings_list_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("Background"), EditorStringName(EditorStyles)));
  208. restart_required_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
  209. custom_theme_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("font_placeholder_color"), EditorStringName(Editor)));
  210. } break;
  211. case NOTIFICATION_VISIBILITY_CHANGED: {
  212. if (is_visible()) {
  213. _update_current_values();
  214. }
  215. } break;
  216. }
  217. }
  218. void QuickSettingsDialog::_bind_methods() {
  219. ADD_SIGNAL(MethodInfo("restart_required"));
  220. }
  221. QuickSettingsDialog::QuickSettingsDialog() {
  222. set_title(TTRC("Quick Settings"));
  223. set_ok_button_text(TTRC("Close"));
  224. VBoxContainer *main_vbox = memnew(VBoxContainer);
  225. add_child(main_vbox);
  226. main_vbox->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  227. // Settings grid.
  228. {
  229. _fetch_setting_values();
  230. settings_list_panel = memnew(PanelContainer);
  231. main_vbox->add_child(settings_list_panel);
  232. settings_list = memnew(VBoxContainer);
  233. settings_list_panel->add_child(settings_list);
  234. #ifndef ANDROID_ENABLED
  235. // Language options.
  236. {
  237. language_option_button = memnew(OptionButton);
  238. language_option_button->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  239. language_option_button->set_fit_to_longest_item(false);
  240. language_option_button->connect(SceneStringName(item_selected), callable_mp(this, &QuickSettingsDialog::_language_selected));
  241. for (int i = 0; i < editor_languages.size(); i++) {
  242. const String &lang_value = editor_languages[i];
  243. String lang_name = TranslationServer::get_singleton()->get_locale_name(lang_value);
  244. language_option_button->add_item(vformat("[%s] %s", lang_value, lang_name), i);
  245. language_option_button->set_item_metadata(i, lang_value);
  246. }
  247. _add_setting_control(TTRC("Language"), language_option_button);
  248. }
  249. #endif
  250. // Theme options.
  251. {
  252. theme_option_button = memnew(OptionButton);
  253. theme_option_button->set_fit_to_longest_item(false);
  254. theme_option_button->connect(SceneStringName(item_selected), callable_mp(this, &QuickSettingsDialog::_theme_selected));
  255. for (int i = 0; i < editor_themes.size(); i++) {
  256. const String &theme_value = editor_themes[i];
  257. theme_option_button->add_item(theme_value, i);
  258. }
  259. _add_setting_control(TTRC("Interface Theme"), theme_option_button);
  260. custom_theme_label = memnew(Label(TTRC("Custom preset can be further configured in the editor.")));
  261. custom_theme_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  262. custom_theme_label->set_custom_minimum_size(Size2(220, 0) * EDSCALE);
  263. custom_theme_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD);
  264. custom_theme_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  265. custom_theme_label->set_stretch_ratio(2.0);
  266. custom_theme_label->hide();
  267. settings_list->add_child(custom_theme_label);
  268. }
  269. // Scale options.
  270. {
  271. scale_option_button = memnew(OptionButton);
  272. scale_option_button->set_fit_to_longest_item(false);
  273. scale_option_button->connect(SceneStringName(item_selected), callable_mp(this, &QuickSettingsDialog::_scale_selected));
  274. for (int i = 0; i < editor_scales.size(); i++) {
  275. const String &scale_value = editor_scales[i];
  276. scale_option_button->add_item(scale_value, i);
  277. }
  278. _add_setting_control(TTRC("Display Scale"), scale_option_button);
  279. }
  280. // Network mode options.
  281. {
  282. network_mode_option_button = memnew(OptionButton);
  283. network_mode_option_button->set_fit_to_longest_item(false);
  284. network_mode_option_button->connect(SceneStringName(item_selected), callable_mp(this, &QuickSettingsDialog::_network_mode_selected));
  285. for (int i = 0; i < editor_network_modes.size(); i++) {
  286. const String &network_mode_value = editor_network_modes[i];
  287. network_mode_option_button->add_item(network_mode_value, i);
  288. }
  289. _add_setting_control(TTRC("Network Mode"), network_mode_option_button);
  290. }
  291. // Check for updates options.
  292. {
  293. check_for_update_button = memnew(OptionButton);
  294. check_for_update_button->set_fit_to_longest_item(false);
  295. check_for_update_button->connect(SceneStringName(item_selected), callable_mp(this, &QuickSettingsDialog::_check_for_update_selected));
  296. for (int i = 0; i < editor_check_for_updates.size(); i++) {
  297. const String &check_for_update_value = editor_check_for_updates[i];
  298. check_for_update_button->add_item(check_for_update_value, i);
  299. }
  300. _add_setting_control(TTRC("Check for Updates"), check_for_update_button);
  301. }
  302. // Project directory naming options.
  303. {
  304. directory_naming_convention_button = memnew(OptionButton);
  305. directory_naming_convention_button->set_fit_to_longest_item(false);
  306. directory_naming_convention_button->connect(SceneStringName(item_selected), callable_mp(this, &QuickSettingsDialog::_directory_naming_convention_selected));
  307. for (int i = 0; i < editor_directory_naming_conventions.size(); i++) {
  308. const String &directory_naming_convention = editor_directory_naming_conventions[i];
  309. directory_naming_convention_button->add_item(directory_naming_convention, i);
  310. }
  311. _add_setting_control(TTRC("Directory Naming Convention"), directory_naming_convention_button);
  312. }
  313. _update_current_values();
  314. }
  315. // Restart required panel.
  316. {
  317. restart_required_label = memnew(Label(TTRC("Settings changed! The project manager must be restarted for changes to take effect.")));
  318. restart_required_label->set_custom_minimum_size(Size2(560, 0) * EDSCALE);
  319. restart_required_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD);
  320. restart_required_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  321. restart_required_label->hide();
  322. main_vbox->add_child(restart_required_label);
  323. }
  324. }