Skip to content

Commit f4bedbb

Browse files
committed
- Added 4 types of pointers: touch, mouse, pen, object.
- Moved pointer cache logic to inputs. - Moved CoordinatesRemapper from IInputSource to IRemapableInputSource. - Touch/Mouse/Windows handlers are now IInputSource and manage their pointers themselves. - Pointers are now created with a reference to the input source they belong to.
1 parent 09d2217 commit f4bedbb

21 files changed

Lines changed: 578 additions & 217 deletions

Source/Assets/TouchScript/Examples/Cube/Scripts/RedirectInput.cs

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@ public class RedirectInput : InputSource
1616
private MetaGesture gesture;
1717
private Dictionary<int, int> map = new Dictionary<int, int>();
1818

19-
public override void CancelPointer(Pointer pointer, bool @return)
19+
public override bool CancelPointer(Pointer pointer, bool @return)
2020
{
2121
base.CancelPointer(pointer, @return);
22-
23-
map.Remove(pointer.Id);
24-
if (@return)
25-
{
26-
TouchHit hit;
27-
if (!gesture.GetTargetHitResult(pointer.Position, out hit)) return;
28-
map.Add(pointer.Id, beginPointer(processCoords(hit.RaycastHit.textureCoord), pointer.Tags).Id);
29-
}
22+
//
23+
// map.Remove(pointer.Id);
24+
// if (@return)
25+
// {
26+
// TouchHit hit;
27+
// if (!gesture.GetTargetHitResult(pointer.Position, out hit)) return true;
28+
// beginPointer(pointer, processCoords(hit.RaycastHit.textureCoord), false)
29+
// map.Add(pointer.Id, .Id);
30+
// }
31+
return true;
3032
}
3133

3234
protected override void OnEnable()
@@ -62,38 +64,38 @@ private Vector2 processCoords(Vector2 value)
6264

6365
private void pointerBeganHandler(object sender, MetaGestureEventArgs metaGestureEventArgs)
6466
{
65-
var pointer = metaGestureEventArgs.Pointer;
66-
if (pointer.InputSource == this) return;
67-
map.Add(pointer.Id, beginPointer(processCoords(pointer.Hit.RaycastHit.textureCoord), pointer.Tags).Id);
67+
// var pointer = metaGestureEventArgs.Pointer;
68+
// if (pointer.InputSource == this) return;
69+
// map.Add(pointer.Id, beginPointer(processCoords(pointer.Hit.RaycastHit.textureCoord), pointer.Tags).Id);
6870
}
6971

7072
private void pointerMovedhandler(object sender, MetaGestureEventArgs metaGestureEventArgs)
7173
{
72-
int id;
73-
TouchHit hit;
74-
var pointer = metaGestureEventArgs.Pointer;
75-
if (pointer.InputSource == this) return;
76-
if (!map.TryGetValue(pointer.Id, out id)) return;
77-
if (!gesture.GetTargetHitResult(pointer.Position, out hit)) return;
78-
movePointer(id, processCoords(hit.RaycastHit.textureCoord));
74+
// int id;
75+
// TouchHit hit;
76+
// var pointer = metaGestureEventArgs.Pointer;
77+
// if (pointer.InputSource == this) return;
78+
// if (!map.TryGetValue(pointer.Id, out id)) return;
79+
// if (!gesture.GetTargetHitResult(pointer.Position, out hit)) return;
80+
// movePointer(id, processCoords(hit.RaycastHit.textureCoord));
7981
}
8082

8183
private void pointerEndedHandler(object sender, MetaGestureEventArgs metaGestureEventArgs)
8284
{
83-
int id;
84-
var pointer = metaGestureEventArgs.Pointer;
85-
if (pointer.InputSource == this) return;
86-
if (!map.TryGetValue(pointer.Id, out id)) return;
87-
endPointer(id);
85+
// int id;
86+
// var pointer = metaGestureEventArgs.Pointer;
87+
// if (pointer.InputSource == this) return;
88+
// if (!map.TryGetValue(pointer.Id, out id)) return;
89+
// endPointer(id);
8890
}
8991

9092
private void pointerCancelledhandler(object sender, MetaGestureEventArgs metaGestureEventArgs)
9193
{
92-
int id;
93-
var pointer = metaGestureEventArgs.Pointer;
94-
if (pointer.InputSource == this) return;
95-
if (!map.TryGetValue(pointer.Id, out id)) return;
96-
cancelPointer(id);
94+
// int id;
95+
// var pointer = metaGestureEventArgs.Pointer;
96+
// if (pointer.InputSource == this) return;
97+
// if (!map.TryGetValue(pointer.Id, out id)) return;
98+
// cancelPointer(id);
9799
}
98100

99101
}

Source/Assets/TouchScript/Modules/TUIO/Scripts/InputSources/TuioInput.cs

Lines changed: 85 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
using System;
77
using System.Collections.Generic;
88
using TouchScript.Pointers;
9+
using TouchScript.Utils;
910
using TUIOsharp;
1011
using TUIOsharp.DataProcessors;
1112
using TUIOsharp.Entities;
1213
using UnityEngine;
1314

14-
namespace TouchScript.InputSources
15+
namespace TouchScript.InputSources
1516
{
1617
/// <summary>
1718
/// Processes TUIO 1.1 input.
@@ -146,6 +147,19 @@ public Tags ObjectTags
146147
private int screenWidth;
147148
private int screenHeight;
148149

150+
private ObjectPool<TouchPointer> touchPool;
151+
private ObjectPool<ObjectPointer> objectPool;
152+
153+
#endregion
154+
155+
#region Constructor
156+
157+
public TuioInput()
158+
{
159+
touchPool = new ObjectPool<TouchPointer>(20, () => new TouchPointer(this), null, (t) => t.INTERNAL_Reset());
160+
objectPool = new ObjectPool<ObjectPointer>(10, () => new ObjectPointer(this), null, (t) => t.INTERNAL_Reset());
161+
}
162+
149163
#endregion
150164

151165
#region Public methods
@@ -159,82 +173,100 @@ public override void UpdateInput()
159173
}
160174

161175
/// <inheritdoc />
162-
public override void CancelPointer(Pointer pointer, bool @return)
176+
public override bool CancelPointer(Pointer pointer, bool @return)
163177
{
164178
base.CancelPointer(pointer, @return);
165179
lock (this)
166180
{
167-
TuioCursor cursor = null;
168-
foreach (var touchPoint in cursorToInternalId)
169-
{
170-
if (touchPoint.Value.Id == pointer.Id)
171-
{
172-
cursor = touchPoint.Key;
173-
break;
174-
}
175-
}
176-
if (cursor != null)
181+
if (pointer.Type == Pointer.PointerType.Touch)
177182
{
178-
cancelPointer(pointer.Id);
179-
if (@return)
183+
TuioCursor cursor = null;
184+
foreach (var touchPoint in cursorToInternalId)
180185
{
181-
cursorToInternalId[cursor] = beginPointer(pointer.Position, pointer.Tags, false);
186+
if (touchPoint.Value.Id == pointer.Id)
187+
{
188+
cursor = touchPoint.Key;
189+
break;
190+
}
182191
}
183-
else
192+
if (cursor != null)
184193
{
185-
cursorToInternalId.Remove(cursor);
194+
cancelPointer(pointer.Id);
195+
if (@return)
196+
{
197+
cursorToInternalId[cursor] = internalBeginTouch(pointer.Position, false);
198+
}
199+
else
200+
{
201+
cursorToInternalId.Remove(cursor);
202+
}
203+
return true;
186204
}
187-
return;
205+
return false;
188206
}
189207

190-
TuioBlob blob = null;
191-
foreach (var touchPoint in blobToInternalId)
208+
TuioObject obj = null;
209+
foreach (var touchPoint in objectToInternalId)
192210
{
193211
if (touchPoint.Value.Id == pointer.Id)
194212
{
195-
blob = touchPoint.Key;
213+
obj = touchPoint.Key;
196214
break;
197215
}
198216
}
199-
if (blob != null)
217+
if (obj != null)
200218
{
201219
cancelPointer(pointer.Id);
202220
if (@return)
203221
{
204-
var t = beginPointer(pointer.Position, pointer.Tags, false);
205-
t.Properties = pointer.Properties;
206-
blobToInternalId[blob] = t;
222+
objectToInternalId[obj] = internalBeginObject(pointer.Position, false);
207223
}
208224
else
209225
{
210-
blobToInternalId.Remove(blob);
226+
objectToInternalId.Remove(obj);
211227
}
212-
return;
228+
return true;
213229
}
214230

215-
TuioObject obj = null;
216-
foreach (var touchPoint in objectToInternalId)
231+
TuioBlob blob = null;
232+
foreach (var touchPoint in blobToInternalId)
217233
{
218234
if (touchPoint.Value.Id == pointer.Id)
219235
{
220-
obj = touchPoint.Key;
236+
blob = touchPoint.Key;
221237
break;
222238
}
223239
}
224-
if (obj != null)
240+
if (blob != null)
225241
{
226242
cancelPointer(pointer.Id);
227243
if (@return)
228244
{
229-
var t = beginPointer(pointer.Position, pointer.Tags, false);
230-
t.Properties = pointer.Properties;
231-
objectToInternalId[obj] = t;
245+
blobToInternalId[blob] = internalBeginObject(pointer.Position, false);
232246
}
233247
else
234248
{
235-
objectToInternalId.Remove(obj);
249+
blobToInternalId.Remove(blob);
236250
}
251+
return true;
237252
}
253+
254+
return false;
255+
}
256+
}
257+
258+
#endregion
259+
260+
#region Internal methods
261+
262+
public override void INTERNAL_ReleasePointer(Pointer pointer)
263+
{
264+
if (pointer.Type == Pointer.PointerType.Touch)
265+
{
266+
touchPool.Release(pointer as TouchPointer);
267+
} else if (pointer.Type == Pointer.PointerType.Object)
268+
{
269+
objectPool.Release(pointer as ObjectPointer);
238270
}
239271
}
240272

@@ -279,6 +311,20 @@ protected override void OnDisable()
279311

280312
#region Private functions
281313

314+
private Pointer internalBeginTouch(Vector2 position, bool remap = true)
315+
{
316+
var pointer = touchPool.Get();
317+
beginPointer(pointer, position, remap);
318+
return pointer;
319+
}
320+
321+
private Pointer internalBeginObject(Vector2 position, bool remap = true)
322+
{
323+
var pointer = objectPool.Get();
324+
beginPointer(pointer, position, remap);
325+
return pointer;
326+
}
327+
282328
private void connect()
283329
{
284330
if (!Application.isPlaying) return;
@@ -359,7 +405,7 @@ private void OnCursorAdded(object sender, TuioCursorEventArgs e)
359405
{
360406
var x = entity.X * screenWidth;
361407
var y = (1 - entity.Y) * screenHeight;
362-
cursorToInternalId.Add(entity, beginPointer(new Vector2(x, y), CursorTags));
408+
cursorToInternalId.Add(entity, internalBeginTouch(new Vector2(x, y)));
363409
}
364410
}
365411

@@ -398,7 +444,7 @@ private void OnBlobAdded(object sender, TuioBlobEventArgs e)
398444
{
399445
var x = entity.X * screenWidth;
400446
var y = (1 - entity.Y) * screenHeight;
401-
var touch = beginPointer(new Vector2(x, y), BlobTags);
447+
var touch = internalBeginObject(new Vector2(x, y));
402448
updateBlobProperties(touch, entity);
403449
blobToInternalId.Add(entity, touch);
404450
}
@@ -440,7 +486,8 @@ private void OnObjectAdded(object sender, TuioObjectEventArgs e)
440486
{
441487
var x = entity.X * screenWidth;
442488
var y = (1 - entity.Y) * screenHeight;
443-
var touch = beginPointer(new Vector2(x, y), new Tags(ObjectTags, getTagById(entity.ClassId)));
489+
var touch = internalBeginObject(new Vector2(x, y));
490+
// , new Tags(ObjectTags, getTagById(entity.ClassId))
444491
updateObjectProperties(touch, entity);
445492
objectToInternalId.Add(entity, touch);
446493
}

Source/Assets/TouchScript/Scripts/InputSources/IInputSource.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,8 @@ namespace TouchScript.InputSources
1313
/// <para>In TouchScript all pointer points (<see cref="Pointer"/>) come from input sources.</para>
1414
/// <para>If you want to feed pointers to the library the best way to do it is to create a custom input source.</para>
1515
/// </remarks>
16-
public interface IInputSource
16+
public interface IInputSource : INTERNAL_IInputSource
1717
{
18-
/// <summary>
19-
/// Gets or sets current coordinates remapper.
20-
/// </summary>
21-
/// <value>An object used to change coordinates of pointer points coming from this input source.</value>
22-
ICoordinatesRemapper CoordinatesRemapper { get; set; }
23-
2418
/// <summary>
2519
/// This method is called by <see cref="TouchManagerInstance"/> to synchronously update the input.
2620
/// </summary>
@@ -31,6 +25,21 @@ public interface IInputSource
3125
/// </summary>
3226
/// <param name="pointer">The pointer.</param>
3327
/// <param name="return">if set to <c>true</c> returns the pointer back to the system with different id.</param>
34-
void CancelPointer(Pointer pointer, bool @return);
28+
/// <returns><c>True</c> if the pointer belongs to this Input and was successfully cancelled; <c>false</c> otherwise.</returns>
29+
bool CancelPointer(Pointer pointer, bool @return);
30+
}
31+
32+
public interface IRemapableInputSource
33+
{
34+
/// <summary>
35+
/// Gets or sets current coordinates remapper.
36+
/// </summary>
37+
/// <value>An object used to change coordinates of pointer points coming from this input source.</value>
38+
ICoordinatesRemapper CoordinatesRemapper { get; set; }
39+
}
40+
41+
public interface INTERNAL_IInputSource
42+
{
43+
void INTERNAL_ReleasePointer(Pointer pointer);
3544
}
3645
}

0 commit comments

Comments
 (0)