quick_settings_dialog.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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/config/project_settings.h"
  32. #include "core/string/translation.h"
  33. #include "editor/editor_settings.h"
  34. #include "editor/editor_string_names.h"
  35. #include "editor/themes/editor_scale.h"
  36. #include "scene/gui/box_container.h"
  37. #include "scene/gui/button.h"
  38. #include "scene/gui/label.h"
  39. #include "scene/gui/margin_container.h"
  40. #include "scene/gui/option_button.h"
  41. #include "scene/gui/panel_container.h"
  42. void QuickSettingsDialog::_fetch_setting_values() {
  43. editor_languages.clear();
  44. editor_themes.clear();
  45. editor_scales.clear();
  46. editor_network_modes.clear();
  47. {
  48. List<PropertyInfo> editor_settings_properties;
  49. EditorSettings::get_singleton()->get_property_list(&editor_settings_properties);
  50. for (const PropertyInfo &pi : editor_settings_properties) {
  51. if (pi.name == "interface/editor/editor_language") {
  52. editor_languages = pi.hint_string.split(",");
  53. } else if (pi.name == "interface/theme/preset") {
  54. editor_themes = pi.hint_string.split(",");
  55. } else if (pi.name == "interface/editor/display_scale") {
  56. editor_scales = pi.hint_string.split(",");
  57. } else if (pi.name == "network/connection/network_mode") {
  58. editor_network_modes = pi.hint_string.split(",");
  59. }
  60. }
  61. }
  62. }
  63. void QuickSettingsDialog::_update_current_values() {
  64. // Language options.
  65. {
  66. const String current_lang = EDITOR_GET("interface/editor/editor_language");
  67. for (int i = 0; i < editor_languages.size(); i++) {
  68. const String &lang_value = editor_languages[i];
  69. if (current_lang == lang_value) {
  70. language_option_button->set_text(current_lang);
  71. language_option_button->select(i);
  72. }
  73. }
  74. }
  75. // Theme options.
  76. {
  77. const String current_theme = EDITOR_GET("interface/theme/preset");
  78. for (int i = 0; i < editor_themes.size(); i++) {
  79. const String &theme_value = editor_themes[i];
  80. if (current_theme == theme_value) {
  81. theme_option_button->set_text(current_theme);
  82. theme_option_button->select(i);
  83. custom_theme_label->set_visible(current_theme == "Custom");
  84. }
  85. }
  86. }
  87. // Scale options.
  88. {
  89. const int current_scale = EDITOR_GET("interface/editor/display_scale");
  90. for (int i = 0; i < editor_scales.size(); i++) {
  91. const String &scale_value = editor_scales[i];
  92. if (current_scale == i) {
  93. scale_option_button->set_text(scale_value);
  94. scale_option_button->select(i);
  95. }
  96. }
  97. }
  98. // Network mode options.
  99. {
  100. const int current_network_mode = EDITOR_GET("network/connection/network_mode");
  101. for (int i = 0; i < editor_network_modes.size(); i++) {
  102. const String &network_mode_value = editor_network_modes[i];
  103. if (current_network_mode == i) {
  104. network_mode_option_button->set_text(network_mode_value);
  105. network_mode_option_button->select(i);
  106. }
  107. }
  108. }
  109. }
  110. void QuickSettingsDialog::_add_setting_control(const String &p_text, Control *p_control) {
  111. HBoxContainer *container = memnew(HBoxContainer);
  112. settings_list->add_child(container);
  113. Label *label = memnew(Label(p_text));
  114. label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  115. container->add_child(label);
  116. p_control->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  117. p_control->set_stretch_ratio(2.0);
  118. container->add_child(p_control);
  119. }
  120. void QuickSettingsDialog::_language_selected(int p_id) {
  121. const String selected_language = language_option_button->get_item_metadata(p_id);
  122. _set_setting_value("interface/editor/editor_language", selected_language, true);
  123. }
  124. void QuickSettingsDialog::_theme_selected(int p_id) {
  125. const String selected_theme = theme_option_button->get_item_text(p_id);
  126. _set_setting_value("interface/theme/preset", selected_theme);
  127. custom_theme_label->set_visible(selected_theme == "Custom");
  128. }
  129. void QuickSettingsDialog::_scale_selected(int p_id) {
  130. _set_setting_value("interface/editor/display_scale", p_id, true);
  131. }
  132. void QuickSettingsDialog::_network_mode_selected(int p_id) {
  133. _set_setting_value("network/connection/network_mode", p_id);
  134. }
  135. void QuickSettingsDialog::_set_setting_value(const String &p_setting, const Variant &p_value, bool p_restart_required) {
  136. EditorSettings::get_singleton()->set(p_setting, p_value);
  137. EditorSettings::get_singleton()->notify_changes();
  138. EditorSettings::get_singleton()->save();
  139. if (p_restart_required) {
  140. restart_required_label->show();
  141. if (!restart_required_button) {
  142. restart_required_button = add_button(TTR("Restart Now"), !GLOBAL_GET("gui/common/swap_cancel_ok"));
  143. restart_required_button->connect("pressed", callable_mp(this, &QuickSettingsDialog::_request_restart));
  144. }
  145. }
  146. }
  147. void QuickSettingsDialog::_request_restart() {
  148. emit_signal("restart_required");
  149. }
  150. void QuickSettingsDialog::update_size_limits(const Size2 &p_max_popup_size) {
  151. language_option_button->get_popup()->set_max_size(p_max_popup_size);
  152. }
  153. void QuickSettingsDialog::_notification(int p_what) {
  154. switch (p_what) {
  155. case NOTIFICATION_THEME_CHANGED: {
  156. settings_list_panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("Background"), EditorStringName(EditorStyles)));
  157. restart_required_label->add_theme_color_override("font_color", get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
  158. custom_theme_label->add_theme_color_override("font_color", get_theme_color(SNAME("font_placeholder_color"), EditorStringName(Editor)));
  159. } break;
  160. case NOTIFICATION_VISIBILITY_CHANGED: {
  161. if (is_visible()) {
  162. _update_current_values();
  163. }
  164. } break;
  165. }
  166. }
  167. void QuickSettingsDialog::_bind_methods() {
  168. ADD_SIGNAL(MethodInfo("restart_required"));
  169. }
  170. QuickSettingsDialog::QuickSettingsDialog() {
  171. set_title(TTR("Quick Settings"));
  172. set_ok_button_text(TTR("Close"));
  173. VBoxContainer *main_vbox = memnew(VBoxContainer);
  174. add_child(main_vbox);
  175. main_vbox->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  176. // Settings grid.
  177. {
  178. _fetch_setting_values();
  179. settings_list_panel = memnew(PanelContainer);
  180. main_vbox->add_child(settings_list_panel);
  181. settings_list = memnew(VBoxContainer);
  182. settings_list_panel->add_child(settings_list);
  183. // Language options.
  184. {
  185. language_option_button = memnew(OptionButton);
  186. language_option_button->set_fit_to_longest_item(false);
  187. language_option_button->connect("item_selected", callable_mp(this, &QuickSettingsDialog::_language_selected));
  188. for (int i = 0; i < editor_languages.size(); i++) {
  189. const String &lang_value = editor_languages[i];
  190. String lang_name = TranslationServer::get_singleton()->get_locale_name(lang_value);
  191. language_option_button->add_item(vformat("[%s] %s", lang_value, lang_name), i);
  192. language_option_button->set_item_metadata(i, lang_value);
  193. }
  194. _add_setting_control(TTR("Language"), language_option_button);
  195. }
  196. // Theme options.
  197. {
  198. theme_option_button = memnew(OptionButton);
  199. theme_option_button->set_fit_to_longest_item(false);
  200. theme_option_button->connect("item_selected", callable_mp(this, &QuickSettingsDialog::_theme_selected));
  201. for (int i = 0; i < editor_themes.size(); i++) {
  202. const String &theme_value = editor_themes[i];
  203. theme_option_button->add_item(theme_value, i);
  204. }
  205. _add_setting_control(TTR("Interface Theme"), theme_option_button);
  206. custom_theme_label = memnew(Label(TTR("Custom preset can be further configured in the editor.")));
  207. custom_theme_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  208. custom_theme_label->set_custom_minimum_size(Size2(220, 0) * EDSCALE);
  209. custom_theme_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD);
  210. custom_theme_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  211. custom_theme_label->set_stretch_ratio(2.0);
  212. custom_theme_label->hide();
  213. settings_list->add_child(custom_theme_label);
  214. }
  215. // Scale options.
  216. {
  217. scale_option_button = memnew(OptionButton);
  218. scale_option_button->set_fit_to_longest_item(false);
  219. scale_option_button->connect("item_selected", callable_mp(this, &QuickSettingsDialog::_scale_selected));
  220. for (int i = 0; i < editor_scales.size(); i++) {
  221. const String &scale_value = editor_scales[i];
  222. scale_option_button->add_item(scale_value, i);
  223. }
  224. _add_setting_control(TTR("Display Scale"), scale_option_button);
  225. }
  226. // Network mode options.
  227. {
  228. network_mode_option_button = memnew(OptionButton);
  229. network_mode_option_button->set_fit_to_longest_item(false);
  230. network_mode_option_button->connect("item_selected", callable_mp(this, &QuickSettingsDialog::_network_mode_selected));
  231. for (int i = 0; i < editor_network_modes.size(); i++) {
  232. const String &network_mode_value = editor_network_modes[i];
  233. network_mode_option_button->add_item(network_mode_value, i);
  234. }
  235. _add_setting_control(TTR("Network Mode"), network_mode_option_button);
  236. }
  237. _update_current_values();
  238. #ifdef ANDROID_ENABLED
  239. // The language selection dropdown doesn't work on Android (as the setting isn't saved), see GH-60353.
  240. // Also, the dropdown it spawns is very tall and can't be scrolled without a hardware mouse.
  241. language_option_button->hide();
  242. scale_option_button->hide();
  243. #endif
  244. }
  245. // Restart required panel.
  246. {
  247. restart_required_label = memnew(Label(TTR("Settings changed! The project manager must be restarted for changes to take effect.")));
  248. restart_required_label->set_custom_minimum_size(Size2(560, 0) * EDSCALE);
  249. restart_required_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD);
  250. restart_required_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  251. restart_required_label->hide();
  252. main_vbox->add_child(restart_required_label);
  253. }
  254. }