2
0

popup.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. //edited on editor
  49. set_as_toplevel(false);
  50. } else
  51. #endif
  52. if (is_visible()) {
  53. hide();
  54. }
  55. }
  56. }
  57. void Popup::_fix_size() {
  58. Point2 pos = get_global_position();
  59. Size2 size = get_size();
  60. Point2 window_size = get_viewport_rect().size;
  61. if (pos.x + size.width > window_size.width)
  62. pos.x = window_size.width - size.width;
  63. if (pos.x < 0)
  64. pos.x = 0;
  65. if (pos.y + size.height > window_size.height)
  66. pos.y = window_size.height - size.height;
  67. if (pos.y < 0)
  68. pos.y = 0;
  69. if (pos != get_position())
  70. set_global_position(pos);
  71. }
  72. void Popup::set_as_minsize() {
  73. Size2 total_minsize;
  74. for (int i = 0; i < get_child_count(); i++) {
  75. Control *c = Object::cast_to<Control>(get_child(i));
  76. if (!c)
  77. continue;
  78. if (!c->is_visible())
  79. continue;
  80. Size2 minsize = c->get_combined_minimum_size();
  81. for (int j = 0; j < 2; j++) {
  82. Margin m_beg = Margin(0 + j);
  83. Margin m_end = Margin(2 + j);
  84. float margin_begin = c->get_margin(m_beg);
  85. float margin_end = c->get_margin(m_end);
  86. float anchor_begin = c->get_anchor(m_beg);
  87. float anchor_end = c->get_anchor(m_end);
  88. minsize[j] += margin_begin * (ANCHOR_END - anchor_begin) + margin_end * anchor_end;
  89. }
  90. total_minsize.width = MAX(total_minsize.width, minsize.width);
  91. total_minsize.height = MAX(total_minsize.height, minsize.height);
  92. }
  93. set_size(total_minsize);
  94. }
  95. void Popup::popup_centered_minsize(const Size2 &p_minsize) {
  96. set_custom_minimum_size(p_minsize);
  97. _fix_size();
  98. popup_centered();
  99. }
  100. void Popup::popup_centered(const Size2 &p_size) {
  101. Rect2 rect;
  102. Size2 window_size = get_viewport_rect().size;
  103. rect.size = p_size == Size2() ? get_size() : p_size;
  104. rect.position = ((window_size - rect.size) / 2.0).floor();
  105. popup(rect);
  106. }
  107. void Popup::popup_centered_ratio(float p_screen_ratio) {
  108. Rect2 rect;
  109. Size2 window_size = get_viewport_rect().size;
  110. rect.size = (window_size * p_screen_ratio).floor();
  111. rect.position = ((window_size - rect.size) / 2.0).floor();
  112. popup(rect);
  113. }
  114. void Popup::popup(const Rect2 &p_bounds) {
  115. emit_signal("about_to_show");
  116. show_modal(exclusive);
  117. // Fit the popup into the optionally provided bounds.
  118. if (!p_bounds.has_no_area()) {
  119. set_position(p_bounds.position);
  120. set_size(p_bounds.size);
  121. }
  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::set_exclusive(bool p_exclusive) {
  131. exclusive = p_exclusive;
  132. }
  133. bool Popup::is_exclusive() const {
  134. return exclusive;
  135. }
  136. void Popup::_bind_methods() {
  137. ClassDB::bind_method(D_METHOD("popup_centered", "size"), &Popup::popup_centered, DEFVAL(Size2()));
  138. ClassDB::bind_method(D_METHOD("popup_centered_ratio", "ratio"), &Popup::popup_centered_ratio, DEFVAL(0.75));
  139. ClassDB::bind_method(D_METHOD("popup_centered_minsize", "minsize"), &Popup::popup_centered_minsize, DEFVAL(Size2()));
  140. ClassDB::bind_method(D_METHOD("popup", "bounds"), &Popup::popup, DEFVAL(Rect2()));
  141. ClassDB::bind_method(D_METHOD("set_exclusive", "enable"), &Popup::set_exclusive);
  142. ClassDB::bind_method(D_METHOD("is_exclusive"), &Popup::is_exclusive);
  143. ADD_SIGNAL(MethodInfo("about_to_show"));
  144. ADD_SIGNAL(MethodInfo("popup_hide"));
  145. ADD_GROUP("Popup", "popup_");
  146. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "popup_exclusive"), "set_exclusive", "is_exclusive");
  147. BIND_CONSTANT(NOTIFICATION_POST_POPUP);
  148. BIND_CONSTANT(NOTIFICATION_POPUP_HIDE);
  149. }
  150. Popup::Popup() {
  151. set_as_toplevel(true);
  152. exclusive = false;
  153. popped_up = false;
  154. hide();
  155. }
  156. String Popup::get_configuration_warning() const {
  157. if (is_visible_in_tree()) {
  158. 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.");
  159. }
  160. return String();
  161. }
  162. Popup::~Popup() {
  163. }
  164. Size2 PopupPanel::get_minimum_size() const {
  165. Ref<StyleBox> p = get_stylebox("panel");
  166. Size2 ms;
  167. for (int i = 0; i < get_child_count(); i++) {
  168. Control *c = Object::cast_to<Control>(get_child(i));
  169. if (!c)
  170. continue;
  171. if (c->is_set_as_toplevel())
  172. continue;
  173. Size2 cms = c->get_combined_minimum_size();
  174. ms.x = MAX(cms.x, ms.x);
  175. ms.y = MAX(cms.y, ms.y);
  176. }
  177. return ms + p->get_minimum_size();
  178. }
  179. void PopupPanel::_update_child_rects() {
  180. Ref<StyleBox> p = get_stylebox("panel");
  181. Vector2 cpos(p->get_offset());
  182. Vector2 csize(get_size() - p->get_minimum_size());
  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. c->set_position(cpos);
  190. c->set_size(csize);
  191. }
  192. }
  193. void PopupPanel::_notification(int p_what) {
  194. if (p_what == NOTIFICATION_DRAW) {
  195. get_stylebox("panel")->draw(get_canvas_item(), Rect2(Point2(), get_size()));
  196. } else if (p_what == NOTIFICATION_READY) {
  197. _update_child_rects();
  198. } else if (p_what == NOTIFICATION_RESIZED) {
  199. _update_child_rects();
  200. }
  201. }
  202. PopupPanel::PopupPanel() {
  203. }