Refactor drawing functions to use offscreen buffer for improved performance and reduced flickering

This commit is contained in:
MayaTheShy
2026-03-15 16:47:14 -04:00
parent 0d5a533fd0
commit 03efd7685c

View File

@@ -96,33 +96,36 @@ end
-- Drawing helpers -- Drawing helpers
------------------------------------------------- -------------------------------------------------
-- Drawing target (set to offscreen buffer in drawDashboard)
local draw = nil
local function monWrite(x, y, text, fg, bg) local function monWrite(x, y, text, fg, bg)
mon.setCursorPos(x, y) draw.setCursorPos(x, y)
if fg then mon.setTextColor(fg) end if fg then draw.setTextColor(fg) end
if bg then mon.setBackgroundColor(bg) end if bg then draw.setBackgroundColor(bg) end
mon.write(text) draw.write(text)
end end
local function monFill(y, color) local function monFill(y, color)
local w, _ = mon.getSize() local w, _ = draw.getSize()
mon.setCursorPos(1, y) draw.setCursorPos(1, y)
mon.setBackgroundColor(color) draw.setBackgroundColor(color)
mon.write(string.rep(" ", w)) draw.write(string.rep(" ", w))
end end
local function monCenter(y, text, fg, bg) local function monCenter(y, text, fg, bg)
local w, _ = mon.getSize() local w, _ = draw.getSize()
local x = math.floor((w - #text) / 2) + 1 local x = math.floor((w - #text) / 2) + 1
monWrite(x, y, text, fg, bg) monWrite(x, y, text, fg, bg)
end end
local function monBar(x, y, width, ratio, barColor, bgColor) local function monBar(x, y, width, ratio, barColor, bgColor)
local filled = math.floor(ratio * width) local filled = math.floor(ratio * width)
mon.setCursorPos(x, y) draw.setCursorPos(x, y)
mon.setBackgroundColor(barColor) draw.setBackgroundColor(barColor)
mon.write(string.rep(" ", filled)) draw.write(string.rep(" ", filled))
mon.setBackgroundColor(bgColor) draw.setBackgroundColor(bgColor)
mon.write(string.rep(" ", width - filled)) draw.write(string.rep(" ", width - filled))
end end
local function drawButton(x, y, text, fg, bg, padLeft, padRight) local function drawButton(x, y, text, fg, bg, padLeft, padRight)
@@ -146,8 +149,10 @@ local function drawDashboard()
local w, h = mon.getSize() local w, h = mon.getSize()
touchZones = {} touchZones = {}
mon.setBackgroundColor(colors.black) -- Create offscreen buffer (invisible)
mon.clear() draw = window.create(mon, 1, 1, w, h, false)
draw.setBackgroundColor(colors.black)
draw.clear()
-- ===== Title bar ===== -- ===== Title bar =====
monFill(1, colors.blue) monFill(1, colors.blue)
@@ -310,6 +315,9 @@ local function drawDashboard()
-- Bottom accent -- Bottom accent
monFill(h, colors.blue) monFill(h, colors.blue)
monCenter(h, " Tap item to order ", colors.lightBlue, colors.blue) monCenter(h, " Tap item to order ", colors.lightBlue, colors.blue)
-- Flush buffer to monitor in one go
draw.setVisible(true)
end end
------------------------------------------------- -------------------------------------------------