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.

191 lines
5.2 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef CVARTOGGLECHECKBUTTON_H
  8. #define CVARTOGGLECHECKBUTTON_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "vgui/vgui.h"
  13. #include "vgui_controls/CheckButton.h"
  14. #include "tier1/utlstring.h"
  15. #include "tier1/keyvalues.h"
  16. namespace vgui
  17. {
  18. template< class T >
  19. class CvarToggleCheckButton : public CheckButton
  20. {
  21. DECLARE_CLASS_SIMPLE( CvarToggleCheckButton, CheckButton );
  22. public:
  23. CvarToggleCheckButton( Panel *parent, const char *panelName, const char *text = "",
  24. char const *cvarname = NULL, bool ignoreMissingCvar = false );
  25. ~CvarToggleCheckButton();
  26. virtual void SetSelected( bool state );
  27. virtual void Paint();
  28. void Reset();
  29. void ApplyChanges();
  30. bool HasBeenModified();
  31. virtual void ApplySettings( KeyValues *inResourceData );
  32. private:
  33. // Called when the OK / Apply button is pressed. Changed data should be written into cvar.
  34. MESSAGE_FUNC( OnApplyChanges, "ApplyChanges" );
  35. MESSAGE_FUNC( OnButtonChecked, "CheckButtonChecked" );
  36. T m_cvar;
  37. bool m_bStartValue;
  38. bool m_bIgnoreMissingCvar;
  39. };
  40. //-----------------------------------------------------------------------------
  41. // Purpose: Constructor
  42. //-----------------------------------------------------------------------------
  43. template< class T >
  44. CvarToggleCheckButton<T>::CvarToggleCheckButton( Panel *parent, const char *panelName, const char *text, char const *cvarname, bool ignoreMissingCvar )
  45. : CheckButton( parent, panelName, text ), m_cvar( (cvarname)?cvarname:"", (cvarname)?ignoreMissingCvar:true )
  46. {
  47. m_bIgnoreMissingCvar = ignoreMissingCvar;
  48. if (m_cvar.IsValid())
  49. {
  50. Reset();
  51. }
  52. AddActionSignalTarget( this );
  53. }
  54. //-----------------------------------------------------------------------------
  55. // Purpose: Destructor
  56. //-----------------------------------------------------------------------------
  57. template< class T >
  58. CvarToggleCheckButton<T>::~CvarToggleCheckButton()
  59. {
  60. }
  61. //-----------------------------------------------------------------------------
  62. // Purpose:
  63. //-----------------------------------------------------------------------------
  64. template< class T >
  65. void CvarToggleCheckButton<T>::Paint()
  66. {
  67. if ( !m_cvar.IsValid() )
  68. {
  69. BaseClass::Paint();
  70. return;
  71. }
  72. bool value = m_cvar.GetBool();
  73. if ( value != m_bStartValue )
  74. {
  75. SetSelected( value );
  76. m_bStartValue = value;
  77. }
  78. BaseClass::Paint();
  79. }
  80. //-----------------------------------------------------------------------------
  81. // Purpose: Called when the OK / Apply button is pressed. Changed data should be written into cvar.
  82. //-----------------------------------------------------------------------------
  83. template< class T >
  84. void CvarToggleCheckButton<T>::OnApplyChanges()
  85. {
  86. ApplyChanges();
  87. }
  88. //-----------------------------------------------------------------------------
  89. // Purpose:
  90. //-----------------------------------------------------------------------------
  91. template< class T >
  92. void CvarToggleCheckButton<T>::ApplyChanges()
  93. {
  94. if ( !m_cvar.IsValid() )
  95. return;
  96. m_bStartValue = IsSelected();
  97. m_cvar.SetValue( m_bStartValue );
  98. }
  99. //-----------------------------------------------------------------------------
  100. // Purpose:
  101. //-----------------------------------------------------------------------------
  102. template< class T >
  103. void CvarToggleCheckButton<T>::Reset()
  104. {
  105. if ( !m_cvar.IsValid() )
  106. return;
  107. m_bStartValue = m_cvar.GetBool();
  108. SetSelected(m_bStartValue);
  109. }
  110. //-----------------------------------------------------------------------------
  111. // Purpose:
  112. //-----------------------------------------------------------------------------
  113. template< class T >
  114. bool CvarToggleCheckButton<T>::HasBeenModified()
  115. {
  116. return IsSelected() != m_bStartValue;
  117. }
  118. //-----------------------------------------------------------------------------
  119. // Purpose:
  120. // Input : *panel -
  121. //-----------------------------------------------------------------------------
  122. template< class T >
  123. void CvarToggleCheckButton<T>::SetSelected( bool state )
  124. {
  125. BaseClass::SetSelected( state );
  126. }
  127. //-----------------------------------------------------------------------------
  128. // Purpose:
  129. //-----------------------------------------------------------------------------
  130. template< class T >
  131. void CvarToggleCheckButton<T>::OnButtonChecked()
  132. {
  133. if (HasBeenModified())
  134. {
  135. PostActionSignal(new KeyValues("ControlModified"));
  136. }
  137. }
  138. //-----------------------------------------------------------------------------
  139. // Purpose:
  140. //-----------------------------------------------------------------------------
  141. template< class T >
  142. void CvarToggleCheckButton<T>::ApplySettings( KeyValues *inResourceData )
  143. {
  144. BaseClass::ApplySettings( inResourceData );
  145. const char *cvarName = inResourceData->GetString("cvar_name", "");
  146. const char *cvarValue = inResourceData->GetString("cvar_value", "");
  147. if( Q_stricmp( cvarName, "") == 0 )
  148. return;// Doesn't have cvar set up in res file, must have been constructed with it.
  149. if( Q_stricmp( cvarValue, "1") == 0 )
  150. m_bStartValue = true;
  151. else
  152. m_bStartValue = false;
  153. m_cvar.Init( cvarName, m_bIgnoreMissingCvar );
  154. if ( m_cvar.IsValid() )
  155. {
  156. SetSelected( m_cvar.GetBool() );
  157. }
  158. }
  159. } // namespace vgui
  160. #endif // CVARTOGGLECHECKBUTTON_H