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