-
Notifications
You must be signed in to change notification settings - Fork 836
Expand file tree
/
Copy pathdcombobox.lua
More file actions
295 lines (191 loc) · 5.92 KB
/
dcombobox.lua
File metadata and controls
295 lines (191 loc) · 5.92 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
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
local PANEL = {}
Derma_Hook( PANEL, "Paint", "Paint", "ComboBox" )
Derma_Install_Convar_Functions( PANEL )
AccessorFunc( PANEL, "m_bDoSort", "SortItems", FORCE_BOOL )
function PANEL:Init()
-- Create button
self.DropButton = vgui.Create( "DPanel", self )
self.DropButton.Paint = function( panel, w, h ) derma.SkinHook( "Paint", "ComboDownArrow", panel, w, h ) end
self.DropButton:SetMouseInputEnabled( false )
self.DropButton.ComboBox = self
-- Setup internals
self:SetTall( 22 )
self:Clear()
self:SetContentAlignment( 4 )
self:SetTextInset( 8, 0 )
self:SetIsMenu( true )
self:SetSortItems( true )
end
function PANEL:Clear()
self:SetText( "" )
self.Choices = {}
self.Data = {}
self.ChoiceIcons = {}
self.Spacers = {}
self.selected = nil
self:CloseMenu()
end
function PANEL:GetOptionText( index )
return self.Choices[ index ]
end
function PANEL:GetOptionData( index )
return self.Data[ index ]
end
function PANEL:GetOptionTextByData( data )
for id, dat in pairs( self.Data ) do
if ( dat == data ) then
return self:GetOptionText( id )
end
end
-- Try interpreting it as a number
for id, dat in pairs( self.Data ) do
if ( dat == tonumber( data ) ) then
return self:GetOptionText( id )
end
end
-- In case we fail
return data
end
function PANEL:PerformLayout( w, h )
self.DropButton:SetSize( 15, 15 )
self.DropButton:AlignRight( 4 )
self.DropButton:CenterVertical()
-- Make sure the text color is updated
DButton.PerformLayout( self, w, h )
end
function PANEL:ChooseOption( value, index )
self:CloseMenu()
self:SetText( value )
-- This should really be the here, but it is too late now and convar
-- changes are handled differently by different child elements
-- self:ConVarChanged( self.Data[ index ] )
self.selected = index
self:OnSelect( index, value, self.Data[ index ] )
end
function PANEL:ChooseOptionID( index )
local value = self:GetOptionText( index )
self:ChooseOption( value, index )
end
function PANEL:GetSelectedID()
return self.selected
end
function PANEL:GetSelected()
if ( !self.selected ) then return end
return self:GetOptionText( self.selected ), self:GetOptionData( self.selected )
end
function PANEL:OnSelect( index, value, data )
-- For override
end
function PANEL:OnMenuOpened( menu )
-- For override
end
function PANEL:AddSpacer()
self.Spacers[ #self.Choices ] = true
end
function PANEL:AddChoice( value, data, select, icon )
local index = table.insert( self.Choices, value )
if ( data ) then
self.Data[ index ] = data
end
if ( icon ) then
self.ChoiceIcons[ index ] = icon
end
if ( select ) then
self:ChooseOption( value, index )
end
return index
end
function PANEL:RemoveChoice( index )
if ( !isnumber( index ) ) then return end
local text = table.remove( self.Choices, index )
local data = table.remove( self.Data, index )
return text, data
end
function PANEL:IsMenuOpen()
return IsValid( self.Menu ) && self.Menu:IsVisible()
end
function PANEL:OpenMenu( pControlOpener )
if ( pControlOpener && pControlOpener == self.TextEntry ) then
return
end
-- Don't do anything if there aren't any options..
if ( #self.Choices == 0 ) then return end
-- If the menu still exists and hasn't been deleted
-- then just close it and don't open a new one.
self:CloseMenu()
-- If we have a modal parent at some level, we gotta parent to
-- that or our menu items are not gonna be selectable
local parent = self
while ( IsValid( parent ) && !parent:IsModal() ) do
parent = parent:GetParent()
end
if ( !IsValid( parent ) ) then parent = self end
self.Menu = DermaMenu( false, parent )
if ( self:GetSortItems() ) then
local sorted = {}
for k, v in pairs( self.Choices ) do
local val = tostring( v ) --tonumber( v ) || v -- This would make nicer number sorting, but SortedPairsByMemberValue doesn't seem to like number-string mixing
if ( string.len( val ) > 1 && !tonumber( val ) && val:StartsWith( "#" ) ) then val = language.GetPhrase( val:sub( 2 ) ) end
table.insert( sorted, { id = k, data = v, label = val } )
end
for k, v in SortedPairsByMemberValue( sorted, "label" ) do
local option = self.Menu:AddOption( v.data, function() self:ChooseOption( v.data, v.id ) end )
if ( self.ChoiceIcons[ v.id ] ) then
option:SetIcon( self.ChoiceIcons[ v.id ] )
end
if ( self.Spacers[ v.id ] ) then
self.Menu:AddSpacer()
end
end
else
for k, v in pairs( self.Choices ) do
local option = self.Menu:AddOption( v, function() self:ChooseOption( v, k ) end )
if ( self.ChoiceIcons[ k ] ) then
option:SetIcon( self.ChoiceIcons[ k ] )
end
if ( self.Spacers[ k ] ) then
self.Menu:AddSpacer()
end
end
end
local x, y = self:LocalToScreen( 0, self:GetTall() )
self.Menu:SetMinimumWidth( self:GetWide() )
self.Menu:Open( x, y, false, self )
self:OnMenuOpened( self.Menu )
end
function PANEL:CloseMenu()
if ( IsValid( self.Menu ) ) then
self.Menu:Remove()
end
self.Menu = nil
end
-- This really should use a convar change hook
function PANEL:CheckConVarChanges()
if ( !self.m_strConVar ) then return end
local strValue = GetConVarString( self.m_strConVar )
if ( self.m_strConVarValue == strValue ) then return end
self.m_strConVarValue = strValue
self:SetValue( self:GetOptionTextByData( self.m_strConVarValue ) )
end
function PANEL:Think()
self:CheckConVarChanges()
end
function PANEL:SetValue( strValue )
self:SetText( strValue )
end
function PANEL:DoClick()
if ( self:IsMenuOpen() ) then
return self:CloseMenu()
end
self:OpenMenu()
end
function PANEL:GenerateExample( ClassName, PropertySheet, Width, Height )
local ctrl = vgui.Create( ClassName )
ctrl:AddChoice( "Some Choice" )
ctrl:AddChoice( "Another Choice", "myData" )
ctrl:AddChoice( "Default Choice", "myData2", true )
ctrl:AddChoice( "Icon Choice", "myData3", false, "icon16/star.png" )
ctrl:SetWide( 150 )
PropertySheet:AddSheet( ClassName, ctrl, nil, true, true )
end
derma.DefineControl( "DComboBox", "", PANEL, "DButton" )