OndrejKunc / SkiaScene

Simple API to transform SkiaSharp objects using functions like zoom and rotate or using gestures like pan and pinch.
MIT License
67 stars 18 forks source link

Android touch coordinates incorrect #24

Closed hrkrx closed 3 years ago

hrkrx commented 3 years ago

I use the TouchTracking.Forms touch effect to get the touch coordinates.

This works flawless on UWP.

But on Android it is a x value between 0 and ca. 410 instead of between 0 and 1080.

I use the default android 9.0 emulator.

I really hope this is a user error, since I haven't found a viable alternative to this :/

My Code:

<Grid Grid.Row="2">
    <!-- Coins -->
    <skia:SKCanvasView x:Name="coinsCanvasView" PaintSurface="CoinsPaintSurface"/>
    <Grid.Effects>
        <tt:TouchEffect Capture="True" TouchAction="OnCoinsTouch"/>
    </Grid.Effects>
</Grid>
void OnCoinsTouch(object sender, TouchActionEventArgs args)
{
    var point = args.Location; //Point location
    var type = args.Type; //Entered, Pressed, Moved ... etc.
    if (type == TouchActionType.Released)
    {
        var coinIndex = CurrentDevice.GetCoinIndexFromTouchCoordinates(point.X);
        if (coinIndex < Game.CoinAmount && coinIndex >= 0)
        {
            Game.CoinCollection[coinIndex] = !Game.CoinCollection[coinIndex];
        }
    }
    coinsCanvasView.InvalidateSurface();
}

EDIT:

I do not know if this is true, but it seems to report the actual size of the view on my screen and not the size it would have on the phone, which would match the 400something value since my desktop resolution is 1920 by 1080 and the emulator takes ca. a 5th of the horizontal space

verified that this is false on an actual phone

hrkrx commented 3 years ago

So i managed to find the Issue:

In Line https://github.com/OndrejKunc/SkiaScene/blob/a68452c889b716c4d82515de5518ea9902846d6b/source/TouchTracking/TouchTracking.Droid/TouchHandler.cs#L262

Is a conversion _fromPixels happening which is not happening in the UWP version.

It is dp vs px, but whatever is subjectivly better it should be the same on every platform.

(haven't tested iOS)

hrkrx commented 3 years ago

Sorry for creating this Issue, pixel density is 1 to 1 on my Desktop so it matches the px value, but not on for example a surface device, so I will expand my knowledge and close this Issue.