guiExpandCtrl.cc 6.2 KB

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