popup.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*************************************************************************/
  2. /* popup.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 "popup.h"
  31. #include "core/engine.h"
  32. #include "core/os/keyboard.h"
  33. void Popup::_gui_input(Ref<InputEvent> p_event) {
  34. }
  35. void Popup::_notification(int p_what) {
  36. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  37. if (popped_up && !is_visible_in_tree()) {
  38. popped_up = false;
  39. notification(NOTIFICATION_POPUP_HIDE);
  40. emit_signal("popup_hide");
  41. }
  42. update_configuration_warning();
  43. }
  44. if (p_what == NOTIFICATION_EXIT_TREE) {
  45. if (popped_up) {
  46. popped_up = false;
  47. notification(NOTIFICATION_POPUP_HIDE);
  48. emit_signal("popup_hide");
  49. }
  50. }
  51. if (p_what == NOTIFICATION_ENTER_TREE) {
  52. //small helper to make editing of these easier in editor
  53. #ifdef TOOLS_ENABLED
  54. if (Engine::get_singleton()->is_editor_hint() && get_tree()->get_edited_scene_root() && get_tree()->get_edited_scene_root()->is_a_parent_of(this)) {
  55. //edited on editor
  56. set_as_toplevel(false);
  57. } else
  58. #endif
  59. if (is_visible()) {
  60. hide();
  61. }
  62. }
  63. }
  64. void Popup::_fix_size() {
  65. Point2 pos = get_global_position();
  66. Size2 size = get_size() * get_scale();
  67. Point2 window_size = get_viewport_rect().size - get_viewport_transform().get_origin();
  68. if (pos.x + size.width > window_size.width)
  69. pos.x = window_size.width - size.width;
  70. if (pos.x < 0)
  71. pos.x = 0;
  72. if (pos.y + size.height > window_size.height)
  73. pos.y = window_size.height - size.height;
  74. if (pos.y < 0)
  75. pos.y = 0;
  76. if (pos != get_position())
  77. set_global_position(pos);
  78. }
  79. void Popup::set_as_minsize() {
  80. Size2 total_minsize;
  81. for (int i = 0; i < get_child_count(); i++) {
  82. Control *c = Object::cast_to<Control>(get_child(i));
  83. if (!c)
  84. continue;
  85. if (!c->is_visible())
  86. continue;
  87. Size2 minsize = c->get_combined_minimum_size();
  88. for (int j = 0; j < 2; j++) {
  89. Margin m_beg = Margin(0 + j);
  90. Margin m_end = Margin(2 + j);
  91. float margin_begin = c->get_margin(m_beg);
  92. float margin_end = c->get_margin(m_end);
  93. float anchor_begin = c->get_anchor(m_beg);
  94. float anchor_end = c->get_anchor(m_end);
  95. minsize[j] += margin_begin * (ANCHOR_END - anchor_begin) + margin_end * anchor_end;
  96. }
  97. total_minsize.width = MAX(total_minsize.width, minsize.width);
  98. total_minsize.height = MAX(total_minsize.height, minsize.height);
  99. }
  100. set_size(total_minsize);
  101. }
  102. void Popup::popup_centered_clamped(const Size2 &p_size, float p_fallback_ratio) {
  103. Size2 popup_size = p_size;
  104. Size2 window_size = get_viewport_rect().size;
  105. // clamp popup size in each dimension if window size is too small (using fallback ratio)
  106. popup_size.x = MIN(window_size.x * p_fallback_ratio, popup_size.x);
  107. popup_size.y = MIN(window_size.y * p_fallback_ratio, popup_size.y);
  108. popup_centered(popup_size);
  109. }
  110. void Popup::popup_centered_minsize(const Size2 &p_minsize) {
  111. set_custom_minimum_size(p_minsize);
  112. _fix_size();
  113. popup_centered();
  114. }
  115. void Popup::popup_centered(const Size2 &p_size) {
  116. Rect2 rect;
  117. Size2 window_size = get_viewport_rect().size;
  118. rect.size = p_size == Size2() ? get_size() : p_size;
  119. rect.position = ((window_size - rect.size * get_scale()) / 2.0).floor();
  120. _popup(rect, true);
  121. }
  122. void Popup::popup_centered_ratio(float p_screen_ratio) {
  123. Rect2 rect;
  124. Size2 window_size = get_viewport_rect().size;
  125. rect.size = (window_size * p_screen_ratio).floor();
  126. rect.position = ((window_size - rect.size * get_scale()) / 2.0).floor();
  127. _popup(rect, true);
  128. }
  129. void Popup::popup(const Rect2 &p_bounds) {
  130. _popup(p_bounds);
  131. }
  132. void Popup::_popup(const Rect2 &p_bounds, const bool p_centered) {
  133. emit_signal("about_to_show");
  134. show_modal(exclusive);
  135. // Fit the popup into the optionally provided bounds.
  136. if (!p_bounds.has_no_area()) {
  137. set_size(p_bounds.size);
  138. // check if p_bounds.size was using an outdated cached values
  139. if (p_centered && p_bounds.size != get_size()) {
  140. set_position(p_bounds.position - ((get_size() - p_bounds.size) / 2.0).floor());
  141. } else {
  142. set_position(p_bounds.position);
  143. }
  144. }
  145. _fix_size();
  146. Control *focusable = find_next_valid_focus();
  147. if (focusable)
  148. focusable->grab_focus();
  149. _post_popup();
  150. notification(NOTIFICATION_POST_POPUP);
  151. popped_up = true;
  152. }
  153. void Popup::set_exclusive(bool p_exclusive) {
  154. exclusive = p_exclusive;
  155. }
  156. bool Popup::is_exclusive() const {
  157. return exclusive;
  158. }
  159. void Popup::_bind_methods() {
  160. ClassDB::bind_method(D_METHOD("set_as_minsize"), &Popup::set_as_minsize);
  161. ClassDB::bind_method(D_METHOD("popup_centered", "size"), &Popup::popup_centered, DEFVAL(Size2()));
  162. ClassDB::bind_method(D_METHOD("popup_centered_ratio", "ratio"), &Popup::popup_centered_ratio, DEFVAL(0.75));
  163. ClassDB::bind_method(D_METHOD("popup_centered_minsize", "minsize"), &Popup::popup_centered_minsize, DEFVAL(Size2()));
  164. ClassDB::bind_method(D_METHOD("popup_centered_clamped", "size", "fallback_ratio"), &Popup::popup_centered_clamped, DEFVAL(Size2()), DEFVAL(0.75));
  165. ClassDB::bind_method(D_METHOD("popup", "bounds"), &Popup::popup, DEFVAL(Rect2()));
  166. ClassDB::bind_method(D_METHOD("set_exclusive", "enable"), &Popup::set_exclusive);
  167. ClassDB::bind_method(D_METHOD("is_exclusive"), &Popup::is_exclusive);
  168. ADD_SIGNAL(MethodInfo("about_to_show"));
  169. ADD_SIGNAL(MethodInfo("popup_hide"));
  170. ADD_GROUP("Popup", "popup_");
  171. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "popup_exclusive"), "set_exclusive", "is_exclusive");
  172. BIND_CONSTANT(NOTIFICATION_POST_POPUP);
  173. BIND_CONSTANT(NOTIFICATION_POPUP_HIDE);
  174. }
  175. Popup::Popup() {
  176. set_as_toplevel(true);
  177. exclusive = false;
  178. popped_up = false;
  179. hide();
  180. }
  181. String Popup::get_configuration_warning() const {
  182. String warning = Control::get_configuration_warning();
  183. if (is_visible_in_tree()) {
  184. if (warning != String()) {
  185. warning += "\n\n";
  186. }
  187. warning += TTR("Popups will hide by default unless you call popup() or any of the popup*() functions. Making them visible for editing is fine, but they will hide upon running.");
  188. }
  189. return warning;
  190. }
  191. Popup::~Popup() {
  192. }
  193. Size2 PopupPanel::get_minimum_size() const {
  194. Ref<StyleBox> p = get_stylebox("panel");
  195. Size2 ms;
  196. for (int i = 0; i < get_child_count(); i++) {
  197. Control *c = Object::cast_to<Control>(get_child(i));
  198. if (!c)
  199. continue;
  200. if (c->is_set_as_toplevel())
  201. continue;
  202. Size2 cms = c->get_combined_minimum_size();
  203. ms.x = MAX(cms.x, ms.x);
  204. ms.y = MAX(cms.y, ms.y);
  205. }
  206. return ms + p->get_minimum_size();
  207. }
  208. void PopupPanel::_update_child_rects() {
  209. Ref<StyleBox> p = get_stylebox("panel");
  210. Vector2 cpos(p->get_offset());
  211. Vector2 csize(get_size() - p->get_minimum_size());
  212. for (int i = 0; i < get_child_count(); i++) {
  213. Control *c = Object::cast_to<Control>(get_child(i));
  214. if (!c)
  215. continue;
  216. if (c->is_set_as_toplevel())
  217. continue;
  218. c->set_position(cpos);
  219. c->set_size(csize);
  220. }
  221. }
  222. void PopupPanel::_notification(int p_what) {
  223. if (p_what == NOTIFICATION_DRAW) {
  224. get_stylebox("panel")->draw(get_canvas_item(), Rect2(Point2(), get_size()));
  225. } else if (p_what == NOTIFICATION_READY) {
  226. _update_child_rects();
  227. } else if (p_what == NOTIFICATION_RESIZED) {
  228. _update_child_rects();
  229. }
  230. }
  231. PopupPanel::PopupPanel() {
  232. }