popup.cpp 8.2 KB

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