Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding changed points and coalescing to pointer tracker. Fixes #133. #134

Merged
merged 2 commits into from
Aug 15, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 33 additions & 19 deletions src/lib/PointerTracker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,28 @@ export class Pointer {
/** ID for this pointer */
id: number = -1;

constructor (nativePointer: Touch | PointerEvent | MouseEvent) {
this.pageX = nativePointer.pageX;
this.pageY = nativePointer.pageY;
this.clientX = nativePointer.clientX;
this.clientY = nativePointer.clientY;

if (self.Touch && nativePointer instanceof Touch) {
this.id = nativePointer.identifier;
} else if (isPointerEvent(nativePointer)) { // is PointerEvent
this.id = nativePointer.pointerId;
constructor (private _nativePointer: Touch | PointerEvent | MouseEvent) {
this.pageX = _nativePointer.pageX;
this.pageY = _nativePointer.pageY;
this.clientX = _nativePointer.clientX;
this.clientY = _nativePointer.clientY;

if (self.Touch && _nativePointer instanceof Touch) {
this.id = _nativePointer.identifier;
} else if (isPointerEvent(_nativePointer)) { // is PointerEvent
this.id = _nativePointer.pointerId;
}
}

/**
* Returns an expanded set of Pointers for high-resolution inputs.
*/
getCoalesced(): Pointer[] {
if ('getCoalescedEvents' in this._nativePointer) {
return this._nativePointer.getCoalescedEvents().map(p => new Pointer(p));
}
return [this];
}
}

const isPointerEvent = (event: any): event is PointerEvent =>
Expand All @@ -33,9 +43,13 @@ const isPointerEvent = (event: any): event is PointerEvent =>
const noop = () => {};

export type InputEvent = TouchEvent | PointerEvent | MouseEvent;
type StartCallback = ((pointer: Pointer, event: InputEvent) => boolean);
type MoveCallback = ((previousPointers: Pointer[], event: InputEvent) => void);
type EndCallback = ((pointer: Pointer, event: InputEvent) => void);
type StartCallback = (pointer: Pointer, event: InputEvent) => boolean;
type MoveCallback = (
previousPointers: Pointer[],
changedPointers: Pointer[],
event: InputEvent,
) => void;
type EndCallback = (pointer: Pointer, event: InputEvent) => void;

interface PointerTrackerCallbacks {
/**
Expand All @@ -54,6 +68,7 @@ interface PointerTrackerCallbacks {
* @param previousPointers The state of the pointers before this event.
* This contains the same number of pointers, in the same order, as
* this.currentPointers and this.startPointers.
* @param changedPointers The pointers that have changed since the last move callback.
* @param event The event related to the pointer changes.
*/
move?: MoveCallback;
Expand Down Expand Up @@ -172,19 +187,18 @@ export class PointerTracker {
const changedPointers = ('changedTouches' in event) ? // Shortcut for 'is touch event'.
Array.from(event.changedTouches).map(t => new Pointer(t)) :
[new Pointer(event)];

let shouldCallback = false;
const trackedChangedPointers = [];

for (const pointer of changedPointers) {
const index = this.currentPointers.findIndex(p => p.id === pointer.id);
if (index === -1) continue;
shouldCallback = true;
if (index === -1) continue; // Not a pointer we're tracking
trackedChangedPointers.push(pointer);
this.currentPointers[index] = pointer;
}

if (!shouldCallback) return;
if (!trackedChangedPointers[0]) return;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trackedChangedPointers.length <= 0?

Copy link
Collaborator Author

@jakearchibald jakearchibald Aug 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't use < unless it could be less than 0, but happy to go for === 0.


this._moveCallback(previousPointers, event);
this._moveCallback(previousPointers, trackedChangedPointers, event);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/lib/PointerTracker/missing-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ interface Window {
PointerEvent: typeof PointerEvent;
Touch: typeof Touch;
}

interface PointerEvent {
getCoalescedEvents(): PointerEvent[];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eugh, sad they didn’t include that in TypeScript v3 :(

}