improved ignoring mouse events for hidden elements

This commit is contained in:
Mikayla Fischler 2023-09-05 15:32:45 -04:00
parent 1525ed9d60
commit 1cb240b1b0
3 changed files with 20 additions and 7 deletions

View File

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

View File

@ -4,6 +4,8 @@
local core = require("graphics.core")
local events = core.events
local element = {}
---@class graphics_args_generic
@ -75,6 +77,7 @@ function element.new(args, child_offset_x, child_offset_y)
next_y = 1, -- next child y coordinate
next_id = 0, -- next child ID
subscriptions = {},
button_down = { events.new_coord_2d(-1, -1), events.new_coord_2d(-1, -1), events.new_coord_2d(-1, -1) },
mt = {}
}
@ -550,12 +553,24 @@ function element.new(args, child_offset_x, child_offset_y)
local ini_in = protected.in_window_bounds(x_ini, y_ini)
if ini_in then
local event_T = core.events.mouse_transposed(event, self.position.x, self.position.y)
if event.type == events.CLICK_TYPE.UP or event.type == events.CLICK_TYPE.DRAG then
-- make sure we don't handle mouse events that started before this element was made visible
if (event.initial.x ~= self.button_down[event.button].x) or (event.initial.y ~= self.button_down[event.button].y) then
return
end
elseif event.type == events.CLICK_TYPE.DOWN then
self.button_down[event.button] = event.initial
end
local event_T = events.mouse_transposed(event, self.position.x, self.position.y)
-- handle the mouse event then pass to children
protected.handle_mouse(event_T)
for _, child in pairs(protected.children) do child.handle_mouse(event_T) end
end
elseif event.type == events.CLICK_TYPE.DOWN then
-- don't track this click
self.button_down[event.button] = events.new_coord_2d(-1, -1)
end
end

View File

@ -30,6 +30,8 @@ events.CLICK_TYPE = {
---@return coordinate_2d
local function _coord2d(x, y) return { x = x, y = y } end
events.new_coord_2d = _coord2d
---@class mouse_interaction
---@field monitor string
---@field button CLICK_BUTTON
@ -39,11 +41,7 @@ local function _coord2d(x, y) return { x = x, y = y } end
local handler = {
-- left, right, middle button down tracking
button_down = {
_coord2d(0, 0),
_coord2d(0, 0),
_coord2d(0, 0)
}
button_down = { _coord2d(0, 0), _coord2d(0, 0), _coord2d(0, 0) }
}
-- create a new monitor touch mouse interaction event