Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GestureRecognizerComposite.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 
5 using SiliconStudio.Core.Mathematics;
6 
7 namespace SiliconStudio.Paradox.Input
8 {
9  internal sealed class GestureRecognizerComposite : GestureRecognizerContMotion
10  {
11  private GestureConfigComposite ConfigComposite { get { return (GestureConfigComposite)Config; } }
12 
13  private int firstFingerId;
14  private int secondFingerId;
15 
16  private Vector2 beginVectorNormalized;
17 
18  private float beginVectorLength;
19 
20  private Vector2 beginCenter;
21 
22  private Vector2 lastCenter;
23 
24  private Vector2 currentCenter;
25 
26  private float currentRotation;
27 
28  private float lastRotation;
29 
30  private float lastScale;
31 
32  private float currentScale;
33 
34  public GestureRecognizerComposite(GestureConfigComposite config, float screenRatio)
35  : base(config, screenRatio)
36  {
37  }
38 
39  protected override void InitializeGestureVariables()
40  {
41  // initialize first and second finger ids
42  var keysEnum = FingerIdsToLastPos.Keys.GetEnumerator();
43  keysEnum.MoveNext(); firstFingerId = keysEnum.Current;
44  keysEnum.MoveNext(); secondFingerId = keysEnum.Current;
45 
46  var beginDirVec = FingerIdsToLastPos[secondFingerId] - FingerIdsToLastPos[firstFingerId];
47  beginVectorNormalized = Vector2.Normalize(beginDirVec);
48  beginVectorLength = beginDirVec.Length();
49 
50  beginCenter = (FingerIdsToLastPos[secondFingerId] + FingerIdsToLastPos[firstFingerId]) / 2;
51  lastCenter = beginCenter;
52  lastRotation = 0;
53  lastScale = 1;
54  }
55 
56  protected override bool GestureBeginningConditionFulfilled()
57  {
58  return Math.Abs(currentRotation) >= ConfigComposite.MinimumRotationAngle
59  || currentScale <= ConfigComposite.MinimumScaleValueInv || currentScale >= ConfigComposite.MinimumScaleValue
60  || (currentCenter - beginCenter).Length() >= ConfigComposite.MinimumTranslationDistance;
61  }
62 
63  protected override void UpdateGestureVarsAndPerfomChecks()
64  {
65  var currentVector = FingerIdsToLastPos[secondFingerId] - FingerIdsToLastPos[firstFingerId];
66  var currentVectorNormalized = Vector2.Normalize(currentVector);
67 
68  // Update the gesture current rotation value
69  var rotSign = beginVectorNormalized[0] * currentVectorNormalized[1] - beginVectorNormalized[1] * currentVectorNormalized[0];
70  currentRotation = Math.Sign(rotSign) * (float)Math.Acos(Vector2.Dot(currentVectorNormalized, beginVectorNormalized));
71 
72  // Update the gesture current center of transformation
73  currentCenter = (FingerIdsToLastPos[secondFingerId] + FingerIdsToLastPos[firstFingerId]) / 2;
74 
75  // Update the gesture current scale
76  currentScale = Math.Abs(beginVectorLength) > MathUtil.ZeroTolerance? currentVector.Length() / beginVectorLength: 0;
77  }
78 
79  protected override void AddGestureEventToCurrentList(GestureState state)
80  {
81  var deltaRotation = currentRotation - lastRotation;
82  var deltaScale = currentScale - lastScale;
83  CurrentGestureEvents.Add(new GestureEventComposite(state, ElapsedSinceLast, ElapsedSinceBeginning, deltaRotation, currentRotation, deltaScale, currentScale,
84  NormalizeVector(beginCenter), NormalizeVector(lastCenter), NormalizeVector(currentCenter)));
85 
86  lastRotation = currentRotation;
87  lastScale = currentScale;
88  lastCenter = currentCenter;
89 
90  base.AddGestureEventToCurrentList(state);
91  }
92  }
93 }
Represents a two dimensional mathematical vector.
Definition: Vector2.cs:42
GestureState
The different possible states of a gestures.
Definition: GestureState.cs:8