From 7f007e032dd72a14fe86a3a43ea15292412013af Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Thu, 16 Jun 2022 11:24:35 -0400 Subject: [PATCH] #62, #72 work on main layout, not using layout class, refactoring and bugfixes --- coordinator/renderer.lua | 14 ++-- coordinator/startup.lua | 6 +- coordinator/ui/components/unit_overview.lua | 43 ++++++++++++ coordinator/ui/layout/main_view.lua | 29 ++++++++ coordinator/ui/layout/unit_view.lua | 22 ++++++ coordinator/ui/main_layout.lua | 21 ------ coordinator/ui/style.lua | 28 +++++++- coordinator/ui/unit_layout.lua | 21 ------ graphics/layout.lua | 75 --------------------- 9 files changed, 133 insertions(+), 126 deletions(-) create mode 100644 coordinator/ui/components/unit_overview.lua create mode 100644 coordinator/ui/layout/main_view.lua create mode 100644 coordinator/ui/layout/unit_view.lua delete mode 100644 coordinator/ui/main_layout.lua delete mode 100644 coordinator/ui/unit_layout.lua delete mode 100644 graphics/layout.lua diff --git a/coordinator/renderer.lua b/coordinator/renderer.lua index 0948e67..8a3df7b 100644 --- a/coordinator/renderer.lua +++ b/coordinator/renderer.lua @@ -3,8 +3,8 @@ local util = require("scada-common.util") local core = require("graphics.core") -local main_layout = require("coordinator.ui.main_layout") -local unit_layout = require("coordinator.ui.unit_layout") +local main_view = require("coordinator.ui.layout.main_view") +local unit_view = require("coordinator.ui.layout.unit_view") local renderer = {} @@ -56,10 +56,15 @@ end -- start the coordinator GUI function renderer.start_ui() - ui.main_layout = main_layout(engine.monitors.primary) + -- hide dmesg + engine.dmesg_window.setVisible(false) + -- show main view on main monitor + ui.main_layout = main_view(engine.monitors.primary) + + -- show unit views on unit displays for id, monitor in pairs(engine.monitors.unit_displays) do - table.insert(ui.unit_layouts, unit_layout(monitor, id)) + table.insert(ui.unit_layouts, unit_view(monitor, id)) end end @@ -73,6 +78,7 @@ function renderer.close_ui() renderer.reset() -- re-draw dmesg + engine.dmesg_window.setVisible(true) engine.dmesg_window.redraw() end diff --git a/coordinator/startup.lua b/coordinator/startup.lua index 3b67dea..6dee519 100644 --- a/coordinator/startup.lua +++ b/coordinator/startup.lua @@ -12,7 +12,7 @@ local config = require("coordinator.config") local coordinator = require("coordinator.coordinator") local renderer = require("coordinator.renderer") -local COORDINATOR_VERSION = "alpha-v0.1.6" +local COORDINATOR_VERSION = "alpha-v0.2.0" local print = util.print local println = util.println @@ -85,8 +85,8 @@ log.dmesg("wireless modem connected", "COMMS", colors.purple) log.dmesg("starting UI...", "GRAPHICS", colors.green) util.psleep(3) -local ui_ok = pcall(renderer.start_ui) +local ui_ok, message = pcall(renderer.start_ui) if not ui_ok then renderer.close_ui() - log.dmesg("UI draw failed", "GRAPHICS", colors.green) + log.dmesg("UI draw failed: " .. message, "GRAPHICS", colors.green) end diff --git a/coordinator/ui/components/unit_overview.lua b/coordinator/ui/components/unit_overview.lua new file mode 100644 index 0000000..413a97a --- /dev/null +++ b/coordinator/ui/components/unit_overview.lua @@ -0,0 +1,43 @@ +local core = require("graphics.core") + +local style = require("coordinator.ui.style") + +local Div = require("graphics.elements.div") +local HorizontalBar = require("graphics.elements.indicators.hbar") +local DataIndicator = require("graphics.elements.indicators.data") +local StateIndicator = require("graphics.elements.indicators.state") +local Rectangle = require("graphics.elements.rectangle") +local TextBox = require("graphics.elements.textbox") + +local TEXT_ALIGN = core.graphics.TEXT_ALIGN + +local cpair = core.graphics.cpair +local border = core.graphics.border + +---@param parent graphics_element +local function make(parent, x, y, unit_id) + -- bounding box div + local root = Div{parent=parent,x=x,y=y,width=75,height=50} + + -- unit header message + TextBox{parent=root,text="Unit #" .. unit_id,alignment=TEXT_ALIGN.CENTER,height=1,fg_bg=style.header} + + -- reactor + local reactor = Rectangle{parent=root,border=border(1, colors.gray, false),width=30,height=10,x=1,y=3} + + local text_fg_bg = cpair(colors.black, colors.lightGray) + local lu_col = cpair(colors.gray, colors.gray) + + local status = StateIndicator{parent=reactor,x=9,y=2,states=style.reactor.states,value=1,min_width=14} + local core_temp = DataIndicator{parent=reactor,x=3,y=4,lu_colors=lu_col,label="Core: ",unit="K",format="%7.0f",value=295,width=26,fg_bg=text_fg_bg} + local heating_r = DataIndicator{parent=reactor,x=3,y=5,lu_colors=lu_col,label="Heating:",unit="mB/t",format="%7.0f",value=359999,width=26,fg_bg=text_fg_bg} + local burn_r = DataIndicator{parent=reactor,x=3,y=6,lu_colors=lu_col,label="Burn: ",unit="mB/t",format="%7.1f",value=40.1,width=26,fg_bg=text_fg_bg} + + local fuel = HorizontalBar{parent=root,x=34,y=4,show_percent=true,bar_fg_bg=cpair(colors.brown,colors.white),height=1,width=14} + local coolant = HorizontalBar{parent=root,x=34,y=5,show_percent=true,bar_fg_bg=cpair(colors.lightBlue,colors.white),height=1,width=14} + + fuel.update(0.85) + coolant.update(0.75) +end + +return make diff --git a/coordinator/ui/layout/main_view.lua b/coordinator/ui/layout/main_view.lua new file mode 100644 index 0000000..7ebb1e4 --- /dev/null +++ b/coordinator/ui/layout/main_view.lua @@ -0,0 +1,29 @@ +-- +-- Main SCADA Coordinator GUI +-- + +local core = require("graphics.core") +local log = require("scada-common.log") + +local style = require("coordinator.ui.style") + +local DisplayBox = require("graphics.elements.displaybox") +local TextBox = require("graphics.elements.textbox") + +local unit_overview = require("coordinator.ui.components.unit_overview") + +local TEXT_ALIGN = core.graphics.TEXT_ALIGN + +local function init(monitor) + local main = DisplayBox{window=monitor,fg_bg=style.root} + + -- window header message + TextBox{parent=main,text="Nuclear Generation Facility SCADA Coordinator",alignment=TEXT_ALIGN.CENTER,height=1,fg_bg=style.header} + + -- unit overviews + unit_overview(main, 5, 5, 1) + + return main +end + +return init diff --git a/coordinator/ui/layout/unit_view.lua b/coordinator/ui/layout/unit_view.lua new file mode 100644 index 0000000..e82657b --- /dev/null +++ b/coordinator/ui/layout/unit_view.lua @@ -0,0 +1,22 @@ +-- +-- Reactor Unit SCADA Coordinator GUI +-- + +local core = require("graphics.core") + +local style = require("coordinator.ui.style") + +local DisplayBox = require("graphics.elements.displaybox") +local TextBox = require("graphics.elements.textbox") + +local TEXT_ALIGN = core.graphics.TEXT_ALIGN + +local function init(monitor, id) + local main = DisplayBox{window=monitor,fg_bg=style.root} + + TextBox{parent=main,text="Reactor Unit #" .. id,alignment=TEXT_ALIGN.CENTER,height=1,fg_bg=style.header} + + return main +end + +return init diff --git a/coordinator/ui/main_layout.lua b/coordinator/ui/main_layout.lua deleted file mode 100644 index 96d0a45..0000000 --- a/coordinator/ui/main_layout.lua +++ /dev/null @@ -1,21 +0,0 @@ --- --- 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 index a148130..04be985 100644 --- a/coordinator/ui/style.lua +++ b/coordinator/ui/style.lua @@ -3,9 +3,33 @@ local core = require("graphics.core") local style = {} +local cpair = core.graphics.cpair + -- MAIN LAYOUT -- -style.root = core.graphics.cpair(colors.black, colors.lightGray) -style.header = core.graphics.cpair(colors.white,colors.gray) +style.root = cpair(colors.black, colors.lightGray) +style.header = cpair(colors.white, colors.gray) + +style.reactor = { + -- reactor states + states = { + { + color = cpair(colors.black, colors.yellow), + text = "DISCONNECTED" + }, + { + color = cpair(colors.white, colors.gray), + text = "DISABLED" + }, + { + color = cpair(colors.black, colors.green), + text = "ACTIVE" + }, + { + color = cpair(colors.black, colors.red), + text = "SCRAM!" + } + } +} return style diff --git a/coordinator/ui/unit_layout.lua b/coordinator/ui/unit_layout.lua deleted file mode 100644 index 45884ca..0000000 --- a/coordinator/ui/unit_layout.lua +++ /dev/null @@ -1,21 +0,0 @@ --- --- 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/layout.lua b/graphics/layout.lua deleted file mode 100644 index ebbcfd3..0000000 --- a/graphics/layout.lua +++ /dev/null @@ -1,75 +0,0 @@ --- --- Graphics View Layout --- - -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