Compare commits
No commits in common. "3f0487bb4832387dbb669a007b67fe97fc3da9ea" and "3951f7cbd4f113d740ff73f10371ccb01c86ecee" have entirely different histories.
3f0487bb48
...
3951f7cbd4
@ -1,4 +1,4 @@
|
|||||||
local Event = require("gfx.event")
|
local Event = require("event")
|
||||||
|
|
||||||
local Element = {
|
local Element = {
|
||||||
x = 1,
|
x = 1,
|
||||||
@ -31,15 +31,11 @@ function Element:new(o)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Element:draw()
|
function Element:draw()
|
||||||
local dirty = self:_isDirty()
|
local win = self:_getWindow()
|
||||||
if dirty then
|
-- Calling draw() without a valid window is a logical error
|
||||||
local win = self:_getWindow()
|
---@diagnostic disable-next-line: need-check-nil
|
||||||
-- Calling draw() without a valid window is a logical error
|
win.setCursorPos(1, 1)
|
||||||
---@diagnostic disable-next-line: need-check-nil
|
|
||||||
win.setCursorPos(1, 1)
|
|
||||||
end
|
|
||||||
self.dirty = false
|
self.dirty = false
|
||||||
return dirty
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function Element:getX()
|
function Element:getX()
|
||||||
@ -80,7 +76,6 @@ function Element:setParent(parent)
|
|||||||
if self.parent ~= parent then
|
if self.parent ~= parent then
|
||||||
self:setDirty()
|
self:setDirty()
|
||||||
self.parent = parent
|
self.parent = parent
|
||||||
self:_reload()
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -120,10 +115,6 @@ function Element:setVisible(visible)
|
|||||||
self:_getWindow().setVisible(visible)
|
self:_getWindow().setVisible(visible)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Element:_isDirty()
|
|
||||||
return self.dirty
|
|
||||||
end
|
|
||||||
|
|
||||||
function Element:resize(opts)
|
function Element:resize(opts)
|
||||||
if (opts.width ~= nil and self.x ~= opts.width) or (opts.height ~= nil and self.y ~= opts.height) then
|
if (opts.width ~= nil and self.x ~= opts.width) or (opts.height ~= nil and self.y ~= opts.height) then
|
||||||
self:setDirty()
|
self:setDirty()
|
||||||
@ -198,12 +189,10 @@ function Element:setOnClick(onClick)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Element:_reload()
|
function Element:_reload()
|
||||||
if self.parent ~= nil then
|
local window = window.create(self.parent, self:getX(), self:getY(), self:getWidth(), self:getHeight(), self:isVisible())
|
||||||
local window = window.create(self.parent, self:getX(), self:getY(), self:getWidth(), self:getHeight(), self:isVisible())
|
window.setBackgroundColor(self:getBgColor())
|
||||||
window.setBackgroundColor(self:getBgColor())
|
window.setTextColor(self:getFgColor())
|
||||||
window.setTextColor(self:getFgColor())
|
self:_setWindow(window)
|
||||||
self:_setWindow(window)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return Element
|
return Element
|
52
gfx/list.lua
52
gfx/list.lua
@ -1,4 +1,4 @@
|
|||||||
local Element = require("gfx.element")
|
local Element = require("element")
|
||||||
local List = Element:new{
|
local List = Element:new{
|
||||||
children = {},
|
children = {},
|
||||||
vertical = false
|
vertical = false
|
||||||
@ -21,22 +21,9 @@ end
|
|||||||
function List:insertChild(child, atIndex)
|
function List:insertChild(child, atIndex)
|
||||||
local index = math.min(math.max(1, atIndex or #self.children), #self.children)
|
local index = math.min(math.max(1, atIndex or #self.children), #self.children)
|
||||||
table.insert(self.children, index, child)
|
table.insert(self.children, index, child)
|
||||||
|
|
||||||
-- Update window references
|
|
||||||
self:_reload()
|
|
||||||
|
|
||||||
-- Update render positions
|
|
||||||
adjustPositions(self.children, self:isVertical(), index)
|
adjustPositions(self.children, self:isVertical(), index)
|
||||||
end
|
end
|
||||||
|
|
||||||
function List:setParent(parent)
|
|
||||||
Element.setParent(self, parent)
|
|
||||||
local win = self:_getWindow()
|
|
||||||
for _,child in ipairs(self.children) do
|
|
||||||
child:setParent(win)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function List:removeChild(child)
|
function List:removeChild(child)
|
||||||
local index
|
local index
|
||||||
local searchType = type(child)
|
local searchType = type(child)
|
||||||
@ -64,7 +51,6 @@ function List:removeChild(child)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local removed = table.remove(self.children, index)
|
local removed = table.remove(self.children, index)
|
||||||
self:_reload()
|
|
||||||
if index <= #self.children then
|
if index <= #self.children then
|
||||||
adjustPositions(self.children, self:isVertical(), index)
|
adjustPositions(self.children, self:isVertical(), index)
|
||||||
end
|
end
|
||||||
@ -81,14 +67,11 @@ function List:isHorizontal()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function List:draw()
|
function List:draw()
|
||||||
local dirty = Element.draw(self)
|
Element.draw(self)
|
||||||
if dirty then
|
self:_getWindow().clear()
|
||||||
self:_getWindow().clear()
|
for _,v in ipairs(self.children) do
|
||||||
for _,child in ipairs(self.children) do
|
v:draw()
|
||||||
child:draw()
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
return dirty
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function List:getHeight()
|
function List:getHeight()
|
||||||
@ -122,29 +105,4 @@ function List:findById(id)
|
|||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
function List:_isDirty()
|
|
||||||
if Element._isDirty(self) then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
for _,child in ipairs(self.childrent) do
|
|
||||||
if child:_isDirty() then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
function List:_reload()
|
|
||||||
Element._reload(self)
|
|
||||||
|
|
||||||
-- Reload child windows
|
|
||||||
local win = self:_getWindow()
|
|
||||||
for _,child in ipairs(self.children) do
|
|
||||||
if child:_getWindow() ~= win then
|
|
||||||
child:setParent(win)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return List
|
return List
|
@ -1,5 +1,5 @@
|
|||||||
local Event = require("gfx.event")
|
local Event = require("event")
|
||||||
local Element = require("gfx.element")
|
local Element = require("element")
|
||||||
local Padding = Element:new{
|
local Padding = Element:new{
|
||||||
left = 0,
|
left = 0,
|
||||||
right = 0,
|
right = 0,
|
||||||
@ -8,17 +8,6 @@ local Padding = Element:new{
|
|||||||
element = nil
|
element = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
function Padding:new(opts)
|
|
||||||
local obj = Element.new(self, opts)
|
|
||||||
obj.element:setPos(obj:getPaddingLeft(), obj:getPaddingTop())
|
|
||||||
obj:resize{
|
|
||||||
width = obj:getWidth(),
|
|
||||||
height = obj:getHeight()
|
|
||||||
}
|
|
||||||
obj.element:setParent(obj:_getWindow())
|
|
||||||
return obj
|
|
||||||
end
|
|
||||||
|
|
||||||
function Padding:resize(opts)
|
function Padding:resize(opts)
|
||||||
-- Un-pad dimensions and pass to child element
|
-- Un-pad dimensions and pass to child element
|
||||||
return self.element:resize{
|
return self.element:resize{
|
||||||
@ -27,16 +16,6 @@ function Padding:resize(opts)
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
function Padding:draw()
|
|
||||||
if Element.draw(self) or self.element:_isDirty() then
|
|
||||||
local win = self:_getWindow()
|
|
||||||
win.clear()
|
|
||||||
self.element:draw()
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
function Padding:getPaddingLeft()
|
function Padding:getPaddingLeft()
|
||||||
return self.left
|
return self.left
|
||||||
end
|
end
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
local Element = require("gfx.element")
|
local Element = require("element")
|
||||||
local Text = Element:new{ text = "" }
|
local Text = Element:new{ text = "" }
|
||||||
|
|
||||||
function Text:new(o)
|
function Text:new(o)
|
||||||
@ -26,11 +26,8 @@ function Text:getText()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Text:draw()
|
function Text:draw()
|
||||||
local dirty = Element.draw(self)
|
Element.draw(self)
|
||||||
if dirty then
|
self:_getWindow().write(self:getText())
|
||||||
self:_getWindow().write(self:getText())
|
|
||||||
end
|
|
||||||
return dirty
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function Text:getHeight()
|
function Text:getHeight()
|
||||||
|
@ -1,146 +0,0 @@
|
|||||||
local Chest = require("storage.chest")
|
|
||||||
local Storage = require("storage")
|
|
||||||
local Text = require("gfx.text")
|
|
||||||
local List = require("gfx.list")
|
|
||||||
local Padding = require("gfx.padding")
|
|
||||||
|
|
||||||
|
|
||||||
local CACHE_FILE = ".storage.cache"
|
|
||||||
local LOCK_FILE = ".storage.lock"
|
|
||||||
|
|
||||||
local function lock()
|
|
||||||
if fs.exists(LOCK_FILE) then
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Create lock file
|
|
||||||
return fs.open(LOCK_FILE, "w+").close()
|
|
||||||
end
|
|
||||||
|
|
||||||
local function unlock()
|
|
||||||
if not fs.exists(LOCK_FILE) then
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Delete lock file
|
|
||||||
local result, reason = pcall(fs.delete, LOCK_FILE)
|
|
||||||
if not result then
|
|
||||||
print("ERROR: Lock file could not be deleted: " .. reason)
|
|
||||||
end
|
|
||||||
|
|
||||||
return result
|
|
||||||
end
|
|
||||||
|
|
||||||
local function isLocked()
|
|
||||||
return fs.exists(LOCK_FILE)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function saveState(fname, ctrl)
|
|
||||||
local ser = ctrl:toSerializable()
|
|
||||||
local file = fs.open(fname, "w+")
|
|
||||||
file.write(textutils.serialize(ser))
|
|
||||||
file.close()
|
|
||||||
end
|
|
||||||
|
|
||||||
local function loadState(fname, node)
|
|
||||||
local controller = nil
|
|
||||||
if not isLocked() then
|
|
||||||
local file = fs.open(fname, "r")
|
|
||||||
if file ~= nil then
|
|
||||||
local ser = textutils.unserialize(file.readAll())
|
|
||||||
file.close()
|
|
||||||
return Storage:fromSerializable(ser)
|
|
||||||
end
|
|
||||||
controller = loadState(CACHE_FILE)
|
|
||||||
end
|
|
||||||
|
|
||||||
if controller == nil then
|
|
||||||
local nodeName = peripheral.getName(node)
|
|
||||||
local storageChests = {peripheral.find("inventory")}
|
|
||||||
for i,v in ipairs(storageChests) do
|
|
||||||
if peripheral.getName(v) == nodeName then
|
|
||||||
table.remove(storageChests, i)
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
controller = Storage:fromPeripherals(Storage.assumeHomogeneous(storageChests))
|
|
||||||
saveState(controller, CACHE_FILE)
|
|
||||||
end
|
|
||||||
return controller
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local accessNode = Chest:fromPeripheral("minecraft:trapped_chest")
|
|
||||||
local controller = loadState(CACHE_FILE, accessNode)
|
|
||||||
local monitor = peripheral.find("monitor")
|
|
||||||
|
|
||||||
local width, height = monitor.getSize()
|
|
||||||
|
|
||||||
|
|
||||||
local function itemList(stacks, wBudget)
|
|
||||||
local bgColors = {
|
|
||||||
colors.gray,
|
|
||||||
colors.black
|
|
||||||
}
|
|
||||||
local entries = {}
|
|
||||||
|
|
||||||
for i=1,#stacks do
|
|
||||||
local stack = stacks[i]
|
|
||||||
local text = stack:getDisplayName()
|
|
||||||
local count = tostring(stack:getCount())
|
|
||||||
|
|
||||||
-- Fit text inside of width budget
|
|
||||||
local countLen = #count
|
|
||||||
if countLen + 2 > wBudget then
|
|
||||||
error("Width budget is too small")
|
|
||||||
end
|
|
||||||
|
|
||||||
local textBudget = wBudget - 2 - countLen
|
|
||||||
if #text > textBudget then
|
|
||||||
-- Truncate to available budget
|
|
||||||
text = text:sub(1,textBudget)
|
|
||||||
end
|
|
||||||
|
|
||||||
local textLabel = Text:new{ text = text, bgColor = bgColors[i % 2] }
|
|
||||||
local countLabel = Text:new{ text = count, bgColor = bgColors[i % 2] }
|
|
||||||
local paddedText = Padding:new{
|
|
||||||
left = 0,
|
|
||||||
right = wBudget - #count - #text,
|
|
||||||
top = 0,
|
|
||||||
bottom = 0,
|
|
||||||
bgColor = bgColors[i % 2],
|
|
||||||
element = textLabel
|
|
||||||
}
|
|
||||||
|
|
||||||
local list = List:new{
|
|
||||||
children = {
|
|
||||||
paddedText,
|
|
||||||
countLabel
|
|
||||||
},
|
|
||||||
vertical = false,
|
|
||||||
onClick = function(table, x, y, source)
|
|
||||||
print("Clicked: "..stack:getDisplayName())
|
|
||||||
end
|
|
||||||
}
|
|
||||||
table.insert(entries, list)
|
|
||||||
end
|
|
||||||
|
|
||||||
return List:new{
|
|
||||||
children = entries,
|
|
||||||
vertical = true
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
local count = 0
|
|
||||||
local found = controller:find(function(stack)
|
|
||||||
local match = not stack:isEmpty()
|
|
||||||
if match then
|
|
||||||
count = count + 1
|
|
||||||
end
|
|
||||||
return match and count <= 10
|
|
||||||
end)
|
|
||||||
|
|
||||||
local listResult = itemList(found, width)
|
|
||||||
|
|
||||||
listResult:setParent(monitor)
|
|
||||||
listResult:draw()
|
|
@ -16,7 +16,6 @@ function ItemStack:fromDetail(inv, detail, slot)
|
|||||||
obj.count = detail.count
|
obj.count = detail.count
|
||||||
obj.maxCount = detail.maxCount
|
obj.maxCount = detail.maxCount
|
||||||
obj.enchantments = detail.enchantments
|
obj.enchantments = detail.enchantments
|
||||||
obj.displayName = detail.displayName
|
|
||||||
end
|
end
|
||||||
|
|
||||||
setmetatable(obj, self)
|
setmetatable(obj, self)
|
||||||
@ -34,8 +33,7 @@ function ItemStack:clone(withSlot)
|
|||||||
maxDamage = self.maxDamage,
|
maxDamage = self.maxDamage,
|
||||||
count = self.count,
|
count = self.count,
|
||||||
maxCount = self.maxCount,
|
maxCount = self.maxCount,
|
||||||
enchantments = self.enchantments,
|
enchantments = self.enchantments
|
||||||
displayName = self.displayName
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setmetatable(obj, self)
|
setmetatable(obj, self)
|
||||||
@ -57,7 +55,6 @@ function ItemStack:toSerializable()
|
|||||||
ser.maxDamage = self.maxDamage
|
ser.maxDamage = self.maxDamage
|
||||||
ser.count = self.count
|
ser.count = self.count
|
||||||
ser.enchantments = self.enchantments
|
ser.enchantments = self.enchantments
|
||||||
ser.displayName = self.displayName
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return ser
|
return ser
|
||||||
@ -108,10 +105,6 @@ function ItemStack:isEmpty()
|
|||||||
return self.count == nil or self.count == 0
|
return self.count == nil or self.count == 0
|
||||||
end
|
end
|
||||||
|
|
||||||
function ItemStack:getDisplayName()
|
|
||||||
return self.displayName
|
|
||||||
end
|
|
||||||
|
|
||||||
function ItemStack:hasChanged(listObj, thorough)
|
function ItemStack:hasChanged(listObj, thorough)
|
||||||
local listItem = listObj[self.slot]
|
local listItem = listObj[self.slot]
|
||||||
if listItem == nil or listItem.name ~= self.name or listItem.count ~= self.count then
|
if listItem == nil or listItem.name ~= self.name or listItem.count ~= self.count then
|
||||||
@ -136,7 +129,6 @@ function ItemStack:_modify(countDelta, stack)
|
|||||||
self.count = nil
|
self.count = nil
|
||||||
self.maxCount = nil
|
self.maxCount = nil
|
||||||
self.enchantments = nil
|
self.enchantments = nil
|
||||||
self.displayName = nil
|
|
||||||
else
|
else
|
||||||
-- If stack is empty, copy stack data from source
|
-- If stack is empty, copy stack data from source
|
||||||
if self:isEmpty() then
|
if self:isEmpty() then
|
||||||
@ -146,7 +138,6 @@ function ItemStack:_modify(countDelta, stack)
|
|||||||
self.maxDamage = stack.maxDamage
|
self.maxDamage = stack.maxDamage
|
||||||
self.maxCount = stack.maxCount
|
self.maxCount = stack.maxCount
|
||||||
self.enchantments = stack.enchantments
|
self.enchantments = stack.enchantments
|
||||||
self.displayName = stack.displayName
|
|
||||||
end
|
end
|
||||||
|
|
||||||
self.count = newCount
|
self.count = newCount
|
||||||
@ -215,7 +206,6 @@ function ItemStack:canTransfer(stack)
|
|||||||
return self.name == stack.name and
|
return self.name == stack.name and
|
||||||
self.damage == stack.damage and
|
self.damage == stack.damage and
|
||||||
self.maxDamage == stack.maxDamage and
|
self.maxDamage == stack.maxDamage and
|
||||||
self.displayName == stack.displayName and
|
|
||||||
objEquals(self.enchantments, stack.enchantments)
|
objEquals(self.enchantments, stack.enchantments)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -244,8 +234,7 @@ function ItemStack:matches(query)
|
|||||||
queryField(query.count, self.count) and
|
queryField(query.count, self.count) and
|
||||||
queryField(query.maxDamage, self.maxDamage) and
|
queryField(query.maxDamage, self.maxDamage) and
|
||||||
queryField(query.enchantments, self.enchantments) and
|
queryField(query.enchantments, self.enchantments) and
|
||||||
queryField(query.maxCount, self.maxCount) and
|
queryField(query.maxCount, self.maxCount)
|
||||||
queryField(query.displayName, self.displayName)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return ItemStack
|
return ItemStack
|
Loading…
x
Reference in New Issue
Block a user