Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GestureRecognizerContMotion.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 using SiliconStudio.Core.Mathematics;
6 
7 namespace SiliconStudio.Paradox.Input
8 {
9  /// <summary>
10  /// A gesture recognizer for continuous motions.
11  /// </summary>
12  internal abstract class GestureRecognizerContMotion : GestureRecognizer
13  {
14  protected bool GestureBeganEventSent { get; private set; }
15 
16  protected GestureRecognizerContMotion(GestureConfig config, float screenRatio)
17  : base(config, screenRatio)
18  {
19  }
20 
21  protected override void ProcessDownEventPointer(int id, Vector2 pos)
22  {
23  UpdateGestureStartEndStatus(true, id, pos);
24  }
25 
26  protected override void ProcessUpEventPointer(int id, Vector2 pos)
27  {
28  if (!FingerIdsToLastPos.ContainsKey(id))
29  return;
30 
31  UpdateGestureStartEndStatus(false, id, pos);
32  }
33 
34  private void UpdateGestureStartEndStatus(bool isKeyDown, int id, Vector2 pos)
35  {
36  var gestureWasStarted = HasGestureStarted;
37  HasGestureStarted = (NbOfFingerOnScreen + (isKeyDown?1:-1) == Config.RequiredNumberOfFingers);
38 
39  UpdateFingerDictionaries(isKeyDown, id, pos);
40 
41  if (HasGestureStarted) // beginning of a new gesture
42  {
43  InitializeGestureVariables();
44  }
45  else if (gestureWasStarted && GestureBeganEventSent) // end of the current gesture
46  {
47  AddGestureEventToCurrentList(GestureState.Ended);
48  }
49  }
50 
51  protected abstract void InitializeGestureVariables();
52 
53  protected override void ProcessMoveEventPointers(Dictionary<int, Vector2> fingerIdsToMovePos)
54  {
55  // update current finger positions.
56  foreach (var id in fingerIdsToMovePos.Keys)
57  FingerIdsToLastPos[id] = fingerIdsToMovePos[id];
58 
59  if (!HasGestureStarted) // nothing more to do is the gesture has not started yet
60  return;
61 
62  UpdateGestureVarsAndPerfomChecks();
63 
64  if (!GestureBeganEventSent && HasGestureStarted && GestureBeginningConditionFulfilled())
65  {
66  AddGestureEventToCurrentList(GestureState.Began);
67  GestureBeganEventSent = true;
68  }
69 
70  if (GestureBeganEventSent)
71  AddGestureEventToCurrentList(HasGestureStarted ? GestureState.Changed : GestureState.Ended);
72  }
73 
74  protected abstract void UpdateGestureVarsAndPerfomChecks();
75 
76  protected abstract bool GestureBeginningConditionFulfilled();
77 
78  private void UpdateFingerDictionaries(bool isKeyDown, int id, Vector2 pos)
79  {
80  if (isKeyDown)
81  {
82  FingerIdToBeginPositions[id] = pos;
83  FingerIdsToLastPos[id] = pos;
84  }
85  else
86  {
87  FingerIdToBeginPositions.Remove(id);
88  FingerIdsToLastPos.Remove(id);
89  }
90  }
91 
92  protected virtual void AddGestureEventToCurrentList(GestureState state)
93  {
94  ElapsedSinceLast = TimeSpan.Zero;
95 
96  if (state == GestureState.Ended)
97  GestureBeganEventSent = false;
98  }
99  }
100 }
Represents a two dimensional mathematical vector.
Definition: Vector2.cs:42
GestureState
The different possible states of a gestures.
Definition: GestureState.cs:8