2
0

guiDynamicCtrlArrayCtrl.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "console/engineAPI.h"
  23. #include "platform/platform.h"
  24. #include "gui/containers/guiDynamicCtrlArrayCtrl.h"
  25. GuiDynamicCtrlArrayControl::GuiDynamicCtrlArrayControl()
  26. {
  27. mCols = 0;
  28. mColSize = 64;
  29. mRows = 0;
  30. mRowSize = 64;
  31. mRowSpacing = 0;
  32. mColSpacing = 0;
  33. mIsContainer = true;
  34. mResizing = false;
  35. mSizeToChildren = false;
  36. mAutoCellSize = false;
  37. mFrozen = false;
  38. mDynamicSize = false;
  39. mFillRowFirst = true;
  40. mPadding.set( 0, 0, 0, 0 );
  41. }
  42. GuiDynamicCtrlArrayControl::~GuiDynamicCtrlArrayControl()
  43. {
  44. }
  45. IMPLEMENT_CONOBJECT(GuiDynamicCtrlArrayControl);
  46. ConsoleDocClass( GuiDynamicCtrlArrayControl,
  47. "@brief A container that arranges children into a grid.\n\n"
  48. "This container maintains a 2D grid of GUI controls. If one is added, deleted, "
  49. "or resized, then the grid is updated. The insertion order into the grid is "
  50. "determined by the internal order of the children (ie. the order of addition).<br>"
  51. "Children are added to the grid by row or column until they fill the assocated "
  52. "GuiDynamicCtrlArrayControl extent (width or height). For example, a "
  53. "GuiDynamicCtrlArrayControl with 15 children, and <i>fillRowFirst</i> set to "
  54. "true may be arranged as follows:\n\n"
  55. "<pre>\n"
  56. "1 2 3 4 5 6\n"
  57. "7 8 9 10 11 12\n"
  58. "13 14 15\n"
  59. "</pre>\n"
  60. "If <i>dynamicSize</i> were set to true in this case, the GuiDynamicCtrlArrayControl "
  61. "height would be calculated to fit the 3 rows of child controls.\n\n"
  62. "@tsexample\n"
  63. "new GuiDynamicCtrlArrayControl()\n"
  64. "{\n"
  65. " colSize = \"128\";\n"
  66. " rowSize = \"18\";\n"
  67. " colSpacing = \"2\";\n"
  68. " rowSpacing = \"2\";\n"
  69. " frozen = \"0\";\n"
  70. " autoCellSize = \"1\";\n"
  71. " fillRowFirst = \"1\";\n"
  72. " dynamicSize = \"1\";\n"
  73. " padding = \"0 0 0 0\";\n"
  74. " //Properties not specific to this control have been omitted from this example.\n"
  75. "};\n"
  76. "@endtsexample\n\n"
  77. "@ingroup GuiContainers"
  78. );
  79. // ConsoleObject...
  80. void GuiDynamicCtrlArrayControl::initPersistFields()
  81. {
  82. addField( "colCount", TypeS32, Offset( mCols, GuiDynamicCtrlArrayControl ),
  83. "Number of columns the child controls have been arranged into. This "
  84. "value is calculated automatically when children are added, removed or "
  85. "resized; writing it directly has no effect." );
  86. addField( "colSize", TypeS32, Offset( mColSize, GuiDynamicCtrlArrayControl ),
  87. "Width of each column. If <i>autoCellSize</i> is set, this will be "
  88. "calculated automatically from the widest child control" );
  89. addField( "rowCount", TypeS32, Offset( mRows, GuiDynamicCtrlArrayControl ),
  90. "Number of rows the child controls have been arranged into. This value "
  91. "is calculated automatically when children are added, removed or resized; "
  92. "writing it directly has no effect." );
  93. addField( "rowSize", TypeS32, Offset( mRowSize, GuiDynamicCtrlArrayControl ),
  94. "Height of each row. If <i>autoCellSize</i> is set, this will be "
  95. "calculated automatically from the tallest child control" );
  96. addField( "rowSpacing", TypeS32, Offset( mRowSpacing, GuiDynamicCtrlArrayControl ),
  97. "Spacing between rows" );
  98. addField( "colSpacing", TypeS32, Offset( mColSpacing, GuiDynamicCtrlArrayControl ),
  99. "Spacing between columns" );
  100. addField( "frozen", TypeBool, Offset( mFrozen, GuiDynamicCtrlArrayControl ),
  101. "When true, the array will not update when new children are added or in "
  102. "response to child resize events. This is useful to prevent unnecessary "
  103. "resizing when adding, removing or resizing a number of child controls." );
  104. addField( "autoCellSize", TypeBool, Offset( mAutoCellSize, GuiDynamicCtrlArrayControl ),
  105. "When true, the cell size is set to the widest/tallest child control." );
  106. addField( "fillRowFirst", TypeBool, Offset( mFillRowFirst, GuiDynamicCtrlArrayControl ),
  107. "Controls whether rows or columns are filled first.\n\nIf true, controls are "
  108. "added to the grid left-to-right (to fill a row); then rows are added "
  109. "top-to-bottom as shown below:\n"
  110. "<pre>1 2 3 4\n"
  111. "5 6 7 8</pre>\n"
  112. "If false, controls are added to the grid top-to-bottom (to fill a column); "
  113. "then columns are added left-to-right as shown below:\n"
  114. "<pre>1 3 5 7\n"
  115. "2 4 6 8</pre>" );
  116. addField( "dynamicSize", TypeBool, Offset( mDynamicSize, GuiDynamicCtrlArrayControl ),
  117. "If true, the width or height of this control will be automatically "
  118. "calculated based on the number of child controls (width if "
  119. "<i>fillRowFirst</i> is false, height if <i>fillRowFirst</i> is true)." );
  120. addField( "padding", TypeRectSpacingI, Offset( mPadding, GuiDynamicCtrlArrayControl ),
  121. "Padding around the top, bottom, left, and right of this control. This "
  122. "reduces the area available for child controls." );
  123. Parent::initPersistFields();
  124. }
  125. // SimObject...
  126. void GuiDynamicCtrlArrayControl::inspectPostApply()
  127. {
  128. resize(getPosition(), getExtent());
  129. Parent::inspectPostApply();
  130. }
  131. // SimSet...
  132. void GuiDynamicCtrlArrayControl::addObject(SimObject *obj)
  133. {
  134. Parent::addObject(obj);
  135. if ( !mFrozen )
  136. refresh();
  137. }
  138. // GuiControl...
  139. bool GuiDynamicCtrlArrayControl::resize(const Point2I &newPosition, const Point2I &newExtent)
  140. {
  141. if ( size() == 0 )
  142. return Parent::resize( newPosition, newExtent );
  143. if ( mResizing )
  144. return false;
  145. mResizing = true;
  146. // Calculate the cellSize based on our widest/tallest child control
  147. // if the flag to do so is set.
  148. if ( mAutoCellSize )
  149. {
  150. mColSize = 1;
  151. mRowSize = 1;
  152. for ( U32 i = 0; i < size(); i++ )
  153. {
  154. GuiControl *child = dynamic_cast<GuiControl*>(operator [](i));
  155. if ( child && child->isVisible() )
  156. {
  157. if ( mColSize < child->getWidth() )
  158. mColSize = child->getWidth();
  159. if ( mRowSize < child->getHeight() )
  160. mRowSize = child->getHeight();
  161. }
  162. }
  163. }
  164. // Count number of visible, children guiControls.
  165. S32 numChildren = 0;
  166. for ( U32 i = 0; i < size(); i++ )
  167. {
  168. GuiControl *child = dynamic_cast<GuiControl*>(operator [](i));
  169. if ( child && child->isVisible() )
  170. numChildren++;
  171. }
  172. // Calculate number of rows and columns.
  173. if ( !mFillRowFirst )
  174. {
  175. mRows = 1;
  176. while ( ( ( mRows + 1 ) * mRowSize + mRows * mRowSpacing ) <= ( newExtent.y - ( mPadding.top + mPadding.bottom ) ) )
  177. mRows++;
  178. mCols = numChildren / mRows;
  179. if ( numChildren % mRows > 0 )
  180. mCols++;
  181. }
  182. else
  183. {
  184. mCols = 1;
  185. while ( ( ( mCols + 1 ) * mColSize + mCols * mColSpacing ) <= ( newExtent.x - ( mPadding.left + mPadding.right ) ) )
  186. mCols++;
  187. mRows = numChildren / mCols;
  188. if ( numChildren % mCols > 0 )
  189. mRows++;
  190. }
  191. // Place each child...
  192. S32 childcount = 0;
  193. for ( S32 i = 0; i < size(); i++ )
  194. {
  195. // Place control
  196. GuiControl *gc = dynamic_cast<GuiControl*>(operator [](i));
  197. // Added check if child is visible. Invisible children don't take part
  198. if ( gc && gc->isVisible() )
  199. {
  200. S32 curCol, curRow;
  201. // Get the current column and row...
  202. if ( mFillRowFirst )
  203. {
  204. curCol = childcount % mCols;
  205. curRow = childcount / mCols;
  206. }
  207. else
  208. {
  209. curCol = childcount / mRows;
  210. curRow = childcount % mRows;
  211. }
  212. // Reposition and resize
  213. Point2I newPos( mPadding.left + curCol * ( mColSize + mColSpacing ), mPadding.top + curRow * ( mRowSize + mRowSpacing ) );
  214. gc->resize( newPos, Point2I( mColSize, mRowSize ) );
  215. childcount++;
  216. }
  217. }
  218. Point2I realExtent( newExtent );
  219. if ( mDynamicSize )
  220. {
  221. if ( mFillRowFirst )
  222. realExtent.y = mRows * mRowSize + ( mRows - 1 ) * mRowSpacing + ( mPadding.top + mPadding.bottom );
  223. else
  224. realExtent.x = mCols * mColSize + ( mCols - 1 ) * mColSpacing + ( mPadding.left + mPadding.right );
  225. }
  226. mResizing = false;
  227. return Parent::resize( newPosition, realExtent );
  228. }
  229. void GuiDynamicCtrlArrayControl::childResized(GuiControl *child)
  230. {
  231. Parent::childResized(child);
  232. if ( !mFrozen )
  233. refresh();
  234. }
  235. void GuiDynamicCtrlArrayControl::refresh()
  236. {
  237. resize( getPosition(), getExtent() );
  238. }
  239. DefineEngineMethod( GuiDynamicCtrlArrayControl, refresh, void, (),,
  240. "Recalculates the position and size of this control and all its children." )
  241. {
  242. object->refresh();
  243. }