#233 fixed mouse enter/exit behavior via simplification

This commit is contained in:
Mikayla Fischler 2023-05-18 10:58:42 -04:00
parent 82e3fa494c
commit beda7624f4
4 changed files with 18 additions and 21 deletions

View File

@ -93,15 +93,6 @@ function element.new(args)
return "graphics.element{" .. self.elem_type .. "} @ " .. tostring(self)
end
-- check if a coordinate is within the bounds of this element
---@param x integer
---@param y integer
local function _in_bounds(x, y)
local in_x = x >= self.bounds.x1 and x <= self.bounds.x2
local in_y = y >= self.bounds.y1 and y <= self.bounds.y2
return in_x and in_y
end
---@class graphics_element
local public = {}
@ -188,6 +179,15 @@ function element.new(args)
self.bounds.y2 = self.position.y + f.h - 1
end
-- check if a coordinate is within the bounds of this element
---@param x integer
---@param y integer
function protected.in_bounds(x, y)
local in_x = x >= self.bounds.x1 and x <= self.bounds.x2
local in_y = y >= self.bounds.y1 and y <= self.bounds.y2
return in_x and in_y
end
-- luacheck: push ignore
---@diagnostic disable: unused-local, unused-vararg
@ -495,14 +495,12 @@ function element.new(args)
-- handle a monitor touch or mouse click
---@param event mouse_interaction mouse interaction event
function public.handle_mouse(event)
local x_ini, y_ini, x_cur, y_cur = event.initial.x, event.initial.y, event.current.x, event.current.y
local x_ini, y_ini = event.initial.x, event.initial.y
local ini_in = _in_bounds(x_ini, y_ini)
local cur_in = _in_bounds(x_cur, y_cur)
local ini_in = protected.in_bounds(x_ini, y_ini)
if ini_in then
local event_T = core.events.mouse_transposed(event, self.position.x, self.position.y)
if not cur_in then event_T.type = core.events.CLICK_TYPE.EXITED end
-- handle the mouse event then pass to children
protected.handle_mouse(event_T)

View File

@ -84,9 +84,9 @@ local function push_button(args)
show_pressed()
elseif event.type == CLICK_TYPE.UP then
show_unpressed()
args.callback()
elseif event.type == CLICK_TYPE.EXITED then
show_unpressed()
if e.in_bounds(event.current.x, event.current.y) then
args.callback()
end
end
end
end

View File

@ -93,14 +93,14 @@ local function sidebar(args)
elseif event.type == CLICK_TYPE.DOWN then
draw(true, cur_idx)
elseif event.type == CLICK_TYPE.UP then
if cur_idx == ini_idx then
if cur_idx == ini_idx and e.in_bounds(event.current.x, event.current.y) then
e.value = cur_idx
draw(false)
args.callback(e.value)
else draw(false) end
elseif event.type == CLICK_TYPE.EXITED then
draw(false)
end
elseif event.type == CLICK_TYPE.UP then
draw(false)
end
end
end

View File

@ -21,8 +21,7 @@ events.CLICK_TYPE = {
UP = 3, -- button up (completed a click)
DRAG = 4, -- mouse dragged
SCROLL_DOWN = 5, -- scroll down
SCROLL_UP = 6, -- scroll up
EXITED = 7 -- cursor exited bounds of element
SCROLL_UP = 6 -- scroll up
}
-- create a new 2D coordinate