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

local inv = {}

local width = ScrW()
--local height = ScrH()
local iconsize = width / 40

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

--[[
local function calcposition(dimx,dimy,x,y)
	return (y * dimx) + 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
				print("Running on ",i,j)
				print("Which translates to", x + j, y + i)
				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]
	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.DoClick = function()
		if item.GetOptions then
			com.CreateMenuFor(tp,item:GetOptions())
		end
	end
end

--Reset the position, using the item and it's shape
local function undrawitemat(self,x,y)
	local item = self:Get({x,y})
	self.gridpanels[x][y]:Droppable("")
	runonshape(self,item.Shape,x,y,function(panel)
		panel:SetVisible(true)
		panel:SetSize(iconsize,iconsize)
		panel.Paint = default_paint
		panel.DoClick = function() end
	end)
end

--[[
local function generatereceiver(tinv,x,y)
	return function(self,panels,dropped,index,cx,cy)
		if dropped then
			local froment,toent = panels[1].info.owner,self.info.owner
			local fromid,toid = panels[1].info.id,self.info.id
			local frompos,topos = panels[1].info.pos,self.info.pos
			local frominv,toinv = panels[1].info.inv,self.info.inv
			print("Something was dropped on:",x,y)
			PrintTable(panels)
			print("froment:",froment)
			print("toent:",toent)
			print("fromid",fromid)
			print("toid",toid)
			print("frompos:",frompos)
			PrintTable(panels[1].info.pos)
			print("topos:",topos)
			PrintTable(self.info.pos)
			print("frominv",frominv)
			print("toinv",toinv)
			local item = frominv:Get(frompos)
			print("item was", item)
			--Fake remove the item, in case the position we want to move it to overlaps with where it is now.
			frominv:Remove(frompos)
			local cf = toinv:CanFitIn(topos,item)
			frominv:Put(frompos,item)
			print("canfit was:",cf)
			if cf == true then
				--Send the request
				net.Start("art_RequestInvMove")
				net.WriteEntity(froment)
				net.WriteEntity(toent)
				net.WriteUInt(fromid,32)
				net.WriteUInt(toid,32)
				net.WriteTable(frompos)
				net.WriteTable(topos)
				net.SendToServer()
			end
		end
	end
end
]]

inv.DrawOnDPanel = function(self,panel)
	print("Drawing shaped on 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.dimx)
	grid:SetColWide(iconsize)
	grid:SetRowHeight(iconsize)
	self.grid = grid

	self.gridpanels = {}

	--Create the full grid of dpanels
	for x = 1, self.dimx do
		for y = 1, self.dimy do
			local dp = vgui.Create("DButton")
			dp:SetSize(iconsize,iconsize)
			dp.Paint = default_paint
			--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.dimx
			local py = math.floor(k / self.dimy)
			drawitemat(self,px,py,v)
		end
	end

	local observer = {}
	observer.Put = function(obs,position,item)
		print("Drawing item at",position[1],position[2])
		drawitemat(self,position[1],position[2],item)
	end
	observer.Remove = function(obs,position)
		print("Undrawing item at",position[1],position[2])
		undrawitemat(self,position[1],position[2])
	end
	return observer
end

return inv