guiCtrlArrayCtrl.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. docsURL;
  47. addGroup( "Array" );
  48. addField( "colCount", TypeS32, Offset(mCols, GuiControlArrayControl),
  49. "Number of colums in the array." );
  50. addField( "colSizes", TypeS32Vector, Offset(mColumnSizes, GuiControlArrayControl),
  51. "Size of each individual column." );
  52. addField( "rowSize", TypeS32, Offset(mRowSize, GuiControlArrayControl),
  53. "Heigth of a row in the array." );
  54. addField( "rowSpacing", TypeS32, Offset(mRowSpacing, GuiControlArrayControl),
  55. "Padding to put between rows." );
  56. addField( "colSpacing", TypeS32, Offset(mColSpacing, GuiControlArrayControl),
  57. "Padding to put between columns." );
  58. endGroup( "Array" );
  59. Parent::initPersistFields();
  60. }
  61. bool GuiControlArrayControl::onWake()
  62. {
  63. if ( !Parent::onWake() )
  64. return false;
  65. return true;
  66. }
  67. void GuiControlArrayControl::onSleep()
  68. {
  69. Parent::onSleep();
  70. }
  71. void GuiControlArrayControl::inspectPostApply()
  72. {
  73. Parent::inspectPostApply();
  74. updateArray();
  75. }
  76. bool GuiControlArrayControl::resize(const Point2I &newPosition, const Point2I &newExtent)
  77. {
  78. if( !Parent::resize(newPosition, newExtent) )
  79. return false;
  80. return updateArray();
  81. }
  82. void GuiControlArrayControl::addObject(SimObject *obj)
  83. {
  84. Parent::addObject(obj);
  85. updateArray();
  86. }
  87. void GuiControlArrayControl::removeObject(SimObject *obj)
  88. {
  89. Parent::removeObject(obj);
  90. updateArray();
  91. }
  92. bool GuiControlArrayControl::reOrder(SimObject* obj, SimObject* target)
  93. {
  94. bool ret = Parent::reOrder(obj, target);
  95. if (ret)
  96. updateArray();
  97. return ret;
  98. }
  99. bool GuiControlArrayControl::updateArray()
  100. {
  101. // Prevent recursion
  102. if(mResizing)
  103. return false;
  104. // Set Resizing.
  105. mResizing = true;
  106. if(mCols < 1 || size() < 1)
  107. {
  108. mResizing = false;
  109. return false;
  110. }
  111. S32 *sizes = new S32[mCols];
  112. S32 *offsets = new S32[mCols];
  113. S32 totalSize = 0;
  114. Point2I extent = getExtent();
  115. // Calculate the column sizes
  116. for(S32 i=0; i<mCols; i++)
  117. {
  118. if( i >= mColumnSizes.size() )
  119. sizes[ i ] = 0;
  120. else
  121. sizes[i] = mColumnSizes[i];
  122. offsets[i] = totalSize;
  123. // If it's an auto-size one, then... auto-size...
  124. if(sizes[i] == -1)
  125. {
  126. sizes[i] = extent.x - totalSize;
  127. break;
  128. }
  129. totalSize += sizes[i] + mColSpacing;
  130. }
  131. // Now iterate through the children and resize them to fit the grid...
  132. for(S32 i=0; i<size(); i++)
  133. {
  134. GuiControl *gc = dynamic_cast<GuiControl*>(operator[](i));
  135. // Get the current column and row...
  136. S32 curCol = i % mCols;
  137. S32 curRow = i / mCols;
  138. if(gc)
  139. {
  140. Point2I newPos(offsets[curCol], curRow * (mRowSize + mRowSpacing));
  141. Point2I newExtents(sizes[curCol], mRowSize);
  142. gc->resize(newPos, newExtents);
  143. }
  144. }
  145. // Clear Sizing Flag.
  146. mResizing = false;
  147. delete [] sizes;
  148. delete [] offsets;
  149. return true;
  150. }