From 238676ddf8199301d5e1f24823885d76f8a1b1a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Jan=C4=8D=C3=ADk?= Date: Fri, 30 Aug 2019 15:55:03 +0200 Subject: [PATCH] FingerSDL PointerID generator PointerIDs from finger touches always try to keep as low as possible - consistent with iOS implementation --- sources/engine/Xenko.Input/SDL/FingerSDL.cs | 41 +++++++++++++++++++-- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/sources/engine/Xenko.Input/SDL/FingerSDL.cs b/sources/engine/Xenko.Input/SDL/FingerSDL.cs index 7b4bcb558a..175aa09b15 100644 --- a/sources/engine/Xenko.Input/SDL/FingerSDL.cs +++ b/sources/engine/Xenko.Input/SDL/FingerSDL.cs @@ -3,7 +3,9 @@ #if XENKO_UI_SDL using System; +using System.Collections.Generic; using SDL2; +using Xenko.Core.Extensions; using Xenko.Core.Mathematics; using Xenko.Games; using Xenko.Graphics.SDL; @@ -14,6 +16,8 @@ namespace Xenko.Input internal class FingerSDL : PointerDeviceBase, IDisposable { private readonly Window uiControl; + private readonly Dictionary touchFingerIndexMap = new Dictionary(); + private int touchCounter; public FingerSDL(InputSourceSDL source, Window uiControl) { @@ -48,13 +52,42 @@ private void OnSizeChanged(SDL.SDL_WindowEvent eventArgs) SetSurfaceSize(new Vector2(uiControl.ClientSize.Width, uiControl.ClientSize.Height)); } + // TODO Code from PointeriOS with slight modifications, consider creating and referencing a utility class + private int GetFingerId(long touchId, PointerEventType type) + { + // Assign finger index (starting at 0) to touch ID + int touchFingerIndex = 0; + if (type == PointerEventType.Pressed) + { + touchFingerIndex = touchCounter++; + touchFingerIndexMap.Add(touchId, touchFingerIndex); + } + else + { + touchFingerIndex = touchFingerIndexMap[touchId]; + } + + // Remove index + if (type == PointerEventType.Released) + { + touchFingerIndexMap.Remove(touchId); + touchCounter = 0; // Reset touch counter + + // Recalculate next finger index + if (touchFingerIndexMap.Count > 0) + { + touchFingerIndexMap.ForEach(pair => touchCounter = Math.Max(touchCounter, pair.Value)); + touchCounter++; // next + } + } + + return touchFingerIndex; + } + private void HandleFingerEvent(SDL.SDL_TouchFingerEvent e, PointerEventType type) { var newPosition = new Vector2(e.x, e.y); - - // TODO own ID counter ala iOS/Android implementations - var id = (int)e.fingerId; - + var id = GetFingerId(e.fingerId, type); PointerState.PointerInputEvents.Add(new PointerDeviceState.InputEvent { Type = type, Position = newPosition, Id = id }); }