quick_settings_dialog.cpp 16 KB

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