Path: blob/master/editor/project_manager/quick_settings_dialog.cpp
9903 views
/**************************************************************************/1/* quick_settings_dialog.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "quick_settings_dialog.h"3132#include "core/string/translation_server.h"33#include "editor/editor_string_names.h"34#include "editor/settings/editor_settings.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/option_button.h"40#include "scene/gui/panel_container.h"4142void QuickSettingsDialog::_fetch_setting_values() {43#ifndef ANDROID_ENABLED44editor_languages.clear();45#endif46editor_themes.clear();47editor_scales.clear();48editor_network_modes.clear();49editor_check_for_updates.clear();50editor_directory_naming_conventions.clear();5152{53List<PropertyInfo> editor_settings_properties;54EditorSettings::get_singleton()->get_property_list(&editor_settings_properties);5556for (const PropertyInfo &pi : editor_settings_properties) {57if (pi.name == "interface/editor/editor_language") {58#ifndef ANDROID_ENABLED59editor_languages = pi.hint_string.split(",");60#endif61} else if (pi.name == "interface/theme/preset") {62editor_themes = pi.hint_string.split(",");63} else if (pi.name == "interface/editor/display_scale") {64editor_scales = pi.hint_string.split(",");65} else if (pi.name == "network/connection/network_mode") {66editor_network_modes = pi.hint_string.split(",");67} else if (pi.name == "network/connection/check_for_updates") {68editor_check_for_updates = pi.hint_string.split(",");69} else if (pi.name == "project_manager/directory_naming_convention") {70editor_directory_naming_conventions = pi.hint_string.split(",");71}72}73}74}7576void QuickSettingsDialog::_update_current_values() {77#ifndef ANDROID_ENABLED78// Language options.79{80const String current_lang = EDITOR_GET("interface/editor/editor_language");8182for (int i = 0; i < editor_languages.size(); i++) {83const String &lang_value = editor_languages[i];84if (current_lang == lang_value) {85language_option_button->set_text(current_lang);86language_option_button->select(i);87}88}89}90#endif9192// Theme options.93{94const String current_theme = EDITOR_GET("interface/theme/preset");9596for (int i = 0; i < editor_themes.size(); i++) {97const String &theme_value = editor_themes[i];98if (current_theme == theme_value) {99theme_option_button->set_text(current_theme);100theme_option_button->select(i);101theme_option_button->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);102103custom_theme_label->set_visible(current_theme == "Custom");104}105}106}107108// Scale options.109{110const int current_scale = EDITOR_GET("interface/editor/display_scale");111112for (int i = 0; i < editor_scales.size(); i++) {113const String &scale_value = editor_scales[i];114if (current_scale == i) {115scale_option_button->set_text(scale_value);116scale_option_button->select(i);117}118}119}120121// Network mode options.122{123const int current_network_mode = EDITOR_GET("network/connection/network_mode");124125for (int i = 0; i < editor_network_modes.size(); i++) {126const String &network_mode_value = editor_network_modes[i];127if (current_network_mode == i) {128network_mode_option_button->set_text(network_mode_value);129network_mode_option_button->select(i);130}131}132}133134// Check for updates options.135{136const int current_update_mode = EDITOR_GET("network/connection/check_for_updates");137138for (int i = 0; i < editor_check_for_updates.size(); i++) {139const String &check_for_update_value = editor_check_for_updates[i];140if (current_update_mode == i) {141check_for_update_button->set_text(check_for_update_value);142check_for_update_button->select(i);143144// Disables Check for Updates selection if Network mode is set to Offline.145check_for_update_button->set_disabled(!EDITOR_GET("network/connection/network_mode"));146}147}148}149150// Project directory naming options.151{152const int current_directory_naming = EDITOR_GET("project_manager/directory_naming_convention");153154for (int i = 0; i < editor_directory_naming_conventions.size(); i++) {155const String &directory_naming_value = editor_directory_naming_conventions[i];156if (current_directory_naming == i) {157directory_naming_convention_button->set_text(directory_naming_value);158directory_naming_convention_button->select(i);159}160}161}162}163164void QuickSettingsDialog::_add_setting_control(const String &p_text, Control *p_control) {165HBoxContainer *container = memnew(HBoxContainer);166settings_list->add_child(container);167168Label *label = memnew(Label(p_text));169label->set_h_size_flags(Control::SIZE_EXPAND_FILL);170container->add_child(label);171172p_control->set_h_size_flags(Control::SIZE_EXPAND_FILL);173p_control->set_stretch_ratio(2.0);174container->add_child(p_control);175}176177#ifndef ANDROID_ENABLED178void QuickSettingsDialog::_language_selected(int p_id) {179const String selected_language = language_option_button->get_item_metadata(p_id);180_set_setting_value("interface/editor/editor_language", selected_language);181}182#endif183184void QuickSettingsDialog::_theme_selected(int p_id) {185const String selected_theme = theme_option_button->get_item_text(p_id);186_set_setting_value("interface/theme/preset", selected_theme);187188custom_theme_label->set_visible(selected_theme == "Custom");189}190191void QuickSettingsDialog::_scale_selected(int p_id) {192_set_setting_value("interface/editor/display_scale", p_id, true);193}194195void QuickSettingsDialog::_network_mode_selected(int p_id) {196_set_setting_value("network/connection/network_mode", p_id);197198// Disables Check for Updates selection if Network mode is set to Offline.199check_for_update_button->set_disabled(!p_id);200}201202void QuickSettingsDialog::_check_for_update_selected(int p_id) {203_set_setting_value("network/connection/check_for_updates", p_id);204}205206void QuickSettingsDialog::_directory_naming_convention_selected(int p_id) {207_set_setting_value("project_manager/directory_naming_convention", p_id);208}209210void QuickSettingsDialog::_set_setting_value(const String &p_setting, const Variant &p_value, bool p_restart_required) {211EditorSettings::get_singleton()->set(p_setting, p_value);212EditorSettings::get_singleton()->notify_changes();213EditorSettings::get_singleton()->save();214215if (p_restart_required) {216restart_required_label->show();217218if (!restart_required_button) {219int ed_swap_cancel_ok = EDITOR_GET("interface/editor/accept_dialog_cancel_ok_buttons");220if (ed_swap_cancel_ok == 0) {221ed_swap_cancel_ok = DisplayServer::get_singleton()->get_swap_cancel_ok() ? 2 : 1;222}223restart_required_button = add_button(TTRC("Restart Now"), ed_swap_cancel_ok != 2);224restart_required_button->connect(SceneStringName(pressed), callable_mp(this, &QuickSettingsDialog::_request_restart));225}226}227}228229void QuickSettingsDialog::_request_restart() {230emit_signal("restart_required");231}232233void QuickSettingsDialog::update_size_limits(const Size2 &p_max_popup_size) {234#ifndef ANDROID_ENABLED235language_option_button->get_popup()->set_max_size(p_max_popup_size);236#endif237}238239void QuickSettingsDialog::_notification(int p_what) {240switch (p_what) {241case NOTIFICATION_THEME_CHANGED: {242settings_list_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("Background"), EditorStringName(EditorStyles)));243244restart_required_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));245custom_theme_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("font_placeholder_color"), EditorStringName(Editor)));246} break;247248case NOTIFICATION_VISIBILITY_CHANGED: {249if (is_visible()) {250_update_current_values();251}252} break;253}254}255256void QuickSettingsDialog::_bind_methods() {257ADD_SIGNAL(MethodInfo("restart_required"));258}259260QuickSettingsDialog::QuickSettingsDialog() {261set_title(TTRC("Quick Settings"));262set_ok_button_text(TTRC("Close"));263264VBoxContainer *main_vbox = memnew(VBoxContainer);265add_child(main_vbox);266main_vbox->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);267268// Settings grid.269{270_fetch_setting_values();271272settings_list_panel = memnew(PanelContainer);273main_vbox->add_child(settings_list_panel);274275settings_list = memnew(VBoxContainer);276settings_list_panel->add_child(settings_list);277278#ifndef ANDROID_ENABLED279// Language options.280{281language_option_button = memnew(OptionButton);282language_option_button->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);283language_option_button->set_fit_to_longest_item(false);284language_option_button->connect(SceneStringName(item_selected), callable_mp(this, &QuickSettingsDialog::_language_selected));285286for (int i = 0; i < editor_languages.size(); i++) {287const String &lang_value = editor_languages[i];288String lang_name = TranslationServer::get_singleton()->get_locale_name(lang_value);289language_option_button->add_item(vformat("[%s] %s", lang_value, lang_name), i);290language_option_button->set_item_metadata(i, lang_value);291}292293_add_setting_control(TTRC("Language"), language_option_button);294}295#endif296297// Theme options.298{299theme_option_button = memnew(OptionButton);300theme_option_button->set_fit_to_longest_item(false);301theme_option_button->connect(SceneStringName(item_selected), callable_mp(this, &QuickSettingsDialog::_theme_selected));302303for (int i = 0; i < editor_themes.size(); i++) {304const String &theme_value = editor_themes[i];305theme_option_button->add_item(theme_value, i);306}307308_add_setting_control(TTRC("Interface Theme"), theme_option_button);309310custom_theme_label = memnew(Label(TTRC("Custom preset can be further configured in the editor.")));311custom_theme_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);312custom_theme_label->set_custom_minimum_size(Size2(220, 0) * EDSCALE);313custom_theme_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD);314custom_theme_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);315custom_theme_label->set_stretch_ratio(2.0);316custom_theme_label->hide();317settings_list->add_child(custom_theme_label);318}319320// Scale options.321{322scale_option_button = memnew(OptionButton);323scale_option_button->set_fit_to_longest_item(false);324scale_option_button->connect(SceneStringName(item_selected), callable_mp(this, &QuickSettingsDialog::_scale_selected));325326for (int i = 0; i < editor_scales.size(); i++) {327const String &scale_value = editor_scales[i];328scale_option_button->add_item(scale_value, i);329}330331_add_setting_control(TTRC("Display Scale"), scale_option_button);332}333334// Network mode options.335{336network_mode_option_button = memnew(OptionButton);337network_mode_option_button->set_fit_to_longest_item(false);338network_mode_option_button->connect(SceneStringName(item_selected), callable_mp(this, &QuickSettingsDialog::_network_mode_selected));339340for (int i = 0; i < editor_network_modes.size(); i++) {341const String &network_mode_value = editor_network_modes[i];342network_mode_option_button->add_item(network_mode_value, i);343}344345_add_setting_control(TTRC("Network Mode"), network_mode_option_button);346}347348// Check for updates options.349{350check_for_update_button = memnew(OptionButton);351check_for_update_button->set_fit_to_longest_item(false);352check_for_update_button->connect(SceneStringName(item_selected), callable_mp(this, &QuickSettingsDialog::_check_for_update_selected));353354for (int i = 0; i < editor_check_for_updates.size(); i++) {355const String &check_for_update_value = editor_check_for_updates[i];356check_for_update_button->add_item(check_for_update_value, i);357}358359_add_setting_control(TTRC("Check for Updates"), check_for_update_button);360}361362// Project directory naming options.363{364directory_naming_convention_button = memnew(OptionButton);365directory_naming_convention_button->set_fit_to_longest_item(false);366directory_naming_convention_button->connect(SceneStringName(item_selected), callable_mp(this, &QuickSettingsDialog::_directory_naming_convention_selected));367368for (int i = 0; i < editor_directory_naming_conventions.size(); i++) {369const String &directory_naming_convention = editor_directory_naming_conventions[i];370directory_naming_convention_button->add_item(directory_naming_convention, i);371}372373_add_setting_control(TTRC("Directory Naming Convention"), directory_naming_convention_button);374}375376_update_current_values();377}378379// Restart required panel.380{381restart_required_label = memnew(Label(TTRC("Settings changed! The project manager must be restarted for changes to take effect.")));382restart_required_label->set_custom_minimum_size(Size2(560, 0) * EDSCALE);383restart_required_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD);384restart_required_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);385restart_required_label->hide();386main_vbox->add_child(restart_required_label);387}388}389390391