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.

136 lines
2.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "CvarNegateCheckButton.h"
  9. #include "EngineInterface.h"
  10. #include <vgui/IVGui.h>
  11. #include "IGameUIFuncs.h"
  12. #include "tier1/KeyValues.h"
  13. #include "tier1/convar.h"
  14. // memdbgon must be the last include file in a .cpp file!!!
  15. #include <tier0/memdbgon.h>
  16. using namespace vgui;
  17. CCvarNegateCheckButton::CCvarNegateCheckButton( Panel *parent, const char *panelName, const char *text,
  18. const char *cvarname )
  19. : CheckButton( parent, panelName, text )
  20. {
  21. m_pszCvarName = cvarname ? strdup( cvarname ) : NULL;
  22. Reset();
  23. AddActionSignalTarget( this );
  24. }
  25. CCvarNegateCheckButton::~CCvarNegateCheckButton()
  26. {
  27. free( m_pszCvarName );
  28. }
  29. //-----------------------------------------------------------------------------
  30. // Purpose:
  31. //-----------------------------------------------------------------------------
  32. void CCvarNegateCheckButton::Paint()
  33. {
  34. if ( !m_pszCvarName )
  35. {
  36. BaseClass::Paint();
  37. return;
  38. }
  39. // Look up current value
  40. // float value = engine->pfnGetCvarFloat( m_pszCvarName );
  41. ConVarRef var( m_pszCvarName );
  42. if ( !var.IsValid() )
  43. return;
  44. float value = var.GetFloat();
  45. if ( value < 0 )
  46. {
  47. if ( !m_bStartState )
  48. {
  49. SetSelected( true );
  50. m_bStartState = true;
  51. }
  52. }
  53. else
  54. {
  55. if ( m_bStartState )
  56. {
  57. SetSelected( false );
  58. m_bStartState = false;
  59. }
  60. }
  61. BaseClass::Paint();
  62. }
  63. void CCvarNegateCheckButton::Reset()
  64. {
  65. // Look up current value
  66. // float value = engine->pfnGetCvarFloat( m_pszCvarName );
  67. ConVarRef var( m_pszCvarName );
  68. if ( !var.IsValid() )
  69. return;
  70. float value = var.GetFloat();
  71. if ( value < 0 )
  72. {
  73. m_bStartState = true;
  74. }
  75. else
  76. {
  77. m_bStartState = false;
  78. }
  79. SetSelected(m_bStartState);
  80. }
  81. bool CCvarNegateCheckButton::HasBeenModified()
  82. {
  83. return IsSelected() != m_bStartState;
  84. }
  85. //-----------------------------------------------------------------------------
  86. // Purpose:
  87. // Input : *panel -
  88. //-----------------------------------------------------------------------------
  89. void CCvarNegateCheckButton::SetSelected( bool state )
  90. {
  91. BaseClass::SetSelected( state );
  92. }
  93. void CCvarNegateCheckButton::ApplyChanges()
  94. {
  95. if ( !m_pszCvarName || !m_pszCvarName[ 0 ] )
  96. return;
  97. ConVarRef var( m_pszCvarName );
  98. float value = var.GetFloat();
  99. value = (float)fabs( value );
  100. if (value < 0.00001)
  101. {
  102. // correct the value if it's not set
  103. value = 0.022f;
  104. }
  105. m_bStartState = IsSelected();
  106. value = -value;
  107. float ans = m_bStartState ? value : -value;
  108. var.SetValue( ans );
  109. }
  110. void CCvarNegateCheckButton::OnButtonChecked()
  111. {
  112. if (HasBeenModified())
  113. {
  114. PostActionSignal(new KeyValues("ControlModified"));
  115. }
  116. }