grid_container.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*************************************************************************/
  2. /* grid_container.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 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 "grid_container.h"
  30. void GridContainer::_notification(int p_what) {
  31. switch(p_what) {
  32. case NOTIFICATION_SORT_CHILDREN: {
  33. Map<int,int> col_minw;
  34. Map<int,int> row_minh;
  35. Set<int> col_expanded;
  36. Set<int> row_expanded;
  37. int hsep=get_constant("hseparation");
  38. int vsep=get_constant("vseparation");
  39. int idx=0;
  40. int max_row=0;
  41. int max_col=0;
  42. Size2 size = get_size();
  43. for(int i=0;i<get_child_count();i++) {
  44. Control *c = get_child(i)->cast_to<Control>();
  45. if (!c || !c->is_visible())
  46. continue;
  47. int row = idx / columns;
  48. int col = idx % columns;
  49. Size2i ms = c->get_combined_minimum_size();
  50. if (col_minw.has(col))
  51. col_minw[col] = MAX(col_minw[col],ms.width);
  52. else
  53. col_minw[col]=ms.width;
  54. if (row_minh.has(row))
  55. row_minh[row] = MAX(row_minh[row],ms.height);
  56. else
  57. row_minh[row]=ms.height;
  58. // print_line("store row "+itos(row)+" mw "+itos(ms.height));
  59. if (c->get_h_size_flags()&SIZE_EXPAND)
  60. col_expanded.insert(col);
  61. if (c->get_v_size_flags()&SIZE_EXPAND)
  62. row_expanded.insert(row);
  63. max_col=MAX(col,max_col);
  64. max_row=MAX(row,max_row);
  65. idx++;
  66. }
  67. Size2 ms;
  68. int expand_rows=0;
  69. int expand_cols=0;
  70. for (Map<int,int>::Element *E=col_minw.front();E;E=E->next()) {
  71. ms.width+=E->get();
  72. if (col_expanded.has(E->key()))
  73. expand_cols++;
  74. }
  75. for (Map<int,int>::Element *E=row_minh.front();E;E=E->next()) {
  76. ms.height+=E->get();
  77. if (row_expanded.has(E->key()))
  78. expand_rows++;
  79. }
  80. ms.height+=vsep*max_row;
  81. ms.width+=hsep*max_col;
  82. int row_expand = expand_rows?(size.y-ms.y)/expand_rows:0;
  83. int col_expand = expand_cols?(size.x-ms.x)/expand_cols:0;
  84. int col_ofs=0;
  85. int row_ofs=0;
  86. idx=0;
  87. for(int i=0;i<get_child_count();i++) {
  88. Control *c = get_child(i)->cast_to<Control>();
  89. if (!c || !c->is_visible())
  90. continue;
  91. int row = idx / columns;
  92. int col = idx % columns;
  93. if (col==0) {
  94. col_ofs=0;
  95. if (row>0 && row_minh.has(row-1))
  96. row_ofs+=row_minh[row-1]+vsep+(row_expanded.has(row-1)?row_expand:0);
  97. }
  98. Size2 s;
  99. if (col_minw.has(col))
  100. s.width=col_minw[col];
  101. if (row_minh.has(row))
  102. s.height=row_minh[row];
  103. if (row_expanded.has(row))
  104. s.height+=row_expand;
  105. if (col_expanded.has(col))
  106. s.width+=col_expand;
  107. Point2 p(col_ofs,row_ofs);
  108. // print_line("col: "+itos(col)+" row: "+itos(row)+" col_ofs: "+itos(col_ofs)+" row_ofs: "+itos(row_ofs));
  109. fit_child_in_rect(c,Rect2(p,s));
  110. //print_line("col: "+itos(col)+" row: "+itos(row)+" rect: "+Rect2(p,s));
  111. if (col_minw.has(col)) {
  112. col_ofs+=col_minw[col]+hsep+(col_expanded.has(col)?col_expand:0);
  113. }
  114. idx++;
  115. }
  116. } break;
  117. }
  118. }
  119. void GridContainer::set_columns(int p_columns) {
  120. ERR_FAIL_COND(p_columns<1);
  121. columns=p_columns;
  122. queue_sort();
  123. }
  124. int GridContainer::get_columns() const{
  125. return columns;
  126. }
  127. void GridContainer::_bind_methods(){
  128. ObjectTypeDB::bind_method(_MD("set_columns","columns"),&GridContainer::set_columns);
  129. ObjectTypeDB::bind_method(_MD("get_columns"),&GridContainer::get_columns);
  130. ADD_PROPERTY( PropertyInfo(Variant::INT,"columns",PROPERTY_HINT_RANGE,"1,1024,1"),_SCS("set_columns"),_SCS("get_columns"));
  131. }
  132. Size2 GridContainer::get_minimum_size() const {
  133. Map<int,int> col_minw;
  134. Map<int,int> row_minh;
  135. int hsep=get_constant("hseparation");
  136. int vsep=get_constant("vseparation");
  137. int idx=0;
  138. int max_row=0;
  139. int max_col=0;
  140. for(int i=0;i<get_child_count();i++) {
  141. Control *c = get_child(i)->cast_to<Control>();
  142. if (!c || !c->is_visible())
  143. continue;
  144. int row = idx / columns;
  145. int col = idx % columns;
  146. Size2i ms = c->get_combined_minimum_size();
  147. if (col_minw.has(col))
  148. col_minw[col] = MAX(col_minw[col],ms.width);
  149. else
  150. col_minw[col]=ms.width;
  151. if (row_minh.has(row))
  152. row_minh[row] = MAX(row_minh[row],ms.height);
  153. else
  154. row_minh[row]=ms.height;
  155. max_col=MAX(col,max_col);
  156. max_row=MAX(row,max_row);
  157. idx++;
  158. }
  159. Size2 ms;
  160. for (Map<int,int>::Element *E=col_minw.front();E;E=E->next()) {
  161. ms.width+=E->get();
  162. }
  163. for (Map<int,int>::Element *E=row_minh.front();E;E=E->next()) {
  164. ms.height+=E->get();
  165. }
  166. ms.height+=vsep*max_row;
  167. ms.width+=hsep*max_col;
  168. return ms;
  169. }
  170. GridContainer::GridContainer() {
  171. set_stop_mouse(false);
  172. columns=1;
  173. }