mirror of
https://github.com/MikaylaFischler/cc-mek-scada.git
synced 2024-08-30 18:22:34 +00:00
#226 multi button updated for new graphics mouse events
This commit is contained in:
parent
40fa0de7a3
commit
4ef1915137
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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<br>
|
||||
-- supports: mouse_click, mouse_up, mouse_drag, mouse_scroll, and monitor_touch
|
||||
---@param event_type os_event OS event to handle
|
||||
|
Loading…
Reference in New Issue
Block a user