Class
Touches
View sourceStores the active touch contacts for an application as a world resource.
TouchPlugin provides one Touches instance in the app’s default world and
keeps it synchronized with touch-only pointer events. It creates a
TouchPointer when a touch starts, updates its position on moves, and
removes it when the browser reports a pointer-up or pointer-cancel event.
Read the resource with world.getResource(Touches); it is input state, not
an ECS component.
The resource owns an indexed active list and a map from browser
pointerId values to compact TouchId values. The list is initialized with
ten normal slots, numbered 0 through 9. A browser pointer ID can be
arbitrary, while a TouchPointer.id is the compact slot assigned by
Touches.set. Deleting a pointer releases its slot for reuse.
Examples
import { Touches } from '@wimaengine/touch'
// Register TouchPlugin before reading the resource.
function readTouches(world) {
const touches = world.getResource(Touches)
for (const pointer of touches.getActive()) {
const dx = pointer.position.x - pointer.lastposition.x
const dy = pointer.position.y - pointer.lastposition.y
console.log(pointer.id, dx, dy)
}
}Constructor
Methods
getId(pointerId: number): number
View sourceFinds the compact slot assigned to a browser pointer ID.
The argument is the browser event’s pointerId, not a compact slot. The
mapping exists after Touches.set adds a new pointer and is removed by
Touches.delete. An unknown or deleted browser pointer ID returns
undefined.
getFirst(): TouchPointer
View sourceReturns the active pointer in the lowest supported slot.
This scans the current list from index 0 in order; with the initialized
list that means slots 0 through 9. It does not sort by browser pointer ID
or by the time a touch was added. If the list has grown beyond those ten
slots, an additional entry can also be found after the normal slots are
empty. It returns undefined when no slot is occupied.
getLast(): TouchPointer
View sourceReturns the active pointer in the highest of the ten initial slots.
The search runs from slot 9 down to slot 0. It returns undefined when
those ten slots are empty and ignores entries beyond slot 9.
getActive(): TouchPointer[]
View sourceReturns all active pointers in slot order.
The returned array is a new array, so adding or removing entries from it
does not change this resource. The TouchPointer objects inside it are the
same objects stored in the resource. In normal use the result has at most
ten pointers; if an additional slot has been created, it is included too.
set(pointerId: number, pointer: TouchPointer): number
View sourceAdds a pointer or replaces the pointer stored for a browser ID.
pointerId is the browser event ID. For a new browser ID, the first empty
slot is selected and the mapping is stored. For an ID already in the map,
its existing slot is reused and the pointer in that slot is replaced. In
both cases this method assigns the compact slot to pointer.id, stores
the same pointer object in the resource, and returns that slot. Deleting a
pointer and setting a new one can therefore reuse a different slot.
The active list is initialized with slots 0 through 9. When all ten slots
are occupied, adding another distinct pointer can create an extra entry at
slot 10 instead of reporting a capacity error. Because that assignment
grows the array, later distinct pointers can extend it again. Such entries
are visible to Touches.getActive and Touches.get; getFirst can find
them after lower slots are empty, but Touches.getLast still only scans
slots 9 through 0. Treat ten simultaneous contacts as the supported
capacity rather than relying on this overflow behavior.
delete(pointerId: number): number
View sourceRemoves the pointer associated with a browser pointer ID.
The argument is the browser pointerId, not the compact TouchId.
When the mapping exists, the slot is cleared, the mapping is deleted, and
the released compact slot is returned. When the pointer ID is unknown or
was already deleted, the resource is unchanged and the method returns
undefined. The removed TouchPointer object is not modified or returned.
get(id: number): TouchPointer
View sourceReads an active pointer by its compact slot.
This is direct indexed lookup, so it expects the TouchId returned by
Touches.getId or Touches.set, not the browser pointerId. It returns
undefined for an empty or out-of-range slot.
