dialogs.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*************************************************************************/
  2. /* dialogs.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "dialogs.h"
  31. #include "core/os/keyboard.h"
  32. #include "core/string/print_string.h"
  33. #include "core/string/translation.h"
  34. #include "scene/gui/line_edit.h"
  35. // AcceptDialog
  36. void AcceptDialog::_input_from_window(const Ref<InputEvent> &p_event) {
  37. Ref<InputEventKey> key = p_event;
  38. if (close_on_escape && key.is_valid() && key->is_pressed() && key->get_keycode() == Key::ESCAPE) {
  39. _cancel_pressed();
  40. }
  41. }
  42. void AcceptDialog::_parent_focused() {
  43. if (close_on_escape && !is_exclusive()) {
  44. _cancel_pressed();
  45. }
  46. }
  47. void AcceptDialog::_update_theme_item_cache() {
  48. Window::_update_theme_item_cache();
  49. theme_cache.panel_style = get_theme_stylebox(SNAME("panel"));
  50. theme_cache.margin = get_theme_constant(SNAME("margin"));
  51. theme_cache.button_margin = get_theme_constant(SNAME("button_margin"));
  52. }
  53. void AcceptDialog::_notification(int p_what) {
  54. switch (p_what) {
  55. case NOTIFICATION_POST_ENTER_TREE: {
  56. if (is_visible()) {
  57. get_ok_button()->grab_focus();
  58. }
  59. } break;
  60. case NOTIFICATION_VISIBILITY_CHANGED: {
  61. if (is_visible()) {
  62. if (get_ok_button()->is_inside_tree()) {
  63. get_ok_button()->grab_focus();
  64. }
  65. _update_child_rects();
  66. parent_visible = get_parent_visible_window();
  67. if (parent_visible) {
  68. parent_visible->connect("focus_entered", callable_mp(this, &AcceptDialog::_parent_focused));
  69. }
  70. } else {
  71. if (parent_visible) {
  72. parent_visible->disconnect("focus_entered", callable_mp(this, &AcceptDialog::_parent_focused));
  73. parent_visible = nullptr;
  74. }
  75. }
  76. } break;
  77. case NOTIFICATION_THEME_CHANGED: {
  78. bg->add_theme_style_override("panel", theme_cache.panel_style);
  79. label->set_begin(Point2(theme_cache.margin, theme_cache.margin));
  80. label->set_end(Point2(-theme_cache.margin, -theme_cache.button_margin - 10));
  81. } break;
  82. case NOTIFICATION_EXIT_TREE: {
  83. if (parent_visible) {
  84. parent_visible->disconnect("focus_entered", callable_mp(this, &AcceptDialog::_parent_focused));
  85. parent_visible = nullptr;
  86. }
  87. } break;
  88. case NOTIFICATION_READY:
  89. case NOTIFICATION_WM_SIZE_CHANGED: {
  90. if (is_visible()) {
  91. _update_child_rects();
  92. }
  93. } break;
  94. case NOTIFICATION_WM_CLOSE_REQUEST: {
  95. _cancel_pressed();
  96. } break;
  97. }
  98. }
  99. void AcceptDialog::_text_submitted(const String &p_text) {
  100. if (get_ok_button() && get_ok_button()->is_disabled()) {
  101. return; // Do not allow submission if OK button is disabled.
  102. }
  103. _ok_pressed();
  104. }
  105. void AcceptDialog::_ok_pressed() {
  106. if (hide_on_ok) {
  107. set_visible(false);
  108. }
  109. ok_pressed();
  110. emit_signal(SNAME("confirmed"));
  111. }
  112. void AcceptDialog::_cancel_pressed() {
  113. Window *parent_window = parent_visible;
  114. if (parent_visible) {
  115. parent_visible->disconnect("focus_entered", callable_mp(this, &AcceptDialog::_parent_focused));
  116. parent_visible = nullptr;
  117. }
  118. call_deferred(SNAME("hide"));
  119. emit_signal(SNAME("cancelled"));
  120. cancel_pressed();
  121. if (parent_window) {
  122. //parent_window->grab_focus();
  123. }
  124. }
  125. String AcceptDialog::get_text() const {
  126. return label->get_text();
  127. }
  128. void AcceptDialog::set_text(String p_text) {
  129. if (label->get_text() == p_text) {
  130. return;
  131. }
  132. label->set_text(p_text);
  133. child_controls_changed();
  134. if (is_visible()) {
  135. _update_child_rects();
  136. }
  137. }
  138. void AcceptDialog::set_hide_on_ok(bool p_hide) {
  139. hide_on_ok = p_hide;
  140. }
  141. bool AcceptDialog::get_hide_on_ok() const {
  142. return hide_on_ok;
  143. }
  144. void AcceptDialog::set_close_on_escape(bool p_hide) {
  145. close_on_escape = p_hide;
  146. }
  147. bool AcceptDialog::get_close_on_escape() const {
  148. return close_on_escape;
  149. }
  150. void AcceptDialog::set_autowrap(bool p_autowrap) {
  151. label->set_autowrap_mode(p_autowrap ? TextServer::AUTOWRAP_WORD : TextServer::AUTOWRAP_OFF);
  152. }
  153. bool AcceptDialog::has_autowrap() {
  154. return label->get_autowrap_mode() != TextServer::AUTOWRAP_OFF;
  155. }
  156. void AcceptDialog::set_ok_button_text(String p_ok_button_text) {
  157. ok->set_text(p_ok_button_text);
  158. }
  159. String AcceptDialog::get_ok_button_text() const {
  160. return ok->get_text();
  161. }
  162. void AcceptDialog::register_text_enter(Control *p_line_edit) {
  163. ERR_FAIL_NULL(p_line_edit);
  164. LineEdit *line_edit = Object::cast_to<LineEdit>(p_line_edit);
  165. if (line_edit) {
  166. line_edit->connect("text_submitted", callable_mp(this, &AcceptDialog::_text_submitted));
  167. }
  168. }
  169. void AcceptDialog::_update_child_rects() {
  170. Size2 label_size = label->get_minimum_size();
  171. if (label->get_text().is_empty()) {
  172. label_size.height = 0;
  173. }
  174. Size2 size = get_size();
  175. Size2 hminsize = hbc->get_combined_minimum_size();
  176. Vector2 cpos(theme_cache.margin, theme_cache.margin + label_size.height);
  177. Vector2 csize(size.x - theme_cache.margin * 2, size.y - theme_cache.margin * 3 - hminsize.y - label_size.height);
  178. for (int i = 0; i < get_child_count(); i++) {
  179. Control *c = Object::cast_to<Control>(get_child(i));
  180. if (!c) {
  181. continue;
  182. }
  183. if (c == hbc || c == label || c == bg || c->is_set_as_top_level()) {
  184. continue;
  185. }
  186. c->set_position(cpos);
  187. c->set_size(csize);
  188. }
  189. cpos.y += csize.y + theme_cache.margin;
  190. csize.y = hminsize.y;
  191. hbc->set_position(cpos);
  192. hbc->set_size(csize);
  193. bg->set_position(Point2());
  194. bg->set_size(size);
  195. }
  196. Size2 AcceptDialog::_get_contents_minimum_size() const {
  197. Size2 minsize = label->get_combined_minimum_size();
  198. for (int i = 0; i < get_child_count(); i++) {
  199. Control *c = Object::cast_to<Control>(get_child(i));
  200. if (!c) {
  201. continue;
  202. }
  203. if (c == hbc || c == label || c->is_set_as_top_level()) {
  204. continue;
  205. }
  206. Size2 cminsize = c->get_combined_minimum_size();
  207. minsize.x = MAX(cminsize.x, minsize.x);
  208. minsize.y = MAX(cminsize.y, minsize.y);
  209. }
  210. Size2 hminsize = hbc->get_combined_minimum_size();
  211. minsize.x = MAX(hminsize.x, minsize.x);
  212. minsize.y += hminsize.y;
  213. minsize.x += theme_cache.margin * 2;
  214. minsize.y += theme_cache.margin * 3; //one as separation between hbc and child
  215. Size2 wmsize = get_min_size();
  216. minsize.x = MAX(wmsize.x, minsize.x);
  217. return minsize;
  218. }
  219. void AcceptDialog::_custom_action(const String &p_action) {
  220. emit_signal(SNAME("custom_action"), p_action);
  221. custom_action(p_action);
  222. }
  223. Button *AcceptDialog::add_button(const String &p_text, bool p_right, const String &p_action) {
  224. Button *button = memnew(Button);
  225. button->set_text(p_text);
  226. if (p_right) {
  227. hbc->add_child(button);
  228. hbc->add_spacer();
  229. } else {
  230. hbc->add_child(button);
  231. hbc->move_child(button, 0);
  232. hbc->add_spacer(true);
  233. }
  234. if (!p_action.is_empty()) {
  235. button->connect("pressed", callable_mp(this, &AcceptDialog::_custom_action).bind(p_action));
  236. }
  237. return button;
  238. }
  239. Button *AcceptDialog::add_cancel_button(const String &p_cancel) {
  240. String c = p_cancel;
  241. if (p_cancel.is_empty()) {
  242. c = "Cancel";
  243. }
  244. Button *b = swap_cancel_ok ? add_button(c, true) : add_button(c);
  245. b->connect("pressed", callable_mp(this, &AcceptDialog::_cancel_pressed));
  246. return b;
  247. }
  248. void AcceptDialog::remove_button(Control *p_button) {
  249. Button *button = Object::cast_to<Button>(p_button);
  250. ERR_FAIL_NULL(button);
  251. ERR_FAIL_COND_MSG(button->get_parent() != hbc, vformat("Cannot remove button %s as it does not belong to this dialog.", button->get_name()));
  252. ERR_FAIL_COND_MSG(button == ok, "Cannot remove dialog's OK button.");
  253. Node *right_spacer = hbc->get_child(button->get_index() + 1);
  254. // Should always be valid but let's avoid crashing
  255. if (right_spacer) {
  256. hbc->remove_child(right_spacer);
  257. memdelete(right_spacer);
  258. }
  259. hbc->remove_child(button);
  260. if (button->is_connected("pressed", callable_mp(this, &AcceptDialog::_custom_action))) {
  261. button->disconnect("pressed", callable_mp(this, &AcceptDialog::_custom_action));
  262. }
  263. if (button->is_connected("pressed", callable_mp(this, &AcceptDialog::_cancel_pressed))) {
  264. button->disconnect("pressed", callable_mp(this, &AcceptDialog::_cancel_pressed));
  265. }
  266. }
  267. void AcceptDialog::_bind_methods() {
  268. ClassDB::bind_method(D_METHOD("get_ok_button"), &AcceptDialog::get_ok_button);
  269. ClassDB::bind_method(D_METHOD("get_label"), &AcceptDialog::get_label);
  270. ClassDB::bind_method(D_METHOD("set_hide_on_ok", "enabled"), &AcceptDialog::set_hide_on_ok);
  271. ClassDB::bind_method(D_METHOD("get_hide_on_ok"), &AcceptDialog::get_hide_on_ok);
  272. ClassDB::bind_method(D_METHOD("set_close_on_escape", "enabled"), &AcceptDialog::set_close_on_escape);
  273. ClassDB::bind_method(D_METHOD("get_close_on_escape"), &AcceptDialog::get_close_on_escape);
  274. ClassDB::bind_method(D_METHOD("add_button", "text", "right", "action"), &AcceptDialog::add_button, DEFVAL(false), DEFVAL(""));
  275. ClassDB::bind_method(D_METHOD("add_cancel_button", "name"), &AcceptDialog::add_cancel_button);
  276. ClassDB::bind_method(D_METHOD("remove_button", "button"), &AcceptDialog::remove_button);
  277. ClassDB::bind_method(D_METHOD("register_text_enter", "line_edit"), &AcceptDialog::register_text_enter);
  278. ClassDB::bind_method(D_METHOD("set_text", "text"), &AcceptDialog::set_text);
  279. ClassDB::bind_method(D_METHOD("get_text"), &AcceptDialog::get_text);
  280. ClassDB::bind_method(D_METHOD("set_autowrap", "autowrap"), &AcceptDialog::set_autowrap);
  281. ClassDB::bind_method(D_METHOD("has_autowrap"), &AcceptDialog::has_autowrap);
  282. ClassDB::bind_method(D_METHOD("set_ok_button_text", "text"), &AcceptDialog::set_ok_button_text);
  283. ClassDB::bind_method(D_METHOD("get_ok_button_text"), &AcceptDialog::get_ok_button_text);
  284. ADD_SIGNAL(MethodInfo("confirmed"));
  285. ADD_SIGNAL(MethodInfo("cancelled"));
  286. ADD_SIGNAL(MethodInfo("custom_action", PropertyInfo(Variant::STRING_NAME, "action")));
  287. ADD_PROPERTY(PropertyInfo(Variant::STRING, "ok_button_text"), "set_ok_button_text", "get_ok_button_text");
  288. ADD_GROUP("Dialog", "dialog");
  289. ADD_PROPERTY(PropertyInfo(Variant::STRING, "dialog_text", PROPERTY_HINT_MULTILINE_TEXT, "", PROPERTY_USAGE_DEFAULT_INTL), "set_text", "get_text");
  290. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dialog_hide_on_ok"), "set_hide_on_ok", "get_hide_on_ok");
  291. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dialog_close_on_escape"), "set_close_on_escape", "get_close_on_escape");
  292. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dialog_autowrap"), "set_autowrap", "has_autowrap");
  293. }
  294. bool AcceptDialog::swap_cancel_ok = false;
  295. void AcceptDialog::set_swap_cancel_ok(bool p_swap) {
  296. swap_cancel_ok = p_swap;
  297. }
  298. AcceptDialog::AcceptDialog() {
  299. set_wrap_controls(true);
  300. set_visible(false);
  301. set_transient(true);
  302. set_exclusive(true);
  303. set_clamp_to_embedder(true);
  304. bg = memnew(Panel);
  305. add_child(bg, false, INTERNAL_MODE_FRONT);
  306. hbc = memnew(HBoxContainer);
  307. label = memnew(Label);
  308. label->set_anchor(SIDE_RIGHT, Control::ANCHOR_END);
  309. label->set_anchor(SIDE_BOTTOM, Control::ANCHOR_END);
  310. add_child(label, false, INTERNAL_MODE_FRONT);
  311. add_child(hbc, false, INTERNAL_MODE_FRONT);
  312. hbc->add_spacer();
  313. ok = memnew(Button);
  314. ok->set_text("OK");
  315. hbc->add_child(ok);
  316. hbc->add_spacer();
  317. ok->connect("pressed", callable_mp(this, &AcceptDialog::_ok_pressed));
  318. set_title(TTRC("Alert!"));
  319. connect("window_input", callable_mp(this, &AcceptDialog::_input_from_window));
  320. }
  321. AcceptDialog::~AcceptDialog() {
  322. }
  323. // ConfirmationDialog
  324. void ConfirmationDialog::set_cancel_button_text(String p_cancel_button_text) {
  325. cancel->set_text(p_cancel_button_text);
  326. }
  327. String ConfirmationDialog::get_cancel_button_text() const {
  328. return cancel->get_text();
  329. }
  330. void ConfirmationDialog::_bind_methods() {
  331. ClassDB::bind_method(D_METHOD("get_cancel_button"), &ConfirmationDialog::get_cancel_button);
  332. ClassDB::bind_method(D_METHOD("set_cancel_button_text", "text"), &ConfirmationDialog::set_cancel_button_text);
  333. ClassDB::bind_method(D_METHOD("get_cancel_button_text"), &ConfirmationDialog::get_cancel_button_text);
  334. ADD_PROPERTY(PropertyInfo(Variant::STRING, "cancel_button_text"), "set_cancel_button_text", "get_cancel_button_text");
  335. }
  336. Button *ConfirmationDialog::get_cancel_button() {
  337. return cancel;
  338. }
  339. ConfirmationDialog::ConfirmationDialog() {
  340. set_title(TTRC("Please Confirm..."));
  341. set_min_size(Size2(200, 70));
  342. cancel = add_cancel_button();
  343. }