-
Notifications
You must be signed in to change notification settings - Fork 836
Expand file tree
/
Copy pathderma.lua
More file actions
245 lines (166 loc) · 5.48 KB
/
derma.lua
File metadata and controls
245 lines (166 loc) · 5.48 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
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
local table = table
local vgui = vgui
local Msg = Msg
local setmetatable = setmetatable
local _G = _G
local gamemode = gamemode
local pairs = pairs
local isfunction = isfunction
module( "derma" )
Controls = {}
SkinList = {}
local DefaultSkin = {}
local SkinMetaTable = {}
local iSkinChangeIndex = 1
SkinMetaTable.__index = function ( self, key )
return DefaultSkin[ key ]
end
local function FindPanelsByClass( SeekingClass )
local outtbl = {}
--
-- Going through the registry is a hacky way to do this.
-- We're only doing it this way because it doesn't matter if it's a
-- bit slow - because this function is only used when reloading.
--
local tbl = vgui.GetAll()
for k, v in pairs( tbl ) do
if ( v.ClassName && v.ClassName == SeekingClass ) then
table.insert( outtbl, v )
end
end
return outtbl
end
--
-- Find all the panels that use this class and
-- if allowed replace the functions with the new ones.
--
local function ReloadClass( classname )
local ctrl = vgui.GetControlTable( classname )
if ( !ctrl ) then return end
local tbl = FindPanelsByClass( classname )
for k, v in pairs ( tbl ) do
if ( !v.AllowAutoRefresh ) then continue end
if ( v.PreAutoRefresh ) then
v:PreAutoRefresh()
end
for name, func in pairs( ctrl ) do
if ( !isfunction( func ) ) then continue end
v[ name ] = func
end
if ( v.PostAutoRefresh ) then
v:PostAutoRefresh()
end
end
end
--[[---------------------------------------------------------
GetControlList
-----------------------------------------------------------]]
function GetControlList()
return Controls
end
--[[---------------------------------------------------------
DefineControl
-----------------------------------------------------------]]
function DefineControl( strName, strDescription, strTable, strBase )
local bReloading = Controls[ strName ] != nil
-- Add Derma table to PANEL table.
strTable.Derma = {
ClassName = strName,
Description = strDescription,
BaseClass = strBase
}
-- Register control with VGUI
vgui.Register( strName, strTable, strBase )
-- Store control
Controls[ strName ] = strTable.Derma
-- Store as a global so controls can 'baseclass' easier
-- TODO: STOP THIS
_G[ strName ] = strTable
if ( bReloading ) then
Msg( "Reloaded Control: ", strName, "\n" )
ReloadClass( strName )
end
return strTable
end
--[[---------------------------------------------------------
DefineSkin
-----------------------------------------------------------]]
function DefineSkin( strName, strDescription, strTable )
strTable.Name = strName
strTable.Description = strDescription
strTable.Base = strTable.Base or "Default"
if ( strName != "Default" ) then
setmetatable( strTable, SkinMetaTable )
else
DefaultSkin = strTable
end
SkinList[ strName ] = strTable
-- Make all panels update their skin
RefreshSkins()
end
--[[---------------------------------------------------------
GetSkin - Returns current skin for panel
-----------------------------------------------------------]]
function GetSkinTable()
return table.Copy( SkinList )
end
--[[---------------------------------------------------------
Returns 'Default' Skin
-----------------------------------------------------------]]
function GetDefaultSkin()
local skin = nil
-- Check gamemode skin preference
if ( gamemode ) then
local skinname = gamemode.Call( "ForceDermaSkin" )
if ( skinname ) then skin = GetNamedSkin( skinname ) end
end
-- default
if ( !skin ) then skin = DefaultSkin end
return skin
end
--[[---------------------------------------------------------
Returns 'Named' Skin
-----------------------------------------------------------]]
function GetNamedSkin( name )
return SkinList[ name ]
end
--[[---------------------------------------------------------
SkinHook( strType, strName, panel )
-----------------------------------------------------------]]
function SkinHook( strType, strName, panel, w, h )
local Skin = panel:GetSkin()
if ( !Skin ) then return end
local func = Skin[ strType .. strName ]
if ( !func ) then return end
return func( Skin, panel, w, h )
end
--[[---------------------------------------------------------
SkinTexture( strName, panel, default )
-----------------------------------------------------------]]
function SkinTexture( strName, panel, default )
local Skin = panel:GetSkin()
if ( !Skin ) then return default end
local Textures = Skin.tex
if ( !Textures ) then return default end
return Textures[ strName ] or default
end
--[[---------------------------------------------------------
Color( strName, panel, default )
-----------------------------------------------------------]]
function Color( strName, panel, default )
local Skin = panel:GetSkin()
if ( !Skin ) then return default end
return Skin[ strName ] or default
end
--[[---------------------------------------------------------
SkinChangeIndex
-----------------------------------------------------------]]
function SkinChangeIndex()
return iSkinChangeIndex
end
--[[---------------------------------------------------------
RefreshSkins - clears all cache'd panels (so they will reassess which skin they should be using)
-----------------------------------------------------------]]
function RefreshSkins()
iSkinChangeIndex = iSkinChangeIndex + 1
end