From aa421bf2779bd1fcdb9a75580abfa020682bcdf5 Mon Sep 17 00:00:00 2001 From: NiLuJe Date: Fri, 31 Jan 2025 18:30:23 +0100 Subject: [PATCH] GestureDetector: Translate start & end gesture positions when rotated (#13101) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * GestureDetector:adjustGesCoordinate: Remove one layer of indirectiçon in direction translation That function call wrapper felt pretty unnecessary to me ;). * GestureDetector: Translate complex gesture positions, too i.e., if there's a start and end position, translate those, too. Fix #13090 --- frontend/device/gesturedetector.lua | 61 ++++++++++++++++++++--------- spec/unit/gesturedetector_spec.lua | 2 + 2 files changed, 45 insertions(+), 18 deletions(-) diff --git a/frontend/device/gesturedetector.lua b/frontend/device/gesturedetector.lua index a23c1cd83..a571a98bd 100644 --- a/frontend/device/gesturedetector.lua +++ b/frontend/device/gesturedetector.lua @@ -1387,13 +1387,44 @@ local ges_coordinate_translation_90 = { southeast = "southwest", southwest = "northwest", } -local function translateGesDirCoordinate(direction, translation_table) - return translation_table[direction] -end local function translateMultiswipeGesDirCoordinate(multiswipe_directions, translation_table) return multiswipe_directions:gsub("%S+", translation_table) end +function GestureDetector:translateCoordinates(ges, mode) + local translate + if mode == self.screen.DEVICE_ROTATED_CLOCKWISE then + -- 1 + local width = self.screen:getWidth() + translate = function(geom) + if not geom then return end + geom.x, geom.y = (width - geom.y), (geom.x) + end + elseif mode == self.screen.DEVICE_ROTATED_COUNTER_CLOCKWISE then + -- 3 + local height = self.screen:getHeight() + translate = function(geom) + if not geom then return end + geom.x, geom.y = (geom.y), (height - geom.x) + end + elseif mode == self.screen.DEVICE_ROTATED_UPSIDE_DOWN then + -- 2 + local width = self.screen:getWidth() + local height = self.screen:getHeight() + translate = function(geom) + if not geom then return end + geom.x, geom.y = (width - geom.x), (height - geom.y) + end + else + -- 0 + return + end + + translate(ges.pos) + translate(ges.start_pos) + translate(ges.end_pos) +end + --[[-- Changes gesture's `x` and `y` coordinates according to screen view mode. @@ -1403,10 +1434,8 @@ end function GestureDetector:adjustGesCoordinate(ges) local mode = self.screen:getTouchRotation() if mode == self.screen.DEVICE_ROTATED_CLOCKWISE then - -- in landscape mode rotated 90 - if ges.pos then - ges.pos.x, ges.pos.y = (self.screen:getWidth() - ges.pos.y), (ges.pos.x) - end + -- in landscape mode rotated 90 (1) + self:translateCoordinates(ges, mode) if ges.ges == "swipe" or ges.ges == "pan" or ges.ges == "hold_pan" or ges.ges == "multiswipe" @@ -1414,7 +1443,7 @@ function GestureDetector:adjustGesCoordinate(ges) or ges.ges == "two_finger_pan" or ges.ges == "two_finger_hold_pan" then - ges.direction = translateGesDirCoordinate(ges.direction, ges_coordinate_translation_90) + ges.direction = ges_coordinate_translation_90[ges.direction] if ges.ges == "multiswipe" then ges.multiswipe_directions = translateMultiswipeGesDirCoordinate(ges.multiswipe_directions, ges_coordinate_translation_90) logger.dbg("GestureDetector: Landscape translation for multiswipe:", ges.multiswipe_directions) @@ -1435,10 +1464,8 @@ function GestureDetector:adjustGesCoordinate(ges) logger.dbg("GestureDetector: Landscape translation for ges:", ges.ges, ges.direction) end elseif mode == self.screen.DEVICE_ROTATED_COUNTER_CLOCKWISE then - -- in landscape mode rotated 270 - if ges.pos then - ges.pos.x, ges.pos.y = (ges.pos.y), (self.screen:getHeight() - ges.pos.x) - end + -- in landscape mode rotated 270 (3) + self:translateCoordinates(ges, mode) if ges.ges == "swipe" or ges.ges == "pan" or ges.ges == "hold_pan" or ges.ges == "multiswipe" @@ -1446,7 +1473,7 @@ function GestureDetector:adjustGesCoordinate(ges) or ges.ges == "two_finger_pan" or ges.ges == "two_finger_hold_pan" then - ges.direction = translateGesDirCoordinate(ges.direction, ges_coordinate_translation_270) + ges.direction = ges_coordinate_translation_270[ges.direction] if ges.ges == "multiswipe" then ges.multiswipe_directions = translateMultiswipeGesDirCoordinate(ges.multiswipe_directions, ges_coordinate_translation_270) logger.dbg("GestureDetector: Inverted landscape translation for multiswipe:", ges.multiswipe_directions) @@ -1467,10 +1494,8 @@ function GestureDetector:adjustGesCoordinate(ges) logger.dbg("GestureDetector: Inverted landscape translation for ges:", ges.ges, ges.direction) end elseif mode == self.screen.DEVICE_ROTATED_UPSIDE_DOWN then - -- in portrait mode rotated 180 - if ges.pos then - ges.pos.x, ges.pos.y = (self.screen:getWidth() - ges.pos.x), (self.screen:getHeight() - ges.pos.y) - end + -- in portrait mode rotated 180 (2) + self:translateCoordinates(ges, mode) if ges.ges == "swipe" or ges.ges == "pan" or ges.ges == "hold_pan" or ges.ges == "multiswipe" @@ -1478,7 +1503,7 @@ function GestureDetector:adjustGesCoordinate(ges) or ges.ges == "two_finger_pan" or ges.ges == "two_finger_hold_pan" then - ges.direction = translateGesDirCoordinate(ges.direction, ges_coordinate_translation_180) + ges.direction = ges_coordinate_translation_180[ges.direction] if ges.ges == "multiswipe" then ges.multiswipe_directions = translateMultiswipeGesDirCoordinate(ges.multiswipe_directions, ges_coordinate_translation_180) logger.dbg("GestureDetector: Inverted portrait translation for multiswipe:", ges.multiswipe_directions) diff --git a/spec/unit/gesturedetector_spec.lua b/spec/unit/gesturedetector_spec.lua index e2ce95b65..7d4ef21df 100644 --- a/spec/unit/gesturedetector_spec.lua +++ b/spec/unit/gesturedetector_spec.lua @@ -19,6 +19,8 @@ describe("gesturedetector module", function() DEVICE_ROTATED_COUNTER_CLOCKWISE = 3, } GestureDetector.screen.getTouchRotation = function() return rotation_mode end + GestureDetector.screen.getHeight = function() return 800 end + GestureDetector.screen.getWidth = function() return 600 end return GestureDetector:adjustGesCoordinate(ges).direction end