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) 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 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) drawitemat(self,position[1],position[2],item) end observer.Remove = function(obs,position) undrawitemat(self,position[1],position[2]) end return observer end return inv