guiExpandCtrl.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 "gui/containers/guiExpandCtrl.h"
  23. #include "guiExpandCtrl_ScriptBinding.h"
  24. IMPLEMENT_CONOBJECT(GuiExpandCtrl);
  25. GuiExpandCtrl::GuiExpandCtrl()
  26. {
  27. mActive = true;
  28. mExpanded = false;
  29. mIsContainer = true;
  30. mCollapsedExtent.set(64,64);
  31. mAnimationProgress = 1;
  32. mExpandedExtent.set(64, 64);
  33. mEasingFunction = EasingFunction::Linear;
  34. mAnimationLength = 500;
  35. }
  36. void GuiExpandCtrl::initPersistFields()
  37. {
  38. Parent::initPersistFields();
  39. addField("easeExpand", TypeEnum, Offset(mEasingFunction, GuiExpandCtrl), 1, &gEasingTable);
  40. addField("easeTimeExpand", TypeS32, Offset(mAnimationLength, GuiExpandCtrl));
  41. }
  42. void GuiExpandCtrl::parentResized(const Point2I &oldParentExtent, const Point2I &newParentExtent)
  43. {
  44. Point2I newPosition = getPosition();
  45. Point2I newExtent = getExtent();
  46. S32 deltaX = newParentExtent.x - oldParentExtent.x;
  47. S32 deltaY = newParentExtent.y - oldParentExtent.y;
  48. if (mHorizSizing == horizResizeCenter)
  49. newPosition.x = (newParentExtent.x - mBounds.extent.x) >> 1;
  50. else if (mHorizSizing == horizResizeWidth)
  51. {
  52. newExtent.x += deltaX;
  53. mCollapsedExtent.x += deltaX;
  54. }
  55. else if (mHorizSizing == horizResizeLeft)
  56. newPosition.x += deltaX;
  57. else if (mHorizSizing == horizResizeRelative && oldParentExtent.x != 0)
  58. {
  59. S32 newLeft = (newPosition.x * newParentExtent.x) / oldParentExtent.x;
  60. S32 newRight = ((newPosition.x + newExtent.x) * newParentExtent.x) / oldParentExtent.x;
  61. newPosition.x = newLeft;
  62. newExtent.x = newRight - newLeft;
  63. S32 collapsedRight = ((newPosition.x + mCollapsedExtent.x) * newParentExtent.x) / oldParentExtent.x;
  64. mCollapsedExtent.x = collapsedRight - newLeft;
  65. }
  66. if (mVertSizing == vertResizeCenter)
  67. newPosition.y = (newParentExtent.y - mBounds.extent.y) >> 1;
  68. else if (mVertSizing == vertResizeHeight)
  69. {
  70. newExtent.y += deltaY;
  71. mCollapsedExtent.y += deltaY;
  72. }
  73. else if (mVertSizing == vertResizeTop)
  74. newPosition.y += deltaY;
  75. else if (mVertSizing == vertResizeRelative && oldParentExtent.y != 0)
  76. {
  77. S32 newTop = (newPosition.y * newParentExtent.y) / oldParentExtent.y;
  78. S32 newBottom = ((newPosition.y + newExtent.y) * newParentExtent.y) / oldParentExtent.y;
  79. newPosition.y = newTop;
  80. newExtent.y = newBottom - newTop;
  81. S32 collapsedBottom = ((newPosition.y + mCollapsedExtent.y) * newParentExtent.y) / oldParentExtent.y;
  82. mCollapsedExtent.y = collapsedBottom - newTop;
  83. }
  84. if (mAnimationProgress == 1 && !mExpanded)
  85. {
  86. setCollapsedExtent(newExtent);
  87. }
  88. resize(newPosition, newExtent);
  89. calcExpandedExtent();
  90. if (mExpanded)
  91. {
  92. mBounds.extent = mExpandedExtent;
  93. }
  94. else
  95. {
  96. mBounds.extent = mCollapsedExtent;
  97. }
  98. setUpdate();
  99. }
  100. void GuiExpandCtrl::childResized(GuiControl* child)
  101. {
  102. calcExpandedExtent();
  103. Parent::childResized(child);
  104. }
  105. void GuiExpandCtrl::setCollapsedExtent(const Point2I &extent)
  106. {
  107. mCollapsedExtent = extent;
  108. mExpandedExtent.set(getMax(extent.x, mExpandedExtent.x), getMax(extent.y, mExpandedExtent.y));
  109. }
  110. bool GuiExpandCtrl::calcExpandedExtent()
  111. {
  112. if (!size())
  113. return false;
  114. mExpandedExtent = Point2I(0, 0);
  115. for (iterator itr = begin(); itr != end(); ++itr)
  116. {
  117. GuiControl* child = dynamic_cast<GuiControl*>(*itr);
  118. mExpandedExtent.setMax(child->getExtent() + child->getPosition());
  119. }
  120. mExpandedExtent = getOuterExtent(mExpandedExtent, GuiControlState::NormalState, mProfile);
  121. mExpandedExtent.set(getMax(mCollapsedExtent.x, mExpandedExtent.x), getMax(mCollapsedExtent.y, mExpandedExtent.y));
  122. return true;
  123. }
  124. void GuiExpandCtrl::setExpanded(bool isExpanded)
  125. {
  126. if ((mExpanded == isExpanded) || (isExpanded && !calcExpandedExtent()))
  127. {
  128. return;
  129. }
  130. mAnimationProgress = 1 - mAnimationProgress;
  131. mExpanded = isExpanded;
  132. setProcessTicks(true);
  133. }
  134. bool GuiExpandCtrl::processExpansion()
  135. {
  136. F32 progress = getProgress(32.0f);
  137. setUpdate();
  138. if (mExpanded)
  139. {
  140. //We are growing
  141. mBounds.extent.x = processValue(progress, mCollapsedExtent.x, mExpandedExtent.x);
  142. mBounds.extent.y = processValue(progress, mCollapsedExtent.y, mExpandedExtent.y);
  143. }
  144. else
  145. {
  146. //We are shrinking
  147. mBounds.extent.x = processValue(progress, mExpandedExtent.x, mCollapsedExtent.x);
  148. mBounds.extent.y = processValue(progress, mExpandedExtent.y, mCollapsedExtent.y);
  149. }
  150. GuiControl* parent = getParent();
  151. if (parent)
  152. {
  153. if (mHorizSizing == horizResizeCenter)
  154. {
  155. mBounds.point.x = (parent->mBounds.extent.x - mBounds.extent.x) / 2;
  156. }
  157. if (mVertSizing == vertResizeCenter)
  158. {
  159. mBounds.point.y = (parent->mBounds.extent.y - mBounds.extent.y) / 2;
  160. }
  161. parent->childResized(this);
  162. }
  163. setUpdate();
  164. if (isMethod("onResize"))
  165. {
  166. Con::executef(this, 2, "onResize");
  167. }
  168. if (mAnimationProgress >= 1.0f)
  169. {
  170. mAnimationProgress = 1.0f;
  171. return false;
  172. }
  173. return true;
  174. }
  175. void GuiExpandCtrl::processTick()
  176. {
  177. bool shouldWeContinue = false;
  178. //Expanding
  179. shouldWeContinue |= processExpansion();
  180. if (!shouldWeContinue)
  181. {
  182. setProcessTicks(false);
  183. }
  184. }