Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
cl_inventory and cl_dev_icon
- Removed code duplication
- Removed unneeded code indent
- Negated ifs in hook to remove indents
- Removed some trailing spaces in cl_dev_icon
  • Loading branch information
StarLight-Oliver committed Jul 3, 2022
commit 2717464976255becacd793eb3376c2ce5d317c87
14 changes: 7 additions & 7 deletions gamemode/core/derma/cl_dev_icon.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function PANEL:Init()

function self.model:LayoutEntity()
end

self:AdjustSize(ICON_INFO.w, ICON_INFO.h)
end

Expand Down Expand Up @@ -81,7 +81,7 @@ function PANEL:Init()
surface.SetDrawColor(255, 255, 255)
surface.DrawOutlinedRect(0, 0, w, h)
end

self.model.Icon:SetVisible(false)
self.model.Paint = function(self, x, y)
local exIcon = ikon:getIcon("iconEditor")
Expand Down Expand Up @@ -114,7 +114,7 @@ local function buildActionText(self, setModel)

local p = self.prev2
local icon = p.model

local iconModel = p1.model
if (not iconModel) then return end

Expand Down Expand Up @@ -255,7 +255,7 @@ function PANEL:Init()

local cfg = self.list:Add("DNumSlider")
cfg:Dock(TOP)
cfg:SetText("W")
cfg:SetText("W")
cfg:SetMin(0)
cfg:SetMax(10)
cfg:SetDecimals(0)
Expand All @@ -269,7 +269,7 @@ function PANEL:Init()

local cfg = self.list:Add("DNumSlider")
cfg:Dock(TOP)
cfg:SetText("H")
cfg:SetText("H")
cfg:SetMin(0)
cfg:SetMax(10)
cfg:SetDecimals(0)
Expand All @@ -285,7 +285,7 @@ function PANEL:Init()

self.camFOV = self.list:Add("DNumSlider")
self.camFOV:Dock(TOP)
self.camFOV:SetText("CAMFOV")
self.camFOV:SetText("CAMFOV")
self.camFOV:SetMin(0)
self.camFOV:SetMax(180)
self.camFOV:SetDecimals(3)
Expand Down Expand Up @@ -361,7 +361,7 @@ function PANEL:Init()
end

local aaoa = self.list:Add("DPanel")
aaoa:Dock(TOP)
aaoa:Dock(TOP)
aaoa:DockMargin(10, 0, 0, 5)
aaoa:SetHeight(250)

Expand Down
243 changes: 101 additions & 142 deletions gamemode/core/derma/cl_inventory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,33 @@ function PANEL:Paint(w, h)
self:ExtraPaint(w, h)
end

local buildActionFunc = function(action, actionIndex, itemTable, invID, sub)
return function()
itemTable.player = LocalPlayer()
local send = true

if (action.onClick) then
send = action.onClick(itemTable, sub and sub.data)
end

local snd = action.sound or SOUND_INVENTORY_INTERACT
if (snd) then
if (istable(snd)) then
LocalPlayer():EmitSound(unpack(snd))
elseif (isstring(snd)) then
surface.PlaySound(snd)
end
end

if (send ~= false) then
netstream.Start("invAct", actionIndex, itemTable.id, invID, sub and sub.data)
end
itemTable.player = nil
end
end



function PANEL:openActionMenu()
local itemTable = self.itemTable

Expand All @@ -151,28 +178,8 @@ function PANEL:openActionMenu()
-- TODO: refactor custom menu options as a method for items
if (v.isMulti) then
local subMenu, subMenuOption =
menu:AddSubMenu(L(v.name or k), function()
itemTable.player = LocalPlayer()
local send = true

if (v.onClick) then
send = v.onClick(itemTable)
end

local snd = v.sound or SOUND_INVENTORY_INTERACT
if (snd) then
if (istable(snd)) then
LocalPlayer():EmitSound(unpack(snd))
elseif (isstring(snd)) then
surface.PlaySound(snd)
end
end

if (send ~= false) then
netstream.Start("invAct", k, itemTable.id, self.invID)
end
itemTable.player = nil
end)
menu:AddSubMenu(L(v.name or k), buildActionFunc(v, k, itemTable, self.invID))
)
subMenuOption:SetImage(v.icon or "icon16/brick.png")

if (not v.multiOptions) then return end
Expand All @@ -181,59 +188,12 @@ function PANEL:openActionMenu()
and v.multiOptions(itemTable, LocalPlayer())
or v.multiOptions
for _, sub in pairs(options) do
subMenu:AddOption(L(sub.name or "subOption"), function()
itemTable.player = LocalPlayer()
local send = true

if (v.onClick) then
send = v.onClick(itemTable, sub.data)
end

local snd = v.sound or SOUND_INVENTORY_INTERACT
if (snd) then
if (type(snd) == 'table') then
LocalPlayer():EmitSound(unpack(snd))
elseif (type(snd) == 'string') then
surface.PlaySound(snd)
end
end

if (send ~= false) then
netstream.Start(
"invAct",
k,
itemTable.id,
self.invID,
sub.data
)
end
itemTable.player = nil
end)
subMenu:AddOption(L(sub.name or "subOption"), buildActionFunc(v, k, itemTable, self.invID, sub))
:SetImage(sub.icon or "icon16/brick.png")
end
else
menu:AddOption(L(v.name or k), function()
-- TODO: refactor this action click function
itemTable.player = LocalPlayer()
local send = true

if (v.onClick) then
send = v.onClick(itemTable)
end

local snd = v.sound or SOUND_INVENTORY_INTERACT
if (snd) then
if (istable(snd)) then
LocalPlayer():EmitSound(unpack(snd))
elseif (isstring(snd)) then
surface.PlaySound(snd)
end
end

if (send ~= false) then
netstream.Start("invAct", k, itemTable.id, self.invID)
end
itemTable.player = nil
end):SetImage(v.icon or "icon16/brick.png")
menu:AddOption(L(v.name or k), buildActionFunc(v, k, itemTable, self.invID))
:SetImage(v.icon or "icon16/brick.png")
end
end

Expand All @@ -244,96 +204,95 @@ end
vgui.Register("nutItemIcon", PANEL, "SpawnIcon")

PANEL = {}
function PANEL:Init()
self:MakePopup()
self:Center()
self:ShowCloseButton(false)
self:SetDraggable(true)
self:SetTitle(L"inv")
end
function PANEL:Init()
self:MakePopup()
self:Center()
self:ShowCloseButton(false)
self:SetDraggable(true)
self:SetTitle(L"inv")
end

-- Sets which inventory this panel is representing.
function PANEL:setInventory(inventory)
self.inventory = inventory
self:nutListenForInventoryChanges(inventory)
end
-- Sets which inventory this panel is representing.
function PANEL:setInventory(inventory)
self.inventory = inventory
self:nutListenForInventoryChanges(inventory)
end

-- Called when the data for the local inventory has been initialized.
-- This shouldn't run unless the inventory got resync'd.
function PANEL:InventoryInitialized()
end
-- Called when the data for the local inventory has been initialized.
-- This shouldn't run unless the inventory got resync'd.
function PANEL:InventoryInitialized()
end

-- Called when a data value has been changed for the inventory.
function PANEL:InventoryDataChanged(key, oldValue, newValue)
end
-- Called when a data value has been changed for the inventory.
function PANEL:InventoryDataChanged(key, oldValue, newValue)
end

-- Called when the inventory for this panel has been deleted. This may
-- be because the local player no longer has access to the inventory!
function PANEL:InventoryDeleted(inventory)
if (self.inventory == inventory) then
self:Remove()
end
-- Called when the inventory for this panel has been deleted. This may
-- be because the local player no longer has access to the inventory!
function PANEL:InventoryDeleted(inventory)
if (self.inventory == inventory) then
self:Remove()
end
end

-- Called when the given item has been added to the inventory.
function PANEL:InventoryItemAdded(item)
end
-- Called when the given item has been added to the inventory.
function PANEL:InventoryItemAdded(item)
end

-- Called when the given item has been removed from the inventory.
function PANEL:InventoryItemRemoved(item)
end
-- Called when the given item has been removed from the inventory.
function PANEL:InventoryItemRemoved(item)
end

-- Called when an item within this inventory has its data changed.
function PANEL:InventoryItemDataChanged(item, key, oldValue, newValue)
end
-- Called when an item within this inventory has its data changed.
function PANEL:InventoryItemDataChanged(item, key, oldValue, newValue)
end

-- Make sure to clean up hooks before removing the panel.
function PANEL:OnRemove()
self:nutDeleteInventoryHooks()
end
-- Make sure to clean up hooks before removing the panel.
function PANEL:OnRemove()
self:nutDeleteInventoryHooks()
end
vgui.Register("nutInventory", PANEL, "DFrame")

local margin = 10
hook.Add("CreateMenuButtons", "nutInventory", function(tabs)
if (hook.Run("CanPlayerViewInventory") ~= false) then
tabs["inv"] = function(panel)
local inventory = LocalPlayer():getChar():getInv()
if (hook.Run("CanPlayerViewInventory") == false) then return end

if (inventory) then
local mainPanel = inventory:show(panel)
tabs["inv"] = function(panel)
local inventory = LocalPlayer():getChar():getInv()

local sortPanels = {}
local totalSize = {x = 0, y = 0, p = 0}
table.insert(sortPanels, mainPanel)
if (not inventory) then return end
local mainPanel = inventory:show(panel)

totalSize.x = totalSize.x + mainPanel:GetWide() + margin
totalSize.y = math.max(totalSize.y, mainPanel:GetTall())
local sortPanels = {}
local totalSize = {x = 0, y = 0, p = 0}
table.insert(sortPanels, mainPanel)

for id, item in pairs(inventory:getItems()) do
if (item.isBag and hook.Run("CanOpenBagPanel", item) ~= false) then
local inventory = item:getInv()
totalSize.x = totalSize.x + mainPanel:GetWide() + margin
totalSize.y = math.max(totalSize.y, mainPanel:GetTall())

local childPanels = inventory:show(mainPanel)
nut.gui["inv"..inventory:getID()] = childPanels
table.insert(sortPanels, childPanels)
for id, item in pairs(inventory:getItems()) do
if (item.isBag and hook.Run("CanOpenBagPanel", item) ~= false) then
local inventory = item:getInv()

totalSize.x = totalSize.x + childPanels:GetWide() + margin
totalSize.y = math.max(totalSize.y, childPanels:GetTall())
end
end
local childPanels = inventory:show(mainPanel)
nut.gui["inv"..inventory:getID()] = childPanels
table.insert(sortPanels, childPanels)

local px, py, pw, ph = mainPanel:GetBounds()
local x, y = px + pw/2 - totalSize.x / 2, py + ph/2
for _, panel in pairs(sortPanels) do
panel:ShowCloseButton(true)
panel:SetPos(x, y - panel:GetTall()/2)
x = x + panel:GetWide() + margin
end

hook.Add("PostRenderVGUI", mainPanel, function()
hook.Run("PostDrawInventory", mainPanel)
end)
totalSize.x = totalSize.x + childPanels:GetWide() + margin
totalSize.y = math.max(totalSize.y, childPanels:GetTall())
end
end

local px, py, pw, ph = mainPanel:GetBounds()
local x, y = px + pw/2 - totalSize.x / 2, py + ph/2
for _, panel in pairs(sortPanels) do
panel:ShowCloseButton(true)
panel:SetPos(x, y - panel:GetTall()/2)
x = x + panel:GetWide() + margin
end

hook.Add("PostRenderVGUI", mainPanel, function()
hook.Run("PostDrawInventory", mainPanel)
end)
end
end)