5 using System.Collections.Generic;
7 using SiliconStudio.Core.Mathematics;
9 namespace SiliconStudio.
Paradox.Input
11 internal abstract class GestureRecognizer
13 protected virtual GestureConfig Config {
get;
private set; }
15 protected List<GestureEvent> CurrentGestureEvents =
new List<GestureEvent>();
17 protected readonly Dictionary<int, Vector2> FingerIdToBeginPositions =
new Dictionary<int, Vector2>();
19 protected readonly Dictionary<int, Vector2> FingerIdsToLastPos =
new Dictionary<int, Vector2>();
21 protected TimeSpan ElapsedSinceBeginning;
22 protected TimeSpan ElapsedSinceLast;
24 private readonly
static List<int> FingerIdsCache =
new List<int>();
26 protected bool HasGestureStarted
28 get {
return hasGestureStarted; }
31 if (value && !hasGestureStarted)
33 ElapsedSinceBeginning = TimeSpan.Zero;
34 ElapsedSinceLast = TimeSpan.Zero;
37 hasGestureStarted = value;
40 private bool hasGestureStarted;
42 protected virtual int NbOfFingerOnScreen
44 get {
return FingerIdsToLastPos.Count; }
47 internal float ScreenRatio {
get; set; }
49 protected GestureRecognizer(GestureConfig config,
float screenRatio)
52 ScreenRatio = screenRatio;
55 public List<GestureEvent> ProcessPointerEvents(TimeSpan deltaTime, List<PointerEvent> events)
57 CurrentGestureEvents.Clear();
59 ElapsedSinceBeginning += deltaTime;
60 ElapsedSinceLast += deltaTime;
62 ProcessPointerEventsImpl(deltaTime, events);
64 return CurrentGestureEvents;
67 protected virtual void ProcessPointerEventsImpl(TimeSpan deltaTime, List<PointerEvent> events)
69 AnalysePointerEvents(events);
75 var accuPos = Vector2.Zero;
76 foreach (var position
in positions)
82 return accuPos /
count;
86 private readonly Dictionary<int, Vector2> fingerIdsToLastMovePos =
new Dictionary<int, Vector2>();
88 protected void AnalysePointerEvents(List<PointerEvent> events)
90 foreach (var pointerEvent
in events)
92 var state = pointerEvent.State;
93 var
id = pointerEvent.PointerId;
94 var pos = pointerEvent.Position;
98 case PointerState.Down:
99 ProcessDownEventPointer(
id, UnnormalizeVector(pos));
101 case PointerState.Move:
103 fingerIdsToLastMovePos[id] = pos;
105 case PointerState.Up:
106 case PointerState.Out:
107 case PointerState.Cancel:
109 ProcessAndClearMovePointerEvents();
112 ProcessUpEventPointer(
id, UnnormalizeVector(pos));
115 throw new ArgumentOutOfRangeException();
120 ProcessAndClearMovePointerEvents();
125 return ScreenRatio > 1 ?
126 new Vector2(inputVector.
X, inputVector.
Y * ScreenRatio) :
127 new Vector2(inputVector.
X / ScreenRatio, inputVector.
Y);
132 return ScreenRatio > 1 ?
133 new Vector2(inputVector.
X, inputVector.
Y / ScreenRatio) :
134 new Vector2(inputVector.
X * ScreenRatio, inputVector.
Y);
137 private void ProcessAndClearMovePointerEvents()
139 if (fingerIdsToLastMovePos.Count > 0)
141 FingerIdsCache.Clear();
145 foreach (var
id in FingerIdsCache)
146 fingerIdsToLastMovePos[id] = UnnormalizeVector(fingerIdsToLastMovePos[
id]);
148 ProcessMoveEventPointers(fingerIdsToLastMovePos);
149 fingerIdsToLastMovePos.Clear();
153 protected abstract void ProcessDownEventPointer(
int id,
Vector2 pos);
155 protected abstract void ProcessMoveEventPointers(Dictionary<int, Vector2> fingerIdsToMovePos);
157 protected abstract void ProcessUpEventPointer(
int id,
Vector2 pos);
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
Represents a two dimensional mathematical vector.
float Y
The Y component of the vector.
float X
The X component of the vector.