grid_container.cpp 6.0 KB

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