Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Android high-frequency touch events #8109

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SimonDarksideJ @harry-cpp @mrhelmut

Are these good changes to the editor config?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know what the resharper one does, but the rest is fine.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, shouldn't embed other product configuration inside MonoGame files, please remove @Mindfulplays

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;
}
}
}
}