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