46 lines
1.6 KiB
Lua
46 lines
1.6 KiB
Lua
-- Button hints as a shared module rather than a firmware paint routine: what the strip looks
|
|
-- like is composition, and only the device knows which roles exist to label.
|
|
|
|
local hints = {}
|
|
|
|
-- Reading order across the strip, so a device showing three of them still reads left to right.
|
|
local ORDER = {"back", "left", "up", "down", "right", "confirm"}
|
|
|
|
local function available()
|
|
local roles = {}
|
|
if input and input.getButtons then
|
|
for _, role in ipairs(input.getButtons()) do roles[role] = true end
|
|
end
|
|
return roles
|
|
end
|
|
|
|
---Paints the labels this device can actually act on, and returns the height it used.
|
|
---@param actions table<string, string> Label per Button role; roles the device lacks are skipped.
|
|
---@param options table|nil `y`, `font`, `color`, and `background` overrides.
|
|
function hints.draw(actions, options)
|
|
options = options or {}
|
|
local font = options.font or gui.FONT_SMALL
|
|
local color = options.color or gui.color(0, 0, 0)
|
|
local background = options.background or gui.color(255, 255, 255)
|
|
local height = gui.getFontHeight(font) + 6
|
|
local y = options.y or (gui.getHeight() - height)
|
|
|
|
local roles = available()
|
|
local labels = {}
|
|
for _, role in ipairs(ORDER) do
|
|
if roles[role] and actions[role] then labels[#labels + 1] = actions[role] end
|
|
end
|
|
|
|
gui.fillRect(0, y, gui.getWidth(), height, background)
|
|
if #labels == 0 then return height end
|
|
|
|
local slot = gui.getWidth() // #labels
|
|
for index, label in ipairs(labels) do
|
|
local left = slot * (index - 1) + (slot - gui.getTextWidth(font, label)) // 2
|
|
gui.drawText(font, left, y + 3, label, color, gui.STYLE_NORMAL, background)
|
|
end
|
|
return height
|
|
end
|
|
|
|
return hints
|