2023-04-07 02:10:33 +00:00
|
|
|
--
|
|
|
|
-- Graphics Rendering Control
|
|
|
|
--
|
|
|
|
|
|
|
|
local panel_view = require("reactor-plc.panel.front_panel")
|
2023-04-21 00:47:14 +00:00
|
|
|
local style = require("reactor-plc.panel.style")
|
2023-04-07 02:10:33 +00:00
|
|
|
|
2023-09-30 17:31:41 +00:00
|
|
|
local core = require("graphics.core")
|
2023-04-08 20:49:54 +00:00
|
|
|
local flasher = require("graphics.flasher")
|
|
|
|
|
2023-04-21 00:47:14 +00:00
|
|
|
local DisplayBox = require("graphics.elements.displaybox")
|
|
|
|
|
2023-09-30 17:31:41 +00:00
|
|
|
---@class reactor_plc_renderer
|
2023-04-07 02:10:33 +00:00
|
|
|
local renderer = {}
|
|
|
|
|
|
|
|
local ui = {
|
2023-04-21 00:47:14 +00:00
|
|
|
display = nil
|
2023-04-07 02:10:33 +00:00
|
|
|
}
|
|
|
|
|
2023-09-30 17:31:41 +00:00
|
|
|
-- try to start the UI
|
2024-03-12 01:25:34 +00:00
|
|
|
---@param theme FP_THEME front panel theme
|
|
|
|
---@param color_mode COLOR_MODE color mode
|
2023-09-30 17:31:41 +00:00
|
|
|
---@return boolean success, any error_msg
|
2024-03-12 01:25:34 +00:00
|
|
|
function renderer.try_start_ui(theme, color_mode)
|
2023-09-30 17:31:41 +00:00
|
|
|
local status, msg = true, nil
|
|
|
|
|
2023-04-21 00:47:14 +00:00
|
|
|
if ui.display == nil then
|
2024-03-07 23:00:33 +00:00
|
|
|
-- set theme
|
|
|
|
style.set_theme(theme)
|
|
|
|
|
2023-04-08 20:49:54 +00:00
|
|
|
-- reset terminal
|
2023-04-07 02:10:33 +00:00
|
|
|
term.setTextColor(colors.white)
|
|
|
|
term.setBackgroundColor(colors.black)
|
|
|
|
term.clear()
|
|
|
|
term.setCursorPos(1, 1)
|
|
|
|
|
|
|
|
-- set overridden colors
|
2024-03-06 17:18:50 +00:00
|
|
|
for i = 1, #style.theme.colors do
|
|
|
|
term.setPaletteColor(style.theme.colors[i].c, style.theme.colors[i].hex)
|
2023-04-07 02:10:33 +00:00
|
|
|
end
|
|
|
|
|
2024-03-12 01:25:34 +00:00
|
|
|
-- apply color mode
|
|
|
|
local c_mode_overrides = style.theme.color_modes[color_mode]
|
|
|
|
for i = 1, #c_mode_overrides do
|
|
|
|
term.setPaletteColor(c_mode_overrides[i].c, c_mode_overrides[i].hex)
|
|
|
|
end
|
|
|
|
|
2023-04-07 02:10:33 +00:00
|
|
|
-- init front panel view
|
2023-09-30 17:31:41 +00:00
|
|
|
status, msg = pcall(function ()
|
2024-03-06 17:18:50 +00:00
|
|
|
ui.display = DisplayBox{window=term.current(),fg_bg=style.fp.root}
|
2024-03-12 01:25:34 +00:00
|
|
|
panel_view(ui.display, color_mode)
|
2023-09-30 17:31:41 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
if status then
|
|
|
|
-- start flasher callback task
|
|
|
|
flasher.run()
|
|
|
|
else
|
|
|
|
-- report fail and close ui
|
|
|
|
msg = core.extract_assert_msg(msg)
|
|
|
|
renderer.close_ui()
|
|
|
|
end
|
2023-04-07 02:10:33 +00:00
|
|
|
end
|
2023-09-30 17:31:41 +00:00
|
|
|
|
|
|
|
return status, msg
|
2023-04-07 02:10:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- close out the UI
|
|
|
|
function renderer.close_ui()
|
2023-04-21 00:47:14 +00:00
|
|
|
if ui.display ~= nil then
|
2023-04-22 03:43:28 +00:00
|
|
|
-- stop blinking indicators
|
|
|
|
flasher.clear()
|
|
|
|
|
2023-05-13 13:43:42 +00:00
|
|
|
-- delete element tree
|
|
|
|
ui.display.delete()
|
2023-04-22 03:43:28 +00:00
|
|
|
ui.display = nil
|
2023-04-07 02:10:33 +00:00
|
|
|
|
2023-04-22 03:43:28 +00:00
|
|
|
-- restore colors
|
2024-03-06 17:18:50 +00:00
|
|
|
for i = 1, #style.theme.colors do
|
|
|
|
local r, g, b = term.nativePaletteColor(style.theme.colors[i].c)
|
|
|
|
term.setPaletteColor(style.theme.colors[i].c, r, g, b)
|
2023-04-22 03:43:28 +00:00
|
|
|
end
|
2023-04-07 02:10:33 +00:00
|
|
|
|
2023-04-22 03:43:28 +00:00
|
|
|
-- reset terminal
|
|
|
|
term.setTextColor(colors.white)
|
|
|
|
term.setBackgroundColor(colors.black)
|
|
|
|
term.clear()
|
|
|
|
term.setCursorPos(1, 1)
|
|
|
|
end
|
2023-04-07 02:10:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- is the UI ready?
|
|
|
|
---@nodiscard
|
|
|
|
---@return boolean ready
|
2023-04-21 00:47:14 +00:00
|
|
|
function renderer.ui_ready() return ui.display ~= nil end
|
2023-04-07 02:10:33 +00:00
|
|
|
|
2023-04-09 02:00:51 +00:00
|
|
|
-- handle a mouse event
|
2023-05-10 19:21:54 +00:00
|
|
|
---@param event mouse_interaction|nil
|
2023-04-09 01:35:44 +00:00
|
|
|
function renderer.handle_mouse(event)
|
2023-05-10 19:21:54 +00:00
|
|
|
if ui.display ~= nil and event ~= nil then
|
2023-04-21 00:47:14 +00:00
|
|
|
ui.display.handle_mouse(event)
|
|
|
|
end
|
2023-04-07 02:10:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return renderer
|