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.

239 lines
7.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #ifndef BUTTON_H
  8. #define BUTTON_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include <vgui/VGUI.h>
  13. #include <vgui/Dar.h>
  14. #include <Color.h>
  15. #include <vgui_controls/Label.h>
  16. #include "vgui/MouseCode.h"
  17. namespace vgui
  18. {
  19. //-----------------------------------------------------------------------------
  20. // Purpose:
  21. //-----------------------------------------------------------------------------
  22. class Button : public Label
  23. {
  24. DECLARE_CLASS_SIMPLE( Button, Label );
  25. public:
  26. // You can optionally pass in the panel to send the click message to and the name of the command to send to that panel.
  27. Button(Panel *parent, const char *panelName, const char *text, Panel *pActionSignalTarget=NULL, const char *pCmd=NULL);
  28. Button(Panel *parent, const char *panelName, const wchar_t *text, Panel *pActionSignalTarget=NULL, const char *pCmd=NULL);
  29. ~Button();
  30. private:
  31. void Init();
  32. public:
  33. // Set armed state.
  34. virtual void SetArmed(bool state);
  35. // Check armed state
  36. virtual bool IsArmed( void );
  37. // Check depressed state
  38. virtual bool IsDepressed();
  39. // Set button force depressed state.
  40. virtual void ForceDepressed(bool state);
  41. // Set button depressed state with respect to the force depressed state.
  42. virtual void RecalculateDepressedState( void );
  43. // Set button selected state.
  44. virtual void SetSelected(bool state);
  45. // Check selected state
  46. virtual bool IsSelected( void );
  47. virtual void SetBlink(bool state);
  48. virtual bool IsBlinking( void );
  49. //Set whether or not the button captures all mouse input when depressed.
  50. virtual void SetUseCaptureMouse( bool state );
  51. // Check if mouse capture is enabled.
  52. virtual bool IsUseCaptureMouseEnabled( void );
  53. // Activate a button click.
  54. MESSAGE_FUNC( DoClick, "PressButton" );
  55. MESSAGE_FUNC( OnHotkey, "Hotkey" )
  56. {
  57. DoClick();
  58. }
  59. // Set button to be mouse clickable or not.
  60. virtual void SetMouseClickEnabled( MouseCode code, bool state );
  61. // Check if button is mouse clickable
  62. virtual bool IsMouseClickEnabled( MouseCode code );
  63. // sets the how this button activates
  64. enum ActivationType_t
  65. {
  66. ACTIVATE_ONPRESSEDANDRELEASED, // normal button behaviour
  67. ACTIVATE_ONPRESSED, // menu buttons, toggle buttons
  68. ACTIVATE_ONRELEASED, // menu items
  69. };
  70. virtual void SetButtonActivationType(ActivationType_t activationType);
  71. // Message targets that the button has been pressed
  72. virtual void FireActionSignal( void );
  73. // Perform graphical layout of button
  74. virtual void PerformLayout();
  75. virtual bool RequestInfo(KeyValues *data);
  76. virtual bool CanBeDefaultButton(void);
  77. // Set this button to be the button that is accessed by default when the user hits ENTER or SPACE
  78. MESSAGE_FUNC_INT( SetAsDefaultButton, "SetAsDefaultButton", state );
  79. // Set this button to be the button that is currently accessed by default when the user hits ENTER or SPACE
  80. MESSAGE_FUNC_INT( SetAsCurrentDefaultButton, "SetAsCurrentDefaultButton", state );
  81. // Respond when key focus is received
  82. virtual void OnSetFocus();
  83. // Respond when focus is killed
  84. virtual void OnKillFocus();
  85. // Set button border attribute enabled, controls display of button.
  86. virtual void SetButtonBorderEnabled( bool state );
  87. // Set default button colors.
  88. virtual void SetDefaultColor(Color fgColor, Color bgColor);
  89. // Set armed button colors
  90. virtual void SetArmedColor(Color fgColor, Color bgColor);
  91. // Set selected button colors
  92. virtual void SetSelectedColor(Color fgColor, Color bgColor);
  93. // Set depressed button colors
  94. virtual void SetDepressedColor(Color fgColor, Color bgColor);
  95. // Set blink button color
  96. virtual void SetBlinkColor(Color fgColor);
  97. // Get button foreground color
  98. virtual Color GetButtonFgColor();
  99. // Get button background color
  100. virtual Color GetButtonBgColor();
  101. Color GetButtonDefaultFgColor() { return _defaultFgColor; }
  102. Color GetButtonDefaultBgColor() { return _defaultBgColor; }
  103. Color GetButtonArmedFgColor() { return _armedFgColor; }
  104. Color GetButtonArmedBgColor() { return _armedBgColor; }
  105. Color GetButtonSelectedFgColor() { return _selectedFgColor; }
  106. Color GetButtonSelectedBgColor() { return _selectedBgColor; }
  107. Color GetButtonDepressedFgColor() { return _depressedFgColor; }
  108. Color GetButtonDepressedBgColor() { return _depressedBgColor; }
  109. // Set default button border attributes.
  110. virtual void SetDefaultBorder(IBorder *border);
  111. // Set depressed button border attributes.
  112. virtual void SetDepressedBorder(IBorder *border);
  113. // Set key focused button border attributes.
  114. virtual void SetKeyFocusBorder(IBorder *border);
  115. // Set the command to send when the button is pressed
  116. // Set the panel to send the command to with AddActionSignalTarget()
  117. virtual void SetCommand( const char *command );
  118. // Set the message to send when the button is pressed
  119. virtual void SetCommand( KeyValues *message );
  120. // sound handling
  121. void SetArmedSound(const char *sound);
  122. void SetDepressedSound(const char *sound);
  123. void SetReleasedSound(const char *sound);
  124. /* CUSTOM MESSAGE HANDLING
  125. "PressButton" - makes the button act as if it had just been pressed by the user (just like DoClick())
  126. input: none
  127. */
  128. virtual void OnCursorEntered();
  129. virtual void OnCursorExited();
  130. virtual void SizeToContents();
  131. virtual KeyValues *GetCommand();
  132. bool IsDrawingFocusBox();
  133. void DrawFocusBox( bool bEnable );
  134. bool ShouldPaint(){ return _paint; }
  135. void SetShouldPaint( bool paint ){ _paint = paint; }
  136. virtual void ApplySettings( KeyValues *inResourceData );
  137. virtual void NavigateTo();
  138. virtual void NavigateFrom();
  139. protected:
  140. virtual void DrawFocusBorder(int tx0, int ty0, int tx1, int ty1);
  141. // Paint button on screen
  142. virtual void Paint(void);
  143. // Get button border attributes.
  144. virtual IBorder *GetBorder(bool depressed, bool armed, bool selected, bool keyfocus);
  145. virtual void ApplySchemeSettings(IScheme *pScheme);
  146. MESSAGE_FUNC_INT( OnSetState, "SetState", state );
  147. virtual void OnMousePressed(MouseCode code);
  148. virtual void OnMouseDoublePressed(MouseCode code);
  149. virtual void OnMouseReleased(MouseCode code);
  150. virtual void OnKeyCodePressed(KeyCode code);
  151. virtual void OnKeyCodeReleased(KeyCode code);
  152. // Get control settings for editing
  153. virtual void GetSettings( KeyValues *outResourceData );
  154. virtual const char *GetDescription( void );
  155. KeyValues *GetActionMessage();
  156. void PlayButtonReleasedSound();
  157. protected:
  158. enum ButtonFlags_t
  159. {
  160. ARMED = 0x0001,
  161. DEPRESSED = 0x0002,
  162. FORCE_DEPRESSED = 0x0004,
  163. BUTTON_BORDER_ENABLED = 0x0008,
  164. USE_CAPTURE_MOUSE = 0x0010,
  165. BUTTON_KEY_DOWN = 0x0020,
  166. DEFAULT_BUTTON = 0x0040,
  167. SELECTED = 0x0080,
  168. DRAW_FOCUS_BOX = 0x0100,
  169. BLINK = 0x0200,
  170. ALL_FLAGS = 0xFFFF,
  171. };
  172. CUtlFlags< unsigned short > _buttonFlags; // see ButtonFlags_t
  173. int _mouseClickMask;
  174. KeyValues *_actionMessage;
  175. ActivationType_t _activationType;
  176. IBorder *_defaultBorder;
  177. IBorder *_depressedBorder;
  178. IBorder *_keyFocusBorder;
  179. Color _defaultFgColor, _defaultBgColor;
  180. Color _armedFgColor, _armedBgColor;
  181. Color _selectedFgColor, _selectedBgColor;
  182. Color _depressedFgColor, _depressedBgColor;
  183. Color _keyboardFocusColor;
  184. Color _blinkFgColor;
  185. bool _paint;
  186. unsigned short m_sArmedSoundName, m_sDepressedSoundName, m_sReleasedSoundName;
  187. bool m_bSelectionStateSaved;
  188. bool m_bStaySelectedOnClick;
  189. bool m_bStayArmedOnClick;
  190. };
  191. } // namespace vgui
  192. #endif // BUTTON_H