guiFlexibleArrayCtrl.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #ifdef TORQUE_WALKABOUT_EXTRAS_ENABLED
  2. //-----------------------------------------------------------------------------
  3. // Copyright (c) 2012 Daniel Buckmaster
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //-----------------------------------------------------------------------------
  23. #include "console/engineAPI.h"
  24. #include "platform/platform.h"
  25. #include "gui/containers/guiFlexibleArrayCtrl.h"
  26. #include "platform/types.h"
  27. GuiFlexibleArrayControl::GuiFlexibleArrayControl()
  28. {
  29. mRows = 0;
  30. mRowSpacing = 0;
  31. mColSpacing = 0;
  32. mIsContainer = true;
  33. mResizing = false;
  34. mFrozen = false;
  35. mPadding.set(0, 0, 0, 0);
  36. }
  37. GuiFlexibleArrayControl::~GuiFlexibleArrayControl()
  38. {
  39. }
  40. IMPLEMENT_CONOBJECT(GuiFlexibleArrayControl);
  41. ConsoleDocClass( GuiFlexibleArrayControl,
  42. "@brief A container that arranges children into a grid.\n\n"
  43. "This container maintains a 2D grid of GUI controls. If one is added, deleted, "
  44. "or resized, then the grid is updated. The insertion order into the grid is "
  45. "determined by the internal order of the children (ie. the order of addition).<br>"
  46. "Children are added to the grid by row or column until they fill the assocated "
  47. "GuiFlexibleArrayControl extent (width or height). For example, a "
  48. "GuiFlexibleArrayControl with 10 children, and <i>fillRowFirst</i> set to "
  49. "true may be arranged as follows:\n\n"
  50. "<pre>\n"
  51. "1 2 ...3... 4\n"
  52. "..5.. 6 7 .8.\n"
  53. "9 ....10....\n"
  54. "</pre>\n"
  55. "@tsexample\n"
  56. "new GuiFlexibleArrayControl()\n"
  57. "{\n"
  58. " colSpacing = \"2\";\n"
  59. " rowSpacing = \"2\";\n"
  60. " frozen = \"0\";\n"
  61. " padding = \"0 0 0 0\";\n"
  62. " //Properties not specific to this control have been omitted from this example.\n"
  63. "};\n"
  64. "@endtsexample\n\n"
  65. "@see GuiDynamicCtrlArrayControl\n"
  66. "@ingroup GuiContainers"
  67. );
  68. // ConsoleObject...
  69. void GuiFlexibleArrayControl::initPersistFields()
  70. {
  71. addField("rowCount", TypeS32, Offset( mRows, GuiFlexibleArrayControl),
  72. "Number of rows the child controls have been arranged into. This value "
  73. "is calculated automatically when children are added, removed or resized; "
  74. "writing it directly has no effect.");
  75. addField("rowSpacing", TypeS32, Offset(mRowSpacing, GuiFlexibleArrayControl),
  76. "Spacing between rows");
  77. addField("colSpacing", TypeS32, Offset(mColSpacing, GuiFlexibleArrayControl),
  78. "Spacing between columns");
  79. addField("frozen", TypeBool, Offset(mFrozen, GuiFlexibleArrayControl),
  80. "When true, the array will not update when new children are added or in "
  81. "response to child resize events. This is useful to prevent unnecessary "
  82. "resizing when adding, removing or resizing a number of child controls.");
  83. addField("padding", TypeRectSpacingI, Offset(mPadding, GuiFlexibleArrayControl),
  84. "Padding around the top, bottom, left, and right of this control. This "
  85. "reduces the area available for child controls.");
  86. Parent::initPersistFields();
  87. }
  88. void GuiFlexibleArrayControl::inspectPostApply()
  89. {
  90. resize(getPosition(), getExtent());
  91. Parent::inspectPostApply();
  92. }
  93. void GuiFlexibleArrayControl::addObject(SimObject *obj)
  94. {
  95. Parent::addObject(obj);
  96. if(!mFrozen)
  97. refresh();
  98. }
  99. bool GuiFlexibleArrayControl::resize(const Point2I &newPosition, const Point2I &newExtent)
  100. {
  101. if(size() == 0)
  102. return Parent::resize(newPosition, newExtent);
  103. if(mResizing)
  104. return false;
  105. mResizing = true;
  106. // Place each child.
  107. S32 childcount = 0;
  108. Point2I pos(mPadding.left, mPadding.top);
  109. mRows = 0;
  110. S32 rowHeight = 0;
  111. for(S32 i = 0; i < size(); i++)
  112. {
  113. GuiControl *gc = dynamic_cast<GuiControl*>(operator [](i));
  114. if(gc && gc->isVisible())
  115. {
  116. if(pos.x + gc->getWidth() > getExtent().x - mPadding.right)
  117. {
  118. pos.y += rowHeight + mRowSpacing;
  119. pos.x = mPadding.left;
  120. rowHeight = 0;
  121. mRows++;
  122. }
  123. gc->setPosition(pos);
  124. rowHeight = getMax(rowHeight, gc->getHeight());
  125. pos.x += mColSpacing + gc->getWidth();
  126. childcount++;
  127. }
  128. }
  129. Point2I realExtent(newExtent);
  130. realExtent.y = pos.y + rowHeight;
  131. realExtent.y += mPadding.bottom;
  132. mResizing = false;
  133. return Parent::resize(newPosition, realExtent);
  134. }
  135. void GuiFlexibleArrayControl::childResized(GuiControl *child)
  136. {
  137. Parent::childResized(child);
  138. if ( !mFrozen )
  139. refresh();
  140. }
  141. void GuiFlexibleArrayControl::refresh()
  142. {
  143. resize(getPosition(), getExtent());
  144. }
  145. DefineEngineMethod( GuiFlexibleArrayControl, refresh, void, (),,
  146. "Recalculates the position and size of this control and all its children." )
  147. {
  148. object->refresh();
  149. }
  150. #endif // TORQUE_WALKABOUT_EXTRAS_ENABLED