#344 element redraws and shorter assert messages

This commit is contained in:
Mikayla Fischler
2023-09-29 19:34:10 -04:00
parent 70831b49d2
commit ed4180a072
35 changed files with 610 additions and 556 deletions

View File

@@ -25,16 +25,14 @@ local element = require("graphics.element")
---@param args state_indicator_args
---@return graphics_element element, element_id id
local function state_indicator(args)
assert(type(args.states) == "table", "graphics.elements.indicators.state: states is a required field")
assert(type(args.states) == "table", "indicators.state: states is a required field")
-- determine height
if util.is_int(args.height) then
assert(args.height % 2 == 1, "graphics.elements.indicators.state: height should be an odd number")
assert(args.height % 2 == 1, "indicators.state: height should be an odd number")
else
args.height = 1
end
-- initial guess at width
args.width = args.min_width or 1
-- state blit strings
@@ -42,7 +40,6 @@ local function state_indicator(args)
for i = 1, #args.states do
local state_def = args.states[i] ---@type state_text_color
-- re-determine width
if string.len(state_def.text) > args.width then
args.width = string.len(state_def.text)
end
@@ -59,13 +56,20 @@ local function state_indicator(args)
-- create new graphics element base object
local e = element.new(args)
e.value = args.value or 1
-- element redraw
function e.redraw()
local blit_cmd = state_blit_cmds[e.value]
e.w_set_cur(1, 1)
e.w_blit(blit_cmd.text, blit_cmd.fgd, blit_cmd.bkg)
end
-- on state change
---@param new_state integer indicator state
function e.on_update(new_state)
local blit_cmd = state_blit_cmds[new_state]
e.value = new_state
e.w_set_cur(1, 1)
e.w_blit(blit_cmd.text, blit_cmd.fgd, blit_cmd.bkg)
e.redraw()
end
-- set indicator state
@@ -73,7 +77,7 @@ local function state_indicator(args)
function e.set_value(val) e.on_update(val) end
-- initial draw
e.on_update(args.value or 1)
e.redraw()
return e.complete()
end