Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GestureRecognizerTap.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 
6 using SiliconStudio.Core.Mathematics;
7 
8 namespace SiliconStudio.Paradox.Input
9 {
10  internal sealed class GestureRecognizerTap : GestureRecognizer
11  {
12  private GestureConfigTap ConfigTap { get { return (GestureConfigTap)Config; } }
13 
14  private int currentNumberOfTaps;
15 
16  private TimeSpan elapsedSinceTakeOff;
17 
18  private TimeSpan elapsedSinceDown;
19 
20  private bool isTapDown;
21 
22  private int maxNbOfFingerTouched;
23 
24  public GestureRecognizerTap(GestureConfigTap configuration, float screenRatio)
25  :base(configuration, screenRatio)
26  {
27  }
28 
29  protected override void ProcessPointerEventsImpl(TimeSpan deltaTime, List<PointerEvent> events)
30  {
31  if (isTapDown)
32  elapsedSinceDown += deltaTime;
33  else
34  elapsedSinceTakeOff += deltaTime;
35 
36  AnalysePointerEvents(events);
37 
38  // examine the tap gesture times and determine if the gesture has ended.
39  if (HasGestureStarted && (elapsedSinceDown > ConfigTap.MaximumPressTime || elapsedSinceTakeOff > ConfigTap.MaximumTimeBetweenTaps))
40  {
41  // The Tap gesture has finished because of time restriction
42  EndCurrentTap();
43  }
44  }
45 
46  protected override void ProcessDownEventPointer(int id, Vector2 pos)
47  {
48  if (!FingerIdToBeginPositions.ContainsKey(id))
49  FingerIdToBeginPositions[id] = pos;
50 
51  FingerIdsToLastPos[id] = pos;
52 
53  maxNbOfFingerTouched = Math.Max(maxNbOfFingerTouched, NbOfFingerOnScreen);
54 
55  isTapDown = true;
56 
57  if (NbOfFingerOnScreen == 1)
58  {
59  elapsedSinceTakeOff = TimeSpan.Zero;
60  elapsedSinceDown = TimeSpan.Zero;
61  HasGestureStarted = true;
62  }
63 
64  if(HasGestureStarted && maxNbOfFingerTouched > ConfigTap.RequiredNumberOfFingers)
65  EndCurrentTap();
66 
67  if (HasGestureStarted && !HadFingerAtThatPosition(pos))
68  {
69  EndCurrentTap();
70 
71  maxNbOfFingerTouched = NbOfFingerOnScreen;
72  elapsedSinceTakeOff = TimeSpan.Zero;
73  elapsedSinceDown = TimeSpan.Zero;
74  HasGestureStarted = true;
75  foreach (var key in FingerIdsToLastPos.Keys)
76  FingerIdToBeginPositions[key] = FingerIdsToLastPos[key];
77  }
78  }
79 
80  private bool HadFingerAtThatPosition(Vector2 pos)
81  {
82  // finger ids can change during between two different taps so we have to check all the begin position
83  foreach (var beginPos in FingerIdToBeginPositions.Values)
84  {
85  if ((pos - beginPos).Length() < ConfigTap.MaximumDistanceTaps)
86  return true;
87  }
88 
89  return false;
90  }
91 
92  protected override void ProcessMoveEventPointers(Dictionary<int, Vector2> fingerIdsToMovePos)
93  {
94  if (!HasGestureStarted) // nothing to do is the gesture has not started yet
95  return;
96 
97  foreach (var id in fingerIdsToMovePos.Keys)
98  {
99  if ((fingerIdsToMovePos[id] - FingerIdsToLastPos[id]).Length() > ConfigTap.MaximumDistanceTaps)
100  {
101  EndCurrentTap();
102  return;
103  }
104  }
105  }
106 
107  protected override void ProcessUpEventPointer(int id, Vector2 pos)
108  {
109  if (!FingerIdsToLastPos.ContainsKey(id))
110  return;
111 
112  FingerIdsToLastPos.Remove(id);
113 
114  if (NbOfFingerOnScreen == 0)
115  {
116  elapsedSinceTakeOff = TimeSpan.Zero;
117  isTapDown = false;
118 
119  if (HasGestureStarted && maxNbOfFingerTouched == ConfigTap.RequiredNumberOfFingers)
120  ++currentNumberOfTaps;
121 
122  maxNbOfFingerTouched = 0;
123  }
124  }
125 
126  private void EndCurrentTap()
127  {
128  // add the gesture to the tap event list if the number of tap requirement is fulfilled
129  if (currentNumberOfTaps == ConfigTap.RequiredNumberOfTaps)
130  {
131  var tapMeanPosition = ComputeMeanPosition(FingerIdToBeginPositions.Values);
132  CurrentGestureEvents.Add(new GestureEventTap(ElapsedSinceBeginning, ConfigTap.RequiredNumberOfFingers, currentNumberOfTaps, NormalizeVector(tapMeanPosition)));
133  }
134 
135  currentNumberOfTaps = 0;
136 
137  HasGestureStarted = false;
138  FingerIdToBeginPositions.Clear();
139  }
140  }
141 }
Represents a two dimensional mathematical vector.
Definition: Vector2.cs:42