#62 graphics layouts

This commit is contained in:
Mikayla Fischler 2022-06-14 12:02:42 -04:00
parent 3593493c98
commit 13513a9ce6
7 changed files with 138 additions and 22 deletions

View File

@ -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()

View File

@ -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

11
coordinator/ui/style.lua Normal file
View File

@ -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

View File

@ -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

View File

@ -1,3 +1,7 @@
--
-- Graphics Core Functions and Objects
--
local core = {}
local events = {}

71
graphics/layout.lua Normal file
View File

@ -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