From beda7624f48b5c414fe4fa2b83221f4a3741b941 Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Thu, 18 May 2023 10:58:42 -0400 Subject: [PATCH] #233 fixed mouse enter/exit behavior via simplification --- graphics/element.lua | 24 ++++++++++------------ graphics/elements/controls/push_button.lua | 6 +++--- graphics/elements/controls/sidebar.lua | 6 +++--- graphics/events.lua | 3 +-- 4 files changed, 18 insertions(+), 21 deletions(-) diff --git a/graphics/element.lua b/graphics/element.lua index 891257d..8355cd6 100644 --- a/graphics/element.lua +++ b/graphics/element.lua @@ -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) diff --git a/graphics/elements/controls/push_button.lua b/graphics/elements/controls/push_button.lua index ed0fc2a..7f91ea5 100644 --- a/graphics/elements/controls/push_button.lua +++ b/graphics/elements/controls/push_button.lua @@ -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 diff --git a/graphics/elements/controls/sidebar.lua b/graphics/elements/controls/sidebar.lua index 997b372..a20cb72 100644 --- a/graphics/elements/controls/sidebar.lua +++ b/graphics/elements/controls/sidebar.lua @@ -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 diff --git a/graphics/events.lua b/graphics/events.lua index 7370481..3391a18 100644 --- a/graphics/events.lua +++ b/graphics/events.lua @@ -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