guiAutoScrollCtrl.cpp 11 KB

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