Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

232 lines
7.1 KiB

  1. //=========== Copyright Valve Corporation, All rights reserved. ===============//
  2. //
  3. // Purpose:
  4. //=============================================================================//
  5. #ifndef CAROUSEL_H
  6. #define CAROUSEL_H
  7. #ifdef _WIN32
  8. #pragma once
  9. #endif
  10. #include "mathlib/mathlib.h"
  11. #include "mathlib/beziercurve.h"
  12. #include "panel2d.h"
  13. #include "panorama/controls/label.h"
  14. #include "panorama/controls/mousescroll.h"
  15. namespace panorama
  16. {
  17. DECLARE_PANORAMA_EVENT0( ResetCarouselMouseWheelCounts );
  18. DECLARE_PANORAMA_EVENT1( SetCarouselSelectedChild, CPanelPtr<CPanel2D> );
  19. //-----------------------------------------------------------------------------
  20. // Purpose: Button
  21. //-----------------------------------------------------------------------------
  22. class CCarousel : public CPanel2D
  23. {
  24. DECLARE_PANEL2D( CCarousel, CPanel2D );
  25. public:
  26. CCarousel( CPanel2D *parent, const char * pchPanelID );
  27. virtual ~CCarousel();
  28. enum EFocusType
  29. {
  30. k_EFocusTypeLeft,
  31. k_EFocusTypeEdge,
  32. k_EFocusTypeCenter
  33. };
  34. void SetTitleText( const char *pchTitle );
  35. void SetTitleVisible( bool bVisible );
  36. void SetWrap( bool bWrap );
  37. void SetFocusType( EFocusType eType );
  38. void SetOffset( CUILength len );
  39. void DrawFocusFrame( bool bDraw );
  40. void DeleteChildren();
  41. bool SetFocusToIndex( int iFocus );
  42. int GetFocusIndex() const { return GetChildIndex( m_pFocusedChild.Get() ); }
  43. CPanel2D *GetFocusChild() const { return m_pFocusedChild.Get(); }
  44. // Sets the child that will get focus when the carousel has focus. Remembered between focus calls
  45. void SetSelectedChild( CPanel2D *pPanel );
  46. // Sets the panel for which focus state is checked when applying focus offset.
  47. void SetFocusOffsetPanel( CPanel2D *pPanel ) { m_ptrPanelFocusOffset = pPanel; }
  48. virtual bool BSetProperty( CPanoramaSymbol symName, const char *pchValue ) OVERRIDE;
  49. virtual void GetDebugPropertyInfo( CUtlVector< DebugPropertyOutput_t *> *pvecProperties );
  50. virtual void OnLayoutTraverse( float flFinalWidth, float flFinalHeight );
  51. virtual void Paint();
  52. virtual bool OnMoveRight( int nRepeats );
  53. virtual bool OnMoveLeft( int nRepeats );
  54. virtual bool OnTabForward( int nRepeats ) { return OnMoveRight( nRepeats ); }
  55. virtual bool OnTabBackward( int nRepeats ) { return OnMoveLeft( nRepeats ); }
  56. virtual bool OnMouseWheel( const panorama::MouseData_t &code );
  57. virtual void OnStylesChanged();
  58. virtual void OnUIScaleFactorChanged( float flScaleFactor ) OVERRIDE;
  59. virtual bool BRequiresContentClipLayer() OVERRIDE { return true; }
  60. virtual void OnInitializedFromLayout();
  61. virtual void SetupJavascriptObjectTemplate() OVERRIDE;
  62. #ifdef DBGFLAG_VALIDATE
  63. virtual void ValidateClientPanel( CValidator &validator, const tchar *pchName );
  64. #endif
  65. protected:
  66. virtual bool OnSetFocusToNextPanel( int nRepeats, EFocusMoveDirection moveType, bool bAllowWrap, float flTabIndexCurrent, float flXPosCurrent, float flYPosCurrent, float flXStart, float fYStart ) OVERRIDE
  67. {
  68. if ( m_bWrap )
  69. {
  70. switch( moveType )
  71. {
  72. case k_ENextInTabOrder:
  73. case k_ENextByXPosition:
  74. return OnMoveRight( nRepeats );
  75. case k_EPrevInTabOrder:
  76. case k_EPrevByXPosition:
  77. return OnMoveLeft( nRepeats );
  78. default:
  79. break;
  80. }
  81. }
  82. else
  83. {
  84. int iFocusChild = GetChildIndex( m_pFocusedChild.Get() );
  85. switch( moveType )
  86. {
  87. case k_ENextInTabOrder:
  88. case k_ENextByXPosition:
  89. if ( iFocusChild < GetChildCount() - 1 )
  90. {
  91. return OnMoveRight( nRepeats );
  92. }
  93. break;
  94. case k_EPrevInTabOrder:
  95. case k_EPrevByXPosition:
  96. if ( iFocusChild > 0 )
  97. {
  98. return OnMoveLeft( nRepeats );
  99. }
  100. break;
  101. default:
  102. break;
  103. }
  104. }
  105. return false;
  106. }
  107. // child management
  108. virtual void OnBeforeChildrenChanged();
  109. virtual void OnCallBeforeStyleAndLayout() { UpdateFocusAndDirtyChildStyles(); }
  110. private:
  111. enum EFocusEdge
  112. {
  113. k_EFocusEdgeLeft,
  114. k_EFocusEdgeRight
  115. };
  116. // event handlers
  117. bool EventInputFocusSet( const CPanelPtr< IUIPanel > &ptrPanel );
  118. bool EventInputFocusLost( const CPanelPtr< IUIPanel > &ptrPanel );
  119. bool OnResetMouseWheelCounts();
  120. bool EventCarouselMouseScroll( const CPanelPtr< IUIPanel > &ptrPanel, int cRepeat );
  121. bool EventWindowCursorShown( IUIWindow *pWindow );
  122. bool EventWindowCursorHidden( IUIWindow *pWindow );
  123. // owned panels
  124. CLabel *CreateTitleLabel();
  125. // focus
  126. bool BSetFocusToChild( CPanel2D *pPanel );
  127. void MarkFocusDirty();
  128. bool UpdateFocusAndDirtyChildStyles();
  129. // helpers
  130. int GetPreviousWrapPanel( int i );
  131. int GetNextWrapPanel( int i );
  132. float GetFinalChildWidth( CPanel2D *pChild, float flContainerHeight );
  133. void GetFinalChildDimensions( float *pflWidth, float *pflHeight, CPanel2D *pChild, float flContainerHeight );
  134. int CalcIndexDistanceBetweenPanels( int iLHS, int iRHS );
  135. int GetNextPanelInLayout( int iStart );
  136. int GetPreviousPanelInLayout( int iStart );
  137. void AddCarouselStyle( CPanel2D *pChild, int iChild, int iCurrentFocus );
  138. void RemoveCarouselStyle( CPanel2D *pChild, int iChild, int iCurrentFocus );
  139. void RegisterForCursorChanges();
  140. void UnregisterForCursorChanges();
  141. // configured offets
  142. void GetPanelOffsets( CUILength *plenX, CUILength *plenY, CUILength *plenZ, int nDistanceFromFocus, float flWidth, float flHeight );
  143. CUILength GetPanelOffset( int nDistanceFromFocus, bool bUseFocus, const CUtlVector< CUILength > &vecOffsets, const CUtlVector< CUILength > &vecFocusOffsets );
  144. // layout related
  145. void GetLayoutStart( int iFocusChild, float *pflOffset, float flLeft, float flCarouselOffset, const float flContainerWidth, const float flContainerHeight );
  146. void LayoutChildPanels( int iFocusChild, float flOffset, float flLeft, float flRight, const float flContainerWidth, const float flContainerHeight, const CUtlVector< CPanel2D* > &vecNewChildren );
  147. bool BPositionPanelRight( int iPanel, int nDistanceFromFocus, float *pflOffset, float flLeft, float flContainerWidth, float flContainerHeight, bool bCheckFits, const CUtlVector< CPanel2D* > &vecNewChildren );
  148. bool BPositionPanelLeft( int iPanel, int nDistanceFromFocus, float *pflOffset, float flLeft, float flContainerWidth, float flContainerHeight, bool bCheckFits, const CUtlVector< CPanel2D* > &vecNewChildren );
  149. void LayoutMouseScrollRegions( float flFinalWidth, float flFinalHeight );
  150. struct DirtyChildStyles_t
  151. {
  152. int m_iOriginalFocus;
  153. CUtlVector< CPanel2D* > m_vecPanels;
  154. };
  155. DirtyChildStyles_t *m_pDirtyChildStyles;
  156. CLabel *m_pTitleLabel;
  157. CMouseScrollRegion *m_pLeftMouseScrollRegion;
  158. CMouseScrollRegion *m_pRightMouseScrollRegion;
  159. CPanelPtr< CPanel2D > m_pFocusedChild;
  160. EFocusType m_eFocusType;
  161. bool m_bWrap;
  162. CUILength m_lenOffset;
  163. bool m_bIncludeScale2d;
  164. // for edge focus
  165. EFocusEdge m_eLastFocusEdge;
  166. int m_iFocusLastEdge;
  167. struct ChildOffsets_t
  168. {
  169. CUtlVector< CUILength > x;
  170. CUtlVector< CUILength > y;
  171. CUtlVector< CUILength > z;
  172. };
  173. ChildOffsets_t m_childOffsets;
  174. ChildOffsets_t m_childOffsetsFocus;
  175. bool m_bFlowingLayout;
  176. bool m_bHadFocus;
  177. double m_flLastMouseWheel;
  178. uint32 m_unMouseWheelCount;
  179. double m_flLastMove;
  180. bool m_bDelayedMovePosted;
  181. bool m_bRegisteredForCursorChanges;
  182. bool m_bShuffleIntoView;
  183. int32 m_nPanelsVisible;
  184. CPanelPtr< CPanel2D > m_ptrPanelFocusOffset;
  185. };
  186. } // namespace panorama
  187. #endif // CAROUSEL_H