-
Notifications
You must be signed in to change notification settings - Fork 841
Expand file tree
/
Copy pathdtextentry.lua
More file actions
439 lines (286 loc) · 9.69 KB
/
dtextentry.lua
File metadata and controls
439 lines (286 loc) · 9.69 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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
local PANEL = {}
local strAllowedNumericCharacters = "1234567890.-"
AccessorFunc( PANEL, "m_bAllowEnter", "EnterAllowed", FORCE_BOOL )
AccessorFunc( PANEL, "m_bUpdateOnType", "UpdateOnType", FORCE_BOOL ) -- Update the convar as we type
AccessorFunc( PANEL, "m_bNumeric", "Numeric", FORCE_BOOL )
AccessorFunc( PANEL, "m_bHistory", "HistoryEnabled", FORCE_BOOL )
AccessorFunc( PANEL, "m_bDisableTabbing", "TabbingDisabled", FORCE_BOOL )
AccessorFunc( PANEL, "m_FontName", "Font" )
AccessorFunc( PANEL, "m_bBorder", "DrawBorder" )
AccessorFunc( PANEL, "m_bBackground", "PaintBackground" )
AccessorFunc( PANEL, "m_bBackground", "DrawBackground" ) -- Deprecated
AccessorFunc( PANEL, "m_colText", "TextColor" )
AccessorFunc( PANEL, "m_colHighlight", "HighlightColor" )
AccessorFunc( PANEL, "m_colCursor", "CursorColor" )
AccessorFunc( PANEL, "m_colPlaceholder", "PlaceholderColor" )
AccessorFunc( PANEL, "m_txtPlaceholder", "PlaceholderText" )
Derma_Install_Convar_Functions( PANEL )
function PANEL:Init()
self:SetHistoryEnabled( false )
self.History = {}
self.HistoryPos = 0
--
-- We're going to draw these ourselves in
-- the skin system - so disable them here.
-- This will leave it only drawing text.
--
self:SetPaintBorderEnabled( false )
self:SetPaintBackgroundEnabled( false )
--
-- These are Lua side commands
-- Defined above using AccessorFunc
--
self:SetDrawBorder( true )
self:SetPaintBackground( true )
self:SetEnterAllowed( true )
self:SetUpdateOnType( false )
self:SetNumeric( false )
self:SetAllowNonAsciiCharacters( true )
-- Nicer default height
self:SetTall( 20 )
-- Clear keyboard focus when we click away
self.m_bLoseFocusOnClickAway = true
-- Beam Me Up Scotty
self:SetCursor( "beam" )
self:SetFont( "DermaDefault" )
end
function PANEL:IsEditing()
return self == vgui.GetKeyboardFocus()
end
function PANEL:OnKeyCodeTyped( code )
self:OnKeyCode( code )
if ( code == KEY_ENTER && !self:IsMultiline() && self:GetEnterAllowed() ) then
if ( IsValid( self.Menu ) ) then
self.Menu:Remove()
end
self:FocusNext()
self:OnEnter( self:GetText() )
self.HistoryPos = 0
end
if ( self.m_bHistory || IsValid( self.Menu ) ) then
if ( code == KEY_UP ) then
self.HistoryPos = self.HistoryPos - 1
self:UpdateFromHistory()
end
if ( code == KEY_DOWN || code == KEY_TAB ) then
self.HistoryPos = self.HistoryPos + 1
self:UpdateFromHistory()
end
end
end
function PANEL:OnKeyCode( code )
end
function PANEL:ApplySchemeSettings()
self:SetFontInternal( self.m_FontName )
derma.SkinHook( "Scheme", "TextEntry", self )
end
function PANEL:GetTextColor()
return self.m_colText || self:GetSkin().colTextEntryText
end
function PANEL:GetPlaceholderColor()
return self.m_colPlaceholder || self:GetSkin().colTextEntryTextPlaceholder
end
function PANEL:GetHighlightColor()
return self.m_colHighlight || self:GetSkin().colTextEntryTextHighlight
end
function PANEL:GetCursorColor()
return self.m_colCursor || self:GetSkin().colTextEntryTextCursor
end
function PANEL:UpdateFromHistory()
if ( IsValid( self.Menu ) ) then
return self:UpdateFromMenu()
end
local pos = self.HistoryPos
-- Is the Pos within bounds?
if ( pos < 0 ) then pos = #self.History end
if ( pos > #self.History ) then pos = 0 end
local text = self.History[ pos ]
if ( !text ) then text = "" end
self:SetText( text )
self:SetCaretPos( utf8.len( text ) )
self:OnTextChanged()
self.HistoryPos = pos
end
function PANEL:UpdateFromMenu()
local pos = self.HistoryPos
local num = self.Menu:ChildCount()
self.Menu:ClearHighlights()
if ( pos < 0 ) then pos = num end
if ( pos > num ) then pos = 0 end
local item = self.Menu:GetChild( pos )
if ( !item ) then
self:SetText( "" )
self.HistoryPos = pos
return
end
self.Menu:HighlightItem( item )
local txt = item:GetText()
self:SetText( txt )
self:SetCaretPos( utf8.len( txt ) )
self:OnTextChanged( true )
self.HistoryPos = pos
end
function PANEL:OnTextChanged( noMenuRemoval )
self.HistoryPos = 0
if ( self:GetUpdateOnType() ) then
self:UpdateConvarValue()
self:OnValueChange( self:GetText() )
end
if ( IsValid( self.Menu ) && !noMenuRemoval ) then
self.Menu:Remove()
end
local tab = self:GetAutoComplete( self:GetText() )
if ( tab ) then
self:OpenAutoComplete( tab )
end
self:OnChange()
end
function PANEL:OnChange()
end
function PANEL:OpenAutoComplete( tab )
if ( !tab ) then return end
if ( #tab == 0 ) then return end
-- 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 )
for k, v in pairs( tab ) do
self.Menu:AddOption( v, function() self:SetText( v ) self:SetCaretPos( utf8.len( v ) ) self:RequestFocus() end )
end
local x, y = self:LocalToScreen( 0, self:GetTall() )
self.Menu:SetMinimumWidth( self:GetWide() )
self.Menu:Open( x, y, true, self )
self.Menu:SetPos( x, y )
self.Menu:SetMaxHeight( ( ScrH() - y ) - 10 )
end
function PANEL:Think()
self:ConVarStringThink()
end
function PANEL:OnRemove()
if ( IsValid( self.Menu ) ) then
self.Menu:Remove()
end
end
function PANEL:OnEnter( val )
-- For override
self:UpdateConvarValue()
self:OnValueChange( self:GetText() )
end
function PANEL:UpdateConvarValue()
-- This only kicks into action if this variable has
-- a ConVar associated with it.
self:ConVarChanged( self:GetValue() )
end
function PANEL:Paint( w, h )
derma.SkinHook( "Paint", "TextEntry", self, w, h )
return false
end
function PANEL:PerformLayout()
derma.SkinHook( "Layout", "TextEntry", self )
end
function PANEL:SetValue( strValue )
-- Don't update if we're typing into it!
-- I'm sure a lot of people will want to reverse this behaviour :(
if ( vgui.GetKeyboardFocus() == self ) then return end
local CaretPos = self:GetCaretPos()
self:SetText( strValue )
self:OnValueChange( strValue )
self:SetCaretPos( CaretPos )
end
function PANEL:OnValueChange( strValue )
-- For Override
end
function PANEL:CheckNumeric( strValue )
-- Not purely numeric, don't run the check
if ( !self:GetNumeric() ) then return false end
-- God I hope numbers look the same in every language
if ( !string.find( strAllowedNumericCharacters, strValue, 1, true ) ) then
-- Noisy Error?
return true
end
return false
end
-- Backwards compatibility. Do not use.
function PANEL:SetDisabled( bDisabled )
self:SetEnabled( !bDisabled )
end
function PANEL:GetDisabled()
return !self:IsEnabled()
end
function PANEL:AllowInput( strValue )
-- This is layed out like this so you can easily override and
-- either keep or remove this numeric check.
if ( self:CheckNumeric( strValue ) ) then return true end
end
function PANEL:SetEditable( b )
self:SetKeyboardInputEnabled( b )
self:SetMouseInputEnabled( b )
end
function PANEL:OnGetFocus()
--
-- These hooks are here for the sake of things like the spawn menu
-- which don't have key focus until you click on one of the text areas.
--
-- If you make a control for the spawnmenu that requires keyboard input
-- You should have these 3 functions in your panel, so it can handle it.
--
hook.Run( "OnTextEntryGetFocus", self )
end
function PANEL:OnLoseFocus()
self:UpdateConvarValue()
hook.Call( "OnTextEntryLoseFocus", nil, self )
end
function PANEL:OnMousePressed( mcode )
if ( mcode == MOUSE_LEFT ) then
self:OnGetFocus()
end
end
function PANEL:AddHistory( txt )
if ( !txt || txt == "" ) then return end
table.RemoveByValue( self.History, txt )
table.insert( self.History, txt )
end
function PANEL:GetAutoComplete( txt )
-- for override. Return a table of strings.
end
function PANEL:GetInt()
local num = tonumber( self:GetText() )
if ( !num ) then return nil end
return math.Round( num )
end
function PANEL:GetFloat()
return tonumber( self:GetText() )
end
function PANEL:GenerateExample( ClassName, PropertySheet, Width, Height )
local ctrl = vgui.Create( ClassName )
ctrl:SetText( "Edit Me!" )
ctrl:SetWide( 150 )
ctrl.OnEnter = function( slf ) Derma_Message( "You Typed: " .. slf:GetValue() ) end
PropertySheet:AddSheet( ClassName, ctrl, nil, true, true )
end
derma.DefineControl( "DTextEntry", "A simple TextEntry control", PANEL, "TextEntry" )
--[[---------------------------------------------------------
Clear the focus when we click away from us..
-----------------------------------------------------------]]
function TextEntryLoseFocus( panel, mcode )
local textArea = vgui.GetKeyboardFocus()
if ( !textArea ) then return end
if ( textArea == panel ) then return end
if ( textArea:IsOurChild( panel ) ) then return end -- Do not lose focus when clicking panels parented to the text entry (Autocomplete DMenu)
if ( !textArea.m_bLoseFocusOnClickAway ) then return end
-- We gotta find the EdtiablePanel parent and call KillFocus on it
-- We do it from the panel clicked, not the KB focus, which is necessary for DTextEntry autocomplete to not break
local prnt = panel
while ( IsValid( prnt ) ) do
if ( prnt:GetClassName() == "EditablePanel" || prnt:GetClassName() == "LuaEditablePanel" ) then
prnt:KillFocus()
return
end
prnt = prnt:GetParent()
end
end
hook.Add( "VGUIMousePressed", "TextEntryLoseFocus", TextEntryLoseFocus )