summaryrefslogtreecommitdiff
path: root/gamemode/vgui/vgui_panelsheet.lua
blob: 38771ed6160b35d9540ed41628c2ee57301f7877 (plain)
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
local PANEL = {}

function PANEL:Init()

	self:ChooseParent()
	self.Stashable = false
	self.StashStyle = "Stash"
	self.PriceScale = 1
	self.Categories = {}
	
	self:SetDraggableName( "GlobalDPanel" );

	self.pnlCanvas 	= vgui.Create( "DPanel", self )
	self.pnlCanvas:SetPaintBackground( false )
	self.pnlCanvas.OnMousePressed = function( self, code ) self:GetParent():OnMousePressed( code ) end
	self.pnlCanvas.OnChildRemoved = function() self:OnChildRemoved() end
	self.pnlCanvas:SetMouseInputEnabled( true )
	self.pnlCanvas.InvalidateLayout = function() self:InvalidateLayout() end
	
	self.Items = {}
	self.YOffset = 0
	self.m_fAnimTime = 0;
	self.m_fAnimEase = -1; -- means ease in out
	self.m_iBuilds = 0
	
	self:SetSpacing( 0 )
	self:SetPadding( 0 )
	self:EnableHorizontal( false )
	self:SetAutoSize( false )
	self:SetDrawBackground( true )
	//self:SetBottomUp( false )
	self:SetNoSizing( false )
	
	self:SetMouseInputEnabled( true )
	
	-- This turns off the engine drawing
	self:SetPaintBackgroundEnabled( false )
	self:SetPaintBorderEnabled( false )
	
	self.ScrollAmt = 0
	
end

function PANEL:ToggleVisible( tbl, bool )

	if bool then
	
		local newtbl = {}
	
		for k,v in pairs( self.Categories ) do // remove all of tbl from categories
		
			if not table.HasValue( tbl, v ) then
			
				table.insert( newtbl, v )
			
			end
		
		end
		
		self.Categories = newtbl
	
	else
	
		for k,v in pairs( tbl ) do // insert all of tbl into categories
		
			if not table.HasValue( self.Categories, v ) then
			
				table.insert( self.Categories, v )
			
			end
		
		end
	
	end

end

function PANEL:ChooseParent()
	
end

function PANEL:SetPriceScale( scale )

	self.PriceScale = scale

end

function PANEL:SetStashable( bool, style, localinv )

	self.Stashable = bool
	self.StashStyle = style
	self.IsLocalInv = localinv
	
	for k,v in pairs( self:GetItems() ) do
	
		v:SetPriceScale( self.PriceScale )
		v:SetStashable( bool, style )
	
	end

end

function PANEL:GetCash()

	return LocalPlayer():GetNWInt( "Cash", 0 )

end

function PANEL:HasItem( id )

	for k,v in pairs( self:GetItems() ) do
	
		if v:GetID() == id then
		
			return v
		
		end
	
	end

end

function PANEL:GetItemPnlSize() // determine the size of item panels, always needs to add up to 1.0

	--[[if self.StashStyle == "Buy" then
	
		return ( self:GetWide() * 0.2 )
	
	end]]

	return ( self:GetWide() * 0.25 ) 

end

function PANEL:IsBlacklisted( id )

	local tbl = item.GetByID( id )
	local cat = tbl.Type
	
	return table.HasValue( self.Categories, cat )

end

function PANEL:RefreshItems( tbl )

	self:Clear( true )
	
	for k,v in pairs( tbl ) do
	
		local pnl = self:HasItem( v )
	
		if pnl and pnl:IsStackable() then
		
			pnl:AddCount( 1 )
		
		elseif not self:IsBlacklisted( v ) then
		
			local pnl = vgui.Create( "ItemPanel" )
			pnl:SetItemTable( item.GetByID( v ) )
			pnl:SetCount( 1 )
			pnl:SetPriceScale( self.PriceScale )
			pnl:SetStashable( self.Stashable, self.StashStyle )
			pnl:SetSizeOverride( self:GetItemPnlSize() ) //bigg0r
		
			self:AddItem( pnl )
		
		end
		
	end
	
	if #tbl < 1 then
	
		self:InvalidateLayout()
	
	end
	
	if self.StashButton then
	
		self.StashButton:Remove()
		self.StashButton = nil
		
	end
	
	if self.CashBox then
	
		self.CashBox:Remove()
		self.CashBox = nil
	
	end
	
	if self.CashButton then
	
		self.CashButton:Remove()
		self.CashButton = nil
		
	end
	
	if self.StashStyle != "Buy" then
	
		self.CashBox = vgui.Create( "DNumberWang", self )
		self.CashBox:SetDecimals( 0 )
		self.CashBox:SetValue( math.max( self:GetCash(), 5 ) )
		self.CashBox:SetMinMax( 5, math.max( self:GetCash(), 5 ) )
		self.CashBox:SetWide( 80 )
		
		self.CashButton = vgui.Create( "DButton", self )
		
		if self.StashStyle == "Take" then
		
			self.CashButton:SetText( self.StashStyle )
			self.CashButton.OnMousePressed = function()
				
				RunConsoleCommand( "cash_take", math.min( tonumber( self.CashBox:GetValue() ) or 0, self:GetCash() ) )
				
			end
		
		elseif self.StashStyle == "Stash" and self.Stashable then
		
			self.CashButton:SetText( self.StashStyle )
			self.CashButton.OnMousePressed = function()
				
				RunConsoleCommand( "cash_stash", math.min( tonumber( self.CashBox:GetValue() ) or 0, self:GetCash() ) )
				
			end
		
		else
		
			self.CashButton:SetText( "Drop" )
			self.CashButton.OnMousePressed = function()
				
				RunConsoleCommand( "cash_drop", math.min( tonumber( self.CashBox:GetValue() ) or 0, self:GetCash() ) )
				
			end
		
		end
	
	end
	
	if ( self.StashStyle == "Stash" or self.StashStyle == "Take" ) and not self.IsLocalInv and #self:GetItems() > 0 then
		
		self.StashButton = vgui.Create( "DButton", self )
		self.StashButton:SetText( "Take All" )
		self.StashButton.OnMousePressed = function()
			
			if #self:GetItems() < 1 then return end

			for k,v in pairs( self:GetItems() ) do
				
				RunConsoleCommand( "inv_take", v:GetID(), v:GetCount() )
				
			end
		
		end
	
	end

end

function PANEL:Think()

	if self.CashBox then

		if self:GetCash() < 5 then
	
			self.CashBox:SetMinMax( 5, 5 )
			self.CashBox:SetValue( 5 )
			self.CashButton:SetDisabled( true )
			
		else
		
			self.CashBox:SetMinMax( 5, math.max( self:GetCash(), 5 ) )
			self.CashButton:SetDisabled( false )
		
		end
		
	end

end

function PANEL:AddScroll( amt )
	
	self.ScrollAmt = self.ScrollAmt + amt
	
	self.pnlCanvas:SetPos( 0, self.ScrollAmt * self:GetItemPnlSize() )

end

function PANEL:OnVScroll( offset )

	self.pnlCanvas:SetPos( 0, offset )

end

function PANEL:PerformLayout()

	local wide = self:GetWide()
	
	if ( !self.Rebuild ) then
		debug.Trace()
	end
	
	self:Rebuild()

	self.pnlCanvas:SetPos( 0, self.ScrollAmt * self:GetItemPnlSize() )
	self.pnlCanvas:SetWide( wide )
	
	self:Rebuild()

end

function PANEL:Rebuild()

	local Offset = 0
	
	if ( self.Horizontal ) then
	
		local x, y = self.Padding, self.Padding
		
		for k, panel in pairs( self.Items ) do
		
			if ( panel:IsVisible() ) then
			
				local w = self:GetItemPnlSize()
				local h = self:GetItemPnlSize()
				
				if ( x + w  > self:GetWide() ) then // move down
				
					x = self.Padding
					y = y + h + self.Spacing
				
				end
				
				panel:SetPos( x, y )
				
				x = x + w + self.Spacing
				Offset = y + h + self.Spacing
			
			end
		
		end
	
	else
	
		for k, panel in pairs( self.Items ) do
		
			if ( panel:IsVisible() ) then
				
				if ( self.m_bNoSizing ) then
					panel:SizeToContents()
					panel:SetPos( (self:GetCanvas():GetWide() - panel:GetWide()) * 0.5, self.Padding + Offset )
				else
					panel:SetSize( self:GetCanvas():GetWide() - self.Padding * 2, panel:GetTall() )
					panel:SetPos( self.Padding, self.Padding + Offset )
				end
		
				panel:InvalidateLayout( true )
				
				Offset = Offset + panel:GetTall() + self.Spacing
				
			end
		
		end
		
		Offset = Offset + self.Padding
		
	end
	
	self:GetCanvas():SetTall( self:GetTall() + Offset - self.Spacing ) 

	if ( self.m_bNoSizing and self:GetCanvas():GetTall() < self:GetTall() ) then

		self:GetCanvas():SetPos( 0, (self:GetTall()-self:GetCanvas():GetTall()) * 0.5 )
	
	end
	
	if self.StashButton then
	
		self.StashButton:SetSize( 48, 20 )
		self.StashButton:SetPos( self:GetWide() - self:GetPadding() * 2 - self.StashButton:GetWide(), self:GetTall() - self:GetPadding() * 2 - self.StashButton:GetTall() )
	
	end
	
	if self.CashBox then
	
		self.CashBox:SetPos( self:GetPadding() * 2, self:GetTall() - ( self:GetPadding() * 2 ) - 20 )
	
	end
	
	if self.CashButton then
	
		self.CashButton:SetSize( 48, 20 )
		self.CashButton:SetPos( ( self:GetPadding() * 2 ) + 5 + self.CashBox:GetWide(), self:GetTall() - ( self:GetPadding() * 2 ) - 20 )
	
	end
	
end

function PANEL:Paint()

	//draw.RoundedBox( 4, 0, 0, self:GetWide(), self:GetTall(), Color( 0, 0, 0, 255 ) )
	//draw.RoundedBox( 4, 1, 1, self:GetWide() - 2, self:GetTall() - 2, Color( 150, 150, 150, 100 ) )
	
	draw.RoundedBox( 4, 0, 0, self:GetWide(), self:GetTall(), Color( 0, 0, 0, 180 ) )
	
	if self.StashStyle == "Buy" then return end
	
	draw.SimpleText( "Cash: $" .. self:GetCash(), "ItemDisplayFont", self:GetPadding() * 2, self:GetTall() - ( self:GetPadding() * 2 ) - 35, Color( 255, 255, 255 ), TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT )
	
	//draw.TexturedQuad( { texture = surface.GetTextureID( "radbox/menu_trade" ), x = self:GetPadding() * 2, y = self:GetTall() - ( self:GetPadding() * 2 ) - 40, w = 40, h = 40, color = Color( 200, 200, 200 ) } )

end

derma.DefineControl( "ItemSheet", "A sheet for storing HUD elements", PANEL, "DPanelList" )