Skip to content

Commit

Permalink
Add high-frequency touch events for Android with documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mindfulplays committed Dec 29, 2023
1 parent 5fc1d04 commit 04dadbe
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .editorconfig
Expand Up @@ -10,3 +10,8 @@ trim_trailing_whitespace = true
[*.cs]
indent_style = space
indent_size = 4
csharp_style_namespace_declarations = block_scoped:suggestion
insert_final_newline = true
trim_trailing_whitespace = true
# ReSharper properties
resharper_indent_inside_namespace = true
3 changes: 2 additions & 1 deletion MonoGame.Framework/Input/Touch/TouchPanel.cs
@@ -1,7 +1,7 @@
#region License
// /*
// Microsoft Public License (Ms-PL)
// XnaTouch - Copyright (C) 2009-2010 The XnaTouch Team
// XnaTouch - Copyright © 2009-2010 The XnaTouch Team
//
// All rights reserved.
//
Expand Down Expand Up @@ -161,6 +161,7 @@ public static bool IsGestureAvailable
/// <summary>
/// Gets or sets if high-frequency events are sent to any listeners (if the underlying OS supports it).
/// By default, it's <see langword="false"/> and hence no additional CPU usage is incurred.
/// This is currently supported on iOS (including support for Apple Pencil) and Android (tablets/phones).
/// </summary>
public static bool EnableHighFrequencyTouch
{
Expand Down
Expand Up @@ -10,6 +10,7 @@ namespace Microsoft.Xna.Framework.Input.Touch
/// Manages touch events for Android. Maps new presses to new touch Ids as per Xna WP7 incrementing touch Id behaviour.
/// This is required as Android reports touch IDs of 0 to 5, which leads to incorrect handling of touch events.
/// Motivation and discussion: http://monogame.codeplex.com/discussions/382252
/// Also supports high-frequency touch events, <see cref="https://developer.android.com/reference/android/view/MotionEvent#batching"/>.
/// </summary>
class AndroidTouchEventManager
{
Expand Down Expand Up @@ -37,15 +38,40 @@ public void OnTouchEvent(MotionEvent e)
// DOWN
case MotionEventActions.Down:
case MotionEventActions.PointerDown:
{
TouchPanel.AddEvent(id, TouchLocationState.Pressed, position);
break;
}
// UP
case MotionEventActions.Up:
case MotionEventActions.PointerUp:
{
TouchPanel.AddEvent(id, TouchLocationState.Released, position);
break;
}
// MOVE
case MotionEventActions.Move:
{
if (TouchPanel.EnableHighFrequencyTouch)
{
// Process historical MOVE events since the last MotionEvent. Note that
// the Android documentation says only MOVE events have history
// See: https://developer.android.com/reference/android/view/MotionEvent#batching.
int historySize = e.HistorySize;
for (int history = 0; history < historySize; history++)
{
for (int i = 0; i < e.PointerCount; i++)
{
id = e.GetPointerId(i);
position.X = e.GetHistoricalX(i, history);
position.Y = e.GetHistoricalY(i, history);
TouchPanel.AddHighResolutionTouchEvent(id, TouchLocationState.Moved, position);
}
}

// Continue processing the current event (i.e. non-high-frequency touch event) next.
}

for (int i = 0; i < e.PointerCount; i++)
{
id = e.GetPointerId(i);
Expand All @@ -54,17 +80,21 @@ public void OnTouchEvent(MotionEvent e)
UpdateTouchPosition(ref position);
TouchPanel.AddEvent(id, TouchLocationState.Moved, position);
}
break;

break;
}
// CANCEL, OUTSIDE
case MotionEventActions.Cancel:
case MotionEventActions.Outside:
{
for (int i = 0; i < e.PointerCount; i++)
{
id = e.GetPointerId(i);
TouchPanel.AddEvent(id, TouchLocationState.Released, position);
}

break;
}
}
}

Expand All @@ -77,4 +107,4 @@ void UpdateTouchPosition(ref Vector2 position)
position.Y -= clientBounds.Y;
}
}
}
}

0 comments on commit 04dadbe

Please sign in to comment.