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.

284 lines
9.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef ATTRIBUTESLIDER_H
  7. #define ATTRIBUTESLIDER_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "vgui_controls/EditablePanel.h"
  12. #include "dme_controls/AnimSetAttributeValue.h"
  13. #include "materialsystem/MaterialSystemUtil.h"
  14. #include "datamodel/dmehandle.h"
  15. using namespace vgui;
  16. //-----------------------------------------------------------------------------
  17. // Forward declarations
  18. //-----------------------------------------------------------------------------
  19. class CBaseAnimSetAttributeSliderPanel;
  20. class CDmElement;
  21. class CAttributeSliderTextEntry;
  22. class CSubRectImage;
  23. //-----------------------------------------------------------------------------
  24. // CAttributeSlider
  25. //-----------------------------------------------------------------------------
  26. // THIS CODE IS KIND OF A MESS WRT THE VARIOUS STATES WE CAN BE IN:
  27. // we can be driven by the preset pane or by dragging on any individual control
  28. // we can also be driven by ctrl hovering over the preset pane or an individual control
  29. // if we move from control to control in the preset or here, we need to be able to decay into/out of the various individual sliders
  30. class CAttributeSlider : public EditablePanel
  31. {
  32. DECLARE_CLASS_SIMPLE( CAttributeSlider, EditablePanel );
  33. // Overridden methods of EditablePanel
  34. public:
  35. virtual void Paint();
  36. virtual void PaintBackground();
  37. virtual void ApplySchemeSettings( IScheme *scheme );
  38. virtual void PerformLayout();
  39. virtual void OnCursorMoved(int x, int y);
  40. virtual void OnMousePressed(MouseCode code);
  41. virtual void OnMouseDoublePressed(MouseCode code);
  42. virtual void OnMouseReleased(MouseCode code);
  43. virtual void OnCursorEntered();
  44. virtual void OnCursorExited();
  45. virtual void OnKeyCodeTyped( KeyCode code );
  46. // Other public methods
  47. public:
  48. CAttributeSlider( CBaseAnimSetAttributeSliderPanel *parent, const char *panelName, CDmElement *control );
  49. virtual ~CAttributeSlider();
  50. // Returns the control we're modifying
  51. CDmElement *GetControl();
  52. // Activates/deactivates a slider control
  53. // NOTE: Slider control 'value' defaults to active, 'balance' and 'multilevel' defaults to inactive
  54. void ActivateControl( AnimationControlType_t type, bool bActive );
  55. bool IsControlActive( AnimationControlType_t type );
  56. // Gets/sets the slider value.
  57. // NOTE: This may not match the value pushed into the control because of fading
  58. void SetValue( AnimationControlType_t type, float flValue );
  59. float GetValue( AnimationControlType_t type ) const;
  60. void SetValue( const AttributeValue_t& value );
  61. const AttributeValue_t& GetValue() const;
  62. // Is this slider manipulating a transform control?
  63. // [NOTE: This is a utility method; the control contains these states]
  64. bool IsTransform() const;
  65. // Returns the default value for a control
  66. // [NOTE: These is a utility method; the control contains these states]
  67. float GetControlDefaultValue( AnimationControlType_t type ) const;
  68. // Are we dragging? If so, what control is being dragged?
  69. bool IsDragging() const;
  70. AnimationControlType_t GetDragControl() const;
  71. // Are we in text entry mode? If so, what control is having text entered?
  72. bool IsInTextEntry() const;
  73. AnimationControlType_t GetTextEntryControl() const;
  74. // Estimates the value of the control given a local coordinate
  75. float EstimateValueAtPos( int nLocalX, int nLocalY ) const;
  76. void SetPreview( const AttributeValue_t &value, const AttributeValue_t &full, bool instantaneous, bool startfromcurrent );
  77. float GetPreview( AnimationControlType_t type ) const;
  78. const AttributeValue_t &GetPreview() const;
  79. void EnablePreview( bool state, bool simple, bool faderdrag );
  80. bool IsPreviewEnabled() const;
  81. bool IsSimplePreview() const;
  82. void UpdateTime( float dt );
  83. void UpdateFaderAmount( float amount );
  84. bool IsRampingTowardPreview() const;
  85. void RampDown();
  86. bool IsFaderBeingDragged();
  87. void SetIsLogPreviewControl( bool state );
  88. void SetSelected( bool state );
  89. bool IsSelected() const;
  90. private:
  91. // Various slider modes
  92. enum SliderMode_t
  93. {
  94. SLIDER_MODE_FIRST_DRAG_MODE = 0x0,
  95. SLIDER_MODE_FIRST_TEXT_MODE = 0x4,
  96. SLIDER_MODE_LAST_DRAG_MODE = SLIDER_MODE_FIRST_DRAG_MODE + ANIM_CONTROL_COUNT - 1,
  97. SLIDER_MODE_LAST_TEXT_MODE = SLIDER_MODE_FIRST_TEXT_MODE + ANIM_CONTROL_COUNT - 1,
  98. SLIDER_MODE_NONE = -1,
  99. SLIDER_MODE_DRAG_VALUE = SLIDER_MODE_FIRST_DRAG_MODE + ANIM_CONTROL_VALUE,
  100. SLIDER_MODE_DRAG_BALANCE = SLIDER_MODE_FIRST_DRAG_MODE + ANIM_CONTROL_BALANCE,
  101. SLIDER_MODE_DRAG_MULTILEVEL = SLIDER_MODE_FIRST_DRAG_MODE + ANIM_CONTROL_MULTILEVEL,
  102. SLIDER_MODE_TEXT_VALUE = SLIDER_MODE_FIRST_TEXT_MODE + ANIM_CONTROL_VALUE,
  103. SLIDER_MODE_TEXT_BALANCE = SLIDER_MODE_FIRST_TEXT_MODE + ANIM_CONTROL_BALANCE,
  104. SLIDER_MODE_TEXT_MULTILEVEL = SLIDER_MODE_FIRST_TEXT_MODE + ANIM_CONTROL_MULTILEVEL,
  105. };
  106. struct Preview_t
  107. {
  108. AttributeValue_t m_Current;
  109. AttributeValue_t m_Full;
  110. };
  111. private:
  112. // Returns the location of a particular control
  113. void GetControlRect( Rect_t *pRect, AnimationControlType_t type ) const;
  114. // Given a mouse position in (x,y) in local coordinates, which animation control is it over?
  115. AnimationControlType_t DetermineControl( int x, int y );
  116. // Draws a tick on a circular control
  117. void DrawCircularTick( const Color& clr, float flValue, int nCenterX, int nCenterY, float flRadius );
  118. // Draws a preview of a circular control
  119. void DrawCircularPreview( AnimationControlType_t type, bool bMainTick, float flRadius );
  120. // Paints the a circular control
  121. void PaintCircularControl( float flValue, const Rect_t& rect );
  122. // Called by the text entry code to enter the value into the logs
  123. void StampValueIntoLogs( AnimationControlType_t type, float flValue );
  124. // Methods related to rendering
  125. void DrawMidpoint( int x, int ty, int ttall );
  126. void DrawPreviewTick( bool mainTick );
  127. void DrawTick( const Color& clr, float frac, int width, int inset );
  128. void DrawNameLabel();
  129. void DrawValueLabel( float flValue );
  130. float GetPreviewAlphaScale() const;
  131. // Methods related to text entry mode
  132. void EnterTextEntryMode( AnimationControlType_t type, bool bRelatchValues );
  133. void AcceptTextEntryValue();
  134. void DiscardTextEntryValue();
  135. private:
  136. CBaseAnimSetAttributeSliderPanel *m_pParent;
  137. TextImage *m_pName;
  138. TextImage *m_pValues[ 3 ];
  139. CSubRectImage *m_pCircleImage; // The background for the balance + multilevel controls
  140. // This is the control we're modifying
  141. CDmeHandle< CDmElement > m_hControl;
  142. // White material used for drawing non-textured things
  143. CMaterialReference m_pWhite;
  144. // The current mode of the slider
  145. SliderMode_t m_SliderMode;
  146. // The slider value; it may not match the control attribute value due to blending
  147. AttributeValue_t m_Control;
  148. // Is the slider control active?
  149. bool m_bIsControlActive[ANIM_CONTROL_COUNT];
  150. // Info used when in text entry mode
  151. AttributeValue_t m_InitialTextEntryValue;
  152. CAttributeSliderTextEntry *m_pTextField; // if this is a stereo control, then this will be the left text field
  153. CAttributeSliderTextEntry *m_pRightTextField;
  154. Preview_t m_Next;
  155. Preview_t m_Previous;
  156. Preview_t m_Preview;
  157. float m_flPreviewGoalTime;
  158. float m_flFaderAmount;
  159. // Fields used to help with drag
  160. int m_nDragStartPosition[2]; // Where was the mouse clicked?
  161. int m_nAccum[2]; // What's the total mouse movement during the drag?
  162. float m_flDragStartValue; // What was the value of the slider before the drag started?
  163. float m_flDragStartBalance; // What was the balance of the slider before the drag started?
  164. bool m_bCursorInsidePanel : 1; // Used to
  165. bool m_bRampUp : 1;
  166. bool m_bPreviewEnabled : 1;
  167. bool m_bSimplePreviewOnly : 1;
  168. bool m_bFaderBeingDragged : 1;
  169. bool m_bIsLogPreviewControl : 1;
  170. bool m_bTransform : 1;
  171. bool m_bSelected : 1;
  172. friend class CAttributeSliderTextEntry;
  173. };
  174. //-----------------------------------------------------------------------------
  175. // Inline methods
  176. //-----------------------------------------------------------------------------
  177. //-----------------------------------------------------------------------------
  178. // Returns the control
  179. //-----------------------------------------------------------------------------
  180. inline CDmElement *CAttributeSlider::GetControl()
  181. {
  182. return m_hControl;
  183. }
  184. //-----------------------------------------------------------------------------
  185. // Returns information about the control
  186. //-----------------------------------------------------------------------------
  187. inline bool CAttributeSlider::IsTransform() const
  188. {
  189. // NOTE: We may well not wish to cache this off in the constructor.
  190. // It's done purely for efficiency reasons.
  191. // Uncomment the line to make it read from the control.
  192. // return m_hControl->GetValue< bool >( "transform" )
  193. return m_bTransform;
  194. }
  195. //-----------------------------------------------------------------------------
  196. // Are we dragging?
  197. //-----------------------------------------------------------------------------
  198. inline bool CAttributeSlider::IsDragging() const
  199. {
  200. COMPILE_TIME_ASSERT( ANIM_CONTROL_COUNT < 4 );
  201. return ( m_SliderMode >= SLIDER_MODE_FIRST_DRAG_MODE && m_SliderMode <= SLIDER_MODE_LAST_DRAG_MODE );
  202. }
  203. inline AnimationControlType_t CAttributeSlider::GetDragControl() const
  204. {
  205. if ( IsDragging() )
  206. return (AnimationControlType_t)( m_SliderMode - SLIDER_MODE_FIRST_DRAG_MODE );
  207. return ANIM_CONTROL_INVALID;
  208. }
  209. //-----------------------------------------------------------------------------
  210. // Are we in text entry mode?
  211. //-----------------------------------------------------------------------------
  212. inline bool CAttributeSlider::IsInTextEntry() const
  213. {
  214. COMPILE_TIME_ASSERT( ANIM_CONTROL_COUNT < 4 );
  215. return ( m_SliderMode >= SLIDER_MODE_FIRST_TEXT_MODE && m_SliderMode <= SLIDER_MODE_LAST_TEXT_MODE );
  216. }
  217. inline AnimationControlType_t CAttributeSlider::GetTextEntryControl() const
  218. {
  219. if ( IsInTextEntry() )
  220. return (AnimationControlType_t)( m_SliderMode - SLIDER_MODE_FIRST_TEXT_MODE );
  221. return ANIM_CONTROL_INVALID;
  222. }
  223. #endif // ATTRIBUTESLIDER_H