ui theme generator + better module handling

This commit is contained in:
xAnavrins 2019-07-16 23:21:33 -04:00
parent c8f200ebb6
commit 672cca3084
6 changed files with 110 additions and 96 deletions

View File

@ -100,4 +100,5 @@ function tab:eventHandler(event)
end
end
return tab
--this needs rework - see 4.user.lua
--return tab

View File

@ -34,25 +34,12 @@ local keys = _G.keys
local mouse = _G.device.mouse
local os = _G.os
local drivers = { }
kernel.hook('peripheral', function(_, eventData)
local side = eventData[1]
if side then
local dev = Peripheral.addDevice(device, side)
if dev then
if drivers[dev.type] then
local e = drivers[dev.type](dev)
if type(e) == 'table' then
for _, v in pairs(e) do
os.queueEvent('device_attach', v.name)
end
elseif e then
os.queueEvent('device_attach', e.name)
end
end
os.queueEvent('device_attach', dev.name, dev)
os.queueEvent('device_attach', dev.name)
end
end
end)
@ -61,12 +48,7 @@ kernel.hook('peripheral_detach', function(_, eventData)
local side = eventData[1]
if side then
for _, dev in pairs(Util.findAll(device, 'side', side)) do
os.queueEvent('device_detach', dev.name, dev)
if dev._children then
for _,v in pairs(dev._children) do
os.queueEvent('peripheral_detach', v.name)
end
end
os.queueEvent('device_detach', dev.name)
device[dev.name] = nil
end
end
@ -124,59 +106,3 @@ end
function keyboard.removeHotkey(code)
keyboard.hotkeys[code] = nil
end
local function createDevice(name, devType, method, manipulator)
local dev = {
name = name,
side = name,
type = devType,
}
local methods = {
'drop', 'getDocs', 'getItem', 'getItemMeta', 'getTransferLocations',
'list', 'pullItems', 'pushItems', 'size', 'suck',
}
if manipulator[method] then
for _,k in pairs(methods) do
dev[k] = function(...)
return manipulator[method]()[k](...)
end
end
if not manipulator._children then
manipulator._children = { dev }
else
table.insert(manipulator._children, dev)
end
device[name] = dev
end
end
drivers['manipulator'] = function(dev)
if dev.getName then
pcall(function()
local name = dev.getName()
if name then
if dev.getInventory then
createDevice(name .. ':inventory', 'inventory', 'getInventory', dev)
end
if dev.getEquipment then
createDevice(name .. ':equipment', 'equipment', 'getEquipment', dev)
end
if dev.getEnder then
createDevice(name .. ':enderChest', 'enderChest', 'getEnder', dev)
end
return dev._children
end
end)
end
end
-- initialize drivers
for _,v in pairs(device) do
if drivers[v.type] then
local s, m = pcall(drivers[v.type], v)
if not s and m then
_G.printError(m)
end
end
end

View File

@ -3,17 +3,17 @@ local Util = require('opus.util')
local device = _G.device
local kernel = _G.kernel
local os = _G.os
local peripheral = _G.peripheral
local containers = {
manipulator = true,
neuralInterface = true,
}
local cache = { }
local function getModules(dev, side)
local list = { }
if dev then
if dev and dev.listModules then
for _, module in pairs(dev.listModules()) do
list[module] = Util.shallowCopy(dev)
list[module].name = module
@ -24,29 +24,86 @@ local function getModules(dev, side)
return list
end
for _,v in pairs(device) do
if containers[v.type] then
local list = getModules(v, v.side)
for k, dev in pairs(list) do
-- neural and attached modules have precedence over manipulator modules
if not device[k] or v.type ~= 'manipulator' then
device[k] = dev
end
-- if a device has been reattached, reuse the existing
-- table so any references to the table are retained
local function addDevice(dev, args, doQueue)
local name = args.name
if not cache[name] then
cache[name] = { }
end
device[name] = cache[name]
Util.merge(device[name], dev)
Util.merge(device[name], args)
if doQueue then
os.queueEvent('device_attach', name)
end
end
local function addContainer(v, doQueue)
-- add devices like plethora:scanner
for name, dev in pairs(getModules(v, v.side)) do
-- neural and attached modules have precedence over manipulator modules
if not device[name] or v.type ~= 'manipulator' then
addDevice(dev, { name = dev.name, type = dev.name, side = dev.side }, doQueue)
end
end
if v.getName then
pcall(function()
local name = v.getName()
if name then
if v.getInventory then
addDevice(v.getInventory(), {
name = name .. ':inventory',
type = 'inventory',
side = v.side
}, doQueue)
end
if v.getEquipment then
addDevice(v.getEquipment(), {
name = name .. ':equipment',
type = 'equipment',
side = v.side
}, doQueue)
end
if v.getEnder then
addDevice(v.getEnder(), {
name = name .. ':enderChest',
type = 'enderChest',
side = v.side
}, doQueue)
end
end
end)
end
end
for k,v in pairs(device) do
if containers[v.type] then
cache[k] = v
addContainer(v)
end
end
-- register modules as peripherals
kernel.hook('device_attach', function(_, eventData)
local dev = eventData[2]
local name = eventData[1]
local dev = device[name]
if dev and containers[dev.type] then
local list = getModules(peripheral.wrap(dev.side), dev.side)
for k,v in pairs(list) do
if not device[k] or dev.type ~= 'manipulator' then
device[k] = v
os.queueEvent('device_attach', k, v)
end
-- so... basically, if you get a handle to device.neuralInterface
-- (or manipulator) - that handle will still be valid after
-- a module is removed
if cache[name] then
device[name] = cache[name]
-- TODO: cannot simply merge - need to remove
-- all functions then merge
Util.merge(device[name], dev)
else
cache[name] = dev
end
addContainer(dev, true)
end
end)

View File

@ -23,5 +23,5 @@ end
-- register oc devices as peripherals
kernel.hook('device_attach', function(_, eventData)
register(device[eventData[2]])
register(device[eventData[1]])
end)

View File

@ -12,6 +12,7 @@ local fs = _G.fs
local os = _G.os
local peripheral = _G.peripheral
local term = _G.term
local textutils = _G.textutils
--[[
Using the shorthand window definition, elements are created from
@ -217,6 +218,34 @@ function Manager:loadTheme(filename)
end
end
function Manager:generateTheme(filename)
local t = { }
for k,v in pairs(self) do
if type(v) == 'table' then
if v._preload then
v._preload()
v = self[k]
end
if v.defaults and v.defaults.UIElement ~= 'Device' then
for p,d in pairs(v.defaults) do
if p:find('olor') then
if not t[k] then
t[k] = { }
end
for c, n in pairs(colors) do
if n == d then
t[k][p] = 'colors.' .. c
break
end
end
end
end
end
end
end
Util.writeFile(filename, textutils.serialize(t):gsub('(")', ''))
end
function Manager:emitEvent(event)
local currentPage = self:getActivePage()
if currentPage and currentPage.focused then

View File

@ -7,6 +7,7 @@ UI.Menu.defaults = {
UIElement = 'Menu',
disableHeader = true,
columns = { { heading = 'Prompt', key = 'prompt', width = 20 } },
menuItems = { },
}
function UI.Menu:postInit()
self.values = self.menuItems