summaryrefslogtreecommitdiff
path: root/scene/gui/dialogs.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'scene/gui/dialogs.cpp')
-rw-r--r--scene/gui/dialogs.cpp89
1 files changed, 53 insertions, 36 deletions
diff --git a/scene/gui/dialogs.cpp b/scene/gui/dialogs.cpp
index 4f59f4a36a..da858e8e83 100644
--- a/scene/gui/dialogs.cpp
+++ b/scene/gui/dialogs.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
@@ -60,7 +60,7 @@ void AcceptDialog::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_VISIBILITY_CHANGED: {
if (is_visible()) {
- get_ok()->grab_focus();
+ get_ok_button()->grab_focus();
_update_child_rects();
parent_visible = get_parent_visible_window();
if (parent_visible) {
@@ -72,13 +72,10 @@ void AcceptDialog::_notification(int p_what) {
parent_visible = nullptr;
}
}
-
} break;
-
case NOTIFICATION_THEME_CHANGED: {
- bg->add_theme_style_override("panel", bg->get_theme_stylebox("panel", "AcceptDialog"));
+ bg->add_theme_style_override("panel", bg->get_theme_stylebox(SNAME("panel"), SNAME("AcceptDialog")));
} break;
-
case NOTIFICATION_EXIT_TREE: {
if (parent_visible) {
parent_visible->disconnect("focus_entered", callable_mp(this, &AcceptDialog::_parent_focused));
@@ -97,7 +94,7 @@ void AcceptDialog::_notification(int p_what) {
}
}
-void AcceptDialog::_text_entered(const String &p_text) {
+void AcceptDialog::_text_submitted(const String &p_text) {
_ok_pressed();
}
@@ -106,7 +103,7 @@ void AcceptDialog::_ok_pressed() {
set_visible(false);
}
ok_pressed();
- emit_signal("confirmed");
+ emit_signal(SNAME("confirmed"));
}
void AcceptDialog::_cancel_pressed() {
@@ -116,9 +113,9 @@ void AcceptDialog::_cancel_pressed() {
parent_visible = nullptr;
}
- call_deferred("hide");
+ call_deferred(SNAME("hide"));
- emit_signal("cancelled");
+ emit_signal(SNAME("cancelled"));
cancel_pressed();
@@ -148,27 +145,27 @@ bool AcceptDialog::get_hide_on_ok() const {
}
void AcceptDialog::set_autowrap(bool p_autowrap) {
- label->set_autowrap(p_autowrap);
+ label->set_autowrap_mode(p_autowrap ? Label::AUTOWRAP_WORD : Label::AUTOWRAP_OFF);
}
bool AcceptDialog::has_autowrap() {
- return label->has_autowrap();
+ return label->get_autowrap_mode() != Label::AUTOWRAP_OFF;
}
-void AcceptDialog::register_text_enter(Node *p_line_edit) {
+void AcceptDialog::register_text_enter(Control *p_line_edit) {
ERR_FAIL_NULL(p_line_edit);
LineEdit *line_edit = Object::cast_to<LineEdit>(p_line_edit);
if (line_edit) {
- line_edit->connect("text_entered", callable_mp(this, &AcceptDialog::_text_entered));
+ line_edit->connect("text_submitted", callable_mp(this, &AcceptDialog::_text_submitted));
}
}
void AcceptDialog::_update_child_rects() {
Size2 label_size = label->get_minimum_size();
- if (label->get_text().empty()) {
+ if (label->get_text().is_empty()) {
label_size.height = 0;
}
- int margin = hbc->get_theme_constant("margin", "Dialogs");
+ int margin = hbc->get_theme_constant(SNAME("margin"), SNAME("Dialogs"));
Size2 size = get_size();
Size2 hminsize = hbc->get_combined_minimum_size();
@@ -200,7 +197,7 @@ void AcceptDialog::_update_child_rects() {
}
Size2 AcceptDialog::_get_contents_minimum_size() const {
- int margin = hbc->get_theme_constant("margin", "Dialogs");
+ int margin = hbc->get_theme_constant(SNAME("margin"), SNAME("Dialogs"));
Size2 minsize = label->get_combined_minimum_size();
for (int i = 0; i < get_child_count(); i++) {
@@ -230,7 +227,7 @@ Size2 AcceptDialog::_get_contents_minimum_size() const {
}
void AcceptDialog::_custom_action(const String &p_action) {
- emit_signal("custom_action", p_action);
+ emit_signal(SNAME("custom_action"), p_action);
custom_action(p_action);
}
@@ -253,23 +250,46 @@ Button *AcceptDialog::add_button(const String &p_text, bool p_right, const Strin
return button;
}
-Button *AcceptDialog::add_cancel(const String &p_cancel) {
+Button *AcceptDialog::add_cancel_button(const String &p_cancel) {
String c = p_cancel;
if (p_cancel == "") {
- c = RTR("Cancel");
+ c = TTRC("Cancel");
}
Button *b = swap_cancel_ok ? add_button(c, true) : add_button(c);
b->connect("pressed", callable_mp(this, &AcceptDialog::_cancel_pressed));
return b;
}
+void AcceptDialog::remove_button(Control *p_button) {
+ Button *button = Object::cast_to<Button>(p_button);
+ ERR_FAIL_NULL(button);
+ ERR_FAIL_COND_MSG(button->get_parent() != hbc, vformat("Cannot remove button %s as it does not belong to this dialog.", button->get_name()));
+ ERR_FAIL_COND_MSG(button == ok, "Cannot remove dialog's OK button.");
+
+ Node *right_spacer = hbc->get_child(button->get_index() + 1);
+ // Should always be valid but let's avoid crashing
+ if (right_spacer) {
+ hbc->remove_child(right_spacer);
+ memdelete(right_spacer);
+ }
+ hbc->remove_child(button);
+
+ if (button->is_connected("pressed", callable_mp(this, &AcceptDialog::_custom_action))) {
+ button->disconnect("pressed", callable_mp(this, &AcceptDialog::_custom_action));
+ }
+ if (button->is_connected("pressed", callable_mp(this, &AcceptDialog::_cancel_pressed))) {
+ button->disconnect("pressed", callable_mp(this, &AcceptDialog::_cancel_pressed));
+ }
+}
+
void AcceptDialog::_bind_methods() {
- ClassDB::bind_method(D_METHOD("get_ok"), &AcceptDialog::get_ok);
+ ClassDB::bind_method(D_METHOD("get_ok_button"), &AcceptDialog::get_ok_button);
ClassDB::bind_method(D_METHOD("get_label"), &AcceptDialog::get_label);
ClassDB::bind_method(D_METHOD("set_hide_on_ok", "enabled"), &AcceptDialog::set_hide_on_ok);
ClassDB::bind_method(D_METHOD("get_hide_on_ok"), &AcceptDialog::get_hide_on_ok);
ClassDB::bind_method(D_METHOD("add_button", "text", "right", "action"), &AcceptDialog::add_button, DEFVAL(false), DEFVAL(""));
- ClassDB::bind_method(D_METHOD("add_cancel", "name"), &AcceptDialog::add_cancel);
+ ClassDB::bind_method(D_METHOD("add_cancel_button", "name"), &AcceptDialog::add_cancel_button);
+ ClassDB::bind_method(D_METHOD("remove_button", "button"), &AcceptDialog::remove_button);
ClassDB::bind_method(D_METHOD("register_text_enter", "line_edit"), &AcceptDialog::register_text_enter);
ClassDB::bind_method(D_METHOD("set_text", "text"), &AcceptDialog::set_text);
ClassDB::bind_method(D_METHOD("get_text"), &AcceptDialog::get_text);
@@ -292,8 +312,6 @@ void AcceptDialog::set_swap_cancel_ok(bool p_swap) {
}
AcceptDialog::AcceptDialog() {
- parent_visible = nullptr;
-
set_wrap_controls(true);
set_visible(false);
set_transient(true);
@@ -305,12 +323,12 @@ AcceptDialog::AcceptDialog() {
hbc = memnew(HBoxContainer);
- int margin = hbc->get_theme_constant("margin", "Dialogs");
- int button_margin = hbc->get_theme_constant("button_margin", "Dialogs");
+ int margin = hbc->get_theme_constant(SNAME("margin"), SNAME("Dialogs"));
+ int button_margin = hbc->get_theme_constant(SNAME("button_margin"), SNAME("Dialogs"));
label = memnew(Label);
- label->set_anchor(MARGIN_RIGHT, Control::ANCHOR_END);
- label->set_anchor(MARGIN_BOTTOM, Control::ANCHOR_END);
+ label->set_anchor(SIDE_RIGHT, Control::ANCHOR_END);
+ label->set_anchor(SIDE_BOTTOM, Control::ANCHOR_END);
label->set_begin(Point2(margin, margin));
label->set_end(Point2(-margin, -button_margin - 10));
add_child(label);
@@ -319,14 +337,13 @@ AcceptDialog::AcceptDialog() {
hbc->add_spacer();
ok = memnew(Button);
- ok->set_text(RTR("OK"));
+ ok->set_text(TTRC("OK"));
hbc->add_child(ok);
hbc->add_spacer();
ok->connect("pressed", callable_mp(this, &AcceptDialog::_ok_pressed));
- hide_on_ok = true;
- set_title(RTR("Alert!"));
+ set_title(TTRC("Alert!"));
connect("window_input", callable_mp(this, &AcceptDialog::_input_from_window));
}
@@ -337,17 +354,17 @@ AcceptDialog::~AcceptDialog() {
// ConfirmationDialog
void ConfirmationDialog::_bind_methods() {
- ClassDB::bind_method(D_METHOD("get_cancel"), &ConfirmationDialog::get_cancel);
+ ClassDB::bind_method(D_METHOD("get_cancel_button"), &ConfirmationDialog::get_cancel_button);
}
-Button *ConfirmationDialog::get_cancel() {
+Button *ConfirmationDialog::get_cancel_button() {
return cancel;
}
ConfirmationDialog::ConfirmationDialog() {
- set_title(RTR("Please Confirm..."));
+ set_title(TTRC("Please Confirm..."));
#ifdef TOOLS_ENABLED
set_min_size(Size2(200, 70) * EDSCALE);
#endif
- cancel = add_cancel();
+ cancel = add_cancel_button();
}