aboutsummaryrefslogtreecommitdiff
path: root/gamemode/inventorysystem/shapedinventory/cl_shaped.lua
blob: dde523f73ca5097bdd5c97620f3f8e855d04cba4 (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
local com = nrequire("cl_common.lua")
local col = nrequire("config/colortheme.lua")

local inv = {}

local width = ScrW()
--local height = ScrH()
local iconsize = ((width / 4) - 20) / 5


local function default_paint(self,w,h)
	--Draw a box
	surface.SetDrawColor(col.ui.border)
	surface.DrawOutlinedRect( 0, 0, w, h )
	--xpcall(self.DrawModel,function() end,self)
end


--[[
local function calcposition(width,height,x,y)
	return (y * width) + x
end
]]

local function shapebounds(shape)
	local maxx = 0
	local maxy = 0
	for rn, row in pairs(shape) do
		if #row > maxx then maxx = #row end
		maxy = rn
	end
	return maxx, maxy
end

--Run a function on all the panels in a shape
local function runonshape(self,shape,x,y,func)
	for i = 1,#shape do
		for j = 1, #shape[i] do
			if shape[i][j] then
				func(self.gridpanels[x + i - 1][y + j - 1])
			end
		end
	end
end

--Draw the item in a position
local function drawitemat(self,x,y,item)
	print("Drawing item at ",x,y)
	local tp = self.gridpanels[x][y]
	if tp == nil then
		error("Unable to continue, could not find item at (" .. x .. "," .. y .. ")")
	end
	tp:Droppable("item")
	runonshape(self,item.Shape,x,y,function(panel)
		panel:SetVisible(false)
	end)
	tp:SetVisible(true)
	local sx,sy = shapebounds(item.Shape)
	tp:SetSize(iconsize * sx, iconsize * sy)
	tp:SetText(item.Name)
	tp.DoClick = function()
		if item.GetOptions then
			local dm = DermaMenu()
			com.CreateMenuFor(dm,item:GetOptions())
			dm:Open()
		end
	end
	if item.DoOnPanel then
		print("Calling cl_shaped's DoOnPanel")
		item:DoOnPanel(tp)
	end
	if item.Paint then
		tp.PaintOver = item.Paint
	end
end

--Reset the position, using the item and it's shape
local function undrawitemat(self,x,y)
	local item = self:Get({x,y})
	local dpn = self.gridpanels[x][y]
	dpn:Droppable("")
	dpn:SetText("")
	if dpn:GetModel() then
		dpn.Entity:Remove()
	end
	for k,v in pairs(dpn:GetChildren()) do v:Remove() end
	runonshape(self,item.Shape,x,y,function(panel)
		panel:SetVisible(true)
		panel:SetSize(iconsize,iconsize)
		panel.PaintOver = default_paint
		panel.DoClick = function() end
	end)
end

inv.DrawOnDPanel = function(self,panel)
	local DScrollPanel = vgui.Create( "DScrollPanel",panel)
	DScrollPanel:SetPos( 0, 0 )
	DScrollPanel:Dock(FILL)

	local grid = vgui.Create( "DGrid", DScrollPanel )
	grid:SetPos(0, 0)
	grid:SetCols(self.width)
	grid:SetColWide(iconsize)
	grid:SetRowHeight(iconsize)
	self.grid = grid

	self.gridpanels = {}

	--Create the full grid of dpanels
	for x = 1, self.width do
		for y = 1, self.height do
			local dp = vgui.Create("DModelPanel")
			function dp:LayoutEntity( Entity ) return end -- disables default rotation
			dp:SetSize(iconsize,iconsize)
			dp.PaintOver = default_paint
			dp:SetText("")
			--dp:Droppable("item")
			dp:Receiver("item",com.generatereceiver(),{"one","two","three"})
			dp.info = {}
			dp.info.owner = self.Owner
			dp.info.id = self.id
			dp.info.pos = {x,y}
			dp.info.inv = self
			grid:AddItem(dp)
			self.gridpanels[x] = self.gridpanels[x] or {}
			self.gridpanels[x][y] = dp
		end
	end

	--Go through the items, and set the dpanels appropriately.
	for k,v in pairs(self.tracker) do
		if type(v) == "table" then
			local px = k % self.width
			local py = math.ceil(k / self.height)
			drawitemat(self,px,py,v)
		end
	end

	local observer = {}
	observer.Put = function(obs,position,item)
		print("observer's put was called")
		drawitemat(self,position[1],position[2],item)
	end
	observer.Remove = function(obs,position)
		print("observer's remove was called")
		undrawitemat(self,position[1],position[2])
	end
	return observer
end

return inv