guiDynamicCtrlArrayCtrl.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/guiDynamicCtrlArrayCtrl.h"
  23. ConsoleMethod(GuiDynamicCtrlArrayControl, refresh, void, 2, 2, "() Forces the child controls to recalculate")
  24. {
  25. object->updateChildControls();
  26. }
  27. IMPLEMENT_CONOBJECT(GuiDynamicCtrlArrayControl);
  28. GuiDynamicCtrlArrayControl::GuiDynamicCtrlArrayControl()
  29. {
  30. mCols = 0;
  31. mColSize = 64;
  32. mRowSize = 64;
  33. mRowSpacing = 0;
  34. mColSpacing = 0;
  35. mIsContainer = true;
  36. mResizing = false;
  37. }
  38. GuiDynamicCtrlArrayControl::~GuiDynamicCtrlArrayControl()
  39. {
  40. }
  41. void GuiDynamicCtrlArrayControl::initPersistFields()
  42. {
  43. Parent::initPersistFields();
  44. addField("colCount", TypeS32, Offset(mCols, GuiDynamicCtrlArrayControl));
  45. addField("colSize", TypeS32, Offset(mColSize, GuiDynamicCtrlArrayControl));
  46. addField("rowSize", TypeS32, Offset(mRowSize, GuiDynamicCtrlArrayControl));
  47. addField("rowSpacing", TypeS32, Offset(mRowSpacing, GuiDynamicCtrlArrayControl));
  48. addField("colSpacing", TypeS32, Offset(mColSpacing, GuiDynamicCtrlArrayControl));
  49. }
  50. void GuiDynamicCtrlArrayControl::inspectPostApply()
  51. {
  52. resize(getPosition(), getExtent());
  53. Parent::inspectPostApply();
  54. }
  55. void GuiDynamicCtrlArrayControl::updateChildControls()
  56. {
  57. // Prevent recursion
  58. if(mResizing) return;
  59. mResizing = true;
  60. // Store the sum of the heights of our controls.
  61. Point2I curPos = getPosition();
  62. // Calculate the number of columns
  63. mCols = ( getExtent().x / (mColSize));// + mColSpacing) ) - mColSpacing;
  64. if(mCols < 1)
  65. mCols = 1;
  66. // Place each child...
  67. S32 childcount = 0;
  68. S32 rows = 0;
  69. for(S32 i=0; i<size(); i++)
  70. {
  71. // Place control
  72. GuiControl * gc = dynamic_cast<GuiControl*>(operator [](i));
  73. if(gc && gc->isVisible()) // DAW: Added check if child is visible. Invisible children don't take part
  74. {
  75. // Get the current column and row...
  76. S32 curCol = childcount % mCols;
  77. S32 curRow = childcount / mCols;
  78. rows = getMax( rows, curRow );
  79. // Reposition and resize
  80. Point2I newPos(mColSpacing + (curCol * (mColSize + mColSpacing)), mRowSpacing + (curRow * (mRowSize + mRowSpacing)));
  81. gc->resize(newPos, Point2I(mColSize, mRowSize));
  82. childcount++;
  83. }
  84. }
  85. rows++;
  86. // Conform our size to the sum of the child sizes.
  87. curPos.x = getExtent().x;
  88. curPos.y = mRowSpacing + (rows * (mRowSize + mRowSpacing));
  89. mBounds.extent.y = curPos.y;
  90. mResizing = false;
  91. }
  92. void GuiDynamicCtrlArrayControl::resize(const Point2I &newPosition, const Point2I &newExtent)
  93. {
  94. // If we don't have any children, return.
  95. if(size() < 1)
  96. {
  97. Parent::resize(newPosition, newExtent);
  98. return;
  99. }
  100. // What we need to do is figure out how many children will fit on a row
  101. // given this control's new extent. This gives us the number of columns.
  102. // Then the number of rows will expand or contract, essentially changing
  103. // the 'y' extent of this control.
  104. //call set update both before and after
  105. setUpdate();
  106. Point2I actualNewExtent = Point2I(getMax(mMinExtent.x, newExtent.x),
  107. getMax(mMinExtent.y, newExtent.y));
  108. mBounds.set(newPosition, actualNewExtent);
  109. GuiControl *parent = getParent();
  110. if (parent)
  111. parent->childResized(this);
  112. setUpdate();
  113. updateChildControls();
  114. }
  115. void GuiDynamicCtrlArrayControl::addObject(SimObject *obj)
  116. {
  117. Parent::addObject(obj);
  118. updateChildControls();
  119. }
  120. void GuiDynamicCtrlArrayControl::childResized(GuiControl *child)
  121. {
  122. Parent::childResized(child);
  123. updateChildControls();
  124. }