From a13b2fb2085db7f2755ab29f99ac83052a183f0e Mon Sep 17 00:00:00 2001 From: Gabriel Tofvesson Date: Tue, 8 Oct 2024 17:33:37 +0200 Subject: [PATCH] Assume element-local coordinates in handler --- gfx/element.lua | 1 - gfx/event.lua | 4 ++++ gfx/list.lua | 13 ++----------- gfx/padding.lua | 4 +--- 4 files changed, 7 insertions(+), 15 deletions(-) diff --git a/gfx/element.lua b/gfx/element.lua index 9494c27..4c6b9cb 100644 --- a/gfx/element.lua +++ b/gfx/element.lua @@ -186,7 +186,6 @@ function Element:redraw() end function Element:handleEvent(evt) - evt = Event.repositionEvent(evt, 1 - self:getX(), 1 - self:getY()) if Event.isClickEvent(evt) and Event.containsClick(self, evt) and self.onClick ~= nil then local x, y, source = Event.getClickParams(evt) return not not self.onClick(self, x, y, source) diff --git a/gfx/event.lua b/gfx/event.lua index d179a6a..ab093db 100644 --- a/gfx/event.lua +++ b/gfx/event.lua @@ -16,6 +16,10 @@ function Event.repositionEvent(evt, dX, dY) end end +function Event.toElementLocalPosition(evt, element) + return {Event.repositionEvent(evt, 1 - element:getX(), 1 - element:getY())} +end + function Event.containsClick(element, evt, dX, dY) local x, y = Event.getClickParams(evt) x = x + (dX or 0) diff --git a/gfx/list.lua b/gfx/list.lua index 641582c..ecaed00 100644 --- a/gfx/list.lua +++ b/gfx/list.lua @@ -127,7 +127,6 @@ function List:findById(id) end function List:handleEvent(evt) - evt = Event.repositionEvent(evt, 1 - self:getX(), 1 - self:getY()) if Element.handleEvent(self, evt) then return true end @@ -138,23 +137,15 @@ function List:handleEvent(evt) return false end - local wOffset = 0 - local hOffset = 0 for _,child in ipairs(self.children) do - if child:handleEvent(evt) then + if child:handleEvent(Event.toElementLocalPosition(evt, child)) then return true end - - if self:isVertical() then - hOffset = hOffset + child:getHeight() - else - wOffset = wOffset + child:getWidth() - end end return false else for _,child in ipairs(self.children) do - if child:handleEvent(evt) then + if child:handleEvent(Event.toElementLocalPosition(evt, child)) then return true end end diff --git a/gfx/padding.lua b/gfx/padding.lua index 88078ac..97a762e 100644 --- a/gfx/padding.lua +++ b/gfx/padding.lua @@ -81,14 +81,12 @@ function Padding:findById(id) end function Padding:handleEvent(evt) - evt = Event.repositionEvent(evt, 1 - self:getX(), 1 - self:getY()) - if Element.handleEvent(self, evt) then return true end if Event.isClickEvent(evt) then - return Event.containsClick(self, evt) and self.element:handleEvent(evt) + return Event.containsClick(self, evt) and self.element:handleEvent(Event.toElementLocalPosition(evt, self.element)) else return self.element:handleEvent(evt) end