From 6e95fe9007bd55a81e4d93fbf984f1e7caa2dd81 Mon Sep 17 00:00:00 2001 From: Gabriel Tofvesson Date: Tue, 8 Oct 2024 16:13:43 +0200 Subject: [PATCH] Simplify click detection --- gfx/element.lua | 2 +- gfx/list.lua | 11 ++++++----- gfx/padding.lua | 4 +--- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/gfx/element.lua b/gfx/element.lua index 9ee5153..ba5efb6 100644 --- a/gfx/element.lua +++ b/gfx/element.lua @@ -186,7 +186,7 @@ function Element:redraw() end function Element:handleEvent(evt) - if Event.isClickEvent(evt) and self.onClick ~= nil then + if Event.isClickEvent(evt) and self.onClick ~= nil and Event.containsClick(self, evt, 0, 0) then local x, y, source = Event.getClickParams(evt) return not not self.onClick(self, x, y, source) end diff --git a/gfx/list.lua b/gfx/list.lua index 920590e..7aa5a3b 100644 --- a/gfx/list.lua +++ b/gfx/list.lua @@ -135,8 +135,8 @@ function List:handleEvent(evt) local wOffset = 0 local hOffset = 0 for _,child in ipairs(self.children) do - if Event.containsClick(child, evt, -wOffset, -hOffset) then - return child:handleEvent({Event.repositionEvent(evt, -wOffset, -hOffset)}) + if child:handleEvent({Event.repositionEvent(evt, -wOffset, -hOffset)}) then + return true end if self:isVertical() then @@ -147,11 +147,12 @@ function List:handleEvent(evt) end return false else - local handled = false for _,child in ipairs(self.children) do - handled = child:handleEvent(evt) or handled + if child:handleEvent(evt) then + return true + end end - return handled + return false end end diff --git a/gfx/padding.lua b/gfx/padding.lua index 7ad347c..f71a4ff 100644 --- a/gfx/padding.lua +++ b/gfx/padding.lua @@ -86,9 +86,7 @@ function Padding:handleEvent(evt) end if Event.isClickEvent(evt) then - if Event.containsClick(self.element, evt, -self:getPaddingLeft(), -self:getPaddingTop()) then - self.element:handleEvent({Event.repositionEvent(evt, -self:getPaddingLeft(), -self:getPaddingTop())}) - end + return self.element:handleEvent({Event.repositionEvent(evt, -self:getPaddingLeft(), -self:getPaddingTop())}) else return self.element:handleEvent(evt) end