2
0

dialogs.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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/os/keyboard.h"
  32. #include "core/string/print_string.h"
  33. #include "core/string/translation.h"
  34. #include "line_edit.h"
  35. #ifdef TOOLS_ENABLED
  36. #include "editor/editor_node.h"
  37. #include "scene/main/window.h" // Only used to check for more modals when dimming the editor.
  38. #endif
  39. // AcceptDialog
  40. void AcceptDialog::_input_from_window(const Ref<InputEvent> &p_event) {
  41. Ref<InputEventKey> key = p_event;
  42. if (key.is_valid() && key->is_pressed() && key->get_keycode() == KEY_ESCAPE) {
  43. _cancel_pressed();
  44. }
  45. }
  46. void AcceptDialog::_parent_focused() {
  47. if (!is_exclusive()) {
  48. _cancel_pressed();
  49. }
  50. }
  51. void AcceptDialog::_notification(int p_what) {
  52. switch (p_what) {
  53. case NOTIFICATION_VISIBILITY_CHANGED: {
  54. if (is_visible()) {
  55. get_ok_button()->grab_focus();
  56. _update_child_rects();
  57. parent_visible = get_parent_visible_window();
  58. if (parent_visible) {
  59. parent_visible->connect("focus_entered", callable_mp(this, &AcceptDialog::_parent_focused));
  60. }
  61. } else {
  62. if (parent_visible) {
  63. parent_visible->disconnect("focus_entered", callable_mp(this, &AcceptDialog::_parent_focused));
  64. parent_visible = nullptr;
  65. }
  66. }
  67. } break;
  68. case NOTIFICATION_THEME_CHANGED: {
  69. bg->add_theme_style_override("panel", bg->get_theme_stylebox(SNAME("panel"), SNAME("AcceptDialog")));
  70. } break;
  71. case NOTIFICATION_EXIT_TREE: {
  72. if (parent_visible) {
  73. parent_visible->disconnect("focus_entered", callable_mp(this, &AcceptDialog::_parent_focused));
  74. parent_visible = nullptr;
  75. }
  76. } break;
  77. case NOTIFICATION_READY:
  78. case NOTIFICATION_WM_SIZE_CHANGED: {
  79. if (is_visible()) {
  80. _update_child_rects();
  81. }
  82. } break;
  83. case NOTIFICATION_WM_CLOSE_REQUEST: {
  84. _cancel_pressed();
  85. } break;
  86. }
  87. }
  88. void AcceptDialog::_text_submitted(const String &p_text) {
  89. _ok_pressed();
  90. }
  91. void AcceptDialog::_ok_pressed() {
  92. if (hide_on_ok) {
  93. set_visible(false);
  94. }
  95. ok_pressed();
  96. emit_signal(SNAME("confirmed"));
  97. }
  98. void AcceptDialog::_cancel_pressed() {
  99. Window *parent_window = parent_visible;
  100. if (parent_visible) {
  101. parent_visible->disconnect("focus_entered", callable_mp(this, &AcceptDialog::_parent_focused));
  102. parent_visible = nullptr;
  103. }
  104. call_deferred(SNAME("hide"));
  105. emit_signal(SNAME("cancelled"));
  106. cancel_pressed();
  107. if (parent_window) {
  108. //parent_window->grab_focus();
  109. }
  110. }
  111. String AcceptDialog::get_text() const {
  112. return label->get_text();
  113. }
  114. void AcceptDialog::set_text(String p_text) {
  115. label->set_text(p_text);
  116. child_controls_changed();
  117. if (is_visible()) {
  118. _update_child_rects();
  119. }
  120. }
  121. void AcceptDialog::set_hide_on_ok(bool p_hide) {
  122. hide_on_ok = p_hide;
  123. }
  124. bool AcceptDialog::get_hide_on_ok() const {
  125. return hide_on_ok;
  126. }
  127. void AcceptDialog::set_autowrap(bool p_autowrap) {
  128. label->set_autowrap_mode(p_autowrap ? Label::AUTOWRAP_WORD : Label::AUTOWRAP_OFF);
  129. }
  130. bool AcceptDialog::has_autowrap() {
  131. return label->get_autowrap_mode() != Label::AUTOWRAP_OFF;
  132. }
  133. void AcceptDialog::register_text_enter(Control *p_line_edit) {
  134. ERR_FAIL_NULL(p_line_edit);
  135. LineEdit *line_edit = Object::cast_to<LineEdit>(p_line_edit);
  136. if (line_edit) {
  137. line_edit->connect("text_submitted", callable_mp(this, &AcceptDialog::_text_submitted));
  138. }
  139. }
  140. void AcceptDialog::_update_child_rects() {
  141. Size2 label_size = label->get_minimum_size();
  142. if (label->get_text().is_empty()) {
  143. label_size.height = 0;
  144. }
  145. int margin = hbc->get_theme_constant(SNAME("margin"), SNAME("Dialogs"));
  146. Size2 size = get_size();
  147. Size2 hminsize = hbc->get_combined_minimum_size();
  148. Vector2 cpos(margin, margin + label_size.height);
  149. Vector2 csize(size.x - margin * 2, size.y - margin * 3 - hminsize.y - label_size.height);
  150. for (int i = 0; i < get_child_count(); i++) {
  151. Control *c = Object::cast_to<Control>(get_child(i));
  152. if (!c) {
  153. continue;
  154. }
  155. if (c == hbc || c == label || c == bg || c->is_set_as_top_level()) {
  156. continue;
  157. }
  158. c->set_position(cpos);
  159. c->set_size(csize);
  160. }
  161. cpos.y += csize.y + margin;
  162. csize.y = hminsize.y;
  163. hbc->set_position(cpos);
  164. hbc->set_size(csize);
  165. bg->set_position(Point2());
  166. bg->set_size(size);
  167. }
  168. Size2 AcceptDialog::_get_contents_minimum_size() const {
  169. int margin = hbc->get_theme_constant(SNAME("margin"), SNAME("Dialogs"));
  170. Size2 minsize = label->get_combined_minimum_size();
  171. for (int i = 0; i < get_child_count(); i++) {
  172. Control *c = Object::cast_to<Control>(get_child(i));
  173. if (!c) {
  174. continue;
  175. }
  176. if (c == hbc || c == label || c->is_set_as_top_level()) {
  177. continue;
  178. }
  179. Size2 cminsize = c->get_combined_minimum_size();
  180. minsize.x = MAX(cminsize.x, minsize.x);
  181. minsize.y = MAX(cminsize.y, minsize.y);
  182. }
  183. Size2 hminsize = hbc->get_combined_minimum_size();
  184. minsize.x = MAX(hminsize.x, minsize.x);
  185. minsize.y += hminsize.y;
  186. minsize.x += margin * 2;
  187. minsize.y += margin * 3; //one as separation between hbc and child
  188. Size2 wmsize = get_min_size();
  189. minsize.x = MAX(wmsize.x, minsize.x);
  190. return minsize;
  191. }
  192. void AcceptDialog::_custom_action(const String &p_action) {
  193. emit_signal(SNAME("custom_action"), p_action);
  194. custom_action(p_action);
  195. }
  196. Button *AcceptDialog::add_button(const String &p_text, bool p_right, const String &p_action) {
  197. Button *button = memnew(Button);
  198. button->set_text(p_text);
  199. if (p_right) {
  200. hbc->add_child(button);
  201. hbc->add_spacer();
  202. } else {
  203. hbc->add_child(button);
  204. hbc->move_child(button, 0);
  205. hbc->add_spacer(true);
  206. }
  207. if (p_action != "") {
  208. button->connect("pressed", callable_mp(this, &AcceptDialog::_custom_action), varray(p_action));
  209. }
  210. return button;
  211. }
  212. Button *AcceptDialog::add_cancel_button(const String &p_cancel) {
  213. String c = p_cancel;
  214. if (p_cancel == "") {
  215. c = TTRC("Cancel");
  216. }
  217. Button *b = swap_cancel_ok ? add_button(c, true) : add_button(c);
  218. b->connect("pressed", callable_mp(this, &AcceptDialog::_cancel_pressed));
  219. return b;
  220. }
  221. void AcceptDialog::remove_button(Control *p_button) {
  222. Button *button = Object::cast_to<Button>(p_button);
  223. ERR_FAIL_NULL(button);
  224. ERR_FAIL_COND_MSG(button->get_parent() != hbc, vformat("Cannot remove button %s as it does not belong to this dialog.", button->get_name()));
  225. ERR_FAIL_COND_MSG(button == ok, "Cannot remove dialog's OK button.");
  226. Node *right_spacer = hbc->get_child(button->get_index() + 1);
  227. // Should always be valid but let's avoid crashing
  228. if (right_spacer) {
  229. hbc->remove_child(right_spacer);
  230. memdelete(right_spacer);
  231. }
  232. hbc->remove_child(button);
  233. if (button->is_connected("pressed", callable_mp(this, &AcceptDialog::_custom_action))) {
  234. button->disconnect("pressed", callable_mp(this, &AcceptDialog::_custom_action));
  235. }
  236. if (button->is_connected("pressed", callable_mp(this, &AcceptDialog::_cancel_pressed))) {
  237. button->disconnect("pressed", callable_mp(this, &AcceptDialog::_cancel_pressed));
  238. }
  239. }
  240. void AcceptDialog::_bind_methods() {
  241. ClassDB::bind_method(D_METHOD("get_ok_button"), &AcceptDialog::get_ok_button);
  242. ClassDB::bind_method(D_METHOD("get_label"), &AcceptDialog::get_label);
  243. ClassDB::bind_method(D_METHOD("set_hide_on_ok", "enabled"), &AcceptDialog::set_hide_on_ok);
  244. ClassDB::bind_method(D_METHOD("get_hide_on_ok"), &AcceptDialog::get_hide_on_ok);
  245. ClassDB::bind_method(D_METHOD("add_button", "text", "right", "action"), &AcceptDialog::add_button, DEFVAL(false), DEFVAL(""));
  246. ClassDB::bind_method(D_METHOD("add_cancel_button", "name"), &AcceptDialog::add_cancel_button);
  247. ClassDB::bind_method(D_METHOD("remove_button", "button"), &AcceptDialog::remove_button);
  248. ClassDB::bind_method(D_METHOD("register_text_enter", "line_edit"), &AcceptDialog::register_text_enter);
  249. ClassDB::bind_method(D_METHOD("set_text", "text"), &AcceptDialog::set_text);
  250. ClassDB::bind_method(D_METHOD("get_text"), &AcceptDialog::get_text);
  251. ClassDB::bind_method(D_METHOD("set_autowrap", "autowrap"), &AcceptDialog::set_autowrap);
  252. ClassDB::bind_method(D_METHOD("has_autowrap"), &AcceptDialog::has_autowrap);
  253. ADD_SIGNAL(MethodInfo("confirmed"));
  254. ADD_SIGNAL(MethodInfo("cancelled"));
  255. ADD_SIGNAL(MethodInfo("custom_action", PropertyInfo(Variant::STRING_NAME, "action")));
  256. ADD_GROUP("Dialog", "dialog");
  257. ADD_PROPERTY(PropertyInfo(Variant::STRING, "dialog_text", PROPERTY_HINT_MULTILINE_TEXT, "", PROPERTY_USAGE_DEFAULT_INTL), "set_text", "get_text");
  258. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dialog_hide_on_ok"), "set_hide_on_ok", "get_hide_on_ok");
  259. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dialog_autowrap"), "set_autowrap", "has_autowrap");
  260. }
  261. bool AcceptDialog::swap_cancel_ok = false;
  262. void AcceptDialog::set_swap_cancel_ok(bool p_swap) {
  263. swap_cancel_ok = p_swap;
  264. }
  265. AcceptDialog::AcceptDialog() {
  266. set_wrap_controls(true);
  267. set_visible(false);
  268. set_transient(true);
  269. set_exclusive(true);
  270. set_clamp_to_embedder(true);
  271. bg = memnew(Panel);
  272. add_child(bg, false, INTERNAL_MODE_FRONT);
  273. hbc = memnew(HBoxContainer);
  274. int margin = hbc->get_theme_constant(SNAME("margin"), SNAME("Dialogs"));
  275. int button_margin = hbc->get_theme_constant(SNAME("button_margin"), SNAME("Dialogs"));
  276. label = memnew(Label);
  277. label->set_anchor(SIDE_RIGHT, Control::ANCHOR_END);
  278. label->set_anchor(SIDE_BOTTOM, Control::ANCHOR_END);
  279. label->set_begin(Point2(margin, margin));
  280. label->set_end(Point2(-margin, -button_margin - 10));
  281. add_child(label, false, INTERNAL_MODE_FRONT);
  282. add_child(hbc, false, INTERNAL_MODE_FRONT);
  283. hbc->add_spacer();
  284. ok = memnew(Button);
  285. ok->set_text(TTRC("OK"));
  286. hbc->add_child(ok);
  287. hbc->add_spacer();
  288. ok->connect("pressed", callable_mp(this, &AcceptDialog::_ok_pressed));
  289. set_title(TTRC("Alert!"));
  290. connect("window_input", callable_mp(this, &AcceptDialog::_input_from_window));
  291. }
  292. AcceptDialog::~AcceptDialog() {
  293. }
  294. // ConfirmationDialog
  295. void ConfirmationDialog::_bind_methods() {
  296. ClassDB::bind_method(D_METHOD("get_cancel_button"), &ConfirmationDialog::get_cancel_button);
  297. }
  298. Button *ConfirmationDialog::get_cancel_button() {
  299. return cancel;
  300. }
  301. ConfirmationDialog::ConfirmationDialog() {
  302. set_title(TTRC("Please Confirm..."));
  303. set_min_size(Size2(200, 70));
  304. cancel = add_cancel_button();
  305. }