-
Notifications
You must be signed in to change notification settings - Fork 841
Expand file tree
/
Copy pathdbutton.lua
More file actions
193 lines (130 loc) · 4.48 KB
/
dbutton.lua
File metadata and controls
193 lines (130 loc) · 4.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
local PANEL = {}
AccessorFunc( PANEL, "m_bBorder", "DrawBorder", FORCE_BOOL )
function PANEL:Init()
self:SetContentAlignment( 5 )
--
-- These are Lua side commands
-- Defined above using AccessorFunc
--
self:SetDrawBorder( true )
self:SetPaintBackground( true )
self:SetTall( 22 )
self:SetMouseInputEnabled( true )
self:SetKeyboardInputEnabled( true )
self:SetCursor( "hand" )
self:SetFont( "DermaDefault" )
end
function PANEL:IsDown()
return self.Depressed
end
function PANEL:SetImage( img )
if ( !img ) then
if ( IsValid( self.m_Image ) ) then
self.m_Image:Remove()
end
return
end
if ( !IsValid( self.m_Image ) ) then
self.m_Image = vgui.Create( "DImage", self )
end
self.m_Image:SetImage( img )
self.m_Image:SizeToContents()
self:InvalidateLayout()
end
PANEL.SetIcon = PANEL.SetImage
function PANEL:SetMaterial( mat )
if ( !mat ) then
if ( IsValid( self.m_Image ) ) then
self.m_Image:Remove()
end
return
end
if ( !IsValid( self.m_Image ) ) then
self.m_Image = vgui.Create( "DImage", self )
end
self.m_Image:SetMaterial( mat )
self.m_Image:SizeToContents()
self:InvalidateLayout()
end
function PANEL:Paint( w, h )
derma.SkinHook( "Paint", "Button", self, w, h )
--
-- Draw the button text
--
return false
end
function PANEL:UpdateColours( skin )
if ( !self:IsEnabled() ) then return self:SetTextStyleColor( skin.Colours.Button.Disabled ) end
if ( self:IsDown() || self.m_bSelected ) then return self:SetTextStyleColor( skin.Colours.Button.Down ) end
if ( self.Hovered ) then return self:SetTextStyleColor( skin.Colours.Button.Hover ) end
return self:SetTextStyleColor( skin.Colours.Button.Normal )
end
function PANEL:PerformLayoutImage()
--
-- If we have an image we have to place the image on the left
-- and make the text align to the left, then set the inset
-- so the text will be to the right of the icon.
--
if ( IsValid( self.m_Image ) ) then
local targetSize = math.min( self:GetWide() - 4, self:GetTall() - 4 )
local imgW, imgH = self.m_Image.ActualWidth, self.m_Image.ActualHeight
local zoom = math.min( targetSize / imgW, targetSize / imgH, 1 )
local newSizeX = math.ceil( imgW * zoom )
local newSizeY = math.ceil( imgH * zoom )
self.m_Image:SetWide( newSizeX )
self.m_Image:SetTall( newSizeY )
if ( self:GetWide() < self:GetTall() ) then
self.m_Image:SetPos( 4, ( self:GetTall() - self.m_Image:GetTall() ) * 0.5 )
else
self.m_Image:SetPos( 2 + ( targetSize - self.m_Image:GetWide() ) * 0.5, ( self:GetTall() - self.m_Image:GetTall() ) * 0.5 )
end
-- For center alignments, reduce the inset of the image, so the text appears more centered visually
local alignment = self:GetContentAlignment()
if ( alignment == 8 || alignment == 5 || alignment == 2 ) then
self:SetTextInset( self.m_Image:GetWide() + 4, 0 )
else
self:SetTextInset( self.m_Image:GetWide() + 8, 0 )
end
end
end
function PANEL:PerformLayout( w, h )
self:PerformLayoutImage()
DLabel.PerformLayout( self, w, h )
end
function PANEL:SetConsoleCommand( strName, strArg, ... )
if ( select( "#", ... ) > 0 ) then
local extraArgs = { ... }
self.DoClick = function( slf, val )
RunConsoleCommand( strName, strArg, unpack( extraArgs ) )
end
return
end
self.DoClick = function( slf, val )
RunConsoleCommand( strName, strArg )
end
end
function PANEL:SizeToContents()
self:PerformLayoutImage() -- Set the text inset first.
local w, h = self:GetContentSize()
self:SetSize( w + 8, h + 4 )
end
function PANEL:SizeToContentsX( addVal )
self:PerformLayoutImage() -- Set the text inset first.
local w, h = self:GetContentSize()
self:SetWide( w + 8 + ( addVal or 0 ) )
end
function PANEL:GenerateExample( ClassName, PropertySheet, Width, Height )
local ctrl = vgui.Create( ClassName )
ctrl:SetText( "Example Button" )
ctrl:SetWide( 200 )
PropertySheet:AddSheet( ClassName, ctrl, nil, true, true )
end
local PANEL = derma.DefineControl( "DButton", "A standard Button", PANEL, "DLabel" )
PANEL = table.Copy( PANEL )
function PANEL:SetActionFunction( func )
self.DoClick = function( slf, val ) func( slf, "Command", 0, 0 ) end
end
-- No example for this control. Should we remove this completely?
function PANEL:GenerateExample( class, tabs, w, h )
end
derma.DefineControl( "Button", "Backwards Compatibility", PANEL, "DLabel" )