Hey guys, I'm working on a slot based inventory and I've gotten some progress but I'm screwing up somewhere along the lines of my logic.
I'm encountering a couple issues, one is that if a item is selected and I want to put it back in its same slot and disappears completely, two if I try to swap two items they just fuck up completely. I believe the 2 issues are tied together somehow. I'm having a severe brain block, I just can't figure out how to fix this.
Here's my code:
[lua]
local invItem = {}
function invItem:Init()
self.OutlineColor = Color(155,155,155,255)
self.BaseColor = Color(105,105,105,255)
self.TextColor = Color(30,144,255,255)
self.Text = ""
self.Font = "combine_small_1"
self.Border = 4
self.Entered = false
self.icon = false
self.iRef = self.icon
self.ghost = false
self:SetText("")
end
function invItem:SetBText(text)
self.Text = text
end
function invItem:SetFont(font)
self.Font = font
end
function invItem:SetOutlineColor(col)
self.OutlineColor = col
end
function invItem:SetBaseColor(col)
self.BaseColor = col
end
function invItem:SetTextColor(col)
self.TextColor = col
end
function invItem:SetItem(item)
if item then
if !self.icon then
self.iRef = item
self.icon = vgui.Create("ModelImage",self)
self.icon:SetSize(self:GetWide(),self:GetTall())
self.icon:SetModel(ITEMS[self.iRef].Model)
self.icon.OnMousePressed = function()
if !LocalPlayer().SelectedItem then
if self.icon:GetParent() then
self.icon:GetParent().ghost = true
self.icon:SetParent(nil)
self.icon:SetDrawOnTop(true)
LocalPlayer().SelectedItem = self
end
end
end
self.icon.OnCursorEntered = function() if self.icon:GetParent() then self.icon:GetParent().Entered = true end end
self.icon.OnCursorExited = function() if self.icon:GetParent() then self.icon:GetParent().Entered = false end end
end
else
if self.icon then
self.ghost = false
self.icon:Remove()
self.icon = false
self.iRef = false
print("REMOVED")
end
end
end
function invItem:GetGhost()
return self.ghost or false
end
function invItem:Think()
if self.ghost then
local x,y = gui.MousePos()
self.icon:SetPos(x - self.icon:GetWide()/2,y - self.icon:GetTall()/2)
end
end
function invItem:OnCursorEntered()
self.Entered = true
end
function invItem:OnCursorExited()
self.Entered = false
end
function invItem:OnMousePressed()
local it = LocalPlayer().SelectedItem
if it == self then
if self.icon then
self.ghost = false
self.icon:SetPos(self:GetPos())
self.icon:SetParent(self)
print("ICON SELECTED IS THE SLOT WE CLICKED")
end
else
if it then
if self.iRef then
it:SetItem(self.iRef)
self:SetItem(it.iRef)
it.ghost = false
print("SWAPPED")
LocalPlayer().SelectedItem = false
else
self:SetItem(it.iRef)
it:SetItem(false)
print("MOVED ITEM TO EMPTY SLOT")
LocalPlayer().SelectedItem = false
end
end
end
end
function invItem:Paint()
if self.Entered || self.ghost then
draw.RoundedBoxEx(1,0,0,self:GetWide(),self:GetTall(),self.TextColor)
end
draw.RoundedBoxEx(1,self.Border/2,self.Border/2,self:GetWide()-self.Border,self:GetTall()-self.Border,self.BaseColor)
end
vgui.Register("iSlot",invItem,"DButton")
[/lua]
This is probably terribly coded in some aspects but only because of my frustration I've been focusing on trying to fix this as much as I can.
Thanks for any help guys.