summaryrefslogtreecommitdiff
path: root/public/panorama/controls/carousel.h
blob: 58bbca6cfe4c0ea57dbf92fa42fab0d6b5b33fa0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//=========== Copyright Valve Corporation, All rights reserved. ===============//
//
// Purpose: 
//=============================================================================//

#ifndef CAROUSEL_H
#define CAROUSEL_H

#ifdef _WIN32
#pragma once
#endif

#include "mathlib/mathlib.h"
#include "mathlib/beziercurve.h"
#include "panel2d.h"
#include "panorama/controls/label.h"
#include "panorama/controls/mousescroll.h"

namespace panorama
{

DECLARE_PANORAMA_EVENT0( ResetCarouselMouseWheelCounts );
DECLARE_PANORAMA_EVENT1( SetCarouselSelectedChild, CPanelPtr<CPanel2D> );


//-----------------------------------------------------------------------------
// Purpose: Button
//-----------------------------------------------------------------------------
class CCarousel : public CPanel2D
{
	DECLARE_PANEL2D( CCarousel, CPanel2D );

public:
	CCarousel( CPanel2D *parent, const char * pchPanelID );
	virtual ~CCarousel();

	enum EFocusType
	{
		k_EFocusTypeLeft,
		k_EFocusTypeEdge,
		k_EFocusTypeCenter
	};

	void SetTitleText( const char *pchTitle );
	void SetTitleVisible( bool bVisible );
	void SetWrap( bool bWrap );
	void SetFocusType( EFocusType eType );
	void SetOffset( CUILength len );
	void DrawFocusFrame( bool bDraw );
	void DeleteChildren();
	
	bool SetFocusToIndex( int iFocus );
	int GetFocusIndex() const { return GetChildIndex( m_pFocusedChild.Get() ); }
	CPanel2D *GetFocusChild() const { return m_pFocusedChild.Get(); }

	// Sets the child that will get focus when the carousel has focus. Remembered between focus calls
	void SetSelectedChild( CPanel2D *pPanel );

	// Sets the panel for which focus state is checked when applying focus offset.
	void SetFocusOffsetPanel( CPanel2D *pPanel ) { m_ptrPanelFocusOffset = pPanel; }

	virtual bool BSetProperty( CPanoramaSymbol symName, const char *pchValue ) OVERRIDE;
	virtual void GetDebugPropertyInfo( CUtlVector< DebugPropertyOutput_t *> *pvecProperties );
	virtual void OnLayoutTraverse( float flFinalWidth, float flFinalHeight );
	virtual void Paint();

	virtual bool OnMoveRight( int nRepeats );
	virtual bool OnMoveLeft( int nRepeats );
	virtual bool OnTabForward( int nRepeats ) { return OnMoveRight( nRepeats ); }
	virtual bool OnTabBackward( int nRepeats ) { return OnMoveLeft( nRepeats ); }
	virtual bool OnMouseWheel( const panorama::MouseData_t &code );
	virtual void OnStylesChanged();

	virtual void OnUIScaleFactorChanged( float flScaleFactor ) OVERRIDE;

	virtual bool BRequiresContentClipLayer() OVERRIDE { return true; }

	virtual void OnInitializedFromLayout();

	virtual void SetupJavascriptObjectTemplate() OVERRIDE;

#ifdef DBGFLAG_VALIDATE
	virtual void ValidateClientPanel( CValidator &validator, const tchar *pchName );
#endif

protected:

	virtual bool OnSetFocusToNextPanel( int nRepeats, EFocusMoveDirection moveType, bool bAllowWrap, float flTabIndexCurrent, float flXPosCurrent, float flYPosCurrent, float flXStart, float fYStart ) OVERRIDE
	{
		if ( m_bWrap )
		{
			switch( moveType )
			{
			case k_ENextInTabOrder:
			case k_ENextByXPosition:
				return OnMoveRight( nRepeats );
			case k_EPrevInTabOrder:
			case k_EPrevByXPosition:
				return OnMoveLeft( nRepeats );
			default:
				break;
			}
		}
		else
		{
			int iFocusChild = GetChildIndex( m_pFocusedChild.Get() );
			switch( moveType )
			{
			case k_ENextInTabOrder:
			case k_ENextByXPosition:
				if ( iFocusChild < GetChildCount() - 1 )
				{
					return OnMoveRight( nRepeats );
				}
				break;
			case k_EPrevInTabOrder:
			case k_EPrevByXPosition:
				if ( iFocusChild > 0 )
				{
					return OnMoveLeft( nRepeats );
				}
				break;
			default:
				break;
			}
		}

		return false;
	}

	// child management
	virtual void OnBeforeChildrenChanged();
	virtual void OnCallBeforeStyleAndLayout() { UpdateFocusAndDirtyChildStyles(); }

private:
	enum EFocusEdge
	{
		k_EFocusEdgeLeft,
		k_EFocusEdgeRight
	};

	// event handlers
	bool EventInputFocusSet( const CPanelPtr< IUIPanel > &ptrPanel );
	bool EventInputFocusLost( const CPanelPtr< IUIPanel > &ptrPanel );
	bool OnResetMouseWheelCounts();
	bool EventCarouselMouseScroll( const CPanelPtr< IUIPanel > &ptrPanel, int cRepeat );
	bool EventWindowCursorShown( IUIWindow *pWindow );
	bool EventWindowCursorHidden( IUIWindow *pWindow );

	// owned panels
	CLabel *CreateTitleLabel();

	// focus
	bool BSetFocusToChild( CPanel2D *pPanel );
	void MarkFocusDirty();
	bool UpdateFocusAndDirtyChildStyles();

	// helpers
	int GetPreviousWrapPanel( int i );
	int GetNextWrapPanel( int i );
	float GetFinalChildWidth( CPanel2D *pChild, float flContainerHeight );
	void GetFinalChildDimensions( float *pflWidth, float *pflHeight, CPanel2D *pChild, float flContainerHeight );		
	int CalcIndexDistanceBetweenPanels( int iLHS, int iRHS );
	int GetNextPanelInLayout( int iStart );
	int GetPreviousPanelInLayout( int iStart );
	void AddCarouselStyle( CPanel2D *pChild, int iChild, int iCurrentFocus );
	void RemoveCarouselStyle( CPanel2D *pChild, int iChild, int iCurrentFocus );
	void RegisterForCursorChanges();
	void UnregisterForCursorChanges();

	// configured offets
	void GetPanelOffsets( CUILength *plenX, CUILength *plenY, CUILength *plenZ, int nDistanceFromFocus, float flWidth, float flHeight );
	CUILength GetPanelOffset( int nDistanceFromFocus, bool bUseFocus, const CUtlVector< CUILength > &vecOffsets, const CUtlVector< CUILength > &vecFocusOffsets );

	// layout related
	void GetLayoutStart( int iFocusChild, float *pflOffset, float flLeft, float flCarouselOffset, const float flContainerWidth, const float flContainerHeight );
	void LayoutChildPanels( int iFocusChild, float flOffset, float flLeft, float flRight, const float flContainerWidth, const float flContainerHeight, const CUtlVector< CPanel2D* > &vecNewChildren );
	bool BPositionPanelRight( int iPanel, int nDistanceFromFocus, float *pflOffset, float flLeft, float flContainerWidth, float flContainerHeight, bool bCheckFits, const CUtlVector< CPanel2D* > &vecNewChildren );
	bool BPositionPanelLeft( int iPanel, int nDistanceFromFocus, float *pflOffset, float flLeft, float flContainerWidth, float flContainerHeight, bool bCheckFits, const CUtlVector< CPanel2D* > &vecNewChildren );
	void LayoutMouseScrollRegions( float flFinalWidth, float flFinalHeight );
	
	struct DirtyChildStyles_t
	{
		int m_iOriginalFocus;
		CUtlVector< CPanel2D* > m_vecPanels;
	};
	DirtyChildStyles_t *m_pDirtyChildStyles;


	CLabel *m_pTitleLabel;
	CMouseScrollRegion *m_pLeftMouseScrollRegion;
	CMouseScrollRegion *m_pRightMouseScrollRegion;
	CPanelPtr< CPanel2D > m_pFocusedChild;

	EFocusType m_eFocusType;
	bool m_bWrap;
	CUILength m_lenOffset;
	bool m_bIncludeScale2d;

	// for edge focus
	EFocusEdge m_eLastFocusEdge;
	int m_iFocusLastEdge;

	struct ChildOffsets_t
	{
		CUtlVector< CUILength > x;
		CUtlVector< CUILength > y;
		CUtlVector< CUILength > z;
	};
	ChildOffsets_t m_childOffsets;
	ChildOffsets_t m_childOffsetsFocus;	
	bool m_bFlowingLayout;
	bool m_bHadFocus;

	double m_flLastMouseWheel;
	uint32 m_unMouseWheelCount;

	double m_flLastMove;
	bool m_bDelayedMovePosted;
	bool m_bRegisteredForCursorChanges;
	
	bool m_bShuffleIntoView;

	int32 m_nPanelsVisible;

	CPanelPtr< CPanel2D > m_ptrPanelFocusOffset;
};


} // namespace panorama

#endif // CAROUSEL_H