diff --git a/coordinator/renderer.lua b/coordinator/renderer.lua index 9c29c4b..0948e67 100644 --- a/coordinator/renderer.lua +++ b/coordinator/renderer.lua @@ -3,29 +3,21 @@ local util = require("scada-common.util") local core = require("graphics.core") -local displaybox = require("graphics.elements.displaybox") -local textbox = require("graphics.elements.textbox") +local main_layout = require("coordinator.ui.main_layout") +local unit_layout = require("coordinator.ui.unit_layout") local renderer = {} -local gconf = { - -- root boxes - root = { - fgd = colors.black, - bkg = colors.lightGray - } -} - -- render engine local engine = { monitors = nil, dmesg_window = nil } --- UI elements +-- UI layouts local ui = { - main_box = nil, - unit_boxes = {} + main_layout = nil, + unit_layouts = {} } -- reset a display to the "default", but set text scale to 0.5 @@ -64,22 +56,18 @@ end -- start the coordinator GUI function renderer.start_ui() - local palette = core.graphics.cpair(gconf.root.fgd, gconf.root.bkg) + ui.main_layout = main_layout(engine.monitors.primary) - ui.main_box = displaybox{window=engine.monitors.primary,fg_bg=palette} - - textbox{parent=ui.main_box,text="Nuclear Generation Facility SCADA Coordinator",alignment=core.graphics.TEXT_ALIGN.CENTER,height=1,fg_bg=core.graphics.cpair(colors.white,colors.gray)} - - for _, monitor in pairs(engine.monitors.unit_displays) do - table.insert(ui.unit_boxes, displaybox{window=monitor,fg_bg=palette}) + for id, monitor in pairs(engine.monitors.unit_displays) do + table.insert(ui.unit_layouts, unit_layout(monitor, id)) end end -- close out the UI function renderer.close_ui() -- clear root UI elements - ui.main_box = nil - ui.unit_boxes = {} + ui.main_layout = nil + ui.unit_layouts = {} -- reset displays renderer.reset() diff --git a/coordinator/util/dialog.lua b/coordinator/ui/dialog.lua similarity index 100% rename from coordinator/util/dialog.lua rename to coordinator/ui/dialog.lua diff --git a/coordinator/ui/main_layout.lua b/coordinator/ui/main_layout.lua new file mode 100644 index 0000000..96d0a45 --- /dev/null +++ b/coordinator/ui/main_layout.lua @@ -0,0 +1,21 @@ +-- +-- Main SCADA Coordinator GUI +-- + +local core = require("graphics.core") +local layout = require("graphics.layout") + +local style = require("coordinator.ui.style") + +local displaybox = require("graphics.elements.displaybox") +local textbox = require("graphics.elements.textbox") + +local function init(monitor) + local main = layout.create(monitor, displaybox{window=monitor,fg_bg=style.root}) + + textbox{parent=main,text="Nuclear Generation Facility SCADA Coordinator",alignment=core.graphics.TEXT_ALIGN.CENTER,height=1,fg_bg=style.header} + + return main +end + +return init diff --git a/coordinator/ui/style.lua b/coordinator/ui/style.lua new file mode 100644 index 0000000..a148130 --- /dev/null +++ b/coordinator/ui/style.lua @@ -0,0 +1,11 @@ + +local core = require("graphics.core") + +local style = {} + +-- MAIN LAYOUT -- + +style.root = core.graphics.cpair(colors.black, colors.lightGray) +style.header = core.graphics.cpair(colors.white,colors.gray) + +return style diff --git a/coordinator/ui/unit_layout.lua b/coordinator/ui/unit_layout.lua new file mode 100644 index 0000000..45884ca --- /dev/null +++ b/coordinator/ui/unit_layout.lua @@ -0,0 +1,21 @@ +-- +-- Reactor Unit SCADA Coordinator GUI +-- + +local core = require("graphics.core") +local layout = require("graphics.layout") + +local style = require("coordinator.ui.style") + +local displaybox = require("graphics.elements.displaybox") +local textbox = require("graphics.elements.textbox") + +local function init(monitor, id) + local main = layout.create(monitor, displaybox{window=monitor,fg_bg=style.root}) + + textbox{parent=main,text="Reactor Unit #" .. id,alignment=core.graphics.TEXT_ALIGN.CENTER,height=1,fg_bg=style.header} + + return main +end + +return init diff --git a/graphics/core.lua b/graphics/core.lua index 91e7915..4bd0d9e 100644 --- a/graphics/core.lua +++ b/graphics/core.lua @@ -1,3 +1,7 @@ +-- +-- Graphics Core Functions and Objects +-- + local core = {} local events = {} diff --git a/graphics/layout.lua b/graphics/layout.lua new file mode 100644 index 0000000..fe91cdd --- /dev/null +++ b/graphics/layout.lua @@ -0,0 +1,71 @@ +local core = require("graphics.core") +local util = require("scada-common.util") + +local displaybox = require("graphics.elements.displaybox") + +local layout = {} + +---@class stem +---@field element graphics_element +---@field children table + +function layout.create(window, default_fg_bg) + local self = { + root = displaybox{window=window,fg_bg=default_fg_bg}, + tree = {} + } + + -- recursive function to search layout tree for an element + ---@param id string element ID to look for + ---@param tree table tree to search in + ---@return stem|nil + local function lookup(id, tree) + for key, stem in pairs(tree) do + if key == id then + return stem + else + stem = lookup(id, stem.children) + if stem ~= nil then return stem end + end + end + + return nil + end + + ---@class layout + local public = {} + + -- insert a new element + ---@param parent_id string|nil parent or nil for root + ---@param id string element ID + ---@param element graphics_element + function public.insert_at(parent_id, id, element) + if parent_id == nil then + self.tree[id] = { element = element, children = {} } + else + local parent = lookup(parent_id, self.tree) + if parent ~= nil then + parent.children[id] = { element = element, children = {} } + end + end + end + + -- get an element by ID + ---@param id string element ID + ---@return graphics_element|nil + function public.get_element_by_id(id) + local elem = lookup(id, self.tree) +---@diagnostic disable-next-line: need-check-nil + return util.trinary(elem == nil, nil, elem.element) + end + + -- get the root element + ---@return graphics_element + function public.get_root() + return self.root + end + + return public +end + +return layout