Vixen
02b45cc4
csharp
public sealed class TouchTracker

Turns whatever a mobile platform calls a finger into a small, stable integer, and works out how far it moved.

No guide page documents this yet — the page shows what the code says about itself.

Remarks

Both mobile platforms need this and neither provides it. UIKit identifies a touch by the address of a UITouch object, which is reused after the touch ends; Android identifies one by a pointer id that is stable for a gesture and reused afterwards, and exposes it by index into a batched event whose indices renumber as fingers leave. Neither is a number an application should ever see, and both need the same bookkeeping to become one: allocate on down, look up on move, release on up.

Small ids, reused lowest-first. DeviceId is the finger, and code downstream indexes arrays by it — a UITouch pointer would not do, and neither would a monotonically increasing counter after an hour of play. So the tracker hands out the lowest free id and takes it back when the finger lifts.

The delta is computed here for the same reason. Android's MotionEvent gives a position and no delta; UIKit gives the previous location only while the touch object is alive. Deriving it from the last position this tracker saw gives one definition of "moved" across both, and it is the definition a gesture recogniser upstream needs.

Not thread-safe, and does not need to be: touches arrive on the platform's own thread, which is the thread that owns the platform.

Fields and properties (2)

  • public const int MaximumTouches

    How many fingers may be tracked at once.

  • public int Count

    How many fingers are down.

Methods (5)

  • public bool TryBegin(long key, Vector2 position, out int finger)

    Starts tracking a finger.

  • public bool TryMove(long key, Vector2 position, out int finger, out Vector2 delta)

    Records a finger moving.

  • public bool TryEnd(long key, out int finger)

    Stops tracking a finger and releases its id.

  • public bool TryGetPosition(long key, out Vector2? position)

    Where a tracked finger was last seen.

  • public IReadOnlyList<int> Clear()

    Releases every finger and returns the ids that were down.

Used by (1)

  • TouchTrackerTestsVixen.Platform.Tests