2
0

guiCtrlArrayCtrl.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 "gui/containers/guiCtrlArrayCtrl.h"
  23. #include "console/engineAPI.h"
  24. IMPLEMENT_CONOBJECT(GuiControlArrayControl);
  25. ConsoleDocClass( GuiControlArrayControl,
  26. "@brief Brief Desc.\n\n"
  27. "@tsexample\n"
  28. "// Comment:\n"
  29. "%okButton = new ClassObject()\n"
  30. "instantiation\n"
  31. "@endtsexample\n\n"
  32. "@ingroup GuiContainers"
  33. );
  34. // One call back that the system isnt ready for yet in this class
  35. GuiControlArrayControl::GuiControlArrayControl()
  36. {
  37. mResizing = false;
  38. mCols = 0;
  39. mRowSize = 30;
  40. mRowSpacing = 2;
  41. mColSpacing = 0;
  42. mIsContainer = true;
  43. }
  44. void GuiControlArrayControl::initPersistFields()
  45. {
  46. addGroup( "Array" );
  47. addField( "colCount", TypeS32, Offset(mCols, GuiControlArrayControl),
  48. "Number of colums in the array." );
  49. addField( "colSizes", TypeS32Vector, Offset(mColumnSizes, GuiControlArrayControl),
  50. "Size of each individual column." );
  51. addField( "rowSize", TypeS32, Offset(mRowSize, GuiControlArrayControl),
  52. "Heigth of a row in the array." );
  53. addField( "rowSpacing", TypeS32, Offset(mRowSpacing, GuiControlArrayControl),
  54. "Padding to put between rows." );
  55. addField( "colSpacing", TypeS32, Offset(mColSpacing, GuiControlArrayControl),
  56. "Padding to put between columns." );
  57. endGroup( "Array" );
  58. Parent::initPersistFields();
  59. }
  60. bool GuiControlArrayControl::onWake()
  61. {
  62. if ( !Parent::onWake() )
  63. return false;
  64. return true;
  65. }
  66. void GuiControlArrayControl::onSleep()
  67. {
  68. Parent::onSleep();
  69. }
  70. void GuiControlArrayControl::inspectPostApply()
  71. {
  72. Parent::inspectPostApply();
  73. updateArray();
  74. }
  75. bool GuiControlArrayControl::resize(const Point2I &newPosition, const Point2I &newExtent)
  76. {
  77. if( !Parent::resize(newPosition, newExtent) )
  78. return false;
  79. return updateArray();
  80. }
  81. void GuiControlArrayControl::addObject(SimObject *obj)
  82. {
  83. Parent::addObject(obj);
  84. updateArray();
  85. }
  86. void GuiControlArrayControl::removeObject(SimObject *obj)
  87. {
  88. Parent::removeObject(obj);
  89. updateArray();
  90. }
  91. bool GuiControlArrayControl::reOrder(SimObject* obj, SimObject* target)
  92. {
  93. bool ret = Parent::reOrder(obj, target);
  94. if (ret)
  95. updateArray();
  96. return ret;
  97. }
  98. bool GuiControlArrayControl::updateArray()
  99. {
  100. // Prevent recursion
  101. if(mResizing)
  102. return false;
  103. // Set Resizing.
  104. mResizing = true;
  105. if(mCols < 1 || size() < 1)
  106. {
  107. mResizing = false;
  108. return false;
  109. }
  110. S32 *sizes = new S32[mCols];
  111. S32 *offsets = new S32[mCols];
  112. S32 totalSize = 0;
  113. Point2I extent = getExtent();
  114. // Calculate the column sizes
  115. for(S32 i=0; i<mCols; i++)
  116. {
  117. if( i >= mColumnSizes.size() )
  118. sizes[ i ] = 0;
  119. else
  120. sizes[i] = mColumnSizes[i];
  121. offsets[i] = totalSize;
  122. // If it's an auto-size one, then... auto-size...
  123. if(sizes[i] == -1)
  124. {
  125. sizes[i] = extent.x - totalSize;
  126. break;
  127. }
  128. totalSize += sizes[i] + mColSpacing;
  129. }
  130. // Now iterate through the children and resize them to fit the grid...
  131. for(S32 i=0; i<size(); i++)
  132. {
  133. GuiControl *gc = dynamic_cast<GuiControl*>(operator[](i));
  134. // Get the current column and row...
  135. S32 curCol = i % mCols;
  136. S32 curRow = i / mCols;
  137. if(gc)
  138. {
  139. Point2I newPos(offsets[curCol], curRow * (mRowSize + mRowSpacing));
  140. Point2I newExtents(sizes[curCol], mRowSize);
  141. gc->resize(newPos, newExtents);
  142. }
  143. }
  144. // Clear Sizing Flag.
  145. mResizing = false;
  146. delete [] sizes;
  147. delete [] offsets;
  148. return true;
  149. }