quick_settings_dialog.cpp 13 KB

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