guiChainCtrl.cc 3.9 KB

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