added text alignment to push buttons and added keyboard events to listbox

This commit is contained in:
Mikayla Fischler 2023-10-19 23:20:04 -04:00
parent 01caca48dc
commit d77a527b15
4 changed files with 41 additions and 3 deletions

View File

@ -7,7 +7,7 @@ local flasher = require("graphics.flasher")
local core = {}
core.version = "2.0.4"
core.version = "2.0.5"
core.flasher = flasher
core.events = events

View File

@ -5,6 +5,8 @@ local tcd = require("scada-common.tcd")
local core = require("graphics.core")
local element = require("graphics.element")
local ALIGN = core.ALIGN
local MOUSE_CLICK = core.events.MOUSE_CLICK
local KEY_CLICK = core.events.KEY_CLICK
@ -12,6 +14,7 @@ local KEY_CLICK = core.events.KEY_CLICK
---@field text string button text
---@field callback function function to call on touch
---@field min_width? integer text length if omitted
---@field alignment? ALIGN text align if min width > length
---@field active_fg_bg? cpair foreground/background colors when pressed
---@field dis_fg_bg? cpair foreground/background colors when disabled
---@field parent graphics_element
@ -31,6 +34,7 @@ local function push_button(args)
element.assert(type(args.min_width) == "nil" or (type(args.min_width) == "number" and args.min_width > 0), "min_width must be nil or a number > 0")
local text_width = string.len(args.text)
local alignment = args.alignment or ALIGN.CENTER
-- set automatic settings
args.can_focus = true
@ -41,9 +45,15 @@ local function push_button(args)
-- create new graphics element base object
local e = element.new(args)
local h_pad = math.floor((e.frame.w - text_width) / 2) + 1
local h_pad = 1
local v_pad = math.floor(e.frame.h / 2) + 1
if alignment == ALIGN.CENTER then
h_pad = math.floor((e.frame.w - text_width) / 2) + 1
elseif alignment == ALIGN.RIGHT then
h_pad = (e.frame.w - text_width) + 1
end
-- draw the button
function e.redraw()
e.window.clear()

View File

@ -133,6 +133,9 @@ local function number_field(args)
elseif type(args.min) == "number" and val < min then
e.value = "" .. min
ifield.nav_start()
else
e.value = "" .. val
ifield.nav_end()
end
else
e.value = ""

View File

@ -5,6 +5,7 @@ local tcd = require("scada-common.tcd")
local core = require("graphics.core")
local element = require("graphics.element")
local KEY_CLICK = core.events.KEY_CLICK
local MOUSE_CLICK = core.events.MOUSE_CLICK
---@class listbox_args
@ -33,6 +34,8 @@ local MOUSE_CLICK = core.events.MOUSE_CLICK
---@param args listbox_args
---@return graphics_element element, element_id id
local function listbox(args)
args.can_focus = true
-- create new graphics element base object
local e = element.new(args)
@ -128,7 +131,7 @@ local function listbox(args)
end
e.w_set_cur(e.frame.w, i)
e.w_write(" ")
if e.is_focused() then e.w_write("\x7f") else e.w_write(" ") end
end
e.w_set_bkg(e.fg_bg.bkg)
@ -222,6 +225,10 @@ local function listbox(args)
end
end
-- handle focus
e.on_focused = draw_bar
e.on_unfocused = draw_bar
-- handle a child in the list being focused, make sure it is visible
function e.on_child_focused(child)
for i = 1, #list do
@ -303,6 +310,24 @@ local function listbox(args)
end
end
-- handle keyboard interaction
---@param event key_interaction key event
function e.handle_key(event)
if event.type == KEY_CLICK.DOWN or event.type == KEY_CLICK.HELD then
if event.key == keys.up then
scroll_up()
elseif event.key == keys.down then
scroll_down()
elseif event.key == keys.home then
scroll_offset = 0
update_positions()
elseif event.key == keys["end"] then
scroll_offset = max_down_scroll
update_positions()
end
end
end
-- element redraw
function e.redraw()
draw_arrows(0)