GestureDetector: Translate start & end gesture positions when rotated (#13101)

* 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
This commit is contained in:
NiLuJe
2025-01-31 18:30:23 +01:00
committed by GitHub
parent 3dba40d884
commit aa421bf277
2 changed files with 45 additions and 18 deletions

View File

@@ -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)

View File

@@ -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