dialogs.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /*************************************************************************/
  2. /* dialogs.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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("_closed"), &WindowDialog::_closed);
  284. ClassDB::bind_method(D_METHOD("get_close_button"), &WindowDialog::get_close_button);
  285. ADD_PROPERTY(PropertyInfo(Variant::STRING, "window_title", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT_INTL), "set_title", "get_title");
  286. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resizable", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT_INTL), "set_resizable", "get_resizable");
  287. }
  288. WindowDialog::WindowDialog() {
  289. drag_type = DRAG_NONE;
  290. resizable = false;
  291. close_button = memnew(TextureButton);
  292. add_child(close_button);
  293. close_button->connect("pressed", this, "_closed");
  294. #ifdef TOOLS_ENABLED
  295. was_editor_dimmed = false;
  296. #endif
  297. }
  298. WindowDialog::~WindowDialog() {
  299. }
  300. // PopupDialog
  301. void PopupDialog::_notification(int p_what) {
  302. if (p_what == NOTIFICATION_DRAW) {
  303. RID ci = get_canvas_item();
  304. get_stylebox("panel")->draw(ci, Rect2(Point2(), get_size()));
  305. }
  306. }
  307. PopupDialog::PopupDialog() {
  308. }
  309. PopupDialog::~PopupDialog() {
  310. }
  311. // AcceptDialog
  312. void AcceptDialog::_post_popup() {
  313. WindowDialog::_post_popup();
  314. get_ok()->grab_focus();
  315. }
  316. void AcceptDialog::_notification(int p_what) {
  317. switch (p_what) {
  318. case NOTIFICATION_MODAL_CLOSE: {
  319. cancel_pressed();
  320. } break;
  321. case NOTIFICATION_READY:
  322. case NOTIFICATION_RESIZED: {
  323. _update_child_rects();
  324. } break;
  325. }
  326. }
  327. void AcceptDialog::_builtin_text_entered(const String &p_text) {
  328. _ok_pressed();
  329. }
  330. void AcceptDialog::_ok_pressed() {
  331. if (hide_on_ok)
  332. hide();
  333. ok_pressed();
  334. emit_signal("confirmed");
  335. }
  336. void AcceptDialog::_close_pressed() {
  337. cancel_pressed();
  338. }
  339. String AcceptDialog::get_text() const {
  340. return label->get_text();
  341. }
  342. void AcceptDialog::set_text(String p_text) {
  343. label->set_text(p_text);
  344. minimum_size_changed();
  345. _update_child_rects();
  346. }
  347. void AcceptDialog::set_hide_on_ok(bool p_hide) {
  348. hide_on_ok = p_hide;
  349. }
  350. bool AcceptDialog::get_hide_on_ok() const {
  351. return hide_on_ok;
  352. }
  353. void AcceptDialog::set_autowrap(bool p_autowrap) {
  354. label->set_autowrap(p_autowrap);
  355. }
  356. bool AcceptDialog::has_autowrap() {
  357. return label->has_autowrap();
  358. }
  359. void AcceptDialog::register_text_enter(Node *p_line_edit) {
  360. ERR_FAIL_NULL(p_line_edit);
  361. LineEdit *line_edit = Object::cast_to<LineEdit>(p_line_edit);
  362. if (line_edit)
  363. line_edit->connect("text_entered", this, "_builtin_text_entered");
  364. }
  365. void AcceptDialog::_update_child_rects() {
  366. Size2 label_size = label->get_minimum_size();
  367. if (label->get_text().empty()) {
  368. label_size.height = 0;
  369. }
  370. int margin = get_constant("margin", "Dialogs");
  371. Size2 size = get_size();
  372. Size2 hminsize = hbc->get_combined_minimum_size();
  373. Vector2 cpos(margin, margin + label_size.height);
  374. Vector2 csize(size.x - margin * 2, size.y - margin * 3 - hminsize.y - label_size.height);
  375. for (int i = 0; i < get_child_count(); i++) {
  376. Control *c = Object::cast_to<Control>(get_child(i));
  377. if (!c)
  378. continue;
  379. if (c == hbc || c == label || c == get_close_button() || c->is_set_as_toplevel())
  380. continue;
  381. c->set_position(cpos);
  382. c->set_size(csize);
  383. }
  384. cpos.y += csize.y + margin;
  385. csize.y = hminsize.y;
  386. hbc->set_position(cpos);
  387. hbc->set_size(csize);
  388. }
  389. Size2 AcceptDialog::get_minimum_size() const {
  390. int margin = get_constant("margin", "Dialogs");
  391. Size2 minsize = label->get_combined_minimum_size();
  392. for (int i = 0; i < get_child_count(); i++) {
  393. Control *c = Object::cast_to<Control>(get_child(i));
  394. if (!c)
  395. continue;
  396. if (c == hbc || c == label || c == const_cast<AcceptDialog *>(this)->get_close_button() || c->is_set_as_toplevel())
  397. continue;
  398. Size2 cminsize = c->get_combined_minimum_size();
  399. minsize.x = MAX(cminsize.x, minsize.x);
  400. minsize.y = MAX(cminsize.y, minsize.y);
  401. }
  402. Size2 hminsize = hbc->get_combined_minimum_size();
  403. minsize.x = MAX(hminsize.x, minsize.x);
  404. minsize.y += hminsize.y;
  405. minsize.x += margin * 2;
  406. minsize.y += margin * 3; //one as separation between hbc and child
  407. Size2 wmsize = WindowDialog::get_minimum_size();
  408. minsize.x = MAX(wmsize.x, minsize.x);
  409. return minsize;
  410. }
  411. void AcceptDialog::_custom_action(const String &p_action) {
  412. emit_signal("custom_action", p_action);
  413. custom_action(p_action);
  414. }
  415. Button *AcceptDialog::add_button(const String &p_text, bool p_right, const String &p_action) {
  416. Button *button = memnew(Button);
  417. button->set_text(p_text);
  418. if (p_right) {
  419. hbc->add_child(button);
  420. hbc->add_spacer();
  421. } else {
  422. hbc->add_child(button);
  423. hbc->move_child(button, 0);
  424. hbc->add_spacer(true);
  425. }
  426. if (p_action != "") {
  427. button->connect("pressed", this, "_custom_action", varray(p_action));
  428. }
  429. return button;
  430. }
  431. Button *AcceptDialog::add_cancel(const String &p_cancel) {
  432. String c = p_cancel;
  433. if (p_cancel == "")
  434. c = RTR("Cancel");
  435. Button *b = swap_ok_cancel ? add_button(c, true) : add_button(c);
  436. b->connect("pressed", this, "_closed");
  437. return b;
  438. }
  439. void AcceptDialog::_bind_methods() {
  440. ClassDB::bind_method(D_METHOD("_ok"), &AcceptDialog::_ok_pressed);
  441. ClassDB::bind_method(D_METHOD("get_ok"), &AcceptDialog::get_ok);
  442. ClassDB::bind_method(D_METHOD("get_label"), &AcceptDialog::get_label);
  443. ClassDB::bind_method(D_METHOD("set_hide_on_ok", "enabled"), &AcceptDialog::set_hide_on_ok);
  444. ClassDB::bind_method(D_METHOD("get_hide_on_ok"), &AcceptDialog::get_hide_on_ok);
  445. ClassDB::bind_method(D_METHOD("add_button", "text", "right", "action"), &AcceptDialog::add_button, DEFVAL(false), DEFVAL(""));
  446. ClassDB::bind_method(D_METHOD("add_cancel", "name"), &AcceptDialog::add_cancel);
  447. ClassDB::bind_method(D_METHOD("_builtin_text_entered"), &AcceptDialog::_builtin_text_entered);
  448. ClassDB::bind_method(D_METHOD("register_text_enter", "line_edit"), &AcceptDialog::register_text_enter);
  449. ClassDB::bind_method(D_METHOD("_custom_action"), &AcceptDialog::_custom_action);
  450. ClassDB::bind_method(D_METHOD("set_text", "text"), &AcceptDialog::set_text);
  451. ClassDB::bind_method(D_METHOD("get_text"), &AcceptDialog::get_text);
  452. ClassDB::bind_method(D_METHOD("set_autowrap", "autowrap"), &AcceptDialog::set_autowrap);
  453. ClassDB::bind_method(D_METHOD("has_autowrap"), &AcceptDialog::has_autowrap);
  454. ADD_SIGNAL(MethodInfo("confirmed"));
  455. ADD_SIGNAL(MethodInfo("custom_action", PropertyInfo(Variant::STRING, "action")));
  456. ADD_GROUP("Dialog", "dialog");
  457. ADD_PROPERTY(PropertyInfo(Variant::STRING, "dialog_text", PROPERTY_HINT_MULTILINE_TEXT, "", PROPERTY_USAGE_DEFAULT_INTL), "set_text", "get_text");
  458. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dialog_hide_on_ok"), "set_hide_on_ok", "get_hide_on_ok");
  459. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dialog_autowrap"), "set_autowrap", "has_autowrap");
  460. }
  461. bool AcceptDialog::swap_ok_cancel = false;
  462. void AcceptDialog::set_swap_ok_cancel(bool p_swap) {
  463. swap_ok_cancel = p_swap;
  464. }
  465. AcceptDialog::AcceptDialog() {
  466. int margin = get_constant("margin", "Dialogs");
  467. int button_margin = get_constant("button_margin", "Dialogs");
  468. label = memnew(Label);
  469. label->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  470. label->set_anchor(MARGIN_BOTTOM, ANCHOR_END);
  471. label->set_begin(Point2(margin, margin));
  472. label->set_end(Point2(-margin, -button_margin - 10));
  473. add_child(label);
  474. hbc = memnew(HBoxContainer);
  475. add_child(hbc);
  476. hbc->add_spacer();
  477. ok = memnew(Button);
  478. ok->set_text(RTR("OK"));
  479. hbc->add_child(ok);
  480. hbc->add_spacer();
  481. ok->connect("pressed", this, "_ok");
  482. set_as_toplevel(true);
  483. hide_on_ok = true;
  484. set_title(RTR("Alert!"));
  485. }
  486. AcceptDialog::~AcceptDialog() {
  487. }
  488. // ConfirmationDialog
  489. void ConfirmationDialog::_bind_methods() {
  490. ClassDB::bind_method(D_METHOD("get_cancel"), &ConfirmationDialog::get_cancel);
  491. }
  492. Button *ConfirmationDialog::get_cancel() {
  493. return cancel;
  494. }
  495. ConfirmationDialog::ConfirmationDialog() {
  496. set_title(RTR("Please Confirm..."));
  497. #ifdef TOOLS_ENABLED
  498. set_custom_minimum_size(Size2(200, 70) * EDSCALE);
  499. #endif
  500. cancel = add_cancel();
  501. }