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.

158 lines
3.8 KiB

  1. //========= Copyright 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, true );
  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, true );
  67. if ( !var.IsValid() )
  68. return;
  69. var.SetValue( m_bStartValue );
  70. }
  71. void CCvarToggleCheckButton::Reset()
  72. {
  73. // m_bStartValue = engine->pfnGetCvarFloat( m_pszCvarName ) > 0.0f ? true : false;
  74. if ( !m_pszCvarName || !m_pszCvarName[ 0 ] )
  75. return;
  76. ConVarRef var( m_pszCvarName, true );
  77. if ( !var.IsValid() )
  78. return;
  79. m_bStartValue = var.GetBool();
  80. SetSelected(m_bStartValue);
  81. }
  82. bool CCvarToggleCheckButton::HasBeenModified()
  83. {
  84. return IsSelected() != m_bStartValue;
  85. }
  86. //-----------------------------------------------------------------------------
  87. // Purpose:
  88. // Input : *panel -
  89. //-----------------------------------------------------------------------------
  90. void CCvarToggleCheckButton::SetSelected( bool state )
  91. {
  92. BaseClass::SetSelected( state );
  93. if ( !m_pszCvarName || !m_pszCvarName[ 0 ] )
  94. return;
  95. /*
  96. // Look up current value
  97. bool value = state;
  98. engine->Cvar_SetValue( m_pszCvarName, value ? 1.0f : 0.0f );*/
  99. }
  100. //-----------------------------------------------------------------------------
  101. void CCvarToggleCheckButton::OnButtonChecked()
  102. {
  103. if (HasBeenModified())
  104. {
  105. PostActionSignal(new KeyValues("ControlModified"));
  106. }
  107. }
  108. //-----------------------------------------------------------------------------
  109. void CCvarToggleCheckButton::ApplySettings( KeyValues *inResourceData )
  110. {
  111. BaseClass::ApplySettings( inResourceData );
  112. const char *cvarName = inResourceData->GetString("cvar_name", "");
  113. const char *cvarValue = inResourceData->GetString("cvar_value", "");
  114. if( Q_stricmp( cvarName, "") == 0 )
  115. return;// Doesn't have cvar set up in res file, must have been constructed with it.
  116. if( m_pszCvarName )
  117. free( m_pszCvarName );// got a "", not a NULL from the create-control call
  118. m_pszCvarName = cvarName ? strdup( cvarName ) : NULL;
  119. if( Q_stricmp( cvarValue, "1") == 0 )
  120. m_bStartValue = true;
  121. else
  122. m_bStartValue = false;
  123. const ConVar *var = cvar->FindVar( m_pszCvarName );
  124. if ( var )
  125. {
  126. if( var->GetBool() )
  127. SetSelected( true );
  128. else
  129. SetSelected( false );
  130. }
  131. }