guiAutoScrollCtrl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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( GuiAutoScrollCtrl::PhaseComplete ),
  80. mCurrentTime( 0.f ),
  81. mCompleteTime(F32_MAX),
  82. mCurrentPosition(0.0f),
  83. mStartDelay( 3.f ),
  84. mResetDelay( 5.f ),
  85. mChildBorder( 10 ),
  86. mScrollOutOfSight( false ),
  87. mScrollSpeed( 1.f )
  88. {
  89. mIsContainer = true;
  90. }
  91. //-----------------------------------------------------------------------------
  92. void GuiAutoScrollCtrl::initPersistFields()
  93. {
  94. addGroup( "Scrolling" );
  95. addField( "scrollDirection", TYPEID< Direction >(), Offset( mDirection, GuiAutoScrollCtrl ),
  96. "Direction in which the child control is moved." );
  97. addField( "startDelay", TypeF32, Offset( mStartDelay, GuiAutoScrollCtrl ),
  98. "Seconds to wait before starting to scroll." );
  99. addField( "resetDelay", TypeF32, Offset( mResetDelay, GuiAutoScrollCtrl ),
  100. "Seconds to wait after scrolling completes before resetting and starting over.\n\n"
  101. "@note Only takes effect if #isLooping is true." );
  102. addField( "childBorder", TypeS32, Offset( mChildBorder, GuiAutoScrollCtrl ),
  103. "Padding to put around child control (in pixels)." );
  104. addField( "scrollSpeed", TypeF32, Offset( mScrollSpeed, GuiAutoScrollCtrl ),
  105. "Scrolling speed in pixels per second." );
  106. addField( "isLooping", TypeBool, Offset( mIsLooping, GuiAutoScrollCtrl ),
  107. "If true, the scrolling will reset to the beginning once completing a cycle." );
  108. addField( "scrollOutOfSight", TypeBool, Offset( mScrollOutOfSight, GuiAutoScrollCtrl ),
  109. "If true, the child control will be completely scrolled out of sight; otherwise it will only scroll "
  110. "until the other end becomes visible." );
  111. endGroup( "Scrolling" );
  112. Parent::initPersistFields();
  113. }
  114. //-----------------------------------------------------------------------------
  115. bool GuiAutoScrollCtrl::onWake()
  116. {
  117. if( !Parent::onWake() )
  118. return false;
  119. setProcessTicks( true );
  120. return true;
  121. }
  122. //-----------------------------------------------------------------------------
  123. void GuiAutoScrollCtrl::onSleep()
  124. {
  125. setProcessTicks( false );
  126. Parent::onSleep();
  127. }
  128. //-----------------------------------------------------------------------------
  129. void GuiAutoScrollCtrl::onChildAdded( GuiControl* control )
  130. {
  131. _reset( control );
  132. Parent::onChildAdded( control );
  133. }
  134. //-----------------------------------------------------------------------------
  135. void GuiAutoScrollCtrl::onChildRemoved( GuiControl* control )
  136. {
  137. mCurrentPhase = PhaseComplete;
  138. Parent::onChildRemoved( control );
  139. }
  140. //-----------------------------------------------------------------------------
  141. bool GuiAutoScrollCtrl::_isScrollComplete() const
  142. {
  143. if( empty() )
  144. return true;
  145. GuiControl* control = static_cast< GuiControl* >( at( 0 ) );
  146. U32 axis = _getScrollAxis();
  147. F32 amount = _getScrollAmount();
  148. if( mScrollOutOfSight )
  149. {
  150. // If scrolling out of sight, scrolling is complete when the control's rectangle
  151. // does not intersect our own rectangle anymore.
  152. RectI thisRect( Point2I( 0, 0 ), getExtent() );
  153. return !( thisRect.overlaps( control->getBounds() ) );
  154. }
  155. else
  156. {
  157. if( amount < 0 )
  158. return ( control->getPosition()[ axis ] + control->getExtent()[ axis ] ) < ( getExtent()[ axis ] - mChildBorder );
  159. else
  160. return ( control->getPosition()[ axis ] >= mChildBorder );
  161. }
  162. }
  163. //-----------------------------------------------------------------------------
  164. void GuiAutoScrollCtrl::_reset( GuiControl* control )
  165. {
  166. U32 axis = _getScrollAxis();
  167. U32 counterAxis = ( axis == 1 ? 0 : 1 );
  168. Point2I newPosition( mChildBorder, mChildBorder );
  169. Point2I newExtent = control->getExtent();
  170. // Fit control on axis that is not scrolled.
  171. newExtent[ counterAxis ] = getExtent()[ counterAxis ] - mChildBorder * 2;
  172. // For the right and down scrolls, position the control away from the
  173. // right/bottom edge of our control.
  174. if( mDirection == Right )
  175. newPosition.x = - ( newExtent.x - getExtent().x + mChildBorder );
  176. else if( mDirection == Down )
  177. newPosition.y = - ( newExtent.y - getExtent().y + mChildBorder );
  178. // Set the child geometry.
  179. control->setPosition( newPosition );
  180. control->setExtent( newExtent );
  181. // Reset counters.
  182. mCurrentTime = 0.0f;
  183. mCurrentPhase = PhaseInitial;
  184. mCurrentPosition = control->getPosition()[ axis ];
  185. }
  186. //-----------------------------------------------------------------------------
  187. void GuiAutoScrollCtrl::reset()
  188. {
  189. if( !empty() )
  190. _reset( static_cast< GuiControl* >( at( 0 ) ) );
  191. }
  192. //-----------------------------------------------------------------------------
  193. bool GuiAutoScrollCtrl::resize( const Point2I &newPosition, const Point2I &newExtent )
  194. {
  195. if( !Parent::resize( newPosition, newExtent ) )
  196. return false;
  197. for( iterator i = begin(); i != end(); ++ i )
  198. {
  199. GuiControl* control = static_cast< GuiControl* >( *i );
  200. if( control )
  201. _reset( control );
  202. }
  203. return true;
  204. }
  205. //-----------------------------------------------------------------------------
  206. void GuiAutoScrollCtrl::childResized( GuiControl* child )
  207. {
  208. Parent::childResized( child );
  209. _reset(child);
  210. }
  211. //-----------------------------------------------------------------------------
  212. void GuiAutoScrollCtrl::processTick()
  213. {
  214. onTick_callback();
  215. }
  216. //-----------------------------------------------------------------------------
  217. void GuiAutoScrollCtrl::advanceTime( F32 timeDelta )
  218. {
  219. if( mCurrentPhase == PhaseComplete )
  220. return;
  221. // Wait out initial delay.
  222. if( ( mCurrentTime + timeDelta ) < mStartDelay)
  223. {
  224. mCurrentTime += timeDelta;
  225. return;
  226. }
  227. // Start scrolling if we haven't already.
  228. if( mCurrentPhase == PhaseInitial )
  229. {
  230. onStart_callback();
  231. mCurrentPhase = PhaseScrolling;
  232. }
  233. GuiControl* control = static_cast< GuiControl* >( at( 0 ) );
  234. if( !control ) // Should not happen.
  235. return;
  236. // If not yet complete, scroll some more.
  237. if( !_isScrollComplete() )
  238. {
  239. U32 axis = _getScrollAxis();
  240. F32 amount = _getScrollAmount();
  241. mCurrentPosition += amount * timeDelta;
  242. Point2I newPosition = control->getPosition();
  243. newPosition[ axis ] = mCurrentPosition;
  244. control->setPosition( newPosition );
  245. }
  246. else
  247. {
  248. mCurrentTime += timeDelta;
  249. if( mCurrentPhase != PhaseComplete && mCurrentPhase != PhaseWait )
  250. {
  251. if( mCurrentPhase != PhaseWait )
  252. {
  253. onComplete_callback();
  254. mCurrentPhase = PhaseComplete;
  255. }
  256. mCompleteTime = mCurrentTime;
  257. }
  258. // Reset, if looping.
  259. if( mIsLooping )
  260. {
  261. // Wait out reset time and restart.
  262. mCurrentPhase = PhaseWait;
  263. if( mCurrentTime > ( mCompleteTime + mResetDelay ) )
  264. {
  265. onReset_callback();
  266. _reset( control );
  267. }
  268. }
  269. }
  270. }
  271. //-----------------------------------------------------------------------------
  272. void GuiAutoScrollCtrl::inspectPostApply()
  273. {
  274. Parent::inspectPostApply();
  275. reset();
  276. }
  277. //=============================================================================
  278. // API.
  279. //=============================================================================
  280. // MARK: ---- API ----
  281. //-----------------------------------------------------------------------------
  282. DefineEngineMethod( GuiAutoScrollCtrl, reset, void, (),,
  283. "Reset scrolling." )
  284. {
  285. object->reset();
  286. }