dialogs.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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.buttons_separation = get_theme_constant(SNAME("buttons_separation"));
  51. }
  52. void AcceptDialog::_notification(int p_what) {
  53. switch (p_what) {
  54. case NOTIFICATION_POST_ENTER_TREE: {
  55. if (is_visible()) {
  56. get_ok_button()->grab_focus();
  57. }
  58. } break;
  59. case NOTIFICATION_VISIBILITY_CHANGED: {
  60. if (is_visible()) {
  61. if (get_ok_button()->is_inside_tree()) {
  62. get_ok_button()->grab_focus();
  63. }
  64. _update_child_rects();
  65. parent_visible = get_parent_visible_window();
  66. if (parent_visible) {
  67. parent_visible->connect("focus_entered", callable_mp(this, &AcceptDialog::_parent_focused));
  68. }
  69. } else {
  70. if (parent_visible) {
  71. parent_visible->disconnect("focus_entered", callable_mp(this, &AcceptDialog::_parent_focused));
  72. parent_visible = nullptr;
  73. }
  74. }
  75. } break;
  76. case NOTIFICATION_THEME_CHANGED: {
  77. bg_panel->add_theme_style_override("panel", theme_cache.panel_style);
  78. child_controls_changed();
  79. if (is_visible()) {
  80. _update_child_rects();
  81. }
  82. } break;
  83. case NOTIFICATION_EXIT_TREE: {
  84. if (parent_visible) {
  85. parent_visible->disconnect("focus_entered", callable_mp(this, &AcceptDialog::_parent_focused));
  86. parent_visible = nullptr;
  87. }
  88. } break;
  89. case NOTIFICATION_READY:
  90. case NOTIFICATION_WM_SIZE_CHANGED: {
  91. if (is_visible()) {
  92. _update_child_rects();
  93. }
  94. } break;
  95. case NOTIFICATION_WM_CLOSE_REQUEST: {
  96. _cancel_pressed();
  97. } break;
  98. }
  99. }
  100. void AcceptDialog::_text_submitted(const String &p_text) {
  101. if (get_ok_button() && get_ok_button()->is_disabled()) {
  102. return; // Do not allow submission if OK button is disabled.
  103. }
  104. _ok_pressed();
  105. }
  106. void AcceptDialog::_ok_pressed() {
  107. if (hide_on_ok) {
  108. set_visible(false);
  109. }
  110. ok_pressed();
  111. emit_signal(SNAME("confirmed"));
  112. }
  113. void AcceptDialog::_cancel_pressed() {
  114. Window *parent_window = parent_visible;
  115. if (parent_visible) {
  116. parent_visible->disconnect("focus_entered", callable_mp(this, &AcceptDialog::_parent_focused));
  117. parent_visible = nullptr;
  118. }
  119. call_deferred(SNAME("hide"));
  120. emit_signal(SNAME("cancelled"));
  121. cancel_pressed();
  122. if (parent_window) {
  123. //parent_window->grab_focus();
  124. }
  125. }
  126. String AcceptDialog::get_text() const {
  127. return message_label->get_text();
  128. }
  129. void AcceptDialog::set_text(String p_text) {
  130. if (message_label->get_text() == p_text) {
  131. return;
  132. }
  133. message_label->set_text(p_text);
  134. child_controls_changed();
  135. if (is_visible()) {
  136. _update_child_rects();
  137. }
  138. }
  139. void AcceptDialog::set_hide_on_ok(bool p_hide) {
  140. hide_on_ok = p_hide;
  141. }
  142. bool AcceptDialog::get_hide_on_ok() const {
  143. return hide_on_ok;
  144. }
  145. void AcceptDialog::set_close_on_escape(bool p_hide) {
  146. close_on_escape = p_hide;
  147. }
  148. bool AcceptDialog::get_close_on_escape() const {
  149. return close_on_escape;
  150. }
  151. void AcceptDialog::set_autowrap(bool p_autowrap) {
  152. message_label->set_autowrap_mode(p_autowrap ? TextServer::AUTOWRAP_WORD : TextServer::AUTOWRAP_OFF);
  153. }
  154. bool AcceptDialog::has_autowrap() {
  155. return message_label->get_autowrap_mode() != TextServer::AUTOWRAP_OFF;
  156. }
  157. void AcceptDialog::set_ok_button_text(String p_ok_button_text) {
  158. ok_button->set_text(p_ok_button_text);
  159. child_controls_changed();
  160. if (is_visible()) {
  161. _update_child_rects();
  162. }
  163. }
  164. String AcceptDialog::get_ok_button_text() const {
  165. return ok_button->get_text();
  166. }
  167. void AcceptDialog::register_text_enter(Control *p_line_edit) {
  168. ERR_FAIL_NULL(p_line_edit);
  169. LineEdit *line_edit = Object::cast_to<LineEdit>(p_line_edit);
  170. if (line_edit) {
  171. line_edit->connect("text_submitted", callable_mp(this, &AcceptDialog::_text_submitted));
  172. }
  173. }
  174. void AcceptDialog::_update_child_rects() {
  175. Size2 dlg_size = get_size();
  176. float h_margins = theme_cache.panel_style->get_margin(SIDE_LEFT) + theme_cache.panel_style->get_margin(SIDE_RIGHT);
  177. float v_margins = theme_cache.panel_style->get_margin(SIDE_TOP) + theme_cache.panel_style->get_margin(SIDE_BOTTOM);
  178. // Fill the entire size of the window with the background.
  179. bg_panel->set_position(Point2());
  180. bg_panel->set_size(dlg_size);
  181. // Place the buttons from the bottom edge to their minimum required size.
  182. Size2 buttons_minsize = buttons_hbox->get_combined_minimum_size();
  183. Size2 buttons_size = Size2(dlg_size.x - h_margins, buttons_minsize.y);
  184. 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);
  185. buttons_hbox->set_position(buttons_position);
  186. buttons_hbox->set_size(buttons_size);
  187. // Place the content from the top to fill the rest of the space (minus the separation).
  188. Point2 content_position = Point2(theme_cache.panel_style->get_margin(SIDE_LEFT), theme_cache.panel_style->get_margin(SIDE_TOP));
  189. Size2 content_size = Size2(dlg_size.x - h_margins, dlg_size.y - v_margins - buttons_size.y - theme_cache.buttons_separation);
  190. for (int i = 0; i < get_child_count(); i++) {
  191. Control *c = Object::cast_to<Control>(get_child(i));
  192. if (!c) {
  193. continue;
  194. }
  195. if (c == buttons_hbox || c == bg_panel || c->is_set_as_top_level()) {
  196. continue;
  197. }
  198. c->set_position(content_position);
  199. c->set_size(content_size);
  200. }
  201. }
  202. Size2 AcceptDialog::_get_contents_minimum_size() const {
  203. // First, we then iterate over the label and any other custom controls
  204. // to try and find the size that encompasses all content.
  205. Size2 content_minsize;
  206. for (int i = 0; i < get_child_count(); i++) {
  207. Control *c = Object::cast_to<Control>(get_child(i));
  208. if (!c) {
  209. continue;
  210. }
  211. // Buttons will be included afterwards.
  212. // The panel only displays the stylebox and doesn't contribute to the size.
  213. if (c == buttons_hbox || c == bg_panel || c->is_set_as_top_level()) {
  214. continue;
  215. }
  216. Size2 child_minsize = c->get_combined_minimum_size();
  217. content_minsize.x = MAX(child_minsize.x, content_minsize.x);
  218. content_minsize.y = MAX(child_minsize.y, content_minsize.y);
  219. }
  220. // Then we take the background panel as it provides the offsets,
  221. // which are always added to the minimum size.
  222. if (theme_cache.panel_style.is_valid()) {
  223. content_minsize += theme_cache.panel_style->get_minimum_size();
  224. }
  225. // Then we add buttons. Horizontally we're interested in whichever
  226. // value is the biggest. Vertically buttons add to the overall size.
  227. Size2 buttons_minsize = buttons_hbox->get_combined_minimum_size();
  228. content_minsize.x = MAX(buttons_minsize.x, content_minsize.x);
  229. content_minsize.y += buttons_minsize.y;
  230. // Plus there is a separation size added on top.
  231. content_minsize.y += theme_cache.buttons_separation;
  232. // Last, we make sure that we aren't below the minimum window size.
  233. Size2 window_minsize = get_min_size();
  234. content_minsize.x = MAX(window_minsize.x, content_minsize.x);
  235. content_minsize.y = MAX(window_minsize.y, content_minsize.y);
  236. return content_minsize;
  237. }
  238. void AcceptDialog::_custom_action(const String &p_action) {
  239. emit_signal(SNAME("custom_action"), p_action);
  240. custom_action(p_action);
  241. }
  242. Button *AcceptDialog::add_button(const String &p_text, bool p_right, const String &p_action) {
  243. Button *button = memnew(Button);
  244. button->set_text(p_text);
  245. if (p_right) {
  246. buttons_hbox->add_child(button);
  247. buttons_hbox->add_spacer();
  248. } else {
  249. buttons_hbox->add_child(button);
  250. buttons_hbox->move_child(button, 0);
  251. buttons_hbox->add_spacer(true);
  252. }
  253. child_controls_changed();
  254. if (is_visible()) {
  255. _update_child_rects();
  256. }
  257. if (!p_action.is_empty()) {
  258. button->connect("pressed", callable_mp(this, &AcceptDialog::_custom_action).bind(p_action));
  259. }
  260. return button;
  261. }
  262. Button *AcceptDialog::add_cancel_button(const String &p_cancel) {
  263. String c = p_cancel;
  264. if (p_cancel.is_empty()) {
  265. c = "Cancel";
  266. }
  267. Button *b = swap_cancel_ok ? add_button(c, true) : add_button(c);
  268. b->connect("pressed", callable_mp(this, &AcceptDialog::_cancel_pressed));
  269. return b;
  270. }
  271. void AcceptDialog::remove_button(Control *p_button) {
  272. Button *button = Object::cast_to<Button>(p_button);
  273. ERR_FAIL_NULL(button);
  274. ERR_FAIL_COND_MSG(button->get_parent() != buttons_hbox, vformat("Cannot remove button %s as it does not belong to this dialog.", button->get_name()));
  275. ERR_FAIL_COND_MSG(button == ok_button, "Cannot remove dialog's OK button.");
  276. if (button->is_connected("pressed", callable_mp(this, &AcceptDialog::_custom_action))) {
  277. button->disconnect("pressed", callable_mp(this, &AcceptDialog::_custom_action));
  278. }
  279. if (button->is_connected("pressed", callable_mp(this, &AcceptDialog::_cancel_pressed))) {
  280. button->disconnect("pressed", callable_mp(this, &AcceptDialog::_cancel_pressed));
  281. }
  282. Node *right_spacer = buttons_hbox->get_child(button->get_index() + 1);
  283. // Should always be valid but let's avoid crashing.
  284. if (right_spacer) {
  285. buttons_hbox->remove_child(right_spacer);
  286. memdelete(right_spacer);
  287. }
  288. buttons_hbox->remove_child(button);
  289. child_controls_changed();
  290. if (is_visible()) {
  291. _update_child_rects();
  292. }
  293. }
  294. void AcceptDialog::_bind_methods() {
  295. ClassDB::bind_method(D_METHOD("get_ok_button"), &AcceptDialog::get_ok_button);
  296. ClassDB::bind_method(D_METHOD("get_label"), &AcceptDialog::get_label);
  297. ClassDB::bind_method(D_METHOD("set_hide_on_ok", "enabled"), &AcceptDialog::set_hide_on_ok);
  298. ClassDB::bind_method(D_METHOD("get_hide_on_ok"), &AcceptDialog::get_hide_on_ok);
  299. ClassDB::bind_method(D_METHOD("set_close_on_escape", "enabled"), &AcceptDialog::set_close_on_escape);
  300. ClassDB::bind_method(D_METHOD("get_close_on_escape"), &AcceptDialog::get_close_on_escape);
  301. ClassDB::bind_method(D_METHOD("add_button", "text", "right", "action"), &AcceptDialog::add_button, DEFVAL(false), DEFVAL(""));
  302. ClassDB::bind_method(D_METHOD("add_cancel_button", "name"), &AcceptDialog::add_cancel_button);
  303. ClassDB::bind_method(D_METHOD("remove_button", "button"), &AcceptDialog::remove_button);
  304. ClassDB::bind_method(D_METHOD("register_text_enter", "line_edit"), &AcceptDialog::register_text_enter);
  305. ClassDB::bind_method(D_METHOD("set_text", "text"), &AcceptDialog::set_text);
  306. ClassDB::bind_method(D_METHOD("get_text"), &AcceptDialog::get_text);
  307. ClassDB::bind_method(D_METHOD("set_autowrap", "autowrap"), &AcceptDialog::set_autowrap);
  308. ClassDB::bind_method(D_METHOD("has_autowrap"), &AcceptDialog::has_autowrap);
  309. ClassDB::bind_method(D_METHOD("set_ok_button_text", "text"), &AcceptDialog::set_ok_button_text);
  310. ClassDB::bind_method(D_METHOD("get_ok_button_text"), &AcceptDialog::get_ok_button_text);
  311. ADD_SIGNAL(MethodInfo("confirmed"));
  312. ADD_SIGNAL(MethodInfo("cancelled"));
  313. ADD_SIGNAL(MethodInfo("custom_action", PropertyInfo(Variant::STRING_NAME, "action")));
  314. ADD_PROPERTY(PropertyInfo(Variant::STRING, "ok_button_text"), "set_ok_button_text", "get_ok_button_text");
  315. ADD_GROUP("Dialog", "dialog_");
  316. ADD_PROPERTY(PropertyInfo(Variant::STRING, "dialog_text", PROPERTY_HINT_MULTILINE_TEXT, "", PROPERTY_USAGE_DEFAULT_INTL), "set_text", "get_text");
  317. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dialog_hide_on_ok"), "set_hide_on_ok", "get_hide_on_ok");
  318. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dialog_close_on_escape"), "set_close_on_escape", "get_close_on_escape");
  319. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dialog_autowrap"), "set_autowrap", "has_autowrap");
  320. }
  321. bool AcceptDialog::swap_cancel_ok = false;
  322. void AcceptDialog::set_swap_cancel_ok(bool p_swap) {
  323. swap_cancel_ok = p_swap;
  324. }
  325. AcceptDialog::AcceptDialog() {
  326. set_wrap_controls(true);
  327. set_visible(false);
  328. set_transient(true);
  329. set_exclusive(true);
  330. set_clamp_to_embedder(true);
  331. bg_panel = memnew(Panel);
  332. add_child(bg_panel, false, INTERNAL_MODE_FRONT);
  333. buttons_hbox = memnew(HBoxContainer);
  334. message_label = memnew(Label);
  335. message_label->set_anchor(SIDE_RIGHT, Control::ANCHOR_END);
  336. message_label->set_anchor(SIDE_BOTTOM, Control::ANCHOR_END);
  337. add_child(message_label, false, INTERNAL_MODE_FRONT);
  338. add_child(buttons_hbox, false, INTERNAL_MODE_FRONT);
  339. buttons_hbox->add_spacer();
  340. ok_button = memnew(Button);
  341. ok_button->set_text("OK");
  342. buttons_hbox->add_child(ok_button);
  343. buttons_hbox->add_spacer();
  344. ok_button->connect("pressed", callable_mp(this, &AcceptDialog::_ok_pressed));
  345. set_title(TTRC("Alert!"));
  346. connect("window_input", callable_mp(this, &AcceptDialog::_input_from_window));
  347. }
  348. AcceptDialog::~AcceptDialog() {
  349. }
  350. // ConfirmationDialog
  351. void ConfirmationDialog::set_cancel_button_text(String p_cancel_button_text) {
  352. cancel->set_text(p_cancel_button_text);
  353. }
  354. String ConfirmationDialog::get_cancel_button_text() const {
  355. return cancel->get_text();
  356. }
  357. void ConfirmationDialog::_bind_methods() {
  358. ClassDB::bind_method(D_METHOD("get_cancel_button"), &ConfirmationDialog::get_cancel_button);
  359. ClassDB::bind_method(D_METHOD("set_cancel_button_text", "text"), &ConfirmationDialog::set_cancel_button_text);
  360. ClassDB::bind_method(D_METHOD("get_cancel_button_text"), &ConfirmationDialog::get_cancel_button_text);
  361. ADD_PROPERTY(PropertyInfo(Variant::STRING, "cancel_button_text"), "set_cancel_button_text", "get_cancel_button_text");
  362. }
  363. Button *ConfirmationDialog::get_cancel_button() {
  364. return cancel;
  365. }
  366. ConfirmationDialog::ConfirmationDialog() {
  367. set_title(TTRC("Please Confirm..."));
  368. set_min_size(Size2(200, 70));
  369. cancel = add_cancel_button();
  370. }