From 4ef1915137503a04f443842f9eeaf03ee5e4630a Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Wed, 10 May 2023 11:08:24 -0400 Subject: [PATCH] #226 multi button updated for new graphics mouse events --- graphics/elements/controls/hazard_button.lua | 4 +-- graphics/elements/controls/multi_button.lua | 36 +++++++++++++------- graphics/events.lua | 5 +++ 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/graphics/elements/controls/hazard_button.lua b/graphics/elements/controls/hazard_button.lua index 19047cb..3db6a3b 100644 --- a/graphics/elements/controls/hazard_button.lua +++ b/graphics/elements/controls/hazard_button.lua @@ -6,8 +6,6 @@ local util = require("scada-common.util") local core = require("graphics.core") local element = require("graphics.element") -local CLICK_TYPE = core.events.CLICK_TYPE - ---@class hazard_button_args ---@field text string text to show on button ---@field accent color accent color for hazard border @@ -146,7 +144,7 @@ local function hazard_button(args) ---@param event mouse_interaction function e.handle_mouse(event) if e.enabled then - if event.type == CLICK_TYPE.TAP or event.type == CLICK_TYPE.UP 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) diff --git a/graphics/elements/controls/multi_button.lua b/graphics/elements/controls/multi_button.lua index 2549e2b..fa177f4 100644 --- a/graphics/elements/controls/multi_button.lua +++ b/graphics/elements/controls/multi_button.lua @@ -2,13 +2,13 @@ local util = require("scada-common.util") +local core = require("graphics.core") local element = require("graphics.element") ---@class button_option ---@field text string ---@field fg_bg cpair ---@field active_fg_bg cpair ----@field _lpad integer automatically calculated left pad ---@field _start_x integer starting touch x range (inclusive) ---@field _end_x integer ending touch x range (inclusive) @@ -62,9 +62,7 @@ local function multi_button(args) local next_x = 2 for i = 1, #args.options do local opt = args.options[i] ---@type button_option - local w = string.len(opt.text) - opt._lpad = math.floor((e.frame.w - w) / 2) opt._start_x = next_x opt._end_x = next_x + button_width - 1 @@ -92,20 +90,32 @@ local function multi_button(args) end end + -- check which button a given x is within + ---@return integer|nil button index or nil if not within a button + local function which_button(x) + for i = 1, #args.options do + local opt = args.options[i] ---@type button_option + if x >= opt._start_x and x <= opt._end_x then return i end + end + + return nil + end + -- handle mouse interaction ---@param event mouse_interaction mouse event ----@diagnostic disable-next-line: unused-local function e.handle_mouse(event) - -- determine what was pressed - if e.enabled and event.y == 1 then - for i = 1, #args.options do - local opt = args.options[i] ---@type button_option + -- 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 + -- 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) - if event.x >= opt._start_x and event.x <= opt._end_x then - e.value = i - draw() - args.callback(e.value) - end + -- mouse up must always have started with a mouse down on the same button to count as a click + -- tap always has identical coordinates, so this always passes for taps + if button_ini == button_cur and button_cur ~= nil then + e.value = button_cur + draw() + args.callback(e.value) end end end diff --git a/graphics/events.lua b/graphics/events.lua index 1b5219c..92055f0 100644 --- a/graphics/events.lua +++ b/graphics/events.lua @@ -109,6 +109,11 @@ function events.mouse_transposed(event, elem_pos_x, elem_pos_y) } end +-- check if an event qualifies as a click (tap or up) +---@nodiscard +---@param t CLICK_TYPE +function events.was_clicked(t) return t == events.CLICK_TYPE.TAP or t == events.CLICK_TYPE.UP end + -- create a new mouse event to pass onto graphics renderer
-- supports: mouse_click, mouse_up, mouse_drag, mouse_scroll, and monitor_touch ---@param event_type os_event OS event to handle