Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GestureRecognizerDrag.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 GestureRecognizerDrag : GestureRecognizerContMotion
11  {
12  private GestureConfigDrag ConfigDrag { get { return (GestureConfigDrag)Config; } }
13 
14  private Dictionary<int, Vector2> fingerIdsToLowFilteredPos = new Dictionary<int, Vector2>();
15 
16  private Vector2 startPosition;
17 
18  private Vector2 lastPosition;
19 
20  private Vector2 currPosition;
21 
22  public GestureRecognizerDrag(GestureConfigDrag config, float screenRatio)
23  : base(config, screenRatio)
24  {
25  }
26 
27  protected override void InitializeGestureVariables()
28  {
29  startPosition = ComputeMeanPosition(FingerIdsToLastPos.Values);
30  lastPosition = startPosition;
31  fingerIdsToLowFilteredPos = new Dictionary<int, Vector2>(FingerIdsToLastPos);
32  }
33 
34  protected override void UpdateGestureVarsAndPerfomChecks()
35  {
36  foreach (var id in FingerIdsToLastPos.Keys)
37  {
38  // check that the drag shape is respected and end the gesture if it is not the case
39  if (ConfigDrag.DragShape != GestureShape.Free)
40  {
41  var compIndex = ConfigDrag.DragShape == GestureShape.Horizontal ? 1 : 0;
42  if (Math.Abs(FingerIdsToLastPos[id][compIndex] - fingerIdsToLowFilteredPos[id][compIndex]) > ConfigDrag.AllowedErrorMargins[compIndex])
43  HasGestureStarted = false;
44  }
45 
46  // update the finger low filtered position for the finger
47  const float lowFilterCoef = 0.9f;
48  fingerIdsToLowFilteredPos[id] = fingerIdsToLowFilteredPos[id] * lowFilterCoef + (1f - lowFilterCoef) * FingerIdsToLastPos[id];
49  }
50 
51  currPosition = ComputeMeanPosition(FingerIdsToLastPos.Values);
52  }
53 
54  protected override bool GestureBeginningConditionFulfilled()
55  {
56  return (currPosition - startPosition).Length() >= ConfigDrag.MinimumDragDistance;
57  }
58 
59  protected override void AddGestureEventToCurrentList(GestureState state)
60  {
61  var deltaTrans = currPosition - lastPosition;
62  CurrentGestureEvents.Add(new GestureEventDrag(state, ConfigDrag.RequiredNumberOfFingers, ElapsedSinceLast, ElapsedSinceBeginning, ConfigDrag.DragShape,
63  NormalizeVector(startPosition), NormalizeVector(currPosition), NormalizeVector(deltaTrans)));
64 
65  lastPosition = currPosition;
66 
67  base.AddGestureEventToCurrentList(state);
68  }
69  }
70 }
Represents a two dimensional mathematical vector.
Definition: Vector2.cs:42
GestureState
The different possible states of a gestures.
Definition: GestureState.cs:8
GestureShape
Gesture possible shapes.
Definition: GestureShape.cs:8