Counter Strike : Global Offensive Source Code
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.

235 lines
7.2 KiB

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