guiChainCtrl.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 "console/consoleTypes.h"
  23. #include "console/console.h"
  24. #include "gui/containers/guiChainCtrl.h"
  25. #include "guiChainCtrl_ScriptBinding.h"
  26. IMPLEMENT_CONOBJECT(GuiChainCtrl);
  27. //------------------------------------------------------------------------------
  28. GuiChainCtrl::GuiChainCtrl()
  29. {
  30. mIsContainer = true;
  31. mChildSpacing = 0;
  32. mIsVertical = true;
  33. }
  34. //------------------------------------------------------------------------------
  35. void GuiChainCtrl::initPersistFields()
  36. {
  37. Parent::initPersistFields();
  38. addField("ChildSpacing", TypeS32, Offset(mChildSpacing, GuiChainCtrl));
  39. addField("IsVertical", TypeBool, Offset(mIsVertical, GuiChainCtrl));
  40. }
  41. //------------------------------------------------------------------------------
  42. void GuiChainCtrl::inspectPostApply()
  43. {
  44. calculateExtent();
  45. Parent::inspectPostApply();
  46. }
  47. //------------------------------------------------------------------------------
  48. void GuiChainCtrl::childResized(GuiControl *child)
  49. {
  50. calculateExtent();
  51. }
  52. void GuiChainCtrl::resize(const Point2I &newPosition, const Point2I &newExtent)
  53. {
  54. Point2I actualNewExtent = newExtent;
  55. if (mIsVertical)
  56. {
  57. actualNewExtent.y = getExtent().y;
  58. }
  59. else
  60. {
  61. actualNewExtent.x = getExtent().x;
  62. }
  63. if(newPosition != getPosition() || actualNewExtent != getExtent())
  64. {
  65. Parent::resize(newPosition, actualNewExtent);
  66. }
  67. }
  68. void GuiChainCtrl::onChildAdded(GuiControl *child)
  69. {
  70. //Ensure the child isn't positioned to the center
  71. if (child->getHorizSizing() == horizResizeCenter && !mIsVertical)
  72. {
  73. child->setHorizSizing(horizResizeLeft);
  74. }
  75. if (child->getVertSizing() == vertResizeCenter && mIsVertical)
  76. {
  77. child->setVertSizing(vertResizeTop);
  78. }
  79. Parent::onChildAdded(child);
  80. calculateExtent();
  81. }
  82. void GuiChainCtrl::onChildRemoved(SimObject *child)
  83. {
  84. calculateExtent();
  85. }
  86. void GuiChainCtrl::calculateExtent()
  87. {
  88. Point2I offset = Point2I(mBounds.point.Zero);
  89. Point2I extent = Point2I(getExtent());
  90. RectI innerRect = getInnerRect(offset, extent, NormalState, mProfile);
  91. S32 length = positionChildren(innerRect);
  92. if (!mIsVertical)
  93. {
  94. innerRect.extent.x = length;
  95. }
  96. else
  97. {
  98. innerRect.extent.y = length;
  99. }
  100. //call set update both before and after
  101. setUpdate();
  102. Point2I oldExtent = getExtent();
  103. mBounds.extent = getOuterExtent(innerRect.extent, NormalState, mProfile);
  104. GuiControl *parent = getParent();
  105. if(oldExtent != getExtent() && parent)
  106. {
  107. parent->childResized(this);
  108. }
  109. setUpdate();
  110. }
  111. S32 GuiChainCtrl::positionChildren(RectI &innerRect)
  112. {
  113. iterator i;
  114. S32 length = 0;
  115. for (i = begin(); i != end(); i++)
  116. {
  117. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  118. if (ctrl->isVisible())
  119. {
  120. if (length != 0)
  121. {
  122. length += mChildSpacing;
  123. }
  124. Point2I childPos = ctrl->getPosition();
  125. if (!mIsVertical)
  126. {
  127. childPos.x = length;
  128. }
  129. else
  130. {
  131. childPos.y = length;
  132. }
  133. ctrl->setPosition(childPos);
  134. length += mIsVertical ? ctrl->getExtent().y : ctrl->getExtent().x;
  135. }
  136. }
  137. return length;
  138. }