summaryrefslogtreecommitdiff
path: root/tracker/AdminServer/VarListPropertyPage.cpp
blob: 15ee3f42ec5cdd175d36621404d6fc8500aca103 (plain) (blame)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================

#include "VarListPropertyPage.h"
#include "RemoteServer.h"
#include "VarEditDialog.h"

#include <KeyValues.h>
#include <vgui/KeyCode.h>

#include <vgui_controls/ListPanel.h>
#include <vgui_controls/Button.h>

#include "filesystem.h"

using namespace vgui;

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CVarListPropertyPage::CVarListPropertyPage(vgui::Panel *parent, const char *name) : vgui::PropertyPage(parent, name)
{
	m_pRulesList = new ListPanel(this, "RulesList");
	m_pRulesList->AddColumnHeader(0, "name", "Variable", 256);
	m_pRulesList->AddColumnHeader(1, "value", "Value", 256);

	m_pEditButton = new Button(this, "EditButton", "Edit...");
	m_pEditButton->SetCommand(new KeyValues("EditVariable"));
}

//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
CVarListPropertyPage::~CVarListPropertyPage()
{
	// remove from callback list
	RemoteServer().RemoveServerDataResponseTarget(this);
}

//-----------------------------------------------------------------------------
// Purpose: loads a list of variables from the file
//-----------------------------------------------------------------------------
bool CVarListPropertyPage::LoadVarList(const char *varfile)
{
	bool bSuccess = false;
	// clear the current list
	m_pRulesList->DeleteAllItems();

	// load list from file
	KeyValues *dat = new KeyValues("VarList");
	if (dat->LoadFromFile( g_pFullFileSystem, varfile, NULL))
	{
		// enter into list
		for (KeyValues *rule = dat->GetFirstSubKey(); rule != NULL; rule = rule->GetNextKey())
		{
			m_pRulesList->AddItem(rule, 0, false, false);
		}
		bSuccess = true;
	}

	dat->deleteThis();
	return bSuccess;
}

//-----------------------------------------------------------------------------
// Purpose: configures layout
//-----------------------------------------------------------------------------
void CVarListPropertyPage::PerformLayout()
{
	BaseClass::PerformLayout();
	OnItemSelected();
}

//-----------------------------------------------------------------------------
// Purpose: Handles edit button press
//-----------------------------------------------------------------------------
void CVarListPropertyPage::EditVariable()
{
	// get rule from the list
	int itemID = m_pRulesList->GetSelectedItem(0);
	KeyValues *rule = m_pRulesList->GetItem(itemID);
	if (!rule)
		return;

	OnEditVariable(rule);
}

//-----------------------------------------------------------------------------
// Purpose: Edits var
//-----------------------------------------------------------------------------
void CVarListPropertyPage::OnEditVariable(KeyValues *rule)
{
	// open an edit box appropriate
	CVarEditDialog *box = new CVarEditDialog(this, "VarEditDialog");
	box->Activate(this, rule);
}

//-----------------------------------------------------------------------------
// Purpose: refreshes all the values of cvars in the list
//-----------------------------------------------------------------------------
void CVarListPropertyPage::RefreshVarList()
{
	// iterate the vars
	for (int i = 0; i < m_pRulesList->GetItemCount(); i++)
	{
		KeyValues *row = m_pRulesList->GetItem(i);
		if (!row)
			continue;

		// request the info from the server on each one of them
		RemoteServer().RequestValue(this, row->GetName());
	}

	RemoteServer().ProcessServerResponse();
}

//-----------------------------------------------------------------------------
// Purpose: sets the value of a variable in the list
//-----------------------------------------------------------------------------
void CVarListPropertyPage::SetVarString(const char *varName, const char *value)
{
	// find the item by name
	int itemID = m_pRulesList->GetItem(varName);
	KeyValues *rule = m_pRulesList->GetItem(itemID);
	if (!rule)
		return;

	// parse the rule
	const char *type = rule->GetString("type");
	if (!stricmp(type, "enumeration"))
	{
		// look up the value in the enumeration
		int iValue = atoi(value);
		const char *result = rule->FindKey("list", true)->GetString(value, "");
		rule->SetString("value", result);
		rule->SetInt("enum", iValue);
	}
	else 
	{
		// no special type, treat it as a string
		rule->SetString("value", value);
	}
		
	m_pRulesList->ApplyItemChanges(itemID);
}

//-----------------------------------------------------------------------------
// Purpose: Sets a custom string list
//-----------------------------------------------------------------------------
void CVarListPropertyPage::SetCustomStringList(const char *varName, const char *stringList)
{
	// find the item by name
	int itemID = m_pRulesList->GetItem(varName);
	KeyValues *rule = m_pRulesList->GetItem(itemID);
	if (!rule)
		return;

	rule->SetString("stringlist", stringList);
}

//-----------------------------------------------------------------------------
// Purpose: gets back the value of a variable
//-----------------------------------------------------------------------------
const char *CVarListPropertyPage::GetVarString(const char *varName)
{
	int itemID = m_pRulesList->GetItem(varName);
	KeyValues *rule = m_pRulesList->GetItem(itemID);
	if (!rule)
		return "";

	return rule->GetString("value");
}

//-----------------------------------------------------------------------------
// Purpose: Causes values to refresh on the user hitting F5
//-----------------------------------------------------------------------------
void CVarListPropertyPage::OnKeyCodeTyped(KeyCode code)
{
	if (code == KEY_F5)
	{
		OnResetData();
	}
	else
	{
		BaseClass::OnKeyCodeTyped(code);
	}
}

//-----------------------------------------------------------------------------
// Purpose: Enables the edit button if any rows in the list are selected
//-----------------------------------------------------------------------------
void CVarListPropertyPage::OnItemSelected()
{
	if (m_pRulesList->GetSelectedItemsCount() > 0)
	{
		m_pEditButton->SetEnabled(true);
	}
	else
	{
		m_pEditButton->SetEnabled(false);
	}
}

//-----------------------------------------------------------------------------
// Purpose: Called when a var gets edited
//-----------------------------------------------------------------------------
void CVarListPropertyPage::OnVarChanged(const char *var)
{
	// request new value from server
	RemoteServer().RequestValue(this, var);
	// process the queue immediately, since if we're running a local server there will already be a reply
	RemoteServer().ProcessServerResponse();
}

//-----------------------------------------------------------------------------
// Purpose: handles responses from the server
//-----------------------------------------------------------------------------
void CVarListPropertyPage::OnServerDataResponse(const char *value, const char *response)
{
	SetVarString(value, response);
}