Compare commits

..

No commits in common. "5fe73663b99c297878fc428c40cf135aface7403" and "741ec36d8496a1584a98b2b9cab803464b0a00cd" have entirely different histories.

3 changed files with 26 additions and 74 deletions

View File

@ -70,7 +70,7 @@ local function loadState(fname, node)
return controller return controller
end end
local function itemList(groups, wBudget, hBudget, savedState, onClick, setPage, runImport) local function itemList(groups, wBudget, hBudget, savedState, onClick, setPage)
local state = savedState or {} local state = savedState or {}
state.tab = state.tab or 1 state.tab = state.tab or 1
@ -86,21 +86,10 @@ local function itemList(groups, wBudget, hBudget, savedState, onClick, setPage,
state.tab = state.tab - 1 state.tab = state.tab - 1
setPage(nil) setPage(nil)
end end
return true
end end
} }
local btnImport = Text:new{ local _btnNext = Text:new{
text = "IMPORT",
bgColor = colors.gray,
fgColor = colors.white,
onClick = function(e, x, y, s)
runImport()
return true
end
}
local btnNext = Text:new{
text = " > ", text = " > ",
bgColor = state.tab == pages and colors.black or colors.gray, bgColor = state.tab == pages and colors.black or colors.gray,
fgColor = state.tab == pages and colors.red or colors.white, fgColor = state.tab == pages and colors.red or colors.white,
@ -109,21 +98,17 @@ local function itemList(groups, wBudget, hBudget, savedState, onClick, setPage,
state.tab = state.tab + 1 state.tab = state.tab + 1
setPage(nil) setPage(nil)
end end
return true
end end
} }
local btnNext = Padding:new{
left = wBudget - _btnNext:getWidth() - btnPrev:getWidth(),
element = _btnNext
}
local tabLine = List:new{ local tabLine = List:new{
children = { children = {
btnPrev, btnPrev,
Padding:new{ btnNext
left = math.floor((wBudget - btnPrev:getWidth() - btnNext:getWidth() - btnImport:getWidth())/2),
element = btnImport
},
Padding:new{
left = wBudget - btnPrev:getWidth() - btnImport:getWidth() - btnNext:getWidth(),
element = btnNext
}
}, },
vertical = false vertical = false
} }
@ -197,8 +182,11 @@ local function updatePageRender(state, pages)
state.currentPage = state.nextPage state.currentPage = state.nextPage
state.nextPage = nil state.nextPage = nil
if changingPage then
state.pageState = nil
end
state.monitor.clear() state.monitor.clear()
state._pageRender = pages[state.currentPage](state, changingPage) state._pageRender = pages[state.currentPage](state)
end end
end end
@ -224,7 +212,7 @@ local function renderDefault(state, rootElement)
end end
local PAGES = { local PAGES = {
MAIN = function(state, newPage) MAIN = function(state)
local found = ItemGroup.collectStacks(state.controller:find(function(stack) local found = ItemGroup.collectStacks(state.controller:find(function(stack)
return not stack:isEmpty() return not stack:isEmpty()
end)) end))
@ -235,27 +223,16 @@ local PAGES = {
found, found,
state.width, state.width,
state.height, state.height,
state.pageState[state.currentPage], state.pageState,
function(element, x, y, source, group) function(element, x, y, source, group)
print("Clicked: "..group:getDisplayName()) print("Clicked: "..group:getDisplayName())
return true return true
end, end,
function(page) function(page)
state.nextPage = page or state.currentPage state.nextPage = page or state.currentPage
end,
function()
print("Importing...")
-- Safely handle transfers, priming computer for a full reset/rescan in case server stops mid-transaction
state:itemTransaction(function()
for _,nodeStack in state.node:find(function(s) return not s:isEmpty() end) do
if not state.controller:insertStack(nodeStack) then
print("Couldn't find a free slot for: "..(nodeStack:getDisplayName() or nodeStack:getName() or "[EMPTY]"))
end
end
end)
end end
) )
state.pageState[state.currentPage] = pageState state.pageState = pageState
listResult:setParent(state.monitor) listResult:setParent(state.monitor)
return function() return function()
@ -263,11 +240,11 @@ local PAGES = {
end end
end, end,
GROUP_DETAIL = function(state, newPage) GROUP_DETAIL = function(state)
end, end,
REQUEST = function(state, newPage) REQUEST = function(state)
end end
} }
@ -281,24 +258,14 @@ local width, height = monitor.getSize()
local CONTROLLER_STATE = { local CONTROLLER_STATE = {
controller = controller, controller = controller,
node = accessNode,
width = width, width = width,
height = height, height = height,
monitor = monitor, monitor = monitor,
nextPage = "MAIN", nextPage = "MAIN",
exit = false, exit = false,
pageState = {}, pageState = {}
lock = lock,
unlock = unlock,
isLocked = isLocked
} }
function CONTROLLER_STATE:itemTransaction(transact)
self.lock()
transact()
self.unlock()
end
os.queueEvent("dummy_event") os.queueEvent("dummy_event")
while not CONTROLLER_STATE.exit do while not CONTROLLER_STATE.exit do
updatePageRender(CONTROLLER_STATE, PAGES) updatePageRender(CONTROLLER_STATE, PAGES)

View File

@ -88,8 +88,8 @@ function Storage:find(query)
end end
-- Find all stacks eligible to accept the given query -- Find all stacks eligible to accept the given query
function Storage:findInsertTargets(sourceStack) function Storage:findInsertTargets(query)
local result = self:find(function(stack) return sourceStack:canTransfer(stack) end) local result = self:find(function(stack) return stack:isEmpty() or stack:matches(query) end)
-- Insertion should prioritize filling populated stacks -- Insertion should prioritize filling populated stacks
table.sort(result, function(a, b) return a:getcount() > b:getCount() end) table.sort(result, function(a, b) return a:getcount() > b:getCount() end)
@ -97,9 +97,8 @@ function Storage:findInsertTargets(sourceStack)
return result return result
end end
function Storage:findExtractTargets(targetStack) function Storage:findExtractTargets(query)
-- Only transfer non-empty stacks local result = self:find(query)
local result = self:find(function(stack) return (not stack:isEmpty()) and stack:canTransfer(targetStack) end)
-- Extraction should prioritize emptying populated stacks -- Extraction should prioritize emptying populated stacks
table.sort(result, function(a, b) return a:getcount() < b:getCount() end) table.sort(result, function(a, b) return a:getcount() < b:getCount() end)
@ -107,17 +106,4 @@ function Storage:findExtractTargets(targetStack)
return result return result
end end
function Storage:insertStack(stack)
local targets = self:findInsertTargets(stack)
for _,target in ipairs(targets) do
if stack:isEmpty() then
return true
end
stack:transferTo(target)
end
return false
end
return Storage return Storage

View File

@ -211,13 +211,12 @@ end
-- Determines if two stacks can be transferred to eachother -- Determines if two stacks can be transferred to eachother
-- Empty stacks are always valid transfer nodes
function ItemStack:canTransfer(stack) function ItemStack:canTransfer(stack)
return self:isEmpty() or stack:isEmpty() or (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 self.displayName == stack.displayName and
objEquals(self.enchantments, stack.enchantments)) objEquals(self.enchantments, stack.enchantments)
end end
local function queryField(query, field) local function queryField(query, field)