guiFlexibleArrayCtrl.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 Daniel Buckmaster
  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/guiFlexibleArrayCtrl.h"
  25. #include "platform/types.h"
  26. GuiFlexibleArrayControl::GuiFlexibleArrayControl()
  27. {
  28. mRows = 0;
  29. mRowSpacing = 0;
  30. mColSpacing = 0;
  31. mIsContainer = true;
  32. mResizing = false;
  33. mFrozen = false;
  34. mPadding.set(0, 0, 0, 0);
  35. }
  36. GuiFlexibleArrayControl::~GuiFlexibleArrayControl()
  37. {
  38. }
  39. IMPLEMENT_CONOBJECT(GuiFlexibleArrayControl);
  40. ConsoleDocClass( GuiFlexibleArrayControl,
  41. "@brief A container that arranges children into a grid.\n\n"
  42. "This container maintains a 2D grid of GUI controls. If one is added, deleted, "
  43. "or resized, then the grid is updated. The insertion order into the grid is "
  44. "determined by the internal order of the children (ie. the order of addition).<br>"
  45. "Children are added to the grid by row or column until they fill the assocated "
  46. "GuiFlexibleArrayControl extent (width or height). For example, a "
  47. "GuiFlexibleArrayControl with 10 children, and <i>fillRowFirst</i> set to "
  48. "true may be arranged as follows:\n\n"
  49. "<pre>\n"
  50. "1 2 ...3... 4\n"
  51. "..5.. 6 7 .8.\n"
  52. "9 ....10....\n"
  53. "</pre>\n"
  54. "@tsexample\n"
  55. "new GuiFlexibleArrayControl()\n"
  56. "{\n"
  57. " colSpacing = \"2\";\n"
  58. " rowSpacing = \"2\";\n"
  59. " frozen = \"0\";\n"
  60. " padding = \"0 0 0 0\";\n"
  61. " //Properties not specific to this control have been omitted from this example.\n"
  62. "};\n"
  63. "@endtsexample\n\n"
  64. "@see GuiDynamicCtrlArrayControl\n"
  65. "@ingroup GuiContainers"
  66. );
  67. // ConsoleObject...
  68. void GuiFlexibleArrayControl::initPersistFields()
  69. {
  70. addField("rowCount", TypeS32, Offset( mRows, GuiFlexibleArrayControl),
  71. "Number of rows the child controls have been arranged into. This value "
  72. "is calculated automatically when children are added, removed or resized; "
  73. "writing it directly has no effect.");
  74. addField("rowSpacing", TypeS32, Offset(mRowSpacing, GuiFlexibleArrayControl),
  75. "Spacing between rows");
  76. addField("colSpacing", TypeS32, Offset(mColSpacing, GuiFlexibleArrayControl),
  77. "Spacing between columns");
  78. addField("frozen", TypeBool, Offset(mFrozen, GuiFlexibleArrayControl),
  79. "When true, the array will not update when new children are added or in "
  80. "response to child resize events. This is useful to prevent unnecessary "
  81. "resizing when adding, removing or resizing a number of child controls.");
  82. addField("padding", TypeRectSpacingI, Offset(mPadding, GuiFlexibleArrayControl),
  83. "Padding around the top, bottom, left, and right of this control. This "
  84. "reduces the area available for child controls.");
  85. Parent::initPersistFields();
  86. }
  87. void GuiFlexibleArrayControl::inspectPostApply()
  88. {
  89. resize(getPosition(), getExtent());
  90. Parent::inspectPostApply();
  91. }
  92. void GuiFlexibleArrayControl::addObject(SimObject *obj)
  93. {
  94. Parent::addObject(obj);
  95. if(!mFrozen)
  96. refresh();
  97. }
  98. bool GuiFlexibleArrayControl::resize(const Point2I &newPosition, const Point2I &newExtent)
  99. {
  100. if(size() == 0)
  101. return Parent::resize(newPosition, newExtent);
  102. if(mResizing)
  103. return false;
  104. mResizing = true;
  105. // Place each child.
  106. S32 childcount = 0;
  107. Point2I pos(mPadding.left, mPadding.top);
  108. mRows = 0;
  109. S32 rowHeight = 0;
  110. for(S32 i = 0; i < size(); i++)
  111. {
  112. GuiControl *gc = dynamic_cast<GuiControl*>(operator [](i));
  113. if(gc && gc->isVisible())
  114. {
  115. if(pos.x + gc->getWidth() > getExtent().x - mPadding.right)
  116. {
  117. pos.y += rowHeight + mRowSpacing;
  118. pos.x = mPadding.left;
  119. rowHeight = 0;
  120. mRows++;
  121. }
  122. gc->setPosition(pos);
  123. rowHeight = getMax(rowHeight, gc->getHeight());
  124. pos.x += mColSpacing + gc->getWidth();
  125. childcount++;
  126. }
  127. }
  128. Point2I realExtent(newExtent);
  129. realExtent.y = pos.y + rowHeight;
  130. realExtent.y += mPadding.bottom;
  131. mResizing = false;
  132. return Parent::resize(newPosition, realExtent);
  133. }
  134. void GuiFlexibleArrayControl::childResized(GuiControl *child)
  135. {
  136. Parent::childResized(child);
  137. if ( !mFrozen )
  138. refresh();
  139. }
  140. void GuiFlexibleArrayControl::refresh()
  141. {
  142. resize(getPosition(), getExtent());
  143. }
  144. DefineEngineMethod( GuiFlexibleArrayControl, refresh, void, (),,
  145. "Recalculates the position and size of this control and all its children." )
  146. {
  147. object->refresh();
  148. }