guiDynamicCtrlArrayCtrl.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. docsURL;
  83. addField( "colCount", TypeS32, Offset( mCols, GuiDynamicCtrlArrayControl ),
  84. "Number of columns the child controls have been arranged into. This "
  85. "value is calculated automatically when children are added, removed or "
  86. "resized; writing it directly has no effect." );
  87. addField( "colSize", TypeS32, Offset( mColSize, GuiDynamicCtrlArrayControl ),
  88. "Width of each column. If <i>autoCellSize</i> is set, this will be "
  89. "calculated automatically from the widest child control" );
  90. addField( "rowCount", TypeS32, Offset( mRows, GuiDynamicCtrlArrayControl ),
  91. "Number of rows the child controls have been arranged into. This value "
  92. "is calculated automatically when children are added, removed or resized; "
  93. "writing it directly has no effect." );
  94. addField( "rowSize", TypeS32, Offset( mRowSize, GuiDynamicCtrlArrayControl ),
  95. "Height of each row. If <i>autoCellSize</i> is set, this will be "
  96. "calculated automatically from the tallest child control" );
  97. addField( "rowSpacing", TypeS32, Offset( mRowSpacing, GuiDynamicCtrlArrayControl ),
  98. "Spacing between rows" );
  99. addField( "colSpacing", TypeS32, Offset( mColSpacing, GuiDynamicCtrlArrayControl ),
  100. "Spacing between columns" );
  101. addField( "frozen", TypeBool, Offset( mFrozen, GuiDynamicCtrlArrayControl ),
  102. "When true, the array will not update when new children are added or in "
  103. "response to child resize events. This is useful to prevent unnecessary "
  104. "resizing when adding, removing or resizing a number of child controls." );
  105. addField( "autoCellSize", TypeBool, Offset( mAutoCellSize, GuiDynamicCtrlArrayControl ),
  106. "When true, the cell size is set to the widest/tallest child control." );
  107. addField( "fillRowFirst", TypeBool, Offset( mFillRowFirst, GuiDynamicCtrlArrayControl ),
  108. "Controls whether rows or columns are filled first.\n\nIf true, controls are "
  109. "added to the grid left-to-right (to fill a row); then rows are added "
  110. "top-to-bottom as shown below:\n"
  111. "<pre>1 2 3 4\n"
  112. "5 6 7 8</pre>\n"
  113. "If false, controls are added to the grid top-to-bottom (to fill a column); "
  114. "then columns are added left-to-right as shown below:\n"
  115. "<pre>1 3 5 7\n"
  116. "2 4 6 8</pre>" );
  117. addField( "dynamicSize", TypeBool, Offset( mDynamicSize, GuiDynamicCtrlArrayControl ),
  118. "If true, the width or height of this control will be automatically "
  119. "calculated based on the number of child controls (width if "
  120. "<i>fillRowFirst</i> is false, height if <i>fillRowFirst</i> is true)." );
  121. addField( "padding", TypeRectSpacingI, Offset( mPadding, GuiDynamicCtrlArrayControl ),
  122. "Padding around the top, bottom, left, and right of this control. This "
  123. "reduces the area available for child controls." );
  124. Parent::initPersistFields();
  125. }
  126. // SimObject...
  127. void GuiDynamicCtrlArrayControl::inspectPostApply()
  128. {
  129. resize(getPosition(), getExtent());
  130. Parent::inspectPostApply();
  131. }
  132. // SimSet...
  133. void GuiDynamicCtrlArrayControl::addObject(SimObject *obj)
  134. {
  135. Parent::addObject(obj);
  136. if ( !mFrozen )
  137. refresh();
  138. }
  139. // GuiControl...
  140. bool GuiDynamicCtrlArrayControl::resize(const Point2I &newPosition, const Point2I &newExtent)
  141. {
  142. if ( size() == 0 )
  143. return Parent::resize( newPosition, newExtent );
  144. if ( mResizing )
  145. return false;
  146. mResizing = true;
  147. // Calculate the cellSize based on our widest/tallest child control
  148. // if the flag to do so is set.
  149. if ( mAutoCellSize )
  150. {
  151. mColSize = 1;
  152. mRowSize = 1;
  153. for ( U32 i = 0; i < size(); i++ )
  154. {
  155. GuiControl *child = dynamic_cast<GuiControl*>(operator [](i));
  156. if ( child && child->isVisible() )
  157. {
  158. if ( mColSize < child->getWidth() )
  159. mColSize = child->getWidth();
  160. if ( mRowSize < child->getHeight() )
  161. mRowSize = child->getHeight();
  162. }
  163. }
  164. }
  165. // Count number of visible, children guiControls.
  166. S32 numChildren = 0;
  167. for ( U32 i = 0; i < size(); i++ )
  168. {
  169. GuiControl *child = dynamic_cast<GuiControl*>(operator [](i));
  170. if ( child && child->isVisible() )
  171. numChildren++;
  172. }
  173. // Calculate number of rows and columns.
  174. if ( !mFillRowFirst )
  175. {
  176. mRows = 1;
  177. while ( ( ( mRows + 1 ) * mRowSize + mRows * mRowSpacing ) <= ( newExtent.y - ( mPadding.top + mPadding.bottom ) ) )
  178. mRows++;
  179. mCols = numChildren / mRows;
  180. if ( numChildren % mRows > 0 )
  181. mCols++;
  182. }
  183. else
  184. {
  185. mCols = 1;
  186. while ( ( ( mCols + 1 ) * mColSize + mCols * mColSpacing ) <= ( newExtent.x - ( mPadding.left + mPadding.right ) ) )
  187. mCols++;
  188. mRows = numChildren / mCols;
  189. if ( numChildren % mCols > 0 )
  190. mRows++;
  191. }
  192. // Place each child...
  193. S32 childcount = 0;
  194. for ( S32 i = 0; i < size(); i++ )
  195. {
  196. // Place control
  197. GuiControl *gc = dynamic_cast<GuiControl*>(operator [](i));
  198. // Added check if child is visible. Invisible children don't take part
  199. if ( gc && gc->isVisible() )
  200. {
  201. S32 curCol, curRow;
  202. // Get the current column and row...
  203. if ( mFillRowFirst )
  204. {
  205. curCol = childcount % mCols;
  206. curRow = childcount / mCols;
  207. }
  208. else
  209. {
  210. curCol = childcount / mRows;
  211. curRow = childcount % mRows;
  212. }
  213. // Reposition and resize
  214. Point2I newPos( mPadding.left + curCol * ( mColSize + mColSpacing ), mPadding.top + curRow * ( mRowSize + mRowSpacing ) );
  215. gc->resize( newPos, Point2I( mColSize, mRowSize ) );
  216. childcount++;
  217. }
  218. }
  219. Point2I realExtent( newExtent );
  220. if ( mDynamicSize )
  221. {
  222. if ( mFillRowFirst )
  223. realExtent.y = mRows * mRowSize + ( mRows - 1 ) * mRowSpacing + ( mPadding.top + mPadding.bottom );
  224. else
  225. realExtent.x = mCols * mColSize + ( mCols - 1 ) * mColSpacing + ( mPadding.left + mPadding.right );
  226. }
  227. mResizing = false;
  228. return Parent::resize( newPosition, realExtent );
  229. }
  230. void GuiDynamicCtrlArrayControl::childResized(GuiControl *child)
  231. {
  232. Parent::childResized(child);
  233. if ( !mFrozen )
  234. refresh();
  235. }
  236. void GuiDynamicCtrlArrayControl::refresh()
  237. {
  238. resize( getPosition(), getExtent() );
  239. }
  240. DefineEngineMethod( GuiDynamicCtrlArrayControl, refresh, void, (),,
  241. "Recalculates the position and size of this control and all its children." )
  242. {
  243. object->refresh();
  244. }