-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathdbutton_ttt2.lua
371 lines (301 loc) · 8.32 KB
/
dbutton_ttt2.lua
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
---
-- @class PANEL
-- @section DButtonTTT2
local PANEL = {}
local soundClick = Sound("common/talk.wav")
---
-- @accessor boolean
-- @realm client
AccessorFunc(PANEL, "m_bBorder", "DrawBorder", FORCE_BOOL)
---
-- @accessor bool
-- @realm client
AccessorFunc(PANEL, "ignoreCallbackEnabledVar", "IgnoreCallbackEnabledVar", FORCE_BOOL)
---
-- @ignore
function PANEL:Init()
self:SetContentAlignment(5)
self:SetTall(22)
self:SetMouseInputEnabled(true)
self:SetKeyboardInputEnabled(true)
self:SetCursor("hand")
self:SetFont("DermaTTT2Button")
-- remove label and overwrite function
self:SetText("")
self.SetText = function(slf, text)
slf.data.text = text
end
self.data = {}
-- hack a sound into the DoClick function after it is initialized
timer.Simple(0, function()
if not IsValid(self) then
return
end
local oldClick = self.DoClick
local oldRightClick = self.DoRightClick
self.DoClick = function(slf)
sound.ConditionalPlay(soundClick, SOUND_TYPE_BUTTONS)
oldClick(slf)
end
self.DoRightClick = function(slf)
sound.ConditionalPlay(soundClick, SOUND_TYPE_BUTTONS)
oldRightClick(slf)
end
end)
end
---
-- This is only used temporarily to keep old variables without breaking the style of "no enable to disable" checkboxes
-- @param bool invert
-- @realm client
function PANEL:SetInverted(invert)
self.inverted = invert
end
---
-- @param string cvar
-- @realm client
function PANEL:SetConVar(cvar)
if not ConVarExists(cvar or "") then
return
end
self.convar = GetConVar(cvar)
self:SetDefaultValue(tobool(self.convar:GetDefault()))
self:SetValue(self.convar:GetBool())
end
local callbackEnabledVarTracker = 0
---
-- @param string cvar
-- @realm client
function PANEL:SetServerConVar(cvar)
if not cvar or cvar == "" then
return
end
self.serverConVar = cvar
-- Check if self is valid before calling SetValue.
cvars.ServerConVarGetValue(cvar, function(wasSuccess, value, default)
if wasSuccess and IsValid(self) then
self:SetValue(tobool(value), true)
self:SetDefaultValue(tobool(default))
end
end)
callbackEnabledVarTracker = callbackEnabledVarTracker + 1
local myIdentifierString = "TTT2CheckBoxConVarChangeCallback"
.. tostring(callbackEnabledVarTracker)
local callback = function(conVarName, oldValue, newValue)
if not IsValid(self) then
-- We need to remove the callback in a timer, because otherwise the ConVar change callback code
-- will throw an error while looping over the callbacks.
-- This happens, because the callback is removed from the same table that is iterated over.
-- Thus, the table size changes while iterating over it and leads to a nil callback as the last entry.
timer.Simple(0, function()
cvars.RemoveChangeCallback(conVarName, myIdentifierString)
end)
return
end
self:SetValue(tobool(newValue), true)
end
cvars.AddChangeCallback(cvar, callback, myIdentifierString)
end
---
-- @param table databaseInfo containing {name, itemName, key}
-- @realm client
function PANEL:SetDatabase(databaseInfo)
if not istable(databaseInfo) then
return
end
local name = databaseInfo.name
local itemName = databaseInfo.itemName
local key = databaseInfo.key
if not name or not itemName or not key then
return
end
self.databaseInfo = databaseInfo
database.GetValue(name, itemName, key, function(databaseExists, value)
if databaseExists then
self:SetValue(value, true)
end
end)
self:SetDefaultValue(database.GetDefaultValue(name, itemName, key))
callbackEnabledVarTracker = callbackEnabledVarTracker + 1
local myIdentifierString = "TTT2CheckBoxDatabaseChangeCallback"
.. tostring(callbackEnabledVarTracker)
local function OnDatabaseChangeCallback(_name, _itemName, _key, oldValue, newValue)
if not IsValid(self) then
database.RemoveChangeCallback(name, itemName, key, myIdentifierString)
return
end
self:SetValue(newValue, true)
end
database.AddChangeCallback(name, itemName, key, OnDatabaseChangeCallback, myIdentifierString)
end
---
-- @param any val
-- @param boolean ignoreCallbackEnabledVar To avoid endless loops, separated setting of convars and UI values
-- @realm client
function PANEL:SetValue(val, ignoreCallbackEnabledVar)
self:SetIgnoreCallbackEnabledVar(ignoreCallbackEnabledVar)
if self.inverted then
val = not val
end
self.value = val
self:OnChange(self.value)
end
---
-- @return boolean Returns the value of the button if there is one
-- assigned. It can be assigned by attaching convars or database values.
-- @realm client
function PANEL:GetValue()
return self.value or false
end
---
-- @param boolean value
-- @realm client
function PANEL:SetDefaultValue(value)
if isbool(value) then
-- note: even when it is inverted, the default is not inverted here
-- as it is inverted when the default button is pressed
self.default = value
noDefault = false
else
self.default = nil
end
end
---
-- @return boolean defaultValue, if unset returns false
-- @realm client
function PANEL:GetDefaultValue()
return tobool(self.default)
end
---
-- @param any val
-- @realm client
function PANEL:ValueChanged(val)
if self.inverted then
val = not val
end
if self.convar then
self.convar:SetBool(val)
elseif self.serverConVar and not self:GetIgnoreCallbackEnabledVar() then
cvars.ChangeServerConVar(self.serverConVar, val and "1" or "0")
elseif self.databaseInfo and not self:GetIgnoreCallbackEnabledVar() then
database.SetValue(
self.databaseInfo.name,
self.databaseInfo.itemName,
self.databaseInfo.key,
val
)
else
self:SetIgnoreCallbackEnabledVar(false)
end
self:OnValueChanged(val)
end
---
-- overwrites the base function with an empty function
-- @param any val
-- @realm client
function PANEL:OnValueChanged(val) end
---
-- @param any val
-- @realm client
function PANEL:OnChange(val) end
---
-- @param any val
-- @realm client
function PANEL:OnDefaultChange(val) end
---
-- @return string
-- @realm client
function PANEL:GetText()
return self.data.text
end
---
-- @param table params
-- @realm client
function PANEL:SetTextParams(params)
self.data.params = params
end
---
-- @return table
-- @realm client
function PANEL:GetTextParams()
return self.data.params
end
---
-- @return boolean
-- @realm client
function PANEL:HasTextParams()
return self.data.params ~= nil
end
---
-- @return boolean
-- @realm client
function PANEL:IsDown()
return self.Depressed
end
---
-- @ignore
function PANEL:Paint(w, h)
derma.SkinHook("Paint", "ButtonTTT2", self, w, h)
return false
end
---
-- @param string strName
-- @param string strArgs
-- @realm client
function PANEL:SetConsoleCommand(strName, strArgs)
self.DoClick = function(slf, val)
RunConsoleCommand(strName, strArgs)
end
end
---
-- @param Color color
-- @realm client
function PANEL:SetColor(color)
self.data.color = color
end
---
-- @return Color|nil
-- @realm client
function PANEL:GetColor()
return self.data.color
end
---
-- @param Material icon
-- @param[default=false] boolean is_shadowed
-- @param[default=32] number size
-- @realm client
function PANEL:SetIcon(icon, is_shadowed, size)
self.data.icon = icon
self.data.icon_shadow = is_shadowed or false
self.data.icon_size = size or 32
end
---
-- @return Material|nil
-- @realm client
function PANEL:GetIcon()
return self.data.icon
end
---
-- @return boolean
-- @realm client
function PANEL:HasIcon()
return self.data.icon ~= nil
end
---
-- @return boolean|nil
-- @realm client
function PANEL:IsIconShadowed()
return self.data.icon_shadow
end
---
-- @return number|nil
-- @realm client
function PANEL:GetIconSize()
return self.data.icon_size
end
---
-- @ignore
function PANEL:SizeToContents()
local w, h = self:GetContentSize()
self:SetSize(w + 8, h + 4)
end
derma.DefineControl("DButtonTTT2", "A standard Button", PANEL, "DLabelTTT2")