Skip to content

Commit

Permalink
Update SKTouchHandler.cs to fix crash bug caused by passing incorrect…
Browse files Browse the repository at this point in the history
… parameter, added pressure and eraser handling (#1212)

* Fixed crash bug where id was incorrectly passed instead pointer to evt.GetToolType().
* Added stylus pressure for Android
* Added eraser handling - to map to middle button.
  • Loading branch information
Michael S. Scherotter authored Apr 7, 2020
1 parent 792b905 commit 58a9a7b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,20 @@ private void OnTouch(object sender, View.TouchEventArgs e)
var id = evt.GetPointerId(pointer);
var coords = scalePixels(evt.GetX(pointer), evt.GetY(pointer));

var toolType = evt.GetToolType(id);
var toolType = evt.GetToolType(pointer);

var deviceType = GetDeviceType(toolType);

var pressure = evt.GetPressure(pointer);

var button = GetButton(evt, toolType);

switch (evt.ActionMasked)
{
case MotionEventActions.Down:
case MotionEventActions.PointerDown:
{
var args = new SKTouchEventArgs(id, SKTouchAction.Pressed, SKMouseButton.Left, deviceType, coords, true);
var args = new SKTouchEventArgs(id, SKTouchAction.Pressed, button, deviceType, coords, true, 0, pressure);

onTouchAction(args);
e.Handled = args.Handled;
Expand All @@ -70,7 +75,8 @@ private void OnTouch(object sender, View.TouchEventArgs e)
id = evt.GetPointerId(pointer);
coords = scalePixels(evt.GetX(pointer), evt.GetY(pointer));

var args = new SKTouchEventArgs(id, SKTouchAction.Moved, SKMouseButton.Left, deviceType, coords, true);
var args = new SKTouchEventArgs(id, SKTouchAction.Moved, button, deviceType, coords, true, 0, pressure);

onTouchAction(args);
e.Handled = e.Handled || args.Handled;
}
Expand All @@ -80,22 +86,40 @@ private void OnTouch(object sender, View.TouchEventArgs e)
case MotionEventActions.Up:
case MotionEventActions.PointerUp:
{
var args = new SKTouchEventArgs(id, SKTouchAction.Released, SKMouseButton.Left, deviceType, coords, false);
var args = new SKTouchEventArgs(id, SKTouchAction.Released, button, deviceType, coords, false, 0, pressure);

onTouchAction(args);
e.Handled = args.Handled;
break;
}

case MotionEventActions.Cancel:
{
var args = new SKTouchEventArgs(id, SKTouchAction.Cancelled, SKMouseButton.Left, deviceType, coords, false);
var args = new SKTouchEventArgs(id, SKTouchAction.Cancelled, button, deviceType, coords, false, 0, pressure);

onTouchAction(args);
e.Handled = args.Handled;
break;
}
}
}

private static SKMouseButton GetButton(MotionEvent evt, MotionEventToolType toolType)
{
var button = SKMouseButton.Left;

if (toolType == MotionEventToolType.Eraser)
{
button = SKMouseButton.Middle;
}
else if (evt.ButtonState.HasFlag(MotionEventButtonState.StylusSecondary))
{
button = SKMouseButton.Right;
}

return button;
}

private static SKTouchDeviceType GetDeviceType(MotionEventToolType toolType) =>
toolType switch
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
using System;
using System.Linq;

namespace SkiaSharp.Views.Forms
{
public class SKTouchEventArgs : EventArgs
{
public SKTouchEventArgs(long id, SKTouchAction type, SKPoint location, bool inContact)
: this(id, type, SKMouseButton.Left, SKTouchDeviceType.Touch, location, inContact, 0)
: this(id, type, SKMouseButton.Left, SKTouchDeviceType.Touch, location, inContact, 0, 1)
{
}

public SKTouchEventArgs(long id, SKTouchAction type, SKMouseButton mouseButton, SKTouchDeviceType deviceType, SKPoint location, bool inContact)
: this(id, type, mouseButton, deviceType, location, inContact, 0)
: this(id, type, mouseButton, deviceType, location, inContact, 0, 1)
{
}

public SKTouchEventArgs(long id, SKTouchAction type, SKMouseButton mouseButton, SKTouchDeviceType deviceType, SKPoint location, bool inContact, int wheelDelta)
: this(id, type, mouseButton, deviceType, location, inContact, wheelDelta, 1)
{
}

public SKTouchEventArgs(long id, SKTouchAction type, SKMouseButton mouseButton, SKTouchDeviceType deviceType, SKPoint location, bool inContact, int wheelDelta, float pressure)
{
Id = id;
ActionType = type;
Expand All @@ -24,6 +28,7 @@ public SKTouchEventArgs(long id, SKTouchAction type, SKMouseButton mouseButton,
Location = location;
InContact = inContact;
WheelDelta = wheelDelta;
Pressure = pressure;
}

public bool Handled { get; set; }
Expand All @@ -42,9 +47,11 @@ public SKTouchEventArgs(long id, SKTouchAction type, SKMouseButton mouseButton,

public int WheelDelta { get; private set; }

public float Pressure { get; private set; }

public override string ToString()
{
return $"{{ActionType={ActionType}, DeviceType={DeviceType}, Handled={Handled}, Id={Id}, InContact={InContact}, Location={Location}, MouseButton={MouseButton}, WheelDelta={WheelDelta}}}";
return $"{{ActionType={ActionType}, DeviceType={DeviceType}, Handled={Handled}, Id={Id}, InContact={InContact}, Location={Location}, MouseButton={MouseButton}, WheelDelta={WheelDelta}, Pressure={Pressure}}}";
}
}

Expand Down

0 comments on commit 58a9a7b

Please sign in to comment.