guiAutoScrollCtrl.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #ifndef _GUIAUTOSCROLLCTRL_H_
  23. #define _GUIAUTOSCROLLCTRL_H_
  24. #ifndef _GUICONTROL_H_
  25. #include "gui/core/guiControl.h"
  26. #endif
  27. #ifndef _GUITICKCTRL_H_
  28. #include "gui/shiny/guiTickCtrl.h"
  29. #endif
  30. /// A control that automatically scrolls its child control upwards.
  31. class GuiAutoScrollCtrl : public GuiTickCtrl
  32. {
  33. public:
  34. typedef GuiTickCtrl Parent;
  35. /// Scrolling direction.
  36. enum Direction
  37. {
  38. Up,
  39. Down,
  40. Left,
  41. Right
  42. };
  43. protected:
  44. enum Phase
  45. {
  46. PhaseInitial, ///< Waiting to begin scrolling.
  47. PhaseScrolling, ///< Currently scrolling.
  48. PhaseComplete, ///< Scrolling complete.
  49. PhaseWait ///< Wait before starting a new loop.
  50. };
  51. /// The direction in which to scroll.
  52. Direction mDirection;
  53. /// If true, scrolling will start from the beginning once finished.
  54. bool mIsLooping;
  55. /// Whether to scroll the child control completely out of sight.
  56. bool mScrollOutOfSight;
  57. /// Current phase in the scrolling animation.
  58. Phase mCurrentPhase;
  59. /// The current animation time.
  60. F32 mCurrentTime;
  61. /// The time scrolling was completed.
  62. F32 mCompleteTime;
  63. /// Current scrolling position. This is kept separate from the control's
  64. /// current position value since we will receive time updates in increments
  65. /// less than a second and thus need to have this value in floating-point.
  66. F32 mCurrentPosition;
  67. /// Seconds to wait before starting to scroll.
  68. F32 mStartDelay;
  69. /// Seconds to wait after scrolling is complete before reseting the control
  70. /// to the initial state (only if #mIsLooping is true).
  71. F32 mResetDelay;
  72. /// Border to put around scrolled child control.
  73. S32 mChildBorder;
  74. /// Speed at which to scroll in pixels per second.
  75. F32 mScrollSpeed;
  76. /// @name Callbacks
  77. /// @{
  78. DECLARE_CALLBACK( void, onTick, () );
  79. DECLARE_CALLBACK( void, onStart, () );
  80. DECLARE_CALLBACK( void, onComplete, () );
  81. DECLARE_CALLBACK( void, onReset, () );
  82. /// @}
  83. void _reset( GuiControl* control );
  84. bool _isScrollComplete() const;
  85. U32 _getScrollAxis() const
  86. {
  87. switch( mDirection )
  88. {
  89. case Up: return 1;
  90. case Down: return 1;
  91. case Left: return 0;
  92. case Right: return 0;
  93. }
  94. return 0;
  95. }
  96. F32 _getScrollAmount() const
  97. {
  98. switch( mDirection )
  99. {
  100. case Up: return - mScrollSpeed;
  101. case Down: return mScrollSpeed;
  102. case Left: return - mScrollSpeed;
  103. case Right: return mScrollSpeed;
  104. }
  105. return 0.f;
  106. }
  107. public:
  108. GuiAutoScrollCtrl();
  109. void reset();
  110. virtual bool onWake();
  111. virtual void onSleep();
  112. virtual void onChildAdded( GuiControl* control );
  113. virtual void onChildRemoved( GuiControl* control );
  114. virtual bool resize( const Point2I& newPosition, const Point2I& newExtent );
  115. virtual void childResized( GuiControl *child );
  116. virtual void processTick();
  117. virtual void advanceTime( F32 timeDelta );
  118. virtual void inspectPostApply();
  119. static void initPersistFields();
  120. DECLARE_CONOBJECT( GuiAutoScrollCtrl );
  121. DECLARE_CATEGORY( "Gui Containers" );
  122. DECLARE_DESCRIPTION( "A container that automatically scrolls its child control upwards.\n"
  123. "Can be used, for example, for credits screens." );
  124. };
  125. typedef GuiAutoScrollCtrl::Direction GuiAutoScrollDirection;
  126. DefineEnumType( GuiAutoScrollDirection );
  127. #endif