dialogs.cpp 17 KB

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