milo: refactor item page
This commit is contained in:
64
milo/plugins/item/machinesTab.lua
Normal file
64
milo/plugins/item/machinesTab.lua
Normal file
@@ -0,0 +1,64 @@
|
||||
local Craft = require('craft2')
|
||||
local Milo = require('milo')
|
||||
local UI = require('ui')
|
||||
local Util = require('util')
|
||||
|
||||
local colors = _G.colors
|
||||
local context = Milo:getContext()
|
||||
|
||||
local machinesTab = UI.Window {
|
||||
tabTitle = 'Machine',
|
||||
index = 3,
|
||||
backgroundColor = colors.cyan,
|
||||
grid = UI.ScrollingGrid {
|
||||
x = 2, ex = -2, y = 2, ey = -2,
|
||||
disableHeader = true,
|
||||
columns = {
|
||||
{ heading = 'Name', key = 'displayName'},
|
||||
},
|
||||
sortColumn = 'displayName',
|
||||
help = 'Double-click to set machine',
|
||||
},
|
||||
}
|
||||
|
||||
function machinesTab:setItem(item)
|
||||
self.item = item
|
||||
local machine = Craft.machineLookup[self.item.key]
|
||||
if machine then
|
||||
local t = Util.filter(context.storage.nodes, function(node)
|
||||
if node.category == 'machine' or node.category == 'custom' then -- TODO: - need a setting instead (ie. canCraft)
|
||||
return node.adapter and node.adapter.online and node.adapter.pushItems
|
||||
end
|
||||
end)
|
||||
self.grid:setValues(t)
|
||||
self.grid:setSelected('name', machine)
|
||||
end
|
||||
self.parent:setActive(self, machine)
|
||||
end
|
||||
|
||||
function machinesTab.grid:getDisplayValues(row)
|
||||
row = Util.shallowCopy(row)
|
||||
row.displayName = row.displayName or row.name
|
||||
return row
|
||||
end
|
||||
|
||||
function machinesTab.grid:getRowTextColor(row, selected)
|
||||
if row.name == Craft.machineLookup[self.parent.item.key] then
|
||||
return colors.yellow
|
||||
end
|
||||
return UI.Grid:getRowTextColor(row, selected)
|
||||
end
|
||||
|
||||
function machinesTab:eventHandler(event)
|
||||
if event.type == 'grid_select' then
|
||||
Craft.machineLookup[self.item.key] = event.selected.name
|
||||
Util.writeTable(Craft.MACHINE_LOOKUP, Craft.machineLookup)
|
||||
|
||||
self.grid:draw()
|
||||
self:emit({ type = 'info_message', message = 'Machine saved' })
|
||||
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
return machinesTab
|
||||
Reference in New Issue
Block a user