Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/gui/dialogs.cpp
9903 views
1
/**************************************************************************/
2
/* dialogs.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 "dialogs.h"
32
#include "dialogs.compat.inc"
33
34
#include "scene/gui/line_edit.h"
35
#include "scene/theme/theme_db.h"
36
37
// AcceptDialog
38
39
void AcceptDialog::_input_from_window(const Ref<InputEvent> &p_event) {
40
if (close_on_escape && p_event->is_action_pressed(SNAME("ui_cancel"), false, true)) {
41
_cancel_pressed();
42
}
43
Window::_input_from_window(p_event);
44
}
45
46
void AcceptDialog::_parent_focused() {
47
if (popped_up && !is_exclusive() && get_flag(FLAG_POPUP)) {
48
_cancel_pressed();
49
}
50
}
51
52
void AcceptDialog::_notification(int p_what) {
53
switch (p_what) {
54
case NOTIFICATION_ACCESSIBILITY_UPDATE: {
55
RID ae = get_accessibility_element();
56
ERR_FAIL_COND(ae.is_null());
57
58
DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_DIALOG);
59
} break;
60
case NOTIFICATION_POST_ENTER_TREE: {
61
if (is_visible()) {
62
get_ok_button()->grab_focus();
63
}
64
} break;
65
case NOTIFICATION_VISIBILITY_CHANGED: {
66
if (is_visible()) {
67
if (get_ok_button()->is_inside_tree()) {
68
get_ok_button()->grab_focus();
69
}
70
_update_child_rects();
71
72
parent_visible = get_parent_visible_window();
73
if (parent_visible) {
74
parent_visible->connect(SceneStringName(focus_entered), callable_mp(this, &AcceptDialog::_parent_focused));
75
}
76
} else {
77
popped_up = false;
78
if (parent_visible) {
79
parent_visible->disconnect(SceneStringName(focus_entered), callable_mp(this, &AcceptDialog::_parent_focused));
80
parent_visible = nullptr;
81
}
82
}
83
} break;
84
85
case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
86
if (!is_in_edited_scene_root()) {
87
if (has_focus()) {
88
popped_up = true;
89
}
90
}
91
} break;
92
93
case NOTIFICATION_THEME_CHANGED: {
94
bg_panel->add_theme_style_override(SceneStringName(panel), theme_cache.panel_style);
95
96
child_controls_changed();
97
if (is_visible()) {
98
_update_child_rects();
99
}
100
} break;
101
102
case NOTIFICATION_EXIT_TREE: {
103
if (parent_visible) {
104
parent_visible->disconnect(SceneStringName(focus_entered), callable_mp(this, &AcceptDialog::_parent_focused));
105
parent_visible = nullptr;
106
}
107
} break;
108
109
case NOTIFICATION_READY:
110
case NOTIFICATION_WM_SIZE_CHANGED: {
111
if (is_visible()) {
112
_update_child_rects();
113
}
114
} break;
115
116
case NOTIFICATION_WM_CLOSE_REQUEST: {
117
_cancel_pressed();
118
} break;
119
}
120
}
121
122
void AcceptDialog::_text_submitted(const String &p_text) {
123
if (get_ok_button() && get_ok_button()->is_disabled()) {
124
return; // Do not allow submission if OK button is disabled.
125
}
126
_ok_pressed();
127
}
128
129
void AcceptDialog::_post_popup() {
130
Window::_post_popup();
131
popped_up = true;
132
}
133
134
void AcceptDialog::_ok_pressed() {
135
if (hide_on_ok) {
136
popped_up = false;
137
set_visible(false);
138
}
139
ok_pressed();
140
emit_signal(SceneStringName(confirmed));
141
set_input_as_handled();
142
}
143
144
void AcceptDialog::_cancel_pressed() {
145
popped_up = false;
146
Window *parent_window = parent_visible;
147
if (parent_visible) {
148
parent_visible->disconnect(SceneStringName(focus_entered), callable_mp(this, &AcceptDialog::_parent_focused));
149
parent_visible = nullptr;
150
}
151
152
callable_mp((Window *)this, &Window::hide).call_deferred();
153
154
emit_signal(SNAME("canceled"));
155
156
cancel_pressed();
157
158
if (parent_window) {
159
//parent_window->grab_focus();
160
}
161
set_input_as_handled();
162
}
163
164
String AcceptDialog::get_text() const {
165
return message_label->get_text();
166
}
167
168
void AcceptDialog::set_text(String p_text) {
169
if (message_label->get_text() == p_text) {
170
return;
171
}
172
173
message_label->set_text(p_text);
174
175
child_controls_changed();
176
if (is_visible()) {
177
_update_child_rects();
178
}
179
}
180
181
void AcceptDialog::set_hide_on_ok(bool p_hide) {
182
hide_on_ok = p_hide;
183
}
184
185
bool AcceptDialog::get_hide_on_ok() const {
186
return hide_on_ok;
187
}
188
189
void AcceptDialog::set_close_on_escape(bool p_hide) {
190
close_on_escape = p_hide;
191
}
192
193
bool AcceptDialog::get_close_on_escape() const {
194
return close_on_escape;
195
}
196
197
void AcceptDialog::set_autowrap(bool p_autowrap) {
198
message_label->set_autowrap_mode(p_autowrap ? TextServer::AUTOWRAP_WORD : TextServer::AUTOWRAP_OFF);
199
}
200
201
bool AcceptDialog::has_autowrap() {
202
return message_label->get_autowrap_mode() != TextServer::AUTOWRAP_OFF;
203
}
204
205
void AcceptDialog::set_ok_button_text(String p_ok_button_text) {
206
ok_text = p_ok_button_text;
207
_update_ok_text();
208
}
209
210
String AcceptDialog::get_ok_button_text() const {
211
return ok_text;
212
}
213
214
void AcceptDialog::register_text_enter(LineEdit *p_line_edit) {
215
ERR_FAIL_NULL(p_line_edit);
216
p_line_edit->connect(SceneStringName(text_submitted), callable_mp(this, &AcceptDialog::_text_submitted));
217
}
218
219
void AcceptDialog::_update_child_rects() {
220
Size2 dlg_size = Vector2(get_size()) / get_content_scale_factor();
221
float h_margins = theme_cache.panel_style->get_margin(SIDE_LEFT) + theme_cache.panel_style->get_margin(SIDE_RIGHT);
222
float v_margins = theme_cache.panel_style->get_margin(SIDE_TOP) + theme_cache.panel_style->get_margin(SIDE_BOTTOM);
223
224
// Fill the entire size of the window with the background.
225
bg_panel->set_position(Point2());
226
bg_panel->set_size(dlg_size);
227
228
for (int i = 0; i < buttons_hbox->get_child_count(); i++) {
229
Button *b = Object::cast_to<Button>(buttons_hbox->get_child(i));
230
if (!b) {
231
continue;
232
}
233
234
b->set_custom_minimum_size(Size2(theme_cache.buttons_min_width, theme_cache.buttons_min_height));
235
}
236
237
// Place the buttons from the bottom edge to their minimum required size.
238
Size2 buttons_minsize = buttons_hbox->get_combined_minimum_size();
239
Size2 buttons_size = Size2(dlg_size.x - h_margins, buttons_minsize.y);
240
Point2 buttons_position = Point2(theme_cache.panel_style->get_margin(SIDE_LEFT), dlg_size.y - theme_cache.panel_style->get_margin(SIDE_BOTTOM) - buttons_size.y);
241
buttons_hbox->set_position(buttons_position);
242
buttons_hbox->set_size(buttons_size);
243
244
// Place the content from the top to fill the rest of the space (minus the separation).
245
Point2 content_position = Point2(theme_cache.panel_style->get_margin(SIDE_LEFT), theme_cache.panel_style->get_margin(SIDE_TOP));
246
Size2 content_size = Size2(dlg_size.x - h_margins, dlg_size.y - v_margins - buttons_size.y - theme_cache.buttons_separation);
247
248
for (int i = 0; i < get_child_count(); i++) {
249
Control *c = Object::cast_to<Control>(get_child(i));
250
if (!c) {
251
continue;
252
}
253
if (c == buttons_hbox || c == bg_panel || c->is_set_as_top_level()) {
254
continue;
255
}
256
257
c->set_position(content_position);
258
c->set_size(content_size);
259
}
260
}
261
262
void AcceptDialog::_update_ok_text() {
263
String prev_text = ok_button->get_text();
264
String new_text = default_ok_text;
265
266
if (!ok_text.is_empty()) {
267
new_text = ok_text;
268
}
269
270
if (new_text == prev_text) {
271
return;
272
}
273
ok_button->set_text(new_text);
274
275
child_controls_changed();
276
if (is_visible()) {
277
_update_child_rects();
278
}
279
}
280
281
Size2 AcceptDialog::_get_contents_minimum_size() const {
282
// First, we then iterate over the label and any other custom controls
283
// to try and find the size that encompasses all content.
284
Size2 content_minsize;
285
for (int i = 0; i < get_child_count(); i++) {
286
Control *c = Object::cast_to<Control>(get_child(i));
287
if (!c) {
288
continue;
289
}
290
291
// Buttons will be included afterwards.
292
// The panel only displays the stylebox and doesn't contribute to the size.
293
if (c == buttons_hbox || c == bg_panel || c->is_set_as_top_level()) {
294
continue;
295
}
296
297
Size2 child_minsize = c->get_combined_minimum_size();
298
content_minsize = child_minsize.max(content_minsize);
299
}
300
301
// Then we add buttons. Horizontally we're interested in whichever
302
// value is the biggest. Vertically buttons add to the overall size.
303
Size2 buttons_minsize = buttons_hbox->get_combined_minimum_size();
304
content_minsize.x = MAX(buttons_minsize.x, content_minsize.x);
305
content_minsize.y += buttons_minsize.y;
306
// Plus there is a separation size added on top.
307
content_minsize.y += theme_cache.buttons_separation;
308
309
// Then we take the background panel as it provides the offsets,
310
// which are always added to the minimum size.
311
if (theme_cache.panel_style.is_valid()) {
312
content_minsize += theme_cache.panel_style->get_minimum_size();
313
}
314
315
return content_minsize;
316
}
317
318
void AcceptDialog::set_default_ok_text(const String &p_text) {
319
if (default_ok_text == p_text) {
320
return;
321
}
322
default_ok_text = p_text;
323
_update_ok_text();
324
notify_property_list_changed();
325
}
326
327
void AcceptDialog::_custom_action(const String &p_action) {
328
emit_signal(SNAME("custom_action"), p_action);
329
custom_action(p_action);
330
}
331
332
void AcceptDialog::_button_visibility_changed(Button *button) {
333
Control *bound_spacer = Object::cast_to<Control>(button->get_meta("__bound_spacer"));
334
if (bound_spacer) {
335
bound_spacer->set_visible(button->is_visible());
336
}
337
}
338
339
Button *AcceptDialog::add_button(const String &p_text, bool p_right, const String &p_action) {
340
Button *button = memnew(Button);
341
button->set_text(p_text);
342
343
Control *bound_spacer;
344
if (p_right) {
345
buttons_hbox->add_child(button);
346
bound_spacer = buttons_hbox->add_spacer();
347
} else {
348
buttons_hbox->add_child(button);
349
buttons_hbox->move_child(button, 0);
350
bound_spacer = buttons_hbox->add_spacer(true);
351
}
352
button->set_meta("__bound_spacer", bound_spacer);
353
354
button->connect(SceneStringName(visibility_changed), callable_mp(this, &AcceptDialog::_button_visibility_changed).bind(button));
355
356
child_controls_changed();
357
if (is_visible()) {
358
_update_child_rects();
359
}
360
361
if (!p_action.is_empty()) {
362
button->connect(SceneStringName(pressed), callable_mp(this, &AcceptDialog::_custom_action).bind(p_action));
363
}
364
365
return button;
366
}
367
368
Button *AcceptDialog::add_cancel_button(const String &p_cancel) {
369
String c = p_cancel;
370
if (p_cancel.is_empty()) {
371
c = ETR("Cancel");
372
}
373
374
Button *b = swap_cancel_ok ? add_button(c, true) : add_button(c);
375
376
b->connect(SceneStringName(pressed), callable_mp(this, &AcceptDialog::_cancel_pressed));
377
378
return b;
379
}
380
381
void AcceptDialog::remove_button(Button *p_button) {
382
ERR_FAIL_NULL(p_button);
383
ERR_FAIL_COND_MSG(p_button->get_parent() != buttons_hbox, vformat("Cannot remove button %s as it does not belong to this dialog.", p_button->get_name()));
384
ERR_FAIL_COND_MSG(p_button == ok_button, "Cannot remove dialog's OK button.");
385
386
Control *bound_spacer = Object::cast_to<Control>(p_button->get_meta("__bound_spacer"));
387
if (bound_spacer) {
388
ERR_FAIL_COND_MSG(bound_spacer->get_parent() != buttons_hbox, vformat("Cannot remove button %s as its associated spacer does not belong to this dialog.", p_button->get_name()));
389
}
390
391
p_button->disconnect(SceneStringName(visibility_changed), callable_mp(this, &AcceptDialog::_button_visibility_changed));
392
if (p_button->is_connected(SceneStringName(pressed), callable_mp(this, &AcceptDialog::_custom_action))) {
393
p_button->disconnect(SceneStringName(pressed), callable_mp(this, &AcceptDialog::_custom_action));
394
}
395
if (p_button->is_connected(SceneStringName(pressed), callable_mp(this, &AcceptDialog::_cancel_pressed))) {
396
p_button->disconnect(SceneStringName(pressed), callable_mp(this, &AcceptDialog::_cancel_pressed));
397
}
398
399
if (bound_spacer) {
400
buttons_hbox->remove_child(bound_spacer);
401
p_button->remove_meta("__bound_spacer");
402
bound_spacer->queue_free();
403
}
404
buttons_hbox->remove_child(p_button);
405
406
child_controls_changed();
407
if (is_visible()) {
408
_update_child_rects();
409
}
410
}
411
412
void AcceptDialog::_bind_methods() {
413
ClassDB::bind_method(D_METHOD("get_ok_button"), &AcceptDialog::get_ok_button);
414
ClassDB::bind_method(D_METHOD("get_label"), &AcceptDialog::get_label);
415
ClassDB::bind_method(D_METHOD("set_hide_on_ok", "enabled"), &AcceptDialog::set_hide_on_ok);
416
ClassDB::bind_method(D_METHOD("get_hide_on_ok"), &AcceptDialog::get_hide_on_ok);
417
ClassDB::bind_method(D_METHOD("set_close_on_escape", "enabled"), &AcceptDialog::set_close_on_escape);
418
ClassDB::bind_method(D_METHOD("get_close_on_escape"), &AcceptDialog::get_close_on_escape);
419
ClassDB::bind_method(D_METHOD("add_button", "text", "right", "action"), &AcceptDialog::add_button, DEFVAL(false), DEFVAL(""));
420
ClassDB::bind_method(D_METHOD("add_cancel_button", "name"), &AcceptDialog::add_cancel_button);
421
ClassDB::bind_method(D_METHOD("remove_button", "button"), &AcceptDialog::remove_button);
422
ClassDB::bind_method(D_METHOD("register_text_enter", "line_edit"), &AcceptDialog::register_text_enter);
423
ClassDB::bind_method(D_METHOD("set_text", "text"), &AcceptDialog::set_text);
424
ClassDB::bind_method(D_METHOD("get_text"), &AcceptDialog::get_text);
425
ClassDB::bind_method(D_METHOD("set_autowrap", "autowrap"), &AcceptDialog::set_autowrap);
426
ClassDB::bind_method(D_METHOD("has_autowrap"), &AcceptDialog::has_autowrap);
427
ClassDB::bind_method(D_METHOD("set_ok_button_text", "text"), &AcceptDialog::set_ok_button_text);
428
ClassDB::bind_method(D_METHOD("get_ok_button_text"), &AcceptDialog::get_ok_button_text);
429
430
ADD_SIGNAL(MethodInfo("confirmed"));
431
ADD_SIGNAL(MethodInfo("canceled"));
432
ADD_SIGNAL(MethodInfo("custom_action", PropertyInfo(Variant::STRING_NAME, "action")));
433
434
ADD_PROPERTY(PropertyInfo(Variant::STRING, "ok_button_text"), "set_ok_button_text", "get_ok_button_text");
435
436
ADD_GROUP("Dialog", "dialog_");
437
ADD_PROPERTY(PropertyInfo(Variant::STRING, "dialog_text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
438
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dialog_hide_on_ok"), "set_hide_on_ok", "get_hide_on_ok");
439
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dialog_close_on_escape"), "set_close_on_escape", "get_close_on_escape");
440
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dialog_autowrap"), "set_autowrap", "has_autowrap");
441
442
BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, AcceptDialog, panel_style, "panel");
443
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, AcceptDialog, buttons_separation);
444
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, AcceptDialog, buttons_min_width);
445
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, AcceptDialog, buttons_min_height);
446
447
ADD_CLASS_DEPENDENCY("Button");
448
}
449
450
void AcceptDialog::_validate_property(PropertyInfo &p_property) const {
451
if (!Engine::get_singleton()->is_editor_hint()) {
452
return;
453
}
454
if (p_property.name == "ok_button_text") {
455
p_property.hint = PROPERTY_HINT_PLACEHOLDER_TEXT;
456
p_property.hint_string = default_ok_text;
457
}
458
}
459
460
void AcceptDialog::set_swap_cancel_ok(bool p_swap) {
461
swap_cancel_ok = p_swap;
462
}
463
464
AcceptDialog::AcceptDialog() {
465
set_wrap_controls(true);
466
set_visible(false);
467
set_transient(true);
468
set_exclusive(true);
469
set_clamp_to_embedder(true);
470
set_keep_title_visible(true);
471
472
set_flag(FLAG_MINIMIZE_DISABLED, true);
473
set_flag(FLAG_MAXIMIZE_DISABLED, true);
474
475
bg_panel = memnew(Panel);
476
add_child(bg_panel, false, INTERNAL_MODE_FRONT);
477
478
buttons_hbox = memnew(HBoxContainer);
479
480
message_label = memnew(Label);
481
message_label->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
482
message_label->set_anchor(SIDE_RIGHT, Control::ANCHOR_END);
483
message_label->set_anchor(SIDE_BOTTOM, Control::ANCHOR_END);
484
add_child(message_label, false, INTERNAL_MODE_FRONT);
485
486
add_child(buttons_hbox, false, INTERNAL_MODE_BACK);
487
488
buttons_hbox->add_spacer();
489
ok_button = memnew(Button);
490
set_default_ok_text(ETR("OK"));
491
buttons_hbox->add_child(ok_button);
492
// Ensure hiding OK button will hide one of the initial spacers.
493
Control *bound_spacer = buttons_hbox->add_spacer();
494
ok_button->set_meta("__bound_spacer", bound_spacer);
495
ok_button->connect(SceneStringName(visibility_changed), callable_mp(this, &AcceptDialog::_button_visibility_changed).bind(ok_button));
496
497
ok_button->connect(SceneStringName(pressed), callable_mp(this, &AcceptDialog::_ok_pressed));
498
499
set_title(ETR("Alert!"));
500
}
501
502
AcceptDialog::~AcceptDialog() {
503
}
504
505
// ConfirmationDialog
506
507
void ConfirmationDialog::set_cancel_button_text(String p_cancel_button_text) {
508
cancel->set_text(p_cancel_button_text);
509
}
510
511
String ConfirmationDialog::get_cancel_button_text() const {
512
return cancel->get_text();
513
}
514
515
void ConfirmationDialog::_bind_methods() {
516
ClassDB::bind_method(D_METHOD("get_cancel_button"), &ConfirmationDialog::get_cancel_button);
517
ClassDB::bind_method(D_METHOD("set_cancel_button_text", "text"), &ConfirmationDialog::set_cancel_button_text);
518
ClassDB::bind_method(D_METHOD("get_cancel_button_text"), &ConfirmationDialog::get_cancel_button_text);
519
520
ADD_PROPERTY(PropertyInfo(Variant::STRING, "cancel_button_text"), "set_cancel_button_text", "get_cancel_button_text");
521
522
ADD_CLASS_DEPENDENCY("Button");
523
}
524
525
Button *ConfirmationDialog::get_cancel_button() {
526
return cancel;
527
}
528
529
ConfirmationDialog::ConfirmationDialog() {
530
set_title(ETR("Please Confirm..."));
531
set_min_size(Size2(200, 70));
532
533
cancel = add_cancel_button();
534
}
535
536