dialogs.cpp 11 KB

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