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.

156 lines
3.7 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cvartogglecheckbutton.h"
  8. #include "engineinterface.h"
  9. #include <vgui/IVGui.h>
  10. #include "tier1/keyvalues.h"
  11. #include "tier1/convar.h"
  12. #include "IGameUIFuncs.h"
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include <tier0/memdbgon.h>
  15. using namespace vgui;
  16. vgui::Panel *CvarToggleCheckButton_Factory()
  17. {
  18. return new CCvarToggleCheckButton( NULL, NULL, "CvarToggleCheckButton", NULL );
  19. }
  20. DECLARE_BUILD_FACTORY_CUSTOM( CCvarToggleCheckButton, CvarToggleCheckButton_Factory );
  21. CCvarToggleCheckButton::CCvarToggleCheckButton( Panel *parent, const char *panelName, const char *text,
  22. char const *cvarname )
  23. : CheckButton( parent, panelName, text )
  24. {
  25. m_pszCvarName = cvarname ? strdup( cvarname ) : NULL;
  26. if (m_pszCvarName)
  27. {
  28. Reset();
  29. }
  30. AddActionSignalTarget( this );
  31. }
  32. CCvarToggleCheckButton::~CCvarToggleCheckButton()
  33. {
  34. if ( m_pszCvarName )
  35. {
  36. free( m_pszCvarName );
  37. }
  38. }
  39. void CCvarToggleCheckButton::Paint()
  40. {
  41. if ( !m_pszCvarName || !m_pszCvarName[ 0 ] )
  42. {
  43. BaseClass::Paint();
  44. return;
  45. }
  46. // Look up current value
  47. // bool value = engine->pfnGetCvarFloat( m_pszCvarName ) > 0.0f ? true : false;
  48. ConVarRef var( m_pszCvarName );
  49. if ( !var.IsValid() )
  50. return;
  51. bool value = var.GetBool();
  52. if ( value != m_bStartValue )
  53. //if ( value != IsSelected() )
  54. {
  55. SetSelected( value );
  56. m_bStartValue = value;
  57. }
  58. BaseClass::Paint();
  59. }
  60. void CCvarToggleCheckButton::ApplyChanges()
  61. {
  62. if ( !m_pszCvarName || !m_pszCvarName[ 0 ] )
  63. return;
  64. m_bStartValue = IsSelected();
  65. // engine->Cvar_SetValue( m_pszCvarName, m_bStartValue ? 1.0f : 0.0f );
  66. ConVarRef var( m_pszCvarName );
  67. var.SetValue( m_bStartValue );
  68. }
  69. void CCvarToggleCheckButton::Reset()
  70. {
  71. // m_bStartValue = engine->pfnGetCvarFloat( m_pszCvarName ) > 0.0f ? true : false;
  72. if ( !m_pszCvarName || !m_pszCvarName[ 0 ] )
  73. return;
  74. ConVarRef var( m_pszCvarName );
  75. if ( !var.IsValid() )
  76. return;
  77. m_bStartValue = var.GetBool();
  78. SetSelected(m_bStartValue);
  79. }
  80. bool CCvarToggleCheckButton::HasBeenModified()
  81. {
  82. return IsSelected() != m_bStartValue;
  83. }
  84. //-----------------------------------------------------------------------------
  85. // Purpose:
  86. // Input : *panel -
  87. //-----------------------------------------------------------------------------
  88. void CCvarToggleCheckButton::SetSelected( bool state )
  89. {
  90. BaseClass::SetSelected( state );
  91. if ( !m_pszCvarName || !m_pszCvarName[ 0 ] )
  92. return;
  93. /*
  94. // Look up current value
  95. bool value = state;
  96. engine->Cvar_SetValue( m_pszCvarName, value ? 1.0f : 0.0f );*/
  97. }
  98. //-----------------------------------------------------------------------------
  99. void CCvarToggleCheckButton::OnButtonChecked()
  100. {
  101. if (HasBeenModified())
  102. {
  103. PostActionSignal(new KeyValues("ControlModified"));
  104. }
  105. }
  106. //-----------------------------------------------------------------------------
  107. void CCvarToggleCheckButton::ApplySettings( KeyValues *inResourceData )
  108. {
  109. BaseClass::ApplySettings( inResourceData );
  110. const char *cvarName = inResourceData->GetString("cvar_name", "");
  111. const char *cvarValue = inResourceData->GetString("cvar_value", "");
  112. if( Q_stricmp( cvarName, "") == 0 )
  113. return;// Doesn't have cvar set up in res file, must have been constructed with it.
  114. if( m_pszCvarName )
  115. free( m_pszCvarName );// got a "", not a NULL from the create-control call
  116. m_pszCvarName = cvarName ? strdup( cvarName ) : NULL;
  117. if( Q_stricmp( cvarValue, "1") == 0 )
  118. m_bStartValue = true;
  119. else
  120. m_bStartValue = false;
  121. const ConVar *var = cvar->FindVar( m_pszCvarName );
  122. if ( var )
  123. {
  124. if( var->GetBool() )
  125. SetSelected( true );
  126. else
  127. SetSelected( false );
  128. }
  129. }