dialogs.cpp 17 KB

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