dialogs.cpp 16 KB

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