bugfixes to graphics elements

This commit is contained in:
Mikayla Fischler 2022-07-16 13:25:07 -04:00
parent 525dedb830
commit c3d6d900a1
5 changed files with 51 additions and 42 deletions

View File

@ -24,13 +24,15 @@ local function push_button(args)
-- single line
args.height = 1
args.min_width = args.min_width or 0
local text_width = string.len(args.text)
args.width = math.max(text_width + 2, args.min_width)
-- create new graphics element base object
local e = element.new(args)
local h_pad = math.floor((e.frame.w - text_width) / 2)
local h_pad = math.floor((e.frame.w - text_width) / 2) + 1
local v_pad = math.floor(e.frame.h / 2) + 1
-- write the button text

View File

@ -11,6 +11,7 @@ local element = require("graphics.element")
---@field parent graphics_element
---@field x? integer 1 if omitted
---@field y? integer 1 if omitted
---@field fg_bg? cpair foreground/background colors
-- new scram button
---@param args scram_button_args
@ -21,9 +22,6 @@ local function scram_button(args)
args.height = 3
args.width = 9
-- static colors
args.fg_bg = core.graphics.cpair(colors.white, colors.black)
-- create new graphics element base object
local e = element.new(args)
@ -35,25 +33,25 @@ local function scram_button(args)
-- top
e.window.setTextColor(colors.yellow)
e.window.setBackgroundColor(colors.black)
e.window.setBackgroundColor(args.fg_bg.bkg)
e.window.setCursorPos(1, 1)
e.window.write("\x99\x89\x89\x89\x89\x89\x89\x89\x99")
-- center left
e.window.setCursorPos(1, 2)
e.window.setTextColor(colors.black)
e.window.setTextColor(args.fg_bg.bkg)
e.window.setBackgroundColor(colors.yellow)
e.window.write("\x99")
-- center right
e.window.setTextColor(colors.black)
e.window.setTextColor(args.fg_bg.bkg)
e.window.setBackgroundColor(colors.yellow)
e.window.setCursorPos(9, 2)
e.window.write("\x99")
-- bottom
e.window.setTextColor(colors.yellow)
e.window.setBackgroundColor(colors.black)
e.window.setBackgroundColor(args.fg_bg.bkg)
e.window.setCursorPos(1, 3)
e.window.write("\x99\x98\x98\x98\x98\x98\x98\x98\x99")

View File

@ -21,11 +21,12 @@ local function spinbox(args)
local digits = {}
local wn_prec = args.whole_num_precision
local fr_prec = args.fractional_precision
local dec_point_x = args.whole_num_precision + 1
assert(util.is_int(wn_prec), "graphics.element.controls.spinbox_numeric: whole number precision must be an integer")
assert(util.is_int(fr_prec), "graphics.element.controls.spinbox_numeric: fractional precision must be an integer")
local dec_point_x = args.whole_num_precision + 1
assert(type(args.arrow_fg_bg) == "table", "graphics.element.spinbox_numeric: arrow_fg_bg is a required field")
local initial_str = util.sprintf("%" .. wn_prec .. "." .. fr_prec .. "f", value)
@ -48,12 +49,23 @@ local function spinbox(args)
e.window.setCursorPos(1, 3)
e.window.write(util.strrep("\x1f", wn_prec))
if fr_prec > 0 then
e.window.setCursorPos(1, 1)
e.window.setCursorPos(1 + wn_prec, 1)
e.window.write(" " .. util.strrep("\x1e", fr_prec))
e.window.setCursorPos(1, 3)
e.window.setCursorPos(1 + wn_prec, 3)
e.window.write(" " .. util.strrep("\x1f", fr_prec))
end
-- print out the current value
local function show_num()
e.window.setBackgroundColor(e.fg_bg.bkg)
e.window.setTextColor(e.fg_bg.fgd)
e.window.setCursorPos(1, 2)
e.window.write(util.sprintf("%" .. wn_prec + fr_prec + 1 .. "." .. fr_prec .. "f", value))
end
-- init with the default value
show_num()
-- handle touch
---@param event monitor_touch monitor touch event
function e.handle_touch(event)
@ -78,6 +90,8 @@ local function spinbox(args)
value = value + (digits[i] * (10 ^ -pow))
end
end
show_num()
end
end

View File

@ -23,31 +23,26 @@ local function indicator_light(args)
args.height = 1
-- determine width
args.width = math.max(args.min_label_width or 1, string.len(args.label)) + 3
args.width = math.max(args.min_label_width or 1, string.len(args.label)) + 2
-- create new graphics element base object
local e = element.new(args)
-- on/off blit strings
local on_blit = util.strrep(args.colors.blit_a, 2)
local off_blit = util.strrep(args.colors.blit_b, 2)
-- write label and initial indicator light
e.window.setCursorPos(1, 1)
e.window.blit(" ", "000", off_blit .. e.fg_bg.blit_bkg)
e.window.write(args.label)
-- on state change
---@param new_state boolean indicator state
function e.on_update(new_state)
e.window.setCursorPos(1, 1)
if new_state then
e.window.blit(" ", "00", on_blit)
e.window.blit(" \x95", "0" .. args.colors.blit_a, args.colors.blit_a .. e.fg_bg.blit_bkg)
else
e.window.blit(" ", "00", off_blit)
e.window.blit(" \x95", "0" .. args.colors.blit_b, args.colors.blit_b .. e.fg_bg.blit_bkg)
end
end
-- write label and initial indicator light
e.on_update(false)
e.window.write(args.label)
return e.get()
end

View File

@ -7,7 +7,7 @@ local element = require("graphics.element")
---@class tiling_args
---@field fill_c cpair colors to fill with
---@field even? boolean whether to account for rectangular pixels
---@field border? graphics_border
---@field border_c? color optional frame color
---@field parent graphics_element
---@field x? integer 1 if omitted
---@field y? integer 1 if omitted
@ -33,37 +33,37 @@ local function tiling(args)
local start_x = 1
local start_y = 1
local width = e.frame.w
local height = e.frame.h
local inner_width = math.floor(e.frame.w / util.trinary(even, 2, 1))
local inner_height = e.frame.h
local alternator = true
-- border
if args.border ~= nil then
e.window.setBackgroundColor(args.border.color)
if args.border_c ~= nil then
e.window.setBackgroundColor(args.border_c)
e.window.clear()
start_x = 1 + util.trinary(args.border.even, args.border.width * 2, args.border.width)
start_y = 1 + args.border.width
start_x = 1 + util.trinary(even, 2, 1)
start_y = 2
width = width - (2 * util.trinary(args.border.even, args.border.width * 2, args.border.width))
height = height - (2 * args.border.width)
inner_width = math.floor((e.frame.w - 2 * util.trinary(even, 2, 1)) / 2)
inner_height = e.frame.h - 2
end
-- check dimensions
assert(start_x <= width, "graphics.elements.tiling: start_x > width")
assert(start_y <= height, "graphics.elements.tiling: start_y > height")
assert(width > 0, "graphics.elements.tiling: width <= 0")
assert(height > 0, "graphics.elements.tiling: height <= 0")
assert(inner_width > 0, "graphics.elements.tiling: inner_width <= 0")
assert(inner_height > 0, "graphics.elements.tiling: inner_height <= 0")
assert(start_x <= inner_width, "graphics.elements.tiling: start_x > inner_width")
assert(start_y <= inner_height, "graphics.elements.tiling: start_y > inner_height")
-- create pattern
for y = start_y, height do
e.window.setCursorPos(1, y)
for _ = start_x, width do
for y = start_y, inner_height + (start_y - 1) do
e.window.setCursorPos(start_x, y)
for x = 1, inner_width do
if alternator then
if even then
e.window.blit(" ", "00", fill_a .. fill_a)
e.window.blit("NF", "00", fill_a .. fill_a)
else
e.window.blit(" ", "0", fill_a)
e.window.blit("F", "0", fill_a)
end
else
if even then
@ -73,7 +73,7 @@ local function tiling(args)
end
end
alternator = not alternator
if x ~= inner_width then alternator = not alternator end
end
end