guiScrollCtrl.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 _GUISCROLLCTRL_H_
  23. #define _GUISCROLLCTRL_H_
  24. #ifndef _GUICONTAINER_H_
  25. #include "gui/containers/guiContainer.h"
  26. #endif
  27. /// A control providing a window inside a larger client area which can be
  28. /// scrolled using scrollbars.
  29. ///
  30. class GuiScrollCtrl : public GuiContainer
  31. {
  32. public:
  33. typedef GuiContainer Parent;
  34. enum Region
  35. {
  36. UpArrow,
  37. DownArrow,
  38. LeftArrow,
  39. RightArrow,
  40. UpPage,
  41. DownPage,
  42. LeftPage,
  43. RightPage,
  44. VertThumb,
  45. HorizThumb,
  46. None
  47. };
  48. enum ScrollBarBehavior
  49. {
  50. ScrollBarAlwaysOn = 0,
  51. ScrollBarAlwaysOff = 1,
  52. ScrollBarDynamic = 2
  53. };
  54. protected:
  55. // the scroll control uses a bitmap array to draw all its
  56. // graphics... these are the bitmaps it needs:
  57. enum BitmapIndices
  58. {
  59. BmpUp,
  60. BmpDown,
  61. BmpVThumbTopCap,
  62. BmpVThumb,
  63. BmpVThumbBottomCap,
  64. BmpVPage,
  65. BmpLeft,
  66. BmpRight,
  67. BmpHThumbLeftCap,
  68. BmpHThumb,
  69. BmpHThumbRightCap,
  70. BmpHPage,
  71. BmpResize,
  72. BmpCount
  73. };
  74. enum BitmapStates
  75. {
  76. BmpDefault = 0,
  77. BmpHilite,
  78. BmpDisabled,
  79. BmpStates
  80. };
  81. RectI *mBitmapBounds; //bmp is [3*n], bmpHL is [3*n + 1], bmpNA is [3*n + 2]
  82. GFXTexHandle mTextureObject;
  83. S32 mBorderThickness; // this gets set per class in the constructor
  84. Point2I mChildMargin; // the thickness of the margin around the child controls
  85. // note - it is implicit in the scroll view that the buttons all have the same
  86. // arrow length and that horizontal and vertical scroll bars have the
  87. // same thickness
  88. S32 mScrollBarThickness; // determined by the width of the vertical page bmp
  89. S32 mScrollBarArrowBtnLength; // determined by the height of the up arrow
  90. S32 mScrollBarDragTolerance; // maximal distance from scrollbar at which a scrollbar drag is still valid
  91. bool mHBarEnabled;
  92. bool mVBarEnabled;
  93. bool mHasHScrollBar;
  94. bool mHasVScrollBar;
  95. Point2I mContentPos; // the position of the content region in the control's coord system
  96. Point2I mContentExt; // the extent of the content region
  97. Point2I mChildPos; // the position of the upper left corner of the child control(s)
  98. Point2I mChildExt;
  99. Point2I mChildRelPos; // the relative position of the upper left content corner in
  100. // the child's coordinate system - 0,0 if scrolled all the way to upper left.
  101. //--------------------------------------
  102. // for mouse dragging the thumb
  103. Point2I mChildRelPosAnchor; // the original childRelPos when scrolling started
  104. S32 mThumbMouseDelta;
  105. S32 mLastUpdated;
  106. S32 mHThumbSize;
  107. S32 mHThumbPos;
  108. S32 mVThumbSize;
  109. S32 mVThumbPos;
  110. S32 mBaseThumbSize;
  111. RectI mUpArrowRect;
  112. RectI mDownArrowRect;
  113. RectI mLeftArrowRect;
  114. RectI mRightArrowRect;
  115. RectI mHTrackRect;
  116. RectI mVTrackRect;
  117. //--------------------------------------
  118. // for determining hit area
  119. bool mStateDepressed; ///< Is the mouse currently depressed on a scroll region
  120. Region mHitRegion; ///< Which region is hit by the mouse
  121. S32 mForceHScrollBar; ///< Force showing the Horizontal scrollbar
  122. S32 mForceVScrollBar; ///< Force showing the Vertical scrollbar
  123. bool mLockHorizScroll; ///< Is horizontal scrolling disabled
  124. bool mLockVertScroll; ///< Is vertical scrolling disabled
  125. bool mUseConstantHeightThumb;
  126. bool mWillFirstRespond; // for automatically handling arrow keys
  127. /// Used internally to prevent infinite recursion.
  128. bool mIgnoreChildResized;
  129. /// MouseWheel scroll animation
  130. /// @{
  131. /// Is currently performing a scroll animation.
  132. bool mAnimating;
  133. /// Pixels moved per tick when performing a scroll animation.
  134. S32 mScrollAnimSpeed;
  135. /// The target position when performing a scroll animation.
  136. Point2I mScrollTargetPos;
  137. /// Platform time of the last call to onPreRender
  138. S32 mLastPreRender;
  139. /// @}
  140. /// @name Callbacks
  141. /// @{
  142. DECLARE_CALLBACK( void, onScroll, () );
  143. /// @}
  144. Region findHitRegion(const Point2I &);
  145. virtual bool calcChildExtents();
  146. virtual void calcScrollRects(void);
  147. void calcThumbs();
  148. void scrollByRegion(Region reg);
  149. void scrollByMouseWheel( const GuiEvent &event );
  150. /// Tell the kids that the mouse moved (relatively)
  151. void updateChildMousePos();
  152. ///
  153. void _onMouseDown( const GuiEvent& event, bool lockMouse );
  154. public:
  155. GuiScrollCtrl();
  156. void autoScroll(Region reg);
  157. void scrollDeltaAnimate(S32 x, S32 y);
  158. void scrollTo(S32 x, S32 y);
  159. void scrollToObject(GuiControl *targetControl);
  160. void scrollDelta(S32 x, S32 y);
  161. void scrollRectVisible(RectI rect);
  162. /// Is the given client space rect completely visible within the actual
  163. /// visible area, or is some of it clipped. Returns true if it is
  164. /// completely visible.
  165. bool isRectCompletelyVisible(const RectI& rect);
  166. bool isPointVisible( const Point2I& point );
  167. void computeSizes();
  168. // you can change the bitmap array dynamically.
  169. void loadBitmapArray();
  170. void addObject(SimObject *obj);
  171. bool resize(const Point2I &newPosition, const Point2I &newExtent);
  172. void childResized(GuiControl *child);
  173. Point2I getChildPos() { return mChildPos; }
  174. Point2I getChildRelPos() { return mChildRelPos; };
  175. Point2I getChildExtent() { return mChildExt; }
  176. Point2I getContentExtent() { return mContentExt; }
  177. Point2I getChildMargin() { return mChildMargin; } // Added to aid in sizing calculations
  178. S32 getBorderThickness(void) { return mBorderThickness; }
  179. S32 scrollBarThickness() const { return(mScrollBarThickness); }
  180. S32 scrollBarArrowBtnLength() const { return(mScrollBarArrowBtnLength); }
  181. bool hasHScrollBar() const { return(mHasHScrollBar); }
  182. bool hasVScrollBar() const { return(mHasVScrollBar); }
  183. bool enabledHScrollBar() const { return(mHBarEnabled); }
  184. bool enabledVScrollBar() const { return(mVBarEnabled); }
  185. bool isScrolledToBottom() { return mChildPos.y + mChildExt.y <= mContentPos.y + mContentExt.y; }
  186. bool wantsTabListMembership();
  187. bool becomeFirstResponder();
  188. bool loseFirstResponder();
  189. Region getCurHitRegion(void) { return mHitRegion; }
  190. // GuiControl
  191. virtual bool onKeyDown(const GuiEvent &event);
  192. virtual void onMouseDown(const GuiEvent &event);
  193. virtual bool onMouseDownEditor( const GuiEvent& event, Point2I offset );
  194. virtual void onMouseUp(const GuiEvent &event);
  195. virtual void onMouseDragged(const GuiEvent &event);
  196. virtual bool onMouseWheelUp(const GuiEvent &event);
  197. virtual bool onMouseWheelDown(const GuiEvent &event);
  198. virtual bool onWake();
  199. virtual void onSleep();
  200. virtual void onPreRender();
  201. virtual void onRender(Point2I offset, const RectI &updateRect);
  202. virtual void drawBorder(const Point2I &offset, bool isFirstResponder);
  203. virtual void drawVScrollBar(const Point2I &offset);
  204. virtual void drawHScrollBar(const Point2I &offset);
  205. virtual void drawScrollCorner(const Point2I &offset);
  206. virtual GuiControl* findHitControl(const Point2I &pt, S32 initialLayer = -1);
  207. static void initPersistFields();
  208. DECLARE_CONOBJECT(GuiScrollCtrl);
  209. DECLARE_DESCRIPTION( "A container that allows to view a larger GUI control inside its smaller area "
  210. "by providing horizontal and/or vertical scroll bars." );
  211. };
  212. typedef GuiScrollCtrl::ScrollBarBehavior GuiScrollBarBehavior;
  213. DefineEnumType( GuiScrollBarBehavior );
  214. #endif //_GUI_SCROLL_CTRL_H