guiChainCtrl.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. Parent::onChildAdded(child);
  71. calculateExtent();
  72. }
  73. void GuiChainCtrl::onChildRemoved(SimObject *child)
  74. {
  75. calculateExtent();
  76. }
  77. void GuiChainCtrl::calculateExtent()
  78. {
  79. RectI innerRect = getInnerRect(Point2I(mBounds.point.Zero), Point2I(getExtent()), NormalState, mProfile);
  80. S32 length = positionChildren(innerRect);
  81. if (!mIsVertical)
  82. {
  83. innerRect.extent.x = length;
  84. }
  85. else
  86. {
  87. innerRect.extent.y = length;
  88. }
  89. //call set update both before and after
  90. setUpdate();
  91. Point2I oldExtent = getExtent();
  92. mBounds.extent = getOuterExtent(innerRect.extent, NormalState, mProfile);
  93. GuiControl *parent = getParent();
  94. if(oldExtent != getExtent() && parent)
  95. {
  96. parent->childResized(this);
  97. }
  98. setUpdate();
  99. }
  100. S32 GuiChainCtrl::positionChildren(RectI &innerRect)
  101. {
  102. iterator i;
  103. S32 length = 0;
  104. for (i = begin(); i != end(); i++)
  105. {
  106. GuiControl *ctrl = static_cast<GuiControl *>(*i);
  107. if (ctrl->isVisible())
  108. {
  109. if (length != 0)
  110. {
  111. length += mChildSpacing;
  112. }
  113. Point2I childPos = ctrl->getPosition();
  114. if (!mIsVertical)
  115. {
  116. childPos.x = length;
  117. }
  118. else
  119. {
  120. childPos.y = length;
  121. }
  122. ctrl->setPosition(childPos);
  123. length += mIsVertical ? ctrl->getExtent().y : ctrl->getExtent().x;
  124. }
  125. }
  126. return length;
  127. }