Skip to content

Commit 73a3799

Browse files
committed
- Gesture.ScreenPosition now returns activePointers[0].Position.
- Transform gestures ScreenPosition now return activePointers[0].Position. - Moved CombineTouches functionality from Gesture to TapGesture since it is the only place it is used.
1 parent 3b612b8 commit 73a3799

File tree

5 files changed

+107
-142
lines changed

5 files changed

+107
-142
lines changed

Source/Assets/TouchScript/Editor/Gestures/GestureEditor.cs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,16 @@ internal class GestureEditor : UnityEditor.Editor
2929
public static readonly GUIContent TEXT_SEND_STATE_CHANGE_MESSAGES = new GUIContent("Send State Change Messages", "If checked, the gesture will send a message for every state change. Gestures usually have their own more specific messages, so you should keep this toggle unchecked unless you really want state change messages.");
3030
public static readonly GUIContent TEXT_SEND_MESSAGE_TARGET = new GUIContent("Target", "The GameObject target of Unity Messages. If null, host GameObject is used.");
3131
public static readonly GUIContent TEXT_SEND_STATE_CHANGE_EVENTS = new GUIContent("Send State Change Events", "If checked, the gesture will send a events for every state change. Gestures usually have their own more specific messages, so you should keep this toggle unchecked unless you really want state change events.");
32-
public static readonly GUIContent TEXT_COMBINE_POINTERS = new GUIContent("Combine Pointers", "When several fingers are used to perform a tap, pointers released not earlier than <CombineInterval> seconds ago are used to calculate gesture's final screen position.");
33-
public static readonly GUIContent TEXT_COMBINE_TOUCH_POINTERS = new GUIContent("Combine Interval (sec)", TEXT_COMBINE_POINTERS.tooltip);
3432
public static readonly GUIContent TEXT_REQUIRE_GESTURE_TO_FAIL = new GUIContent("Require Other Gesture to Fail", "Another gesture must fail for this gesture to start.");
3533
public static readonly GUIContent TEXT_LIMIT_POINTERS = new GUIContent(" Limit Pointers", "");
3634

37-
protected bool shouldDrawCombineTouches = false;
3835
protected bool shouldDrawAdvanced = false;
3936
protected bool shouldDrawGeneral = true;
4037

4138
private Gesture instance;
4239

4340
private SerializedProperty debugMode, friendlyGestures, requireGestureToFail,
44-
minPointers, maxPointers, combinePointers, combinePointersInterval,
41+
minPointers, maxPointers,
4542
useSendMessage, sendMessageTarget, sendStateChangeMessages,
4643
useUnityEvents, sendStateChangeEvents;
4744
private SerializedProperty OnStateChange;
@@ -63,8 +60,6 @@ protected virtual void OnEnable()
6360
debugMode = serializedObject.FindProperty("debugMode");
6461
friendlyGestures = serializedObject.FindProperty("friendlyGestures");
6562
requireGestureToFail = serializedObject.FindProperty("requireGestureToFail");
66-
combinePointers = serializedObject.FindProperty("combinePointers");
67-
combinePointersInterval = serializedObject.FindProperty("combinePointersInterval");
6863
useSendMessage = serializedObject.FindProperty("useSendMessage");
6964
sendMessageTarget = serializedObject.FindProperty("sendMessageTarget");
7065
sendStateChangeMessages = serializedObject.FindProperty("sendStateChangeMessages");
@@ -246,28 +241,9 @@ protected virtual void drawSendMessage()
246241

247242
protected virtual void drawAdvanced()
248243
{
249-
drawCombineTouches();
250244
drawDebug();
251245
}
252246

253-
protected virtual void drawCombineTouches()
254-
{
255-
if (shouldDrawCombineTouches)
256-
{
257-
EditorGUILayout.PropertyField(combinePointers, TEXT_COMBINE_POINTERS);
258-
if (combinePointers.boolValue)
259-
{
260-
EditorGUIUtility.labelWidth = 160;
261-
EditorGUILayout.BeginHorizontal();
262-
GUILayout.Label(GUIContent.none, GUILayout.Width(10));
263-
EditorGUILayout.BeginVertical(GUILayout.ExpandWidth(true));
264-
EditorGUILayout.PropertyField(combinePointersInterval, TEXT_COMBINE_TOUCH_POINTERS);
265-
EditorGUILayout.EndVertical();
266-
EditorGUILayout.EndHorizontal();
267-
}
268-
}
269-
}
270-
271247
protected virtual void drawDebug()
272248
{
273249
if (debugMode == null) return;

Source/Assets/TouchScript/Editor/Gestures/TapGestureEditor.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,40 @@ internal sealed class TapGestureEditor : GestureEditor
1414
public static readonly GUIContent TEXT_TIME_LIMIT = new GUIContent("Limit Time (sec)", "Gesture fails if in <value> seconds user didn't do the required number of taps.");
1515
public static readonly GUIContent TEXT_DISTANCE_LIMIT = new GUIContent("Limit Movement (cm)", "Gesture fails if taps are made more than <value> cm away from the first pointer position.");
1616
public static readonly GUIContent TEXT_NUMBER_OF_TAPS_REQUIRED = new GUIContent("Number of Taps Required", "Number of taps required for this gesture to be recognized.");
17+
public static readonly GUIContent TEXT_COMBINE_POINTERS = new GUIContent("Combine Pointers", "When several fingers are used to perform a tap, pointers released not earlier than <CombineInterval> seconds ago are used to calculate gesture's final screen position.");
18+
public static readonly GUIContent TEXT_COMBINE_TOUCH_POINTERS = new GUIContent("Combine Interval (sec)", TEXT_COMBINE_POINTERS.tooltip);
1719

18-
private SerializedProperty numberOfTapsRequired, distanceLimit, timeLimit;
20+
private SerializedProperty numberOfTapsRequired, distanceLimit, timeLimit, combinePointers, combinePointersInterval;
1921
private SerializedProperty OnTap;
2022

2123
protected override void OnEnable()
2224
{
2325
numberOfTapsRequired = serializedObject.FindProperty("numberOfTapsRequired");
2426
timeLimit = serializedObject.FindProperty("timeLimit");
2527
distanceLimit = serializedObject.FindProperty("distanceLimit");
28+
combinePointers = serializedObject.FindProperty("combinePointers");
29+
combinePointersInterval = serializedObject.FindProperty("combinePointersInterval");
2630

2731
OnTap = serializedObject.FindProperty("OnTap");
2832

29-
shouldDrawCombineTouches = true;
30-
3133
base.OnEnable();
3234
}
3335

3436
protected override void drawGeneral()
3537
{
3638
EditorGUIUtility.labelWidth = 180;
3739
EditorGUILayout.IntPopup(numberOfTapsRequired, new[] {new GUIContent("One"), new GUIContent("Two"), new GUIContent("Three")}, new[] {1, 2, 3}, TEXT_NUMBER_OF_TAPS_REQUIRED, GUILayout.ExpandWidth(true));
38-
40+
EditorGUILayout.PropertyField(combinePointers, TEXT_COMBINE_POINTERS);
41+
if (combinePointers.boolValue)
42+
{
43+
EditorGUIUtility.labelWidth = 160;
44+
EditorGUILayout.BeginHorizontal();
45+
GUILayout.Label(GUIContent.none, GUILayout.Width(10));
46+
EditorGUILayout.BeginVertical(GUILayout.ExpandWidth(true));
47+
EditorGUILayout.PropertyField(combinePointersInterval, TEXT_COMBINE_TOUCH_POINTERS);
48+
EditorGUILayout.EndVertical();
49+
EditorGUILayout.EndHorizontal();
50+
}
3951
base.drawGeneral ();
4052
}
4153

@@ -47,11 +59,12 @@ protected override void drawLimits()
4759
base.drawLimits();
4860
}
4961

50-
protected override void drawUnityEvents ()
62+
protected override void drawUnityEvents()
5163
{
5264
EditorGUILayout.PropertyField(OnTap);
5365

5466
base.drawUnityEvents();
5567
}
68+
5669
}
5770
}

Source/Assets/TouchScript/Scripts/Gestures/Gesture.cs

Lines changed: 18 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -200,29 +200,6 @@ public Gesture RequireGestureToFail
200200
}
201201
}
202202

203-
/// <summary>
204-
/// Gets or sets the flag if pointers should be treated as a cluster.
205-
/// </summary>
206-
/// <value> <c>true</c> if pointers should be treated as a cluster; otherwise, <c>false</c>. </value>
207-
/// <remarks>
208-
/// At the end of a gesture when pointers are lifted off due to the fact that computers are faster than humans the very last pointer's position will be gesture's <see cref="ScreenPosition"/> after that. This flag is used to combine several pointers which from the point of a user were lifted off simultaneously and set their centroid as gesture's <see cref="ScreenPosition"/>.
209-
/// </remarks>
210-
public bool CombinePointers
211-
{
212-
get { return combinePointers; }
213-
set { combinePointers = value; }
214-
}
215-
216-
/// <summary>
217-
/// Gets or sets time interval before gesture is recognized to combine all lifted pointers into a cluster to use its center as <see cref="ScreenPosition"/>.
218-
/// </summary>
219-
/// <value> Time in seconds to treat pointers lifted off during this interval as a single gesture. </value>
220-
public float CombinePointersInterval
221-
{
222-
get { return combinePointersInterval; }
223-
set { combinePointersInterval = value; }
224-
}
225-
226203
/// <summary>
227204
/// Gets or sets whether gesture should use Unity's SendMessage in addition to C# events.
228205
/// </summary>
@@ -347,7 +324,7 @@ public virtual Vector2 ScreenPosition
347324
if (!TouchManager.IsInvalidPosition(cachedScreenPosition)) return cachedScreenPosition;
348325
return TouchManager.INVALID_POSITION;
349326
}
350-
return ClusterUtils.Get2DCenterPosition(activePointers);
327+
return activePointers[0].Position;
351328
}
352329
}
353330

@@ -365,7 +342,7 @@ public virtual Vector2 PreviousScreenPosition
365342
return cachedPreviousScreenPosition;
366343
return TouchManager.INVALID_POSITION;
367344
}
368-
return ClusterUtils.GetPrevious2DCenterPosition(activePointers);
345+
return activePointers[0].PreviousPosition;
369346
}
370347
}
371348

@@ -474,13 +451,6 @@ protected IGestureManager gestureManager
474451
[SerializeField]
475452
private int maxPointers = 0;
476453

477-
[SerializeField]
478-
[ToggleLeft]
479-
private bool combinePointers = false;
480-
481-
[SerializeField]
482-
private float combinePointersInterval = .3f;
483-
484454
[SerializeField]
485455
[ToggleLeft]
486456
private bool useSendMessage = false;
@@ -509,7 +479,6 @@ protected IGestureManager gestureManager
509479

510480
private int numPointers;
511481
private ReadOnlyCollection<Pointer> readonlyActivePointers;
512-
private TimedSequence<Pointer> pointerSequence = new TimedSequence<Pointer>();
513482
private GestureManagerInstance gestureManagerInstance;
514483
private GestureState delayedStateChange = GestureState.Idle;
515484
private bool requiredGestureFailed = false;
@@ -520,13 +489,13 @@ protected IGestureManager gestureManager
520489
/// Cached screen position.
521490
/// Used to keep tap's position which can't be calculated from pointers when the gesture is recognized since all pointers are gone.
522491
/// </summary>
523-
private Vector2 cachedScreenPosition;
492+
protected Vector2 cachedScreenPosition;
524493

525494
/// <summary>
526495
/// Cached previous screen position.
527496
/// Used to keep tap's position which can't be calculated from pointers when the gesture is recognized since all pointers are gone.
528497
/// </summary>
529-
private Vector2 cachedPreviousScreenPosition;
498+
protected Vector2 cachedPreviousScreenPosition;
530499

531500
#endregion
532501

@@ -822,36 +791,20 @@ internal void INTERNAL_PointersReleased(IList<Pointer> pointers)
822791
for (var i = 0; i < count; i++) activePointers.Remove(pointers[i]);
823792
numPointers = total;
824793

825-
if (combinePointers)
826-
{
827-
for (var i = 0; i < count; i++) pointerSequence.Add(pointers[i]);
828-
829-
if (NumPointers == 0)
830-
{
831-
// Checking which points were removed in clusterExistenceTime seconds to set their centroid as cached screen position
832-
var cluster = pointerSequence.FindElementsLaterThan(Time.time - combinePointersInterval,
833-
shouldCachePointerPosition);
834-
cachedScreenPosition = ClusterUtils.Get2DCenterPosition(cluster);
835-
cachedPreviousScreenPosition = ClusterUtils.GetPrevious2DCenterPosition(cluster);
836-
}
837-
}
838-
else
839-
{
840-
if (NumPointers == 0)
841-
{
842-
var lastPoint = pointers[count - 1];
843-
if (shouldCachePointerPosition(lastPoint))
844-
{
845-
cachedScreenPosition = lastPoint.Position;
846-
cachedPreviousScreenPosition = lastPoint.PreviousPosition;
847-
}
848-
else
849-
{
850-
cachedScreenPosition = TouchManager.INVALID_POSITION;
851-
cachedPreviousScreenPosition = TouchManager.INVALID_POSITION;
852-
}
853-
}
854-
}
794+
if (NumPointers == 0)
795+
{
796+
var lastPoint = pointers[count - 1];
797+
if (shouldCachePointerPosition(lastPoint))
798+
{
799+
cachedScreenPosition = lastPoint.Position;
800+
cachedPreviousScreenPosition = lastPoint.PreviousPosition;
801+
}
802+
else
803+
{
804+
cachedScreenPosition = TouchManager.INVALID_POSITION;
805+
cachedPreviousScreenPosition = TouchManager.INVALID_POSITION;
806+
}
807+
}
855808

856809
pointersReleased(pointers);
857810
}

Source/Assets/TouchScript/Scripts/Gestures/TapGesture.cs

Lines changed: 70 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,29 @@ public float DistanceLimit
8686
}
8787
}
8888

89+
/// <summary>
90+
/// Gets or sets the flag if pointers should be treated as a cluster.
91+
/// </summary>
92+
/// <value> <c>true</c> if pointers should be treated as a cluster; otherwise, <c>false</c>. </value>
93+
/// <remarks>
94+
/// At the end of a gesture when pointers are lifted off due to the fact that computers are faster than humans the very last pointer's position will be gesture's <see cref="ScreenPosition"/> after that. This flag is used to combine several pointers which from the point of a user were lifted off simultaneously and set their centroid as gesture's <see cref="ScreenPosition"/>.
95+
/// </remarks>
96+
public bool CombinePointers
97+
{
98+
get { return combinePointers; }
99+
set { combinePointers = value; }
100+
}
101+
102+
/// <summary>
103+
/// Gets or sets time interval before gesture is recognized to combine all lifted pointers into a cluster to use its center as <see cref="ScreenPosition"/>.
104+
/// </summary>
105+
/// <value> Time in seconds to treat pointers lifted off during this interval as a single gesture. </value>
106+
public float CombinePointersInterval
107+
{
108+
get { return combinePointersInterval; }
109+
set { combinePointersInterval = value; }
110+
}
111+
89112
#endregion
90113

91114
#region Private variables
@@ -95,13 +118,18 @@ public float DistanceLimit
95118

96119
[SerializeField]
97120
[NullToggle(NullFloatValue = float.PositiveInfinity)]
98-
private float timeLimit =
99-
float.PositiveInfinity;
121+
private float timeLimit = float.PositiveInfinity;
100122

101123
[SerializeField]
102124
[NullToggle(NullFloatValue = float.PositiveInfinity)]
103-
private float distanceLimit =
104-
float.PositiveInfinity;
125+
private float distanceLimit = float.PositiveInfinity;
126+
127+
[SerializeField]
128+
[ToggleLeft]
129+
private bool combinePointers = false;
130+
131+
[SerializeField]
132+
private float combinePointersInterval = .3f;
105133

106134
private float distanceLimitInPixelsSquared;
107135

@@ -111,6 +139,7 @@ public float DistanceLimit
111139
private int tapsDone;
112140
private Vector2 startPosition;
113141
private Vector2 totalMovement;
142+
private TimedSequence<Pointer> pointerSequence = new TimedSequence<Pointer>();
114143

115144
#endregion
116145

@@ -208,27 +237,43 @@ protected override void pointersReleased(IList<Pointer> pointers)
208237
{
209238
base.pointersReleased(pointers);
210239

211-
if (NumPointers == 0)
212-
{
213-
if (!isActive)
214-
{
215-
setState(GestureState.Failed);
216-
return;
217-
}
218-
219-
// pointers outside of gesture target are ignored in shouldCachePointerPosition()
220-
// if all pointers are outside ScreenPosition will be invalid
221-
if (TouchManager.IsInvalidPosition(ScreenPosition))
222-
{
223-
setState(GestureState.Failed);
224-
}
225-
else
226-
{
227-
tapsDone++;
228-
isActive = false;
229-
if (tapsDone >= numberOfTapsRequired) setState(GestureState.Recognized);
230-
}
231-
}
240+
if (combinePointers)
241+
{
242+
var count = pointers.Count;
243+
for (var i = 0; i < count; i++) pointerSequence.Add(pointers[i]);
244+
245+
if (NumPointers == 0)
246+
{
247+
// Checking which points were removed in clusterExistenceTime seconds to set their centroid as cached screen position
248+
var cluster = pointerSequence.FindElementsLaterThan(Time.time - combinePointersInterval, shouldCachePointerPosition);
249+
cachedScreenPosition = ClusterUtils.Get2DCenterPosition(cluster);
250+
cachedPreviousScreenPosition = ClusterUtils.GetPrevious2DCenterPosition(cluster);
251+
}
252+
}
253+
else
254+
{
255+
if (NumPointers == 0)
256+
{
257+
if (!isActive)
258+
{
259+
setState(GestureState.Failed);
260+
return;
261+
}
262+
263+
// pointers outside of gesture target are ignored in shouldCachePointerPosition()
264+
// if all pointers are outside ScreenPosition will be invalid
265+
if (TouchManager.IsInvalidPosition(ScreenPosition))
266+
{
267+
setState(GestureState.Failed);
268+
}
269+
else
270+
{
271+
tapsDone++;
272+
isActive = false;
273+
if (tapsDone >= numberOfTapsRequired) setState(GestureState.Recognized);
274+
}
275+
}
276+
}
232277
}
233278

234279
/// <inheritdoc />

0 commit comments

Comments
 (0)