guiAutoScrollCtrl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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/guiAutoScrollCtrl.h"
  23. #include "console/consoleTypes.h"
  24. #include "console/engineAPI.h"
  25. IMPLEMENT_CONOBJECT( GuiAutoScrollCtrl );
  26. ConsoleDocClass( GuiAutoScrollCtrl,
  27. "@brief A container that scrolls its child control up over time.\n\n"
  28. "This container can be used to scroll a single child control in either of the four directions.\n\n"
  29. "@tsexample\n"
  30. "// Create a GuiAutoScrollCtrl that scrolls a long text of credits.\n"
  31. "new GuiAutoScrollCtrl( CreditsScroller )\n"
  32. "{\n"
  33. " position = \"0 0\";\n"
  34. " extent = Canvas.extent.x SPC Canvas.extent.y;\n"
  35. "\n"
  36. " scrollDirection = \"Up\"; // Scroll upwards.\n"
  37. " startDelay = 4; // Wait 4 seconds before starting to scroll.\n"
  38. " isLooping = false; // Don't loop the credits.\n"
  39. " scrollOutOfSight = true; // Scroll up fully.\n"
  40. "\n"
  41. " new GuiMLTextCtrl()\n"
  42. " {\n"
  43. " text = $CREDITS;\n"
  44. " };\n"
  45. "};\n"
  46. "\n"
  47. "function CreditsScroller::onComplete( %this )\n"
  48. "{\n"
  49. " // Switch back to main menu after credits have rolled.\n"
  50. " Canvas.setContent( MainMenu );\n"
  51. "}\n"
  52. "\n"
  53. "// Start rolling credits.\n"
  54. "Canvas.setContent( CreditsScroller );\n"
  55. "@endtsexample\n\n"
  56. "@note Only the first child will be scrolled.\n\n"
  57. "@ingroup GuiContainers"
  58. );
  59. IMPLEMENT_CALLBACK( GuiAutoScrollCtrl, onTick, void, (), (),
  60. "Called every 32ms on the control." );
  61. IMPLEMENT_CALLBACK( GuiAutoScrollCtrl, onStart, void, (), (),
  62. "Called when the control starts to scroll." );
  63. IMPLEMENT_CALLBACK( GuiAutoScrollCtrl, onComplete, void, (), (),
  64. "Called when the child control has been scrolled in entirety." );
  65. IMPLEMENT_CALLBACK( GuiAutoScrollCtrl, onReset, void, (), (),
  66. "Called when the child control is reset to its initial position and the cycle starts again." );
  67. ImplementEnumType( GuiAutoScrollDirection,
  68. "Direction in which to scroll the child control.\n\n"
  69. "@ingroup GuiContainers" )
  70. { GuiAutoScrollCtrl::Up, "Up", "Scroll from bottom towards top." },
  71. { GuiAutoScrollCtrl::Down, "Down", "Scroll from top towards bottom." },
  72. { GuiAutoScrollCtrl::Left, "Left", "Scroll from right towards left." },
  73. { GuiAutoScrollCtrl::Right, "Right", "Scroll from left towards right." },
  74. EndImplementEnumType;
  75. //-----------------------------------------------------------------------------
  76. GuiAutoScrollCtrl::GuiAutoScrollCtrl()
  77. : mDirection( Up ),
  78. mIsLooping( true ),
  79. mCurrentPhase( PhaseComplete ),
  80. mCurrentTime( 0.f ),
  81. mStartDelay( 3.f ),
  82. mResetDelay( 5.f ),
  83. mChildBorder( 10 ),
  84. mScrollSpeed( 1.f ),
  85. mScrollOutOfSight( false )
  86. {
  87. mIsContainer = true;
  88. }
  89. //-----------------------------------------------------------------------------
  90. void GuiAutoScrollCtrl::initPersistFields()
  91. {
  92. addGroup( "Scrolling" );
  93. addField( "scrollDirection", TYPEID< Direction >(), Offset( mDirection, GuiAutoScrollCtrl ),
  94. "Direction in which the child control is moved." );
  95. addField( "startDelay", TypeF32, Offset( mStartDelay, GuiAutoScrollCtrl ),
  96. "Seconds to wait before starting to scroll." );
  97. addField( "resetDelay", TypeF32, Offset( mResetDelay, GuiAutoScrollCtrl ),
  98. "Seconds to wait after scrolling completes before resetting and starting over.\n\n"
  99. "@note Only takes effect if #isLooping is true." );
  100. addField( "childBorder", TypeS32, Offset( mChildBorder, GuiAutoScrollCtrl ),
  101. "Padding to put around child control (in pixels)." );
  102. addField( "scrollSpeed", TypeF32, Offset( mScrollSpeed, GuiAutoScrollCtrl ),
  103. "Scrolling speed in pixels per second." );
  104. addField( "isLooping", TypeBool, Offset( mIsLooping, GuiAutoScrollCtrl ),
  105. "If true, the scrolling will reset to the beginning once completing a cycle." );
  106. addField( "scrollOutOfSight", TypeBool, Offset( mScrollOutOfSight, GuiAutoScrollCtrl ),
  107. "If true, the child control will be completely scrolled out of sight; otherwise it will only scroll "
  108. "until the other end becomes visible." );
  109. endGroup( "Scrolling" );
  110. Parent::initPersistFields();
  111. }
  112. //-----------------------------------------------------------------------------
  113. bool GuiAutoScrollCtrl::onWake()
  114. {
  115. if( !Parent::onWake() )
  116. return false;
  117. setProcessTicks( true );
  118. return true;
  119. }
  120. //-----------------------------------------------------------------------------
  121. void GuiAutoScrollCtrl::onSleep()
  122. {
  123. setProcessTicks( false );
  124. Parent::onSleep();
  125. }
  126. //-----------------------------------------------------------------------------
  127. void GuiAutoScrollCtrl::onChildAdded( GuiControl* control )
  128. {
  129. _reset( control );
  130. Parent::onChildAdded( control );
  131. }
  132. //-----------------------------------------------------------------------------
  133. void GuiAutoScrollCtrl::onChildRemoved( GuiControl* control )
  134. {
  135. mCurrentPhase = PhaseComplete;
  136. Parent::onChildRemoved( control );
  137. }
  138. //-----------------------------------------------------------------------------
  139. bool GuiAutoScrollCtrl::_isScrollComplete() const
  140. {
  141. if( empty() )
  142. return true;
  143. GuiControl* control = static_cast< GuiControl* >( at( 0 ) );
  144. U32 axis = _getScrollAxis();
  145. F32 amount = _getScrollAmount();
  146. if( mScrollOutOfSight )
  147. {
  148. // If scrolling out of sight, scrolling is complete when the control's rectangle
  149. // does not intersect our own rectangle anymore.
  150. RectI thisRect( Point2I( 0, 0 ), getExtent() );
  151. return !( thisRect.overlaps( control->getBounds() ) );
  152. }
  153. else
  154. {
  155. if( amount < 0 )
  156. return ( control->getPosition()[ axis ] + control->getExtent()[ axis ] ) < ( getExtent()[ axis ] - mChildBorder );
  157. else
  158. return ( control->getPosition()[ axis ] >= mChildBorder );
  159. }
  160. }
  161. //-----------------------------------------------------------------------------
  162. void GuiAutoScrollCtrl::_reset( GuiControl* control )
  163. {
  164. U32 axis = _getScrollAxis();
  165. U32 counterAxis = ( axis == 1 ? 0 : 1 );
  166. Point2I newPosition( mChildBorder, mChildBorder );
  167. Point2I newExtent = control->getExtent();
  168. // Fit control on axis that is not scrolled.
  169. newExtent[ counterAxis ] = getExtent()[ counterAxis ] - mChildBorder * 2;
  170. // For the right and down scrolls, position the control away from the
  171. // right/bottom edge of our control.
  172. if( mDirection == Right )
  173. newPosition.x = - ( newExtent.x - getExtent().x + mChildBorder );
  174. else if( mDirection == Down )
  175. newPosition.y = - ( newExtent.y - getExtent().y + mChildBorder );
  176. // Set the child geometry.
  177. control->setPosition( newPosition );
  178. control->setExtent( newExtent );
  179. // Reset counters.
  180. mCurrentTime = 0.0f;
  181. mCurrentPhase = PhaseInitial;
  182. mCurrentPosition = control->getPosition()[ axis ];
  183. }
  184. //-----------------------------------------------------------------------------
  185. void GuiAutoScrollCtrl::reset()
  186. {
  187. if( !empty() )
  188. _reset( static_cast< GuiControl* >( at( 0 ) ) );
  189. }
  190. //-----------------------------------------------------------------------------
  191. bool GuiAutoScrollCtrl::resize( const Point2I &newPosition, const Point2I &newExtent )
  192. {
  193. if( !Parent::resize( newPosition, newExtent ) )
  194. return false;
  195. for( iterator i = begin(); i != end(); ++ i )
  196. {
  197. GuiControl* control = static_cast< GuiControl* >( *i );
  198. if( control )
  199. _reset( control );
  200. }
  201. return true;
  202. }
  203. //-----------------------------------------------------------------------------
  204. void GuiAutoScrollCtrl::childResized( GuiControl* child )
  205. {
  206. Parent::childResized( child );
  207. _reset(child);
  208. }
  209. //-----------------------------------------------------------------------------
  210. void GuiAutoScrollCtrl::processTick()
  211. {
  212. onTick_callback();
  213. }
  214. //-----------------------------------------------------------------------------
  215. void GuiAutoScrollCtrl::advanceTime( F32 timeDelta )
  216. {
  217. if( mCurrentPhase == PhaseComplete )
  218. return;
  219. // Wait out initial delay.
  220. if( ( mCurrentTime + timeDelta ) < mStartDelay)
  221. {
  222. mCurrentTime += timeDelta;
  223. return;
  224. }
  225. // Start scrolling if we haven't already.
  226. if( mCurrentPhase == PhaseInitial )
  227. {
  228. onStart_callback();
  229. mCurrentPhase = PhaseScrolling;
  230. }
  231. GuiControl* control = static_cast< GuiControl* >( at( 0 ) );
  232. if( !control ) // Should not happen.
  233. return;
  234. // If not yet complete, scroll some more.
  235. if( !_isScrollComplete() )
  236. {
  237. U32 axis = _getScrollAxis();
  238. F32 amount = _getScrollAmount();
  239. mCurrentPosition += amount * timeDelta;
  240. Point2I newPosition = control->getPosition();
  241. newPosition[ axis ] = mCurrentPosition;
  242. control->setPosition( newPosition );
  243. }
  244. else
  245. {
  246. mCurrentTime += timeDelta;
  247. if( mCurrentPhase != PhaseComplete && mCurrentPhase != PhaseWait )
  248. {
  249. if( mCurrentPhase != PhaseWait )
  250. {
  251. onComplete_callback();
  252. mCurrentPhase = PhaseComplete;
  253. }
  254. mCompleteTime = mCurrentTime;
  255. }
  256. // Reset, if looping.
  257. if( mIsLooping )
  258. {
  259. // Wait out reset time and restart.
  260. mCurrentPhase = PhaseWait;
  261. if( mCurrentTime > ( mCompleteTime + mResetDelay ) )
  262. {
  263. onReset_callback();
  264. _reset( control );
  265. }
  266. }
  267. }
  268. }
  269. //-----------------------------------------------------------------------------
  270. void GuiAutoScrollCtrl::inspectPostApply()
  271. {
  272. Parent::inspectPostApply();
  273. reset();
  274. }
  275. //=============================================================================
  276. // API.
  277. //=============================================================================
  278. // MARK: ---- API ----
  279. //-----------------------------------------------------------------------------
  280. DefineEngineMethod( GuiAutoScrollCtrl, reset, void, (),,
  281. "Reset scrolling." )
  282. {
  283. object->reset();
  284. }