popup.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*************************************************************************/
  2. /* popup.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "popup.h"
  30. #include "os/keyboard.h"
  31. void Popup::_input_event(InputEvent p_event) {
  32. }
  33. void Popup::_notification(int p_what) {
  34. if (p_what==NOTIFICATION_VISIBILITY_CHANGED) {
  35. if (popped_up && !is_visible()) {
  36. popped_up=false;
  37. notification(NOTIFICATION_POPUP_HIDE);
  38. emit_signal("popup_hide");
  39. }
  40. }
  41. }
  42. void Popup::_fix_size() {
  43. Control *window = get_window();
  44. ERR_FAIL_COND(!window);
  45. Point2 pos = get_pos();
  46. Size2 size = get_size();
  47. Point2 window_size = window==this ? get_parent_area_size() :window->get_size();
  48. if (pos.x+size.width > window_size.width)
  49. pos.x=window_size.width-size.width;
  50. if (pos.x<0)
  51. pos.x=0;
  52. if (pos.y+size.height > window_size.height)
  53. pos.y=window_size.height-size.height;
  54. if (pos.y<0)
  55. pos.y=0;
  56. if (pos!=get_pos())
  57. set_pos(pos);
  58. }
  59. void Popup::popup_centered_minsize(const Size2& p_minsize) {
  60. Size2 total_minsize=p_minsize;
  61. for(int i=0;i<get_child_count();i++) {
  62. Control *c=get_child(i)->cast_to<Control>();
  63. if (!c)
  64. continue;
  65. Size2 minsize = c->get_combined_minimum_size();
  66. for(int j=0;j<2;j++) {
  67. Margin m_beg = Margin(0+j);
  68. Margin m_end = Margin(2+j);
  69. float margin_begin = c->get_margin(m_beg);
  70. float margin_end = c->get_margin(m_end);
  71. AnchorType anchor_begin = c->get_anchor(m_beg);
  72. AnchorType anchor_end = c->get_anchor(m_end);
  73. if (anchor_begin == ANCHOR_BEGIN)
  74. minsize[j]+=margin_begin;
  75. if (anchor_end == ANCHOR_END)
  76. minsize[j]+=margin_end;
  77. }
  78. total_minsize.width = MAX( total_minsize.width, minsize.width );
  79. total_minsize.height = MAX( total_minsize.height, minsize.height );
  80. }
  81. popup_centered( total_minsize );
  82. popped_up=true;
  83. }
  84. void Popup::popup_centered(const Size2& p_size) {
  85. Control *window = get_window();
  86. ERR_FAIL_COND(!window);
  87. emit_signal("about_to_show");
  88. Rect2 rect;
  89. rect.size = p_size==Size2()?get_size():p_size;
  90. Point2 window_size = window==this ? get_parent_area_size() :window->get_size();
  91. rect.pos = ((window_size-rect.size)/2.0).floor();
  92. set_pos( rect.pos );
  93. set_size( rect.size );
  94. show_modal(exclusive);
  95. _fix_size();
  96. Control *focusable = find_next_valid_focus();
  97. if (focusable)
  98. focusable->grab_focus();
  99. _post_popup();
  100. notification(NOTIFICATION_POST_POPUP);
  101. popped_up=true;
  102. }
  103. void Popup::popup_centered_ratio(float p_screen_ratio) {
  104. Control *window = get_window();
  105. ERR_FAIL_COND(!window);
  106. emit_signal("about_to_show");
  107. Rect2 rect;
  108. Point2 window_size = window==this ? get_parent_area_size() :window->get_size();
  109. rect.size = (window_size * p_screen_ratio).floor();
  110. rect.pos = ((window_size-rect.size)/2.0).floor();
  111. set_pos( rect.pos );
  112. set_size( rect.size );
  113. show_modal(exclusive);
  114. _fix_size();
  115. Control *focusable = find_next_valid_focus();
  116. if (focusable)
  117. focusable->grab_focus();
  118. _post_popup();
  119. notification(NOTIFICATION_POST_POPUP);
  120. popped_up=true;
  121. }
  122. void Popup::popup() {
  123. emit_signal("about_to_show");
  124. show_modal(exclusive);
  125. _fix_size();
  126. Control *focusable = find_next_valid_focus();
  127. if (focusable)
  128. focusable->grab_focus();
  129. _post_popup();
  130. notification(NOTIFICATION_POST_POPUP);
  131. popped_up=true;
  132. }
  133. void Popup::set_exclusive(bool p_exclusive) {
  134. exclusive=p_exclusive;
  135. }
  136. bool Popup::is_exclusive() const {
  137. return exclusive;
  138. }
  139. void Popup::_bind_methods() {
  140. ObjectTypeDB::bind_method(_MD("popup_centered","size"),&Popup::popup_centered,DEFVAL(Size2()));
  141. ObjectTypeDB::bind_method(_MD("popup_centered_ratio","ratio"),&Popup::popup_centered_ratio,DEFVAL(0.75));
  142. ObjectTypeDB::bind_method(_MD("popup_centered_minsize","minsize"),&Popup::popup_centered_minsize,DEFVAL(Size2()));
  143. ObjectTypeDB::bind_method(_MD("popup"),&Popup::popup);
  144. ObjectTypeDB::bind_method(_MD("set_exclusive","enable"),&Popup::set_exclusive);
  145. ObjectTypeDB::bind_method(_MD("is_exclusive"),&Popup::is_exclusive);
  146. ADD_SIGNAL( MethodInfo("about_to_show") );
  147. ADD_SIGNAL( MethodInfo("popup_hide") );
  148. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "popup/exclusive"), _SCS("set_exclusive"),_SCS("is_exclusive") );
  149. BIND_CONSTANT(NOTIFICATION_POST_POPUP);
  150. BIND_CONSTANT(NOTIFICATION_POPUP_HIDE);
  151. }
  152. Popup::Popup() {
  153. set_as_toplevel(true);
  154. exclusive=false;
  155. popped_up=false;
  156. hide();
  157. }
  158. Popup::~Popup()
  159. {
  160. }
  161. void PopupPanel::set_child_rect(Control *p_child) {
  162. ERR_FAIL_NULL(p_child);
  163. Ref<StyleBox> p = get_stylebox("panel");
  164. p_child->set_area_as_parent_rect();
  165. for(int i=0;i<4;i++) {
  166. p_child->set_margin(Margin(i),p->get_margin(Margin(i)));
  167. }
  168. }
  169. void PopupPanel::_notification(int p_what) {
  170. if (p_what==NOTIFICATION_DRAW) {
  171. get_stylebox("panel")->draw(get_canvas_item(),Rect2(Point2(),get_size()));
  172. }
  173. }
  174. PopupPanel::PopupPanel() {
  175. }