/
dpanpanel.lua
245 lines (178 loc) · 4.48 KB
/
dpanpanel.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
local PANEL = {}
AccessorFunc( PANEL, "pnlCanvas", "Canvas" )
function PANEL:Init()
self.pnlCanvas = vgui.Create( "Panel", self )
self:GetCanvas().OnMousePressed = function( s, code )
s:GetParent():OnMousePressed( code )
end
self:GetCanvas():SetMouseInputEnabled( true )
self:GetCanvas().PerformLayout = function( pnl )
self:PerformLayout()
self:InvalidateParent()
end
function self.pnlCanvas:Think()
local mousex = math.Clamp( gui.MouseX(), 1, ScrW() - 1 )
local mousey = math.Clamp( gui.MouseY(), 1, ScrH() - 1 )
if ( self.Dragging ) then
local x = mousex - self.Dragging[1]
local y = mousey - self.Dragging[2]
self:SetPos( x, y )
self:PerformLayout()
end
if ( self.Hovered ) then
self:SetCursor( "sizeall" )
return
end
self:SetCursor( "arrow" )
end
function self.pnlCanvas:OnMousePressed( keyCode )
self.Dragging = { gui.MouseX() - self.x, gui.MouseY() - self.y }
self:MouseCapture( true )
end
function self.pnlCanvas:OnMouseReleased( keyCode )
self.Dragging = nil
self.Sizing = nil
self:MouseCapture( false )
self:InvalidateParent()
end
self:SetMouseInputEnabled( true )
--This turns off the engine drawing
self:SetPaintBackgroundEnabled( false )
self:SetPaintBorderEnabled( false )
self:SetPaintBackground( false )
end
function PANEL:Think()
if ( self.Hovered ) then
self:SetCursor( "sizeall" )
return
end
self:SetCursor( "arrow" )
end
function PANEL:OnMousePressed( ... )
self.pnlCanvas:OnMousePressed( ... )
end
function PANEL:OnMouseReleased( ... )
self.pnlCanvas:OnMouseReleased( ... )
end
function PANEL:AddItem( pnl )
pnl:SetParent( self:GetCanvas() )
end
function PANEL:OnChildAdded( child )
self:AddItem( child )
-- Anchor all children to top-left (so they are all snug)
local can = self:GetCanvas()
local mx, my
for _, p in pairs( can:GetChildren() ) do
local x, y = p:GetPos()
mx = mx or x
my = my or y
if ( x < mx ) then mx = x end
if ( y < my ) then my = y end
end
if ( mx ) then
for _, p in pairs( can:GetChildren() ) do
local x, y = p:GetPos()
p:SetPos( x - mx, y - my )
end
local x, y = can:GetPos()
can:SetPos( x + mx, y + my )
end
end
function PANEL:SizeToContents()
self:SetSize( self:GetCanvas():GetSize() )
end
function PANEL:OnScroll( x, y )
self:GetCanvas():SetPos( x, y )
end
function PANEL:ScrollToChild( panel )
self:PerformLayout()
local x, y = self:GetCanvas():GetChildPosition( panel )
local w, h = panel:GetSize()
y = y + h * 0.5
x = x + w * 0.5
y = y - self:GetTall() * 0.5
x = x - self:GetWide() * 0.5
self:OnScroll( x, y )
end
function PANEL:PerformLayout()
local can = self:GetCanvas()
can:SizeToChildren( true, true )
-- Restrict movement to the size of this panel
local x, y, w, h = can:GetBounds()
if ( w > self:GetWide() ) then
if ( x > 0 ) then
can:SetPos( 0, y )
x = 0
end
--right
if ( x + w < self:GetWide() ) then
local nx = self:GetWide() - w
can:SetPos( nx, y )
x = nx
end
end
if ( w < self:GetWide() ) then
if ( x < 0 ) then
can:SetPos( 0, y )
x = 0
end
--left
if ( x + w > self:GetWide() ) then
local nx = self:GetWide() - w
can:SetPos( nx, y )
x = nx
end
end
if ( h > self:GetTall() ) then
if ( y > 0 ) then
can:SetPos( x, 0 )
y = 0
end
--up
if ( y + h < self:GetTall() ) then
local ny = self:GetTall() - h
can:SetPos( x, ny )
y = ny
end
end
if ( h < self:GetTall() ) then
if ( y < 0 ) then
can:SetPos( x, 0 )
y = 0
end
--down
if ( y + h > self:GetTall() ) then
local ny = self:GetTall() - h
can:SetPos( x, ny )
y = ny
end
end
end
function PANEL:Clear() return self:GetCanvas():Clear() end
function PANEL:GenerateExample( class, propsheet, width, height )
local dpan = vgui.Create( "Panel" )
dpan:Dock( FILL )
propsheet:AddSheet( class, dpan )
local dpl = vgui.Create( "DPanel", dpan )
dpl:SetSize( 200, 200 )
dpl:SetPos( 100, 100 )
local panl = vgui.Create( "DPanPanel", dpl )
panl:Dock( FILL )
local bl1 = panl:Add( "DButton" )
bl1:SetText( "Small" )
local bl2 = panl:Add( "DButton" )
bl2:SetPos( 100, 100 )
bl2:SetText( "Contents" )
local dpr = vgui.Create( "DPanel", dpan )
dpr:SetWide( width / 2 )
dpr:SetSize( 200, 200 )
dpr:SetPos( 310, 100 )
local panr = vgui.Create( "DPanPanel", dpr )
panr:Dock( FILL )
local br1 = panr:Add( "DButton" )
br1:SetText( "Big" )
local br2 = panr:Add( "DButton" )
br2:SetPos( 300, 300 )
br2:SetText( "Contents" )
end
derma.DefineControl( "DPanPanel", "", PANEL, "DPanel" )