Skip to content

Commit

Permalink
support for container sorting, reorder buttons and include slots labe…
Browse files Browse the repository at this point in the history
…l as part of take all button
  • Loading branch information
zetaPRIME committed Aug 26, 2022
1 parent a75bbf9 commit 3943572
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 9 deletions.
16 changes: 8 additions & 8 deletions StardustUI/stardustui/ui/buildchest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ cfg.children = { { scissoring = false }, -- allow count to slightly overlap wind
grid,
{ { size = barHeight },
-1, -- tiny bit of space away from edge
{ type = "label", text = numSlots .. " slots" },
"spacer",
{ -- bar and label is an extension of the Take All button
{ id = "slotsBar", mode = "h", expandMode = {2, 0}, size = barHeight },
{ id = "slotsLabel", type = "label", text = numSlots .. " slots", expand = true },
{ id = "takeAll", type = "iconButton", image = "takeall.png", toolTip = "Take All" },
},
-3, { id = "quickStack", type = "iconButton", image = "quickstack.png", toolTip = "Quick Stack" },
-3, { id = "sort", type = "iconButton", image = "sort.png", toolTip = "Sort" },
hasES and -3 or { },
{ id = "esOptions", type = "iconButton", image = "minimenu.png", toolTip = "Container Options", visible = not not hasES },
-3, -- slightly less space
--{ id = "sort", type = "iconButton", image = "sort.png", toolTip = "Sort" }, -3,
{ id = "quickStack", type = "iconButton", image = "quickstack.png", toolTip = "Quick Stack" },
-3,
{ id = "takeAll", type = "iconButton", image = "takeall.png", toolTip = "Take All" },
--{ id = "takeAll", type = "button", caption = "Take All", size = 38, color = "accent" },
-1, -- same spacer on the end too
},
}
Expand Down
88 changes: 87 additions & 1 deletion StardustUI/stardustui/ui/chest.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
-- Stardust UI chest interface - runtime component

require "/lib/stardust/itemutil.lua"

local src = pane.sourceEntity()

function takeAll:onClick()
--local slotsBar = slotsLabel
slotsBar.state = "idle"
function slotsBar:isMouseInteractable() return true end
slotsBar.onMouseEnter = metagui.widgetTypes.button.onMouseEnter
slotsBar.onMouseLeave = metagui.widgetTypes.button.onMouseLeave
slotsBar.onMouseButtonEvent = metagui.widgetTypes.button.onMouseButtonEvent
slotsBar.onCaptureMouseMove = metagui.widgetTypes.button.onCaptureMouseMove
function slotsBar:queueRedraw()
takeAll.state = self.state
takeAll:queueRedraw()
if self.state == "idle" then
slotsLabel:setText(world.containerSize(src) .. " slots")
else
local txt = "Take All"
if self.state == "press" then txt = "^accent;" .. txt end
slotsLabel:setText(txt)
end
end

function slotsBar:onClick()
if not metagui.checkSync(true) then return end
local id = pane.sourceEntity()
local numSlots = world.containerSize(id)
Expand All @@ -11,6 +32,7 @@ function takeAll:onClick()
world.containerTakeAt(id, i)
end
end
function takeAll:isMouseInteractable() return false end

function quickStack:onClick()
if not metagui.checkSync(true) then return end
Expand Down Expand Up @@ -82,3 +104,67 @@ do -- mimic ES effect of activating retention on first open
local keep = world.getObjectParameter(src, "keepContent")
if keep == nil then world.sendEntityMessage(src, "keepContent", true) end
end

function getCompSort(lst)
return function(a, b)
local r
for _,f in pairs(lst) do
r = f(a,b)
if r ~= nil then return r end
end
return r
end
end

local rarityNum = {
common = 1, uncommon = 2, rare = 3, legendary = 4, essential = 100
}

local sc = { }
do
local function cmp(a, b)
if a < b then return true end
if a > b then return false end
return nil
end

function sc.rarity(a, b)
return cmp(b.rarity, a.rarity)
end
function sc.name(a, b)
return cmp(a.sortName, b.sortName)
end
function sc.count(a, b)
return cmp(b.itm.count, a.itm.count)
end
function sc.level(a, b)
return cmp(b.level, a.level)
end
function sc.rot(a, b)
return cmp(b.rot, a.rot)
end
end

function sort:onClick()
local id = pane.sourceEntity()
local itms = world.containerItems(id)

local inf = { }
for _,itm in pairs(itms) do -- assemble info table
local e = { itm = itm }
table.insert(inf, e)
e.rarity = rarityNum[string.lower(itemutil.property(itm, "rarity"))]
e.sortName = string.lower(string.gsub(itemutil.property(itm, "shortdescription"), "(^.-;)", ""))
e.level = itemutil.property(itm, "level") or 0
e.rot = itm.parameters.timeToRot or 0
end

local fcmp = getCompSort { sc.rarity, sc.level, sc.name, sc.rot, sc.count }
table.sort(inf, fcmp)

local fi = { }
for k,v in pairs(inf) do fi[k] = v.itm end

world.containerTakeAll(id)
for slot, it in pairs(fi) do world.containerSwapItems(id, it, slot-1) end
end

0 comments on commit 3943572

Please sign in to comment.