diff --git a/graphics/elements/controls/multi_button.lua b/graphics/elements/controls/multi_button.lua index fa177f4..e44bad0 100644 --- a/graphics/elements/controls/multi_button.lua +++ b/graphics/elements/controls/multi_button.lua @@ -105,7 +105,7 @@ local function multi_button(args) ---@param event mouse_interaction mouse event function e.handle_mouse(event) -- if enabled and the button row was pressed... - if e.enabled and core.events.was_clicked(event.type) and (event.initial.y == 1) and (event.current.y == 1) then + if e.enabled and core.events.was_clicked(event.type) then -- a button may have been pressed, which one was it? local button_ini = which_button(event.initial.x) local button_cur = which_button(event.current.x) diff --git a/graphics/elements/controls/push_button.lua b/graphics/elements/controls/push_button.lua index 64a5aa4..ed0fc2a 100644 --- a/graphics/elements/controls/push_button.lua +++ b/graphics/elements/controls/push_button.lua @@ -26,6 +26,8 @@ local CLICK_TYPE = core.events.CLICK_TYPE local function push_button(args) assert(type(args.text) == "string", "graphics.elements.controls.push_button: text is a required field") assert(type(args.callback) == "function", "graphics.elements.controls.push_button: callback is a required field") + assert(type(args.min_width) == "nil" or (type(args.min_width) == "number" and args.min_width > 0), + "graphics.elements.controls.push_button: min_width must be nil or a number > 0") local text_width = string.len(args.text) diff --git a/graphics/elements/controls/switch_button.lua b/graphics/elements/controls/switch_button.lua index 344f757..645bf8a 100644 --- a/graphics/elements/controls/switch_button.lua +++ b/graphics/elements/controls/switch_button.lua @@ -23,13 +23,15 @@ local function switch_button(args) assert(type(args.text) == "string", "graphics.elements.controls.switch_button: text is a required field") assert(type(args.callback) == "function", "graphics.elements.controls.switch_button: callback is a required field") assert(type(args.active_fg_bg) == "table", "graphics.elements.controls.switch_button: active_fg_bg is a required field") + assert(type(args.min_width) == "nil" or (type(args.min_width) == "number" and args.min_width > 0), + "graphics.elements.controls.switch_button: min_width must be nil or a number > 0") - -- single line - args.height = 1 - - -- determine widths local text_width = string.len(args.text) - args.width = math.max(text_width + 2, args.min_width) + + -- single line height, calculate width + args.height = 1 + args.min_width = args.min_width or 0 + args.width = math.max(text_width, args.min_width) -- create new graphics element base object local e = element.new(args) diff --git a/graphics/events.lua b/graphics/events.lua index 92055f0..7370481 100644 --- a/graphics/events.lua +++ b/graphics/events.lua @@ -25,6 +25,12 @@ events.CLICK_TYPE = { EXITED = 7 -- cursor exited bounds of element } +-- create a new 2D coordinate +---@param x integer +---@param y integer +---@return coordinate_2d +local function _coord2d(x, y) return { x = x, y = y } end + ---@class mouse_interaction ---@field monitor string ---@field button CLICK_BUTTON @@ -33,15 +39,14 @@ events.CLICK_TYPE = { ---@field current coordinate_2d local handler = { - button_down = { { 0, 0 }, { 0, 0 }, { 0, 0 } } -- left, right, middle button down tracking + -- left, right, middle button down tracking + button_down = { + _coord2d(0, 0), + _coord2d(0, 0), + _coord2d(0, 0) + } } --- create a new 2D coordinate ----@param x integer ----@param y integer ----@return coordinate_2d -local function _coord2d(x, y) return { x = x, y = y } end - -- create a new monitor touch mouse interaction event ---@nodiscard ---@param monitor string @@ -124,7 +129,7 @@ function events.was_clicked(t) return t == events.CLICK_TYPE.TAP or t == events. function events.new_mouse_event(event_type, opt, x, y) if event_type == "mouse_click" then ---@cast opt 1|2|3 - handler.button_down[opt] = { x, y } + handler.button_down[opt] = _coord2d(x, y) return _mouse_event(opt, events.CLICK_TYPE.DOWN, x, y, x, y) elseif event_type == "mouse_up" then ---@cast opt 1|2|3 diff --git a/pocket/renderer.lua b/pocket/renderer.lua index 1ecf3ab..fa25bcd 100644 --- a/pocket/renderer.lua +++ b/pocket/renderer.lua @@ -70,9 +70,9 @@ end function renderer.ui_ready() return ui.display ~= nil end -- handle a mouse event ----@param event mouse_interaction +---@param event mouse_interaction|nil function renderer.handle_mouse(event) - if ui.display ~= nil then + if ui.display ~= nil and event ~= nil then ui.display.handle_mouse(event) end end diff --git a/pocket/startup.lua b/pocket/startup.lua index 9516340..42752a8 100644 --- a/pocket/startup.lua +++ b/pocket/startup.lua @@ -152,9 +152,9 @@ local function main() -- got a packet local packet = pocket_comms.parse_packet(param1, param2, param3, param4, param5) pocket_comms.handle_packet(packet) - elseif event == "mouse_click" then + elseif event == "mouse_click" or event == "mouse_up" or event == "mouse_drag" or event == "mouse_scroll" then -- handle a monitor touch event - renderer.handle_mouse(core.events.touch(param1, param2, param3)) + renderer.handle_mouse(core.events.new_mouse_event(event, param1, param2, param3)) end -- check for termination request