guiCtrlArrayCtrl.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. IMPLEMENT_CONOBJECT(GuiControlArrayControl);
  24. GuiControlArrayControl::GuiControlArrayControl()
  25. {
  26. mCols = 0;
  27. mRowSize = 30;
  28. mRowSpacing = 2;
  29. mColSpacing = 0;
  30. mIsContainer = true;
  31. }
  32. void GuiControlArrayControl::initPersistFields()
  33. {
  34. Parent::initPersistFields();
  35. addField("colCount", TypeS32, Offset(mCols, GuiControlArrayControl));
  36. addField("colSizes", TypeS32Vector, Offset(mColumnSizes, GuiControlArrayControl));
  37. addField("rowSize", TypeS32, Offset(mRowSize, GuiControlArrayControl));
  38. addField("rowSpacing", TypeS32, Offset(mRowSpacing, GuiControlArrayControl));
  39. addField("colSpacing", TypeS32, Offset(mColSpacing, GuiControlArrayControl));
  40. }
  41. bool GuiControlArrayControl::onWake()
  42. {
  43. if ( !Parent::onWake() )
  44. return false;
  45. return true;
  46. }
  47. void GuiControlArrayControl::onSleep()
  48. {
  49. Parent::onSleep();
  50. }
  51. void GuiControlArrayControl::inspectPostApply()
  52. {
  53. resize(getPosition(), getExtent());
  54. }
  55. void GuiControlArrayControl::resize(const Point2I &newPosition, const Point2I &newExtent)
  56. {
  57. Parent::resize(newPosition, newExtent);
  58. if(mCols < 1 || size() < 1)
  59. return;
  60. S32 *sizes = new S32[mCols];
  61. S32 *offsets = new S32[mCols];
  62. S32 totalSize = 0;
  63. // Calculate the column sizes
  64. for(S32 i=0; i<mCols; i++)
  65. {
  66. sizes[i] = mColumnSizes[i];
  67. offsets[i] = totalSize;
  68. // If it's an auto-size one, then... auto-size...
  69. if(sizes[i] == -1)
  70. {
  71. sizes[i] = newExtent.x - totalSize;
  72. break;
  73. }
  74. totalSize += sizes[i] + mColSpacing;
  75. }
  76. // Now iterate through the children and resize them to fit the grid...
  77. for(S32 i=0; i<size(); i++)
  78. {
  79. GuiControl *gc = dynamic_cast<GuiControl*>(operator[](i));
  80. // Get the current column and row...
  81. S32 curCol = i % mCols;
  82. S32 curRow = i / mCols;
  83. if(gc)
  84. {
  85. Point2I newPos(offsets[curCol], curRow * (mRowSize + mRowSpacing));
  86. Point2I newExtents(sizes[curCol], mRowSize);
  87. gc->resize(newPos, newExtents);
  88. }
  89. }
  90. }