guiExpandCtrl.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. mCollapsedExtent.set(64,64);
  30. mAnimationProgress = 1;
  31. mExpandedExtent.set(64, 64);
  32. mEasingFunction = EasingFunction::Linear;
  33. mAnimationLength = 500;
  34. mCalcGuard = false;
  35. setField("profile", "GuiDefaultProfile");
  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::addObject(SimObject* obj)
  44. {
  45. Parent::addObject(obj);
  46. toggleHiddenChildren();
  47. }
  48. void GuiExpandCtrl::parentResized(const Point2I &oldParentExtent, const Point2I &newParentExtent)
  49. {
  50. Point2I newPosition = getPosition();
  51. Point2I newExtent = getExtent();
  52. S32 deltaX = newParentExtent.x - oldParentExtent.x;
  53. S32 deltaY = newParentExtent.y - oldParentExtent.y;
  54. if (mHorizSizing == horizResizeCenter)
  55. newPosition.x = (newParentExtent.x - mBounds.extent.x) >> 1;
  56. else if (mHorizSizing == horizResizeWidth)
  57. {
  58. newExtent.x += deltaX;
  59. mCollapsedExtent.x += deltaX;
  60. }
  61. else if (mHorizSizing == horizResizeLeft)
  62. newPosition.x += deltaX;
  63. else if (mHorizSizing == horizResizeRelative && oldParentExtent.x != 0)
  64. {
  65. S32 newLeft = (newPosition.x * newParentExtent.x) / oldParentExtent.x;
  66. S32 newRight = ((newPosition.x + newExtent.x) * newParentExtent.x) / oldParentExtent.x;
  67. newPosition.x = newLeft;
  68. newExtent.x = newRight - newLeft;
  69. S32 collapsedRight = ((newPosition.x + mCollapsedExtent.x) * newParentExtent.x) / oldParentExtent.x;
  70. mCollapsedExtent.x = collapsedRight - newLeft;
  71. }
  72. if (mVertSizing == vertResizeCenter)
  73. newPosition.y = (newParentExtent.y - mBounds.extent.y) >> 1;
  74. else if (mVertSizing == vertResizeHeight)
  75. {
  76. newExtent.y += deltaY;
  77. mCollapsedExtent.y += deltaY;
  78. }
  79. else if (mVertSizing == vertResizeTop)
  80. newPosition.y += deltaY;
  81. else if (mVertSizing == vertResizeRelative && oldParentExtent.y != 0)
  82. {
  83. S32 newTop = (newPosition.y * newParentExtent.y) / oldParentExtent.y;
  84. S32 newBottom = ((newPosition.y + newExtent.y) * newParentExtent.y) / oldParentExtent.y;
  85. newPosition.y = newTop;
  86. newExtent.y = newBottom - newTop;
  87. S32 collapsedBottom = ((newPosition.y + mCollapsedExtent.y) * newParentExtent.y) / oldParentExtent.y;
  88. mCollapsedExtent.y = collapsedBottom - newTop;
  89. }
  90. if (mAnimationProgress == 1 && !mExpanded)
  91. {
  92. setCollapsedExtent(newExtent);
  93. }
  94. mCalcGuard = true;
  95. resize(newPosition, newExtent);
  96. mCalcGuard = false;
  97. calcExpandedExtent();
  98. if (mExpanded)
  99. {
  100. mBounds.extent = mExpandedExtent;
  101. }
  102. else
  103. {
  104. mBounds.extent = mCollapsedExtent;
  105. toggleHiddenChildren();
  106. }
  107. setUpdate();
  108. }
  109. void GuiExpandCtrl::childResized(GuiControl* child)
  110. {
  111. calcExpandedExtent();
  112. Parent::childResized(child);
  113. toggleHiddenChildren();
  114. }
  115. void GuiExpandCtrl::setCollapsedExtent(const Point2I &extent)
  116. {
  117. mCollapsedExtent = extent;
  118. mExpandedExtent.set(getMax(extent.x, mExpandedExtent.x), getMax(extent.y, mExpandedExtent.y));
  119. }
  120. bool GuiExpandCtrl::calcExpandedExtent()
  121. {
  122. if (!size())
  123. return false;
  124. if(!mCalcGuard)//Prevent needless calcuations
  125. {
  126. mExpandedExtent = Point2I(0, 0);
  127. for (iterator itr = begin(); itr != end(); ++itr)
  128. {
  129. GuiControl* child = dynamic_cast<GuiControl*>(*itr);
  130. mExpandedExtent.setMax(child->getExtent() + child->getPosition());
  131. }
  132. mExpandedExtent = getOuterExtent(mExpandedExtent, GuiControlState::NormalState, mProfile);
  133. mExpandedExtent.set(getMax(mCollapsedExtent.x, mExpandedExtent.x), getMax(mCollapsedExtent.y, mExpandedExtent.y));
  134. }
  135. return true;
  136. }
  137. void GuiExpandCtrl::setExpanded(bool isExpanded)
  138. {
  139. if ((mExpanded == isExpanded) || (isExpanded && !calcExpandedExtent()))
  140. {
  141. return;
  142. }
  143. mAnimationProgress = 1 - mAnimationProgress;
  144. mExpanded = isExpanded;
  145. setProcessTicks(true);
  146. if (mExpanded)
  147. {
  148. startExpand();
  149. }
  150. else
  151. {
  152. startCollapse();
  153. }
  154. }
  155. void GuiExpandCtrl::setExpandedInstant(bool isExpanded)
  156. {
  157. mExpanded = isExpanded;
  158. if (mExpanded)
  159. {
  160. mBounds.extent.set(mExpandedExtent.x, mExpandedExtent.y);
  161. }
  162. else
  163. {
  164. mBounds.extent.set(mCollapsedExtent.x, mCollapsedExtent.y);
  165. }
  166. toggleHiddenChildren();
  167. }
  168. bool GuiExpandCtrl::processExpansion()
  169. {
  170. F32 progress = getProgress(32.0f);
  171. setUpdate();
  172. if (mExpanded)
  173. {
  174. //We are growing
  175. mBounds.extent.x = processValue(progress, mCollapsedExtent.x, mExpandedExtent.x);
  176. mBounds.extent.y = processValue(progress, mCollapsedExtent.y, mExpandedExtent.y);
  177. }
  178. else
  179. {
  180. //We are shrinking
  181. mBounds.extent.x = processValue(progress, mExpandedExtent.x, mCollapsedExtent.x);
  182. mBounds.extent.y = processValue(progress, mExpandedExtent.y, mCollapsedExtent.y);
  183. }
  184. GuiControl* parent = getParent();
  185. if (parent)
  186. {
  187. if (mHorizSizing == horizResizeCenter)
  188. {
  189. mBounds.point.x = (parent->mBounds.extent.x - mBounds.extent.x) / 2;
  190. }
  191. if (mVertSizing == vertResizeCenter)
  192. {
  193. mBounds.point.y = (parent->mBounds.extent.y - mBounds.extent.y) / 2;
  194. }
  195. parent->childResized(this);
  196. }
  197. setUpdate();
  198. if (isMethod("onResize"))
  199. {
  200. Con::executef(this, 2, "onResize");
  201. }
  202. if (mAnimationProgress >= 1.0f)
  203. {
  204. mAnimationProgress = 1.0f;
  205. if (!mExpanded)
  206. {
  207. collapseComplete();
  208. }
  209. else
  210. {
  211. expandComplete();
  212. }
  213. return false;
  214. }
  215. return true;
  216. }
  217. void GuiExpandCtrl::processTick()
  218. {
  219. bool shouldWeContinue = false;
  220. //Expanding
  221. shouldWeContinue |= processExpansion();
  222. if (!shouldWeContinue)
  223. {
  224. setProcessTicks(false);
  225. }
  226. }
  227. void GuiExpandCtrl::startExpand()
  228. {
  229. if (isMethod("onStartExpand"))
  230. {
  231. Con::executef(this, 2, "onStartExpand");
  232. }
  233. toggleHiddenChildren();
  234. }
  235. void GuiExpandCtrl::startCollapse()
  236. {
  237. if (isMethod("onStartCollapse"))
  238. {
  239. Con::executef(this, 2, "onStartCollapse");
  240. }
  241. }
  242. void GuiExpandCtrl::expandComplete()
  243. {
  244. if (isMethod("onExpandComplete"))
  245. {
  246. Con::executef(this, 2, "onExpandComplete");
  247. }
  248. }
  249. void GuiExpandCtrl::collapseComplete()
  250. {
  251. if (isMethod("onCollapseComplete"))
  252. {
  253. Con::executef(this, 2, "onCollapseComplete");
  254. }
  255. toggleHiddenChildren();
  256. }
  257. void GuiExpandCtrl::toggleHiddenChildren()
  258. {
  259. RectI innerRect = getInnerRect();
  260. innerRect.point = Point2I::Zero;
  261. for (iterator i = begin(); i != end(); i++)
  262. {
  263. GuiControl* ctrl = static_cast<GuiControl*>(*i);
  264. RectI childBounds = ctrl->getBounds();
  265. RectI parentBounds = RectI(innerRect);
  266. if (!mExpanded && !parentBounds.intersect(childBounds))
  267. {
  268. ctrl->mVisible = false;
  269. }
  270. else
  271. {
  272. ctrl->mVisible = true;
  273. }
  274. }
  275. }