2023-04-12 16:40:13 +00:00
|
|
|
--
|
|
|
|
-- Graphics Rendering Control
|
|
|
|
--
|
|
|
|
|
2023-04-21 18:53:28 +00:00
|
|
|
local main_view = require("pocket.ui.main")
|
|
|
|
local style = require("pocket.ui.style")
|
2023-04-12 16:40:13 +00:00
|
|
|
|
2023-09-30 17:31:41 +00:00
|
|
|
local core = require("graphics.core")
|
2023-04-21 18:53:28 +00:00
|
|
|
local flasher = require("graphics.flasher")
|
|
|
|
|
|
|
|
local DisplayBox = require("graphics.elements.displaybox")
|
2023-04-12 16:40:13 +00:00
|
|
|
|
2023-09-30 17:31:41 +00:00
|
|
|
---@class pocket_renderer
|
2023-04-12 16:40:13 +00:00
|
|
|
local renderer = {}
|
|
|
|
|
|
|
|
local ui = {
|
2023-04-21 18:53:28 +00:00
|
|
|
display = nil
|
2023-04-12 16:40:13 +00:00
|
|
|
}
|
|
|
|
|
2023-09-30 17:31:41 +00:00
|
|
|
-- try to start the pocket GUI
|
|
|
|
---@return boolean success, any error_msg
|
|
|
|
function renderer.try_start_ui()
|
|
|
|
local status, msg = true, nil
|
|
|
|
|
2023-04-21 18:53:28 +00:00
|
|
|
if ui.display == nil then
|
2023-04-12 16:40:13 +00:00
|
|
|
-- reset screen
|
|
|
|
term.setTextColor(colors.white)
|
|
|
|
term.setBackgroundColor(colors.black)
|
|
|
|
term.clear()
|
|
|
|
term.setCursorPos(1, 1)
|
|
|
|
|
|
|
|
-- set overridden colors
|
|
|
|
for i = 1, #style.colors do
|
|
|
|
term.setPaletteColor(style.colors[i].c, style.colors[i].hex)
|
|
|
|
end
|
|
|
|
|
2023-04-21 18:53:28 +00:00
|
|
|
-- init front panel view
|
2023-09-30 17:31:41 +00:00
|
|
|
status, msg = pcall(function ()
|
|
|
|
ui.display = DisplayBox{window=term.current(),fg_bg=style.root}
|
|
|
|
main_view(ui.display)
|
|
|
|
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-12 16:40:13 +00:00
|
|
|
end
|
2023-09-30 17:31:41 +00:00
|
|
|
|
|
|
|
return status, msg
|
2023-04-12 16:40:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- close out the UI
|
|
|
|
function renderer.close_ui()
|
2023-04-21 18:53:28 +00:00
|
|
|
if ui.display ~= nil then
|
2023-04-22 03:43:28 +00:00
|
|
|
-- stop blinking indicators
|
|
|
|
flasher.clear()
|
|
|
|
|
2023-04-12 16:40:13 +00:00
|
|
|
-- hide to stop animation callbacks
|
2023-04-21 18:53:28 +00:00
|
|
|
ui.display.hide()
|
2023-04-12 16:40:13 +00:00
|
|
|
|
2023-04-22 03:43:28 +00:00
|
|
|
-- clear root UI elements
|
|
|
|
ui.display = nil
|
2023-04-12 16:40:13 +00:00
|
|
|
|
2023-04-22 03:43:28 +00:00
|
|
|
-- restore colors
|
|
|
|
for i = 1, #style.colors do
|
|
|
|
local r, g, b = term.nativePaletteColor(style.colors[i].c)
|
|
|
|
term.setPaletteColor(style.colors[i].c, r, g, b)
|
|
|
|
end
|
2023-04-12 16:40:13 +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-12 16:40:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- is the UI ready?
|
|
|
|
---@nodiscard
|
|
|
|
---@return boolean ready
|
2023-04-21 18:53:28 +00:00
|
|
|
function renderer.ui_ready() return ui.display ~= nil end
|
2023-04-12 16:40:13 +00:00
|
|
|
|
|
|
|
-- handle a mouse event
|
2023-05-11 23:55:02 +00:00
|
|
|
---@param event mouse_interaction|nil
|
2023-04-12 16:40:13 +00:00
|
|
|
function renderer.handle_mouse(event)
|
2023-05-11 23:55:02 +00:00
|
|
|
if ui.display ~= nil and event ~= nil then
|
2023-04-21 18:53:28 +00:00
|
|
|
ui.display.handle_mouse(event)
|
|
|
|
end
|
2023-04-12 16:40:13 +00:00
|
|
|
end
|
|
|
|
|
2024-06-05 00:31:47 +00:00
|
|
|
-- handle a keyboard event
|
|
|
|
---@param event key_interaction|nil
|
|
|
|
function renderer.handle_key(event)
|
|
|
|
if ui.display ~= nil and event ~= nil then
|
|
|
|
ui.display.handle_key(event)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- handle a paste event
|
|
|
|
---@param text string
|
|
|
|
function renderer.handle_paste(text)
|
|
|
|
if ui.display ~= nil then
|
|
|
|
ui.display.handle_paste(text)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-12 16:40:13 +00:00
|
|
|
return renderer
|