dialogs.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /*************************************************************************/
  2. /* dialogs.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/print_string.h"
  32. #include "core/translation.h"
  33. #include "line_edit.h"
  34. #ifdef TOOLS_ENABLED
  35. #include "editor/editor_node.h"
  36. #include "editor/editor_scale.h"
  37. #include "scene/main/viewport.h" // Only used to check for more modals when dimming the editor.
  38. #endif
  39. // WindowDialog
  40. void WindowDialog::_post_popup() {
  41. drag_type = DRAG_NONE; // just in case
  42. }
  43. void WindowDialog::_fix_size() {
  44. // Perhaps this should be called when the viewport resizes as well or windows go out of bounds...
  45. // Ensure the whole window is visible.
  46. Point2i pos = get_global_position();
  47. Size2i size = get_size();
  48. Size2i viewport_size = get_viewport_rect().size;
  49. // Windows require additional padding to keep the window chrome visible.
  50. Ref<StyleBox> panel = get_stylebox("panel", "WindowDialog");
  51. float top = 0;
  52. float left = 0;
  53. float bottom = 0;
  54. float right = 0;
  55. // Check validity, because the theme could contain a different type of StyleBox.
  56. if (panel->get_class() == "StyleBoxTexture") {
  57. Ref<StyleBoxTexture> panel_texture = Object::cast_to<StyleBoxTexture>(*panel);
  58. top = panel_texture->get_expand_margin_size(MARGIN_TOP);
  59. left = panel_texture->get_expand_margin_size(MARGIN_LEFT);
  60. bottom = panel_texture->get_expand_margin_size(MARGIN_BOTTOM);
  61. right = panel_texture->get_expand_margin_size(MARGIN_RIGHT);
  62. } else if (panel->get_class() == "StyleBoxFlat") {
  63. Ref<StyleBoxFlat> panel_flat = Object::cast_to<StyleBoxFlat>(*panel);
  64. top = panel_flat->get_expand_margin_size(MARGIN_TOP);
  65. left = panel_flat->get_expand_margin_size(MARGIN_LEFT);
  66. bottom = panel_flat->get_expand_margin_size(MARGIN_BOTTOM);
  67. right = panel_flat->get_expand_margin_size(MARGIN_RIGHT);
  68. }
  69. pos.x = MAX(left, MIN(pos.x, viewport_size.x - size.x - right));
  70. pos.y = MAX(top, MIN(pos.y, viewport_size.y - size.y - bottom));
  71. set_global_position(pos);
  72. if (resizable) {
  73. size.x = MIN(size.x, viewport_size.x - left - right);
  74. size.y = MIN(size.y, viewport_size.y - top - bottom);
  75. set_size(size);
  76. }
  77. }
  78. bool WindowDialog::has_point(const Point2 &p_point) const {
  79. Rect2 r(Point2(), get_size());
  80. // Enlarge upwards for title bar.
  81. int title_height = get_constant("title_height", "WindowDialog");
  82. r.position.y -= title_height;
  83. r.size.y += title_height;
  84. // Inflate by the resizable border thickness.
  85. if (resizable) {
  86. int scaleborder_size = get_constant("scaleborder_size", "WindowDialog");
  87. r.position.x -= scaleborder_size;
  88. r.size.width += scaleborder_size * 2;
  89. r.position.y -= scaleborder_size;
  90. r.size.height += scaleborder_size * 2;
  91. }
  92. return r.has_point(p_point);
  93. }
  94. void WindowDialog::_gui_input(const Ref<InputEvent> &p_event) {
  95. Ref<InputEventMouseButton> mb = p_event;
  96. if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT) {
  97. if (mb->is_pressed()) {
  98. // Begin a possible dragging operation.
  99. drag_type = _drag_hit_test(Point2(mb->get_position().x, mb->get_position().y));
  100. if (drag_type != DRAG_NONE)
  101. drag_offset = get_global_mouse_position() - get_position();
  102. drag_offset_far = get_position() + get_size() - get_global_mouse_position();
  103. } else if (drag_type != DRAG_NONE && !mb->is_pressed()) {
  104. // End a dragging operation.
  105. drag_type = DRAG_NONE;
  106. }
  107. }
  108. Ref<InputEventMouseMotion> mm = p_event;
  109. if (mm.is_valid()) {
  110. if (drag_type == DRAG_NONE) {
  111. // Update the cursor while moving along the borders.
  112. CursorShape cursor = CURSOR_ARROW;
  113. if (resizable) {
  114. int preview_drag_type = _drag_hit_test(Point2(mm->get_position().x, mm->get_position().y));
  115. switch (preview_drag_type) {
  116. case DRAG_RESIZE_TOP:
  117. case DRAG_RESIZE_BOTTOM:
  118. cursor = CURSOR_VSIZE;
  119. break;
  120. case DRAG_RESIZE_LEFT:
  121. case DRAG_RESIZE_RIGHT:
  122. cursor = CURSOR_HSIZE;
  123. break;
  124. case DRAG_RESIZE_TOP + DRAG_RESIZE_LEFT:
  125. case DRAG_RESIZE_BOTTOM + DRAG_RESIZE_RIGHT:
  126. cursor = CURSOR_FDIAGSIZE;
  127. break;
  128. case DRAG_RESIZE_TOP + DRAG_RESIZE_RIGHT:
  129. case DRAG_RESIZE_BOTTOM + DRAG_RESIZE_LEFT:
  130. cursor = CURSOR_BDIAGSIZE;
  131. break;
  132. }
  133. }
  134. if (get_cursor_shape() != cursor)
  135. set_default_cursor_shape(cursor);
  136. } else {
  137. // Update while in a dragging operation.
  138. Point2 global_pos = get_global_mouse_position();
  139. global_pos.y = MAX(global_pos.y, 0); // Ensure title bar stays visible.
  140. Rect2 rect = get_rect();
  141. Size2 min_size = get_combined_minimum_size();
  142. if (drag_type == DRAG_MOVE) {
  143. rect.position = global_pos - drag_offset;
  144. } else {
  145. if (drag_type & DRAG_RESIZE_TOP) {
  146. int bottom = rect.position.y + rect.size.height;
  147. int max_y = bottom - min_size.height;
  148. rect.position.y = MIN(global_pos.y - drag_offset.y, max_y);
  149. rect.size.height = bottom - rect.position.y;
  150. } else if (drag_type & DRAG_RESIZE_BOTTOM) {
  151. rect.size.height = global_pos.y - rect.position.y + drag_offset_far.y;
  152. }
  153. if (drag_type & DRAG_RESIZE_LEFT) {
  154. int right = rect.position.x + rect.size.width;
  155. int max_x = right - min_size.width;
  156. rect.position.x = MIN(global_pos.x - drag_offset.x, max_x);
  157. rect.size.width = right - rect.position.x;
  158. } else if (drag_type & DRAG_RESIZE_RIGHT) {
  159. rect.size.width = global_pos.x - rect.position.x + drag_offset_far.x;
  160. }
  161. }
  162. set_size(rect.size);
  163. set_position(rect.position);
  164. }
  165. }
  166. }
  167. void WindowDialog::_notification(int p_what) {
  168. switch (p_what) {
  169. case NOTIFICATION_DRAW: {
  170. RID canvas = get_canvas_item();
  171. // Draw the background.
  172. Ref<StyleBox> panel = get_stylebox("panel");
  173. Size2 size = get_size();
  174. panel->draw(canvas, Rect2(0, 0, size.x, size.y));
  175. // Draw the title bar text.
  176. Ref<Font> title_font = get_font("title_font", "WindowDialog");
  177. Color title_color = get_color("title_color", "WindowDialog");
  178. int title_height = get_constant("title_height", "WindowDialog");
  179. int font_height = title_font->get_height() - title_font->get_descent() * 2;
  180. int x = (size.x - title_font->get_string_size(xl_title).x) / 2;
  181. int y = (-title_height + font_height) / 2;
  182. title_font->draw(canvas, Point2(x, y), xl_title, title_color, size.x - panel->get_minimum_size().x);
  183. } break;
  184. case NOTIFICATION_THEME_CHANGED:
  185. case NOTIFICATION_ENTER_TREE: {
  186. close_button->set_normal_texture(get_icon("close", "WindowDialog"));
  187. close_button->set_pressed_texture(get_icon("close", "WindowDialog"));
  188. close_button->set_hover_texture(get_icon("close_highlight", "WindowDialog"));
  189. close_button->set_anchor(MARGIN_LEFT, ANCHOR_END);
  190. close_button->set_begin(Point2(-get_constant("close_h_ofs", "WindowDialog"), -get_constant("close_v_ofs", "WindowDialog")));
  191. } break;
  192. case NOTIFICATION_TRANSLATION_CHANGED: {
  193. String new_title = tr(title);
  194. if (new_title != xl_title) {
  195. xl_title = new_title;
  196. minimum_size_changed();
  197. update();
  198. }
  199. } break;
  200. case NOTIFICATION_MOUSE_EXIT: {
  201. // Reset the mouse cursor when leaving the resizable window border.
  202. if (resizable && !drag_type) {
  203. if (get_default_cursor_shape() != CURSOR_ARROW)
  204. set_default_cursor_shape(CURSOR_ARROW);
  205. }
  206. } break;
  207. #ifdef TOOLS_ENABLED
  208. case NOTIFICATION_POST_POPUP: {
  209. if (get_tree() && Engine::get_singleton()->is_editor_hint() && EditorNode::get_singleton()) {
  210. was_editor_dimmed = EditorNode::get_singleton()->is_editor_dimmed();
  211. EditorNode::get_singleton()->dim_editor(true);
  212. }
  213. } break;
  214. case NOTIFICATION_POPUP_HIDE: {
  215. if (get_tree() && Engine::get_singleton()->is_editor_hint() && EditorNode::get_singleton() && !was_editor_dimmed) {
  216. EditorNode::get_singleton()->dim_editor(false);
  217. set_pass_on_modal_close_click(false);
  218. }
  219. } break;
  220. #endif
  221. }
  222. }
  223. void WindowDialog::_closed() {
  224. _close_pressed();
  225. hide();
  226. }
  227. int WindowDialog::_drag_hit_test(const Point2 &pos) const {
  228. int drag_type = DRAG_NONE;
  229. if (resizable) {
  230. int title_height = get_constant("title_height", "WindowDialog");
  231. int scaleborder_size = get_constant("scaleborder_size", "WindowDialog");
  232. Rect2 rect = get_rect();
  233. if (pos.y < (-title_height + scaleborder_size))
  234. drag_type = DRAG_RESIZE_TOP;
  235. else if (pos.y >= (rect.size.height - scaleborder_size))
  236. drag_type = DRAG_RESIZE_BOTTOM;
  237. if (pos.x < scaleborder_size)
  238. drag_type |= DRAG_RESIZE_LEFT;
  239. else if (pos.x >= (rect.size.width - scaleborder_size))
  240. drag_type |= DRAG_RESIZE_RIGHT;
  241. }
  242. if (drag_type == DRAG_NONE && pos.y < 0)
  243. drag_type = DRAG_MOVE;
  244. return drag_type;
  245. }
  246. void WindowDialog::set_title(const String &p_title) {
  247. if (title != p_title) {
  248. title = p_title;
  249. xl_title = tr(p_title);
  250. minimum_size_changed();
  251. update();
  252. }
  253. }
  254. String WindowDialog::get_title() const {
  255. return title;
  256. }
  257. void WindowDialog::set_resizable(bool p_resizable) {
  258. resizable = p_resizable;
  259. }
  260. bool WindowDialog::get_resizable() const {
  261. return resizable;
  262. }
  263. Size2 WindowDialog::get_minimum_size() const {
  264. Ref<Font> font = get_font("title_font", "WindowDialog");
  265. const int button_width = close_button->get_combined_minimum_size().x;
  266. const int title_width = font->get_string_size(xl_title).x;
  267. const int padding = button_width / 2;
  268. const int button_area = button_width + padding;
  269. // As the title gets centered, title_width + close_button_width is not enough.
  270. // We want a width w, such that w / 2 - title_width / 2 >= button_area, i.e.
  271. // w >= 2 * button_area + title_width
  272. return Size2(2 * button_area + title_width, 1);
  273. }
  274. TextureButton *WindowDialog::get_close_button() {
  275. return close_button;
  276. }
  277. void WindowDialog::_bind_methods() {
  278. ClassDB::bind_method(D_METHOD("_gui_input"), &WindowDialog::_gui_input);
  279. ClassDB::bind_method(D_METHOD("set_title", "title"), &WindowDialog::set_title);
  280. ClassDB::bind_method(D_METHOD("get_title"), &WindowDialog::get_title);
  281. ClassDB::bind_method(D_METHOD("set_resizable", "resizable"), &WindowDialog::set_resizable);
  282. ClassDB::bind_method(D_METHOD("get_resizable"), &WindowDialog::get_resizable);
  283. ClassDB::bind_method(D_METHOD("get_close_button"), &WindowDialog::get_close_button);
  284. ADD_PROPERTY(PropertyInfo(Variant::STRING, "window_title", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT_INTL), "set_title", "get_title");
  285. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resizable", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT_INTL), "set_resizable", "get_resizable");
  286. }
  287. WindowDialog::WindowDialog() {
  288. drag_type = DRAG_NONE;
  289. resizable = false;
  290. close_button = memnew(TextureButton);
  291. add_child(close_button);
  292. close_button->connect("pressed", callable_mp(this, &WindowDialog::_closed));
  293. #ifdef TOOLS_ENABLED
  294. was_editor_dimmed = false;
  295. #endif
  296. }
  297. WindowDialog::~WindowDialog() {
  298. }
  299. // PopupDialog
  300. void PopupDialog::_notification(int p_what) {
  301. if (p_what == NOTIFICATION_DRAW) {
  302. RID ci = get_canvas_item();
  303. get_stylebox("panel")->draw(ci, Rect2(Point2(), get_size()));
  304. }
  305. }
  306. PopupDialog::PopupDialog() {
  307. }
  308. PopupDialog::~PopupDialog() {
  309. }
  310. // AcceptDialog
  311. void AcceptDialog::_post_popup() {
  312. WindowDialog::_post_popup();
  313. get_ok()->grab_focus();
  314. }
  315. void AcceptDialog::_notification(int p_what) {
  316. switch (p_what) {
  317. case NOTIFICATION_MODAL_CLOSE: {
  318. cancel_pressed();
  319. } break;
  320. case NOTIFICATION_READY:
  321. case NOTIFICATION_RESIZED: {
  322. _update_child_rects();
  323. } break;
  324. }
  325. }
  326. void AcceptDialog::_text_entered(const String &p_text) {
  327. _ok_pressed();
  328. }
  329. void AcceptDialog::_ok_pressed() {
  330. if (hide_on_ok)
  331. hide();
  332. ok_pressed();
  333. emit_signal("confirmed");
  334. }
  335. void AcceptDialog::_close_pressed() {
  336. cancel_pressed();
  337. }
  338. // FIXME: This is redundant with _closed_pressed, but there's a slight behavior
  339. // change (WindowDialog's _closed() also calls hide()) which should be assessed.
  340. void AcceptDialog::_on_close_pressed() {
  341. _closed(); // From WindowDialog.
  342. }
  343. String AcceptDialog::get_text() const {
  344. return label->get_text();
  345. }
  346. void AcceptDialog::set_text(String p_text) {
  347. label->set_text(p_text);
  348. minimum_size_changed();
  349. _update_child_rects();
  350. }
  351. void AcceptDialog::set_hide_on_ok(bool p_hide) {
  352. hide_on_ok = p_hide;
  353. }
  354. bool AcceptDialog::get_hide_on_ok() const {
  355. return hide_on_ok;
  356. }
  357. void AcceptDialog::set_autowrap(bool p_autowrap) {
  358. label->set_autowrap(p_autowrap);
  359. }
  360. bool AcceptDialog::has_autowrap() {
  361. return label->has_autowrap();
  362. }
  363. void AcceptDialog::register_text_enter(Node *p_line_edit) {
  364. ERR_FAIL_NULL(p_line_edit);
  365. LineEdit *line_edit = Object::cast_to<LineEdit>(p_line_edit);
  366. if (line_edit)
  367. line_edit->connect("text_entered", callable_mp(this, &AcceptDialog::_text_entered));
  368. }
  369. void AcceptDialog::_update_child_rects() {
  370. Size2 label_size = label->get_minimum_size();
  371. if (label->get_text().empty()) {
  372. label_size.height = 0;
  373. }
  374. int margin = get_constant("margin", "Dialogs");
  375. Size2 size = get_size();
  376. Size2 hminsize = hbc->get_combined_minimum_size();
  377. Vector2 cpos(margin, margin + label_size.height);
  378. Vector2 csize(size.x - margin * 2, size.y - margin * 3 - hminsize.y - label_size.height);
  379. for (int i = 0; i < get_child_count(); i++) {
  380. Control *c = Object::cast_to<Control>(get_child(i));
  381. if (!c)
  382. continue;
  383. if (c == hbc || c == label || c == get_close_button() || c->is_set_as_toplevel())
  384. continue;
  385. c->set_position(cpos);
  386. c->set_size(csize);
  387. }
  388. cpos.y += csize.y + margin;
  389. csize.y = hminsize.y;
  390. hbc->set_position(cpos);
  391. hbc->set_size(csize);
  392. }
  393. Size2 AcceptDialog::get_minimum_size() const {
  394. int margin = get_constant("margin", "Dialogs");
  395. Size2 minsize = label->get_combined_minimum_size();
  396. for (int i = 0; i < get_child_count(); i++) {
  397. Control *c = Object::cast_to<Control>(get_child(i));
  398. if (!c)
  399. continue;
  400. if (c == hbc || c == label || c == const_cast<AcceptDialog *>(this)->get_close_button() || c->is_set_as_toplevel())
  401. continue;
  402. Size2 cminsize = c->get_combined_minimum_size();
  403. minsize.x = MAX(cminsize.x, minsize.x);
  404. minsize.y = MAX(cminsize.y, minsize.y);
  405. }
  406. Size2 hminsize = hbc->get_combined_minimum_size();
  407. minsize.x = MAX(hminsize.x, minsize.x);
  408. minsize.y += hminsize.y;
  409. minsize.x += margin * 2;
  410. minsize.y += margin * 3; //one as separation between hbc and child
  411. Size2 wmsize = WindowDialog::get_minimum_size();
  412. minsize.x = MAX(wmsize.x, minsize.x);
  413. return minsize;
  414. }
  415. void AcceptDialog::_custom_action(const String &p_action) {
  416. emit_signal("custom_action", p_action);
  417. custom_action(p_action);
  418. }
  419. Button *AcceptDialog::add_button(const String &p_text, bool p_right, const String &p_action) {
  420. Button *button = memnew(Button);
  421. button->set_text(p_text);
  422. if (p_right) {
  423. hbc->add_child(button);
  424. hbc->add_spacer();
  425. } else {
  426. hbc->add_child(button);
  427. hbc->move_child(button, 0);
  428. hbc->add_spacer(true);
  429. }
  430. if (p_action != "") {
  431. button->connect("pressed", callable_mp(this, &AcceptDialog::_custom_action), varray(p_action));
  432. }
  433. return button;
  434. }
  435. Button *AcceptDialog::add_cancel(const String &p_cancel) {
  436. String c = p_cancel;
  437. if (p_cancel == "")
  438. c = RTR("Cancel");
  439. Button *b = swap_ok_cancel ? add_button(c, true) : add_button(c);
  440. b->connect("pressed", callable_mp(this, &AcceptDialog::_on_close_pressed));
  441. return b;
  442. }
  443. void AcceptDialog::_bind_methods() {
  444. ClassDB::bind_method(D_METHOD("get_ok"), &AcceptDialog::get_ok);
  445. ClassDB::bind_method(D_METHOD("get_label"), &AcceptDialog::get_label);
  446. ClassDB::bind_method(D_METHOD("set_hide_on_ok", "enabled"), &AcceptDialog::set_hide_on_ok);
  447. ClassDB::bind_method(D_METHOD("get_hide_on_ok"), &AcceptDialog::get_hide_on_ok);
  448. ClassDB::bind_method(D_METHOD("add_button", "text", "right", "action"), &AcceptDialog::add_button, DEFVAL(false), DEFVAL(""));
  449. ClassDB::bind_method(D_METHOD("add_cancel", "name"), &AcceptDialog::add_cancel);
  450. ClassDB::bind_method(D_METHOD("register_text_enter", "line_edit"), &AcceptDialog::register_text_enter);
  451. ClassDB::bind_method(D_METHOD("set_text", "text"), &AcceptDialog::set_text);
  452. ClassDB::bind_method(D_METHOD("get_text"), &AcceptDialog::get_text);
  453. ClassDB::bind_method(D_METHOD("set_autowrap", "autowrap"), &AcceptDialog::set_autowrap);
  454. ClassDB::bind_method(D_METHOD("has_autowrap"), &AcceptDialog::has_autowrap);
  455. ADD_SIGNAL(MethodInfo("confirmed"));
  456. ADD_SIGNAL(MethodInfo("custom_action", PropertyInfo(Variant::STRING_NAME, "action")));
  457. ADD_GROUP("Dialog", "dialog");
  458. ADD_PROPERTY(PropertyInfo(Variant::STRING, "dialog_text", PROPERTY_HINT_MULTILINE_TEXT, "", PROPERTY_USAGE_DEFAULT_INTL), "set_text", "get_text");
  459. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dialog_hide_on_ok"), "set_hide_on_ok", "get_hide_on_ok");
  460. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dialog_autowrap"), "set_autowrap", "has_autowrap");
  461. }
  462. bool AcceptDialog::swap_ok_cancel = false;
  463. void AcceptDialog::set_swap_ok_cancel(bool p_swap) {
  464. swap_ok_cancel = p_swap;
  465. }
  466. AcceptDialog::AcceptDialog() {
  467. int margin = get_constant("margin", "Dialogs");
  468. int button_margin = get_constant("button_margin", "Dialogs");
  469. label = memnew(Label);
  470. label->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  471. label->set_anchor(MARGIN_BOTTOM, ANCHOR_END);
  472. label->set_begin(Point2(margin, margin));
  473. label->set_end(Point2(-margin, -button_margin - 10));
  474. add_child(label);
  475. hbc = memnew(HBoxContainer);
  476. add_child(hbc);
  477. hbc->add_spacer();
  478. ok = memnew(Button);
  479. ok->set_text(RTR("OK"));
  480. hbc->add_child(ok);
  481. hbc->add_spacer();
  482. ok->connect("pressed", callable_mp(this, &AcceptDialog::_ok_pressed));
  483. set_as_toplevel(true);
  484. hide_on_ok = true;
  485. set_title(RTR("Alert!"));
  486. }
  487. AcceptDialog::~AcceptDialog() {
  488. }
  489. // ConfirmationDialog
  490. void ConfirmationDialog::_bind_methods() {
  491. ClassDB::bind_method(D_METHOD("get_cancel"), &ConfirmationDialog::get_cancel);
  492. }
  493. Button *ConfirmationDialog::get_cancel() {
  494. return cancel;
  495. }
  496. ConfirmationDialog::ConfirmationDialog() {
  497. set_title(RTR("Please Confirm..."));
  498. #ifdef TOOLS_ENABLED
  499. set_custom_minimum_size(Size2(200, 70) * EDSCALE);
  500. #endif
  501. cancel = add_cancel();
  502. }