popup.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*************************************************************************/
  2. /* popup.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "engine.h"
  32. #include "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. Size2 total_minsize = p_minsize;
  93. for (int i = 0; i < get_child_count(); i++) {
  94. Control *c = Object::cast_to<Control>(get_child(i));
  95. if (!c)
  96. continue;
  97. if (!c->is_visible())
  98. continue;
  99. Size2 minsize = c->get_combined_minimum_size();
  100. for (int j = 0; j < 2; j++) {
  101. Margin m_beg = Margin(0 + j);
  102. Margin m_end = Margin(2 + j);
  103. float margin_begin = c->get_margin(m_beg);
  104. float margin_end = c->get_margin(m_end);
  105. float anchor_begin = c->get_anchor(m_beg);
  106. float anchor_end = c->get_anchor(m_end);
  107. minsize[j] += margin_begin * (ANCHOR_END - anchor_begin) + margin_end * anchor_end;
  108. }
  109. total_minsize.width = MAX(total_minsize.width, minsize.width);
  110. total_minsize.height = MAX(total_minsize.height, minsize.height);
  111. }
  112. popup_centered(total_minsize);
  113. popped_up = true;
  114. }
  115. void Popup::popup_centered(const Size2 &p_size) {
  116. Point2 window_size = get_viewport_rect().size;
  117. emit_signal("about_to_show");
  118. Rect2 rect;
  119. rect.size = p_size == Size2() ? get_size() : p_size;
  120. rect.position = ((window_size - rect.size) / 2.0).floor();
  121. set_position(rect.position);
  122. set_size(rect.size);
  123. show_modal(exclusive);
  124. _fix_size();
  125. Control *focusable = find_next_valid_focus();
  126. if (focusable)
  127. focusable->grab_focus();
  128. _post_popup();
  129. notification(NOTIFICATION_POST_POPUP);
  130. popped_up = true;
  131. }
  132. void Popup::popup_centered_ratio(float p_screen_ratio) {
  133. emit_signal("about_to_show");
  134. Rect2 rect;
  135. Point2 window_size = get_viewport_rect().size;
  136. rect.size = (window_size * p_screen_ratio).floor();
  137. rect.position = ((window_size - rect.size) / 2.0).floor();
  138. set_position(rect.position);
  139. set_size(rect.size);
  140. show_modal(exclusive);
  141. _fix_size();
  142. Control *focusable = find_next_valid_focus();
  143. if (focusable)
  144. focusable->grab_focus();
  145. _post_popup();
  146. notification(NOTIFICATION_POST_POPUP);
  147. popped_up = true;
  148. }
  149. void Popup::popup(const Rect2 &p_bounds) {
  150. emit_signal("about_to_show");
  151. show_modal(exclusive);
  152. // Fit the popup into the optionally provided bounds.
  153. if (!p_bounds.has_no_area()) {
  154. set_position(p_bounds.position);
  155. set_size(p_bounds.size);
  156. }
  157. _fix_size();
  158. Control *focusable = find_next_valid_focus();
  159. if (focusable)
  160. focusable->grab_focus();
  161. _post_popup();
  162. notification(NOTIFICATION_POST_POPUP);
  163. popped_up = true;
  164. }
  165. void Popup::set_exclusive(bool p_exclusive) {
  166. exclusive = p_exclusive;
  167. }
  168. bool Popup::is_exclusive() const {
  169. return exclusive;
  170. }
  171. void Popup::_bind_methods() {
  172. ClassDB::bind_method(D_METHOD("popup_centered", "size"), &Popup::popup_centered, DEFVAL(Size2()));
  173. ClassDB::bind_method(D_METHOD("popup_centered_ratio", "ratio"), &Popup::popup_centered_ratio, DEFVAL(0.75));
  174. ClassDB::bind_method(D_METHOD("popup_centered_minsize", "minsize"), &Popup::popup_centered_minsize, DEFVAL(Size2()));
  175. ClassDB::bind_method(D_METHOD("popup", "bounds"), &Popup::popup, DEFVAL(Rect2()));
  176. ClassDB::bind_method(D_METHOD("set_exclusive", "enable"), &Popup::set_exclusive);
  177. ClassDB::bind_method(D_METHOD("is_exclusive"), &Popup::is_exclusive);
  178. ADD_SIGNAL(MethodInfo("about_to_show"));
  179. ADD_SIGNAL(MethodInfo("popup_hide"));
  180. ADD_GROUP("Popup", "popup_");
  181. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "popup_exclusive"), "set_exclusive", "is_exclusive");
  182. BIND_CONSTANT(NOTIFICATION_POST_POPUP);
  183. BIND_CONSTANT(NOTIFICATION_POPUP_HIDE);
  184. }
  185. Popup::Popup() {
  186. set_as_toplevel(true);
  187. exclusive = false;
  188. popped_up = false;
  189. hide();
  190. }
  191. String Popup::get_configuration_warning() const {
  192. if (is_visible_in_tree()) {
  193. 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.");
  194. }
  195. return String();
  196. }
  197. Popup::~Popup() {
  198. }
  199. void PopupPanel::set_child_rect(Control *p_child) {
  200. ERR_FAIL_NULL(p_child);
  201. Ref<StyleBox> p = get_stylebox("panel");
  202. p_child->set_anchors_preset(Control::PRESET_WIDE);
  203. p_child->set_margin(MARGIN_LEFT, p->get_margin(MARGIN_LEFT));
  204. p_child->set_margin(MARGIN_RIGHT, -p->get_margin(MARGIN_RIGHT));
  205. p_child->set_margin(MARGIN_TOP, p->get_margin(MARGIN_TOP));
  206. p_child->set_margin(MARGIN_BOTTOM, -p->get_margin(MARGIN_BOTTOM));
  207. }
  208. void PopupPanel::_notification(int p_what) {
  209. if (p_what == NOTIFICATION_DRAW) {
  210. get_stylebox("panel")->draw(get_canvas_item(), Rect2(Point2(), get_size()));
  211. }
  212. }
  213. PopupPanel::PopupPanel() {
  214. }