#300 graphics alias functions

This commit is contained in:
Mikayla Fischler
2023-08-30 21:11:57 -04:00
parent 048714817e
commit 3afc765f72
33 changed files with 402 additions and 387 deletions

View File

@ -36,9 +36,8 @@ local function app_button(args)
local e = element.new(args)
-- write app title, centered
e.window.setCursorPos(1, 4)
e.window.setCursorPos(math.floor((e.frame.w - string.len(args.title)) / 2) + 1, 4)
e.window.write(args.title)
e.w_set_cur(math.floor((e.frame.w - string.len(args.title)) / 2) + 1, 4)
e.w_write(args.title)
-- draw the app button
local function draw()
@ -51,36 +50,36 @@ local function app_button(args)
end
-- draw icon
e.window.setCursorPos(1, 1)
e.window.setTextColor(fgd)
e.window.setBackgroundColor(bkg)
e.window.write("\x9f\x83\x83\x83")
e.window.setTextColor(bkg)
e.window.setBackgroundColor(fgd)
e.window.write("\x90")
e.window.setTextColor(fgd)
e.window.setBackgroundColor(bkg)
e.window.setCursorPos(1, 2)
e.window.write("\x95 ")
e.window.setTextColor(bkg)
e.window.setBackgroundColor(fgd)
e.window.write("\x95")
e.window.setCursorPos(1, 3)
e.window.write("\x82\x8f\x8f\x8f\x81")
e.w_set_cur(1, 1)
e.w_set_fgd(fgd)
e.w_set_bkg(bkg)
e.w_write("\x9f\x83\x83\x83")
e.w_set_fgd(bkg)
e.w_set_bkg(fgd)
e.w_write("\x90")
e.w_set_fgd(fgd)
e.w_set_bkg(bkg)
e.w_set_cur(1, 2)
e.w_write("\x95 ")
e.w_set_fgd(bkg)
e.w_set_bkg(fgd)
e.w_write("\x95")
e.w_set_cur(1, 3)
e.w_write("\x82\x8f\x8f\x8f\x81")
-- write the icon text
e.window.setCursorPos(3, 2)
e.window.setTextColor(fgd)
e.window.setBackgroundColor(bkg)
e.window.write(args.text)
e.w_set_cur(3, 2)
e.w_set_fgd(fgd)
e.w_set_bkg(bkg)
e.w_write(args.text)
end
-- draw the app button as pressed (if active_fg_bg set)
local function show_pressed()
if e.enabled and args.active_fg_bg ~= nil then
e.value = true
e.window.setTextColor(args.active_fg_bg.fgd)
e.window.setBackgroundColor(args.active_fg_bg.bkg)
e.w_set_fgd(args.active_fg_bg.fgd)
e.w_set_bkg(args.active_fg_bg.bkg)
draw()
end
end
@ -89,8 +88,8 @@ local function app_button(args)
local function show_unpressed()
if e.enabled and args.active_fg_bg ~= nil then
e.value = false
e.window.setTextColor(e.fg_bg.fgd)
e.window.setBackgroundColor(e.fg_bg.bkg)
e.w_set_fgd(e.fg_bg.fgd)
e.w_set_bkg(e.fg_bg.bkg)
draw()
end
end

View File

@ -32,24 +32,24 @@ local function checkbox(args)
-- show the button state
local function draw()
e.window.setCursorPos(1, 1)
e.w_set_cur(1, 1)
if e.value then
-- show as selected
e.window.setTextColor(args.box_fg_bg.bkg)
e.window.setBackgroundColor(args.box_fg_bg.fgd)
e.window.write("\x88")
e.window.setTextColor(args.box_fg_bg.fgd)
e.window.setBackgroundColor(e.fg_bg.bkg)
e.window.write("\x95")
e.w_set_fgd(args.box_fg_bg.bkg)
e.w_set_bkg(args.box_fg_bg.fgd)
e.w_write("\x88")
e.w_set_fgd(args.box_fg_bg.fgd)
e.w_set_bkg(e.fg_bg.bkg)
e.w_write("\x95")
else
-- show as unselected
e.window.setTextColor(e.fg_bg.bkg)
e.window.setBackgroundColor(args.box_fg_bg.bkg)
e.window.write("\x88")
e.window.setTextColor(args.box_fg_bg.bkg)
e.window.setBackgroundColor(e.fg_bg.bkg)
e.window.write("\x95")
e.w_set_fgd(e.fg_bg.bkg)
e.w_set_bkg(args.box_fg_bg.bkg)
e.w_write("\x88")
e.w_set_fgd(args.box_fg_bg.bkg)
e.w_set_bkg(e.fg_bg.bkg)
e.w_write("\x95")
end
end
@ -71,10 +71,10 @@ local function checkbox(args)
end
-- write label text
e.window.setCursorPos(3, 1)
e.window.setTextColor(e.fg_bg.fgd)
e.window.setBackgroundColor(e.fg_bg.bkg)
e.window.write(args.label)
e.w_set_cur(3, 1)
e.w_set_fgd(e.fg_bg.fgd)
e.w_set_bkg(e.fg_bg.bkg)
e.w_write(args.label)
-- initial draw
draw()

View File

@ -33,35 +33,35 @@ local function hazard_button(args)
local e = element.new(args)
-- write the button text
e.window.setCursorPos(3, 2)
e.window.write(args.text)
e.w_set_cur(3, 2)
e.w_write(args.text)
-- draw border
---@param accent color accent color
local function draw_border(accent)
-- top
e.window.setTextColor(accent)
e.window.setBackgroundColor(args.fg_bg.bkg)
e.window.setCursorPos(1, 1)
e.window.write("\x99" .. string.rep("\x89", args.width - 2) .. "\x99")
e.w_set_fgd(accent)
e.w_set_bkg(args.fg_bg.bkg)
e.w_set_cur(1, 1)
e.w_write("\x99" .. string.rep("\x89", args.width - 2) .. "\x99")
-- center left
e.window.setCursorPos(1, 2)
e.window.setTextColor(args.fg_bg.bkg)
e.window.setBackgroundColor(accent)
e.window.write("\x99")
e.w_set_cur(1, 2)
e.w_set_fgd(args.fg_bg.bkg)
e.w_set_bkg(accent)
e.w_write("\x99")
-- center right
e.window.setTextColor(args.fg_bg.bkg)
e.window.setBackgroundColor(accent)
e.window.setCursorPos(args.width, 2)
e.window.write("\x99")
e.w_set_fgd(args.fg_bg.bkg)
e.w_set_bkg(accent)
e.w_set_cur(args.width, 2)
e.w_write("\x99")
-- bottom
e.window.setTextColor(accent)
e.window.setBackgroundColor(args.fg_bg.bkg)
e.window.setCursorPos(1, 3)
e.window.write("\x99" .. string.rep("\x98", args.width - 2) .. "\x99")
e.w_set_fgd(accent)
e.w_set_bkg(args.fg_bg.bkg)
e.w_set_cur(1, 3)
e.w_write("\x99" .. string.rep("\x98", args.width - 2) .. "\x99")
end
-- on request timeout: recursively calls itself to double flash button text
@ -72,9 +72,9 @@ local function hazard_button(args)
if n == 0 then
-- go back off
e.window.setTextColor(args.fg_bg.fgd)
e.window.setCursorPos(3, 2)
e.window.write(args.text)
e.w_set_fgd(args.fg_bg.fgd)
e.w_set_cur(3, 2)
e.w_write(args.text)
end
if n >= 4 then
@ -82,18 +82,18 @@ local function hazard_button(args)
elseif n % 2 == 0 then
-- toggle text color on after 0.25 seconds
tcd.dispatch(0.25, function ()
e.window.setTextColor(args.accent)
e.window.setCursorPos(3, 2)
e.window.write(args.text)
e.w_set_fgd(args.accent)
e.w_set_cur(3, 2)
e.w_write(args.text)
on_timeout(n + 1)
on_timeout(n + 1)
end)
elseif n % 1 then
-- toggle text color off after 0.25 seconds
tcd.dispatch(0.25, function ()
e.window.setTextColor(args.fg_bg.fgd)
e.window.setCursorPos(3, 2)
e.window.write(args.text)
e.w_set_fgd(args.fg_bg.fgd)
e.w_set_cur(3, 2)
e.w_write(args.text)
on_timeout(n + 1)
end)
end
@ -101,9 +101,9 @@ local function hazard_button(args)
-- blink routine for success indication
local function on_success()
e.window.setTextColor(args.fg_bg.fgd)
e.window.setCursorPos(3, 2)
e.window.write(args.text)
e.w_set_fgd(args.fg_bg.fgd)
e.w_set_cur(3, 2)
e.w_write(args.text)
end
-- blink routine for failure indication
@ -114,9 +114,9 @@ local function hazard_button(args)
if n == 0 then
-- go back off
e.window.setTextColor(args.fg_bg.fgd)
e.window.setCursorPos(3, 2)
e.window.write(args.text)
e.w_set_fgd(args.fg_bg.fgd)
e.w_set_cur(3, 2)
e.w_write(args.text)
end
if n >= 2 then
@ -124,17 +124,17 @@ local function hazard_button(args)
elseif n % 2 == 0 then
-- toggle text color on after 0.5 seconds
tcd.dispatch(0.5, function ()
e.window.setTextColor(args.accent)
e.window.setCursorPos(3, 2)
e.window.write(args.text)
e.w_set_fgd(args.accent)
e.w_set_cur(3, 2)
e.w_write(args.text)
on_failure(n + 1)
end)
elseif n % 1 then
-- toggle text color off after 0.25 seconds
tcd.dispatch(0.25, function ()
e.window.setTextColor(args.fg_bg.fgd)
e.window.setCursorPos(3, 2)
e.window.write(args.text)
e.w_set_fgd(args.fg_bg.fgd)
e.w_set_cur(3, 2)
e.w_write(args.text)
on_failure(n + 1)
end)
end
@ -146,9 +146,9 @@ local function hazard_button(args)
if e.enabled then
if core.events.was_clicked(event.type) then
-- change text color to indicate clicked
e.window.setTextColor(args.accent)
e.window.setCursorPos(3, 2)
e.window.write(args.text)
e.w_set_fgd(args.accent)
e.w_set_cur(3, 2)
e.w_write(args.text)
-- abort any other callbacks
tcd.abort(on_timeout)
@ -181,18 +181,18 @@ local function hazard_button(args)
function e.disable()
if args.dis_colors then
draw_border(args.dis_colors.color_a)
e.window.setTextColor(args.dis_colors.color_b)
e.window.setCursorPos(3, 2)
e.window.write(args.text)
e.w_set_fgd(args.dis_colors.color_b)
e.w_set_cur(3, 2)
e.w_write(args.text)
end
end
-- show the button as enabled
function e.enable()
draw_border(args.accent)
e.window.setTextColor(args.fg_bg.fgd)
e.window.setCursorPos(3, 2)
e.window.write(args.text)
e.w_set_fgd(args.fg_bg.fgd)
e.w_set_cur(3, 2)
e.w_write(args.text)
end
-- initial draw of border

View File

@ -75,19 +75,19 @@ local function multi_button(args)
for i = 1, #args.options do
local opt = args.options[i] ---@type button_option
e.window.setCursorPos(opt._start_x, 1)
e.w_set_cur(opt._start_x, 1)
if e.value == i then
-- show as pressed
e.window.setTextColor(opt.active_fg_bg.fgd)
e.window.setBackgroundColor(opt.active_fg_bg.bkg)
e.w_set_fgd(opt.active_fg_bg.fgd)
e.w_set_bkg(opt.active_fg_bg.bkg)
else
-- show as unpressed
e.window.setTextColor(opt.fg_bg.fgd)
e.window.setBackgroundColor(opt.fg_bg.bkg)
e.w_set_fgd(opt.fg_bg.fgd)
e.w_set_bkg(opt.fg_bg.bkg)
end
e.window.write(util.pad(opt.text, button_width))
e.w_write(util.pad(opt.text, button_width))
end
end

View File

@ -48,16 +48,16 @@ local function push_button(args)
e.window.clear()
-- write the button text
e.window.setCursorPos(h_pad, v_pad)
e.window.write(args.text)
e.w_set_cur(h_pad, v_pad)
e.w_write(args.text)
end
-- draw the button as pressed (if active_fg_bg set)
local function show_pressed()
if e.enabled and args.active_fg_bg ~= nil then
e.value = true
e.window.setTextColor(args.active_fg_bg.fgd)
e.window.setBackgroundColor(args.active_fg_bg.bkg)
e.w_set_fgd(args.active_fg_bg.fgd)
e.w_set_bkg(args.active_fg_bg.bkg)
draw()
end
end
@ -66,8 +66,8 @@ local function push_button(args)
local function show_unpressed()
if e.enabled and args.active_fg_bg ~= nil then
e.value = false
e.window.setTextColor(e.fg_bg.fgd)
e.window.setBackgroundColor(e.fg_bg.bkg)
e.w_set_fgd(e.fg_bg.fgd)
e.w_set_bkg(e.fg_bg.bkg)
draw()
end
end
@ -102,8 +102,8 @@ local function push_button(args)
function e.enable()
if args.dis_fg_bg ~= nil then
e.value = false
e.window.setTextColor(e.fg_bg.fgd)
e.window.setBackgroundColor(e.fg_bg.bkg)
e.w_set_fgd(e.fg_bg.fgd)
e.w_set_bkg(e.fg_bg.bkg)
draw()
end
end
@ -112,8 +112,8 @@ local function push_button(args)
function e.disable()
if args.dis_fg_bg ~= nil then
e.value = false
e.window.setTextColor(args.dis_fg_bg.fgd)
e.window.setBackgroundColor(args.dis_fg_bg.bkg)
e.w_set_fgd(args.dis_fg_bg.fgd)
e.w_set_bkg(args.dis_fg_bg.bkg)
draw()
end
end

View File

@ -56,28 +56,28 @@ local function radio_button(args)
for i = 1, #args.options do
local opt = args.options[i] ---@type string
e.window.setCursorPos(1, i)
e.w_set_cur(1, i)
if e.value == i then
-- show as selected
e.window.setTextColor(args.radio_colors.color_a)
e.window.setBackgroundColor(args.radio_bg)
e.w_set_fgd(args.radio_colors.color_a)
e.w_set_bkg(args.radio_bg)
else
-- show as unselected
e.window.setTextColor(args.radio_colors.color_b)
e.window.setBackgroundColor(args.radio_bg)
e.w_set_fgd(args.radio_colors.color_b)
e.w_set_bkg(args.radio_bg)
end
e.window.write("\x88")
e.w_write("\x88")
e.window.setTextColor(args.radio_bg)
e.window.setBackgroundColor(e.fg_bg.bkg)
e.window.write("\x95")
e.w_set_fgd(args.radio_bg)
e.w_set_bkg(e.fg_bg.bkg)
e.w_write("\x95")
-- write button text
e.window.setTextColor(e.fg_bg.fgd)
e.window.setBackgroundColor(e.fg_bg.bkg)
e.window.write(opt)
e.w_set_fgd(e.fg_bg.fgd)
e.w_set_bkg(e.fg_bg.bkg)
e.w_write(opt)
end
end

View File

@ -52,27 +52,27 @@ local function sidebar(args)
local y = ((i - 1) * 3) + 1
e.window.setCursorPos(1, y)
e.w_set_cur(1, y)
if pressed and i == pressed_idx then
e.window.setTextColor(e.fg_bg.fgd)
e.window.setBackgroundColor(e.fg_bg.bkg)
e.w_set_fgd(e.fg_bg.fgd)
e.w_set_bkg(e.fg_bg.bkg)
else
e.window.setTextColor(tab.color.fgd)
e.window.setBackgroundColor(tab.color.bkg)
e.w_set_fgd(tab.color.fgd)
e.w_set_bkg(tab.color.bkg)
end
e.window.write(" ")
e.window.setCursorPos(1, y + 1)
e.w_write(" ")
e.w_set_cur(1, y + 1)
if e.value == i then
-- show as selected
e.window.write(" " .. tab.char .. "\x10")
e.w_write(" " .. tab.char .. "\x10")
else
-- show as unselected
e.window.write(" " .. tab.char .. " ")
e.w_write(" " .. tab.char .. " ")
end
e.window.setCursorPos(1, y + 2)
e.window.write(" ")
e.w_set_cur(1, y + 2)
e.w_write(" ")
end
end

View File

@ -58,17 +58,17 @@ local function spinbox(args)
-- draw the arrows
local function draw_arrows(color)
e.window.setBackgroundColor(args.arrow_fg_bg.bkg)
e.window.setTextColor(color)
e.window.setCursorPos(1, 1)
e.window.write(string.rep("\x1e", wn_prec))
e.window.setCursorPos(1, 3)
e.window.write(string.rep("\x1f", wn_prec))
e.w_set_bkg(args.arrow_fg_bg.bkg)
e.w_set_fgd(color)
e.w_set_cur(1, 1)
e.w_write(string.rep("\x1e", wn_prec))
e.w_set_cur(1, 3)
e.w_write(string.rep("\x1f", wn_prec))
if fr_prec > 0 then
e.window.setCursorPos(1 + wn_prec, 1)
e.window.write(" " .. string.rep("\x1e", fr_prec))
e.window.setCursorPos(1 + wn_prec, 3)
e.window.write(" " .. string.rep("\x1f", fr_prec))
e.w_set_cur(1 + wn_prec, 1)
e.w_write(" " .. string.rep("\x1e", fr_prec))
e.w_set_cur(1 + wn_prec, 3)
e.w_write(" " .. string.rep("\x1f", fr_prec))
end
end
@ -119,10 +119,10 @@ local function spinbox(args)
end
-- draw
e.window.setBackgroundColor(e.fg_bg.bkg)
e.window.setTextColor(e.fg_bg.fgd)
e.window.setCursorPos(1, 2)
e.window.write(util.sprintf(fmt, e.value))
e.w_set_bkg(e.fg_bg.bkg)
e.w_set_fgd(e.fg_bg.fgd)
e.w_set_cur(1, 2)
e.w_write(util.sprintf(fmt, e.value))
end
-- init with the default value

View File

@ -47,20 +47,20 @@ local function switch_button(args)
local function draw_state()
if e.value then
-- show as pressed
e.window.setTextColor(args.active_fg_bg.fgd)
e.window.setBackgroundColor(args.active_fg_bg.bkg)
e.w_set_fgd(args.active_fg_bg.fgd)
e.w_set_bkg(args.active_fg_bg.bkg)
else
-- show as unpressed
e.window.setTextColor(e.fg_bg.fgd)
e.window.setBackgroundColor(e.fg_bg.bkg)
e.w_set_fgd(e.fg_bg.fgd)
e.w_set_bkg(e.fg_bg.bkg)
end
-- clear to redraw background
e.window.clear()
-- write the button text
e.window.setCursorPos(h_pad, v_pad)
e.window.write(args.text)
e.w_set_cur(h_pad, v_pad)
e.w_write(args.text)
end
-- initial draw

View File

@ -71,17 +71,17 @@ local function tabbar(args)
for i = 1, #args.tabs do
local tab = args.tabs[i] ---@type tabbar_tab
e.window.setCursorPos(tab._start_x, 1)
e.w_set_cur(tab._start_x, 1)
if e.value == i then
e.window.setTextColor(tab.color.fgd)
e.window.setBackgroundColor(tab.color.bkg)
e.w_set_fgd(tab.color.fgd)
e.w_set_bkg(tab.color.bkg)
else
e.window.setTextColor(e.fg_bg.fgd)
e.window.setBackgroundColor(e.fg_bg.bkg)
e.w_set_fgd(e.fg_bg.fgd)
e.w_set_bkg(e.fg_bg.bkg)
end
e.window.write(util.pad(tab.name, button_width))
e.w_write(util.pad(tab.name, button_width))
end
end