Skip to content

Commit dd4d40f

Browse files
authored
PointerTracker refactor. (#2931)
## Description Fixing #2929 required adding additional information to `TrackerElement`. If we were to leave current structure of `PointerTracker`, we would have to add few more methods. After refactoring, there are only 2 that were not present before: - `getLastViewRelativeCoords` - `getViewRelativeCoordsSum` I'd like to make similar changes to our handlers in the future (i.e. use `coords` object instead of `lastX` and `lastY` variables). ## Test plan Tested on example app.
1 parent 4def677 commit dd4d40f

9 files changed

+158
-159
lines changed

src/web/detectors/RotationGestureDetector.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,14 @@ export default class RotationGestureDetector
3939

4040
const [firstPointerID, secondPointerID] = this.keyPointers;
4141

42-
const firstPointerX: number = tracker.getLastX(firstPointerID);
43-
const firstPointerY: number = tracker.getLastY(firstPointerID);
44-
const secondPointerX: number = tracker.getLastX(secondPointerID);
45-
const secondPointerY: number = tracker.getLastY(secondPointerID);
42+
const firstPointerCoords = tracker.getLastAbsoluteCoords(firstPointerID);
43+
const secondPointerCoords = tracker.getLastAbsoluteCoords(secondPointerID);
4644

47-
const vectorX: number = secondPointerX - firstPointerX;
48-
const vectorY: number = secondPointerY - firstPointerY;
45+
const vectorX: number = secondPointerCoords.x - firstPointerCoords.x;
46+
const vectorY: number = secondPointerCoords.y - firstPointerCoords.y;
4947

50-
this.anchorX = (firstPointerX + secondPointerX) / 2;
51-
this.anchorY = (firstPointerY + secondPointerY) / 2;
48+
this.anchorX = (firstPointerCoords.x + secondPointerCoords.x) / 2;
49+
this.anchorY = (firstPointerCoords.y + secondPointerCoords.y) / 2;
5250

5351
//Angle diff should be positive when rotating in clockwise direction
5452
const angle: number = -Math.atan2(vectorY, vectorX);

src/web/detectors/ScaleGestureDetector.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,10 @@ export default class ScaleGestureDetector implements ScaleGestureListener {
7676

7777
const div: number = pointerUp ? numOfPointers - 1 : numOfPointers;
7878

79-
const sumX = tracker.getSumX(ignoredPointer);
80-
const sumY = tracker.getSumY(ignoredPointer);
79+
const coordsSum = tracker.getAbsoluteCoordsSum();
8180

82-
const focusX = sumX / div;
83-
const focusY = sumY / div;
81+
const focusX = coordsSum.x / div;
82+
const focusY = coordsSum.y / div;
8483

8584
//Determine average deviation from focal point
8685

@@ -92,8 +91,8 @@ export default class ScaleGestureDetector implements ScaleGestureListener {
9291
return;
9392
}
9493

95-
devSumX += Math.abs(value.lastX - focusX);
96-
devSumY += Math.abs(value.lastY - focusY);
94+
devSumX += Math.abs(value.abosoluteCoords.x - focusX);
95+
devSumY += Math.abs(value.abosoluteCoords.y - focusY);
9796
});
9897

9998
const devX: number = devSumX / div;

src/web/handlers/GestureHandler.ts

+27-26
Original file line numberDiff line numberDiff line change
@@ -405,10 +405,9 @@ export default abstract class GestureHandler implements IGestureHandler {
405405
nativeEvent: {
406406
numberOfPointers: this.tracker.getTrackedPointersCount(),
407407
state: newState,
408-
pointerInside: this.delegate.isPointerInBounds({
409-
x: this.tracker.getLastAvgX(),
410-
y: this.tracker.getLastAvgY(),
411-
}),
408+
pointerInside: this.delegate.isPointerInBounds(
409+
this.tracker.getAbsoluteCoordsAverage()
410+
),
412411
...this.transformNativeEvent(),
413412
handlerTag: this.handlerTag,
414413
target: this.viewRef,
@@ -442,10 +441,10 @@ export default abstract class GestureHandler implements IGestureHandler {
442441

443442
all.push({
444443
id: id,
445-
x: element.lastX - rect.pageX,
446-
y: element.lastY - rect.pageY,
447-
absoluteX: element.lastX,
448-
absoluteY: element.lastY,
444+
x: element.abosoluteCoords.x - rect.pageX,
445+
y: element.abosoluteCoords.y - rect.pageY,
446+
absoluteX: element.abosoluteCoords.x,
447+
absoluteY: element.abosoluteCoords.y,
449448
});
450449
});
451450

@@ -465,10 +464,10 @@ export default abstract class GestureHandler implements IGestureHandler {
465464

466465
changed.push({
467466
id: id,
468-
x: element.lastX - rect.pageX,
469-
y: element.lastY - rect.pageY,
470-
absoluteX: element.lastX,
471-
absoluteY: element.lastY,
467+
x: element.abosoluteCoords.x - rect.pageX,
468+
y: element.abosoluteCoords.y - rect.pageY,
469+
absoluteX: element.abosoluteCoords.x,
470+
absoluteY: element.abosoluteCoords.y,
472471
});
473472
});
474473
}
@@ -534,18 +533,18 @@ export default abstract class GestureHandler implements IGestureHandler {
534533

535534
all.push({
536535
id: id,
537-
x: element.lastX - rect.pageX,
538-
y: element.lastY - rect.pageY,
539-
absoluteX: element.lastX,
540-
absoluteY: element.lastY,
536+
x: element.abosoluteCoords.x - rect.pageX,
537+
y: element.abosoluteCoords.y - rect.pageY,
538+
absoluteX: element.abosoluteCoords.x,
539+
absoluteY: element.abosoluteCoords.y,
541540
});
542541

543542
changed.push({
544543
id: id,
545-
x: element.lastX - rect.pageX,
546-
y: element.lastY - rect.pageY,
547-
absoluteX: element.lastX,
548-
absoluteY: element.lastY,
544+
x: element.abosoluteCoords.x - rect.pageX,
545+
y: element.abosoluteCoords.y - rect.pageY,
546+
absoluteX: element.abosoluteCoords.x,
547+
absoluteY: element.abosoluteCoords.y,
549548
});
550549
});
551550

@@ -570,12 +569,13 @@ export default abstract class GestureHandler implements IGestureHandler {
570569
protected transformNativeEvent(): Record<string, unknown> {
571570
// those properties are shared by most handlers and if not this method will be overriden
572571
const rect = this.delegate.measureView();
572+
const lastCoords = this.tracker.getAbsoluteCoordsAverage();
573573

574574
return {
575-
x: this.tracker.getLastAvgX() - rect.pageX,
576-
y: this.tracker.getLastAvgY() - rect.pageY,
577-
absoluteX: this.tracker.getLastAvgX(),
578-
absoluteY: this.tracker.getLastAvgY(),
575+
x: lastCoords.x - rect.pageX,
576+
y: lastCoords.y - rect.pageY,
577+
absoluteX: lastCoords.x,
578+
absoluteY: lastCoords.y,
579579
};
580580
}
581581

@@ -720,8 +720,9 @@ export default abstract class GestureHandler implements IGestureHandler {
720720
}
721721

722722
const rect = this.delegate.measureView();
723-
const offsetX: number = this.tracker.getLastX() - rect.pageX;
724-
const offsetY: number = this.tracker.getLastY() - rect.pageY;
723+
const { x, y } = this.tracker.getLastAbsoluteCoords();
724+
const offsetX: number = x - rect.pageX;
725+
const offsetY: number = y - rect.pageY;
725726

726727
if (
727728
offsetX >= left &&

src/web/handlers/NativeViewGestureHandler.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ export default class NativeViewGestureHandler extends GestureHandler {
6464
}
6565

6666
private newPointerAction(): void {
67-
this.startX = this.tracker.getLastAvgX();
68-
this.startY = this.tracker.getLastAvgY();
67+
const lastCoords = this.tracker.getAbsoluteCoordsAverage();
68+
this.startX = lastCoords.x;
69+
this.startY = lastCoords.y;
6970

7071
if (this.currentState !== State.UNDETERMINED) {
7172
return;
@@ -80,8 +81,9 @@ export default class NativeViewGestureHandler extends GestureHandler {
8081
protected onPointerMove(event: AdaptedEvent): void {
8182
this.tracker.track(event);
8283

83-
const dx = this.startX - this.tracker.getLastAvgX();
84-
const dy = this.startY - this.tracker.getLastAvgY();
84+
const lastCoords = this.tracker.getAbsoluteCoordsAverage();
85+
const dx = this.startX - lastCoords.x;
86+
const dy = this.startY - lastCoords.y;
8587
const distSq = dx * dx + dy * dy;
8688

8789
if (distSq >= this.minDistSq) {

src/web/handlers/PanGestureHandler.ts

+29-18
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,9 @@ export default class PanGestureHandler extends GestureHandler {
219219
this.tracker.addToTracker(event);
220220
super.onPointerDown(event);
221221

222-
this.lastX = this.tracker.getLastAvgX();
223-
this.lastY = this.tracker.getLastAvgY();
222+
const lastCoords = this.tracker.getAbsoluteCoordsAverage();
223+
this.lastX = lastCoords.x;
224+
this.lastY = lastCoords.y;
224225

225226
this.startX = this.lastX;
226227
this.startY = this.lastY;
@@ -239,8 +240,9 @@ export default class PanGestureHandler extends GestureHandler {
239240
this.offsetX += this.lastX - this.startX;
240241
this.offsetY += this.lastY - this.startY;
241242

242-
this.lastX = this.tracker.getLastAvgX();
243-
this.lastY = this.tracker.getLastAvgY();
243+
const lastCoords = this.tracker.getAbsoluteCoordsAverage();
244+
this.lastX = lastCoords.x;
245+
this.lastY = lastCoords.y;
244246

245247
this.startX = this.lastX;
246248
this.startY = this.lastY;
@@ -259,8 +261,9 @@ export default class PanGestureHandler extends GestureHandler {
259261
protected onPointerUp(event: AdaptedEvent): void {
260262
super.onPointerUp(event);
261263
if (this.currentState === State.ACTIVE) {
262-
this.lastX = this.tracker.getLastAvgX();
263-
this.lastY = this.tracker.getLastAvgY();
264+
const lastCoords = this.tracker.getAbsoluteCoordsAverage();
265+
this.lastX = lastCoords.x;
266+
this.lastY = lastCoords.y;
264267
}
265268

266269
this.tracker.removeFromTracker(event.pointerId);
@@ -280,8 +283,9 @@ export default class PanGestureHandler extends GestureHandler {
280283
this.offsetX += this.lastX - this.startX;
281284
this.offsetY += this.lastY - this.startY;
282285

283-
this.lastX = this.tracker.getLastAvgX();
284-
this.lastY = this.tracker.getLastAvgY();
286+
const lastCoords = this.tracker.getAbsoluteCoordsAverage();
287+
this.lastX = lastCoords.x;
288+
this.lastY = lastCoords.y;
285289

286290
this.startX = this.lastX;
287291
this.startY = this.lastY;
@@ -299,10 +303,13 @@ export default class PanGestureHandler extends GestureHandler {
299303
protected onPointerMove(event: AdaptedEvent): void {
300304
this.tracker.track(event);
301305

302-
this.lastX = this.tracker.getLastAvgX();
303-
this.lastY = this.tracker.getLastAvgY();
304-
this.velocityX = this.tracker.getVelocityX(event.pointerId);
305-
this.velocityY = this.tracker.getVelocityY(event.pointerId);
306+
const lastCoords = this.tracker.getAbsoluteCoordsAverage();
307+
this.lastX = lastCoords.x;
308+
this.lastY = lastCoords.y;
309+
310+
const velocity = this.tracker.getVelocity(event.pointerId);
311+
this.velocityX = velocity.x;
312+
this.velocityY = velocity.y;
306313

307314
this.checkBegan();
308315

@@ -316,10 +323,13 @@ export default class PanGestureHandler extends GestureHandler {
316323

317324
this.tracker.track(event);
318325

319-
this.lastX = this.tracker.getLastAvgX();
320-
this.lastY = this.tracker.getLastAvgY();
321-
this.velocityX = this.tracker.getVelocityX(event.pointerId);
322-
this.velocityY = this.tracker.getVelocityY(event.pointerId);
326+
const lastCoords = this.tracker.getAbsoluteCoordsAverage();
327+
this.lastX = lastCoords.x;
328+
this.lastY = lastCoords.y;
329+
330+
const velocity = this.tracker.getVelocity(event.pointerId);
331+
this.velocityX = velocity.x;
332+
this.velocityY = velocity.y;
323333

324334
this.checkBegan();
325335

@@ -453,8 +463,9 @@ export default class PanGestureHandler extends GestureHandler {
453463
}, this.activateAfterLongPress);
454464
}
455465
} else {
456-
this.velocityX = this.tracker.getVelocityX(event.pointerId);
457-
this.velocityY = this.tracker.getVelocityY(event.pointerId);
466+
const velocity = this.tracker.getVelocity(event.pointerId);
467+
this.velocityX = velocity.x;
468+
this.velocityY = velocity.y;
458469
}
459470
}
460471

src/web/handlers/TapGestureHandler.ts

+18-12
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,22 @@ export default class TapGestureHandler extends GestureHandler {
133133
this.offsetX += this.lastX - this.startX;
134134
this.offsetY += this.lastY - this.startY;
135135

136-
this.lastX = this.tracker.getLastAvgX();
137-
this.lastY = this.tracker.getLastAvgY();
136+
const lastCoords = this.tracker.getAbsoluteCoordsAverage();
137+
this.lastX = lastCoords.x;
138+
this.lastY = lastCoords.y;
138139

139-
this.startX = this.tracker.getLastAvgX();
140-
this.startY = this.tracker.getLastAvgY();
140+
this.startX = lastCoords.x;
141+
this.startY = lastCoords.y;
141142

142143
this.updateState(event);
143144
}
144145

145146
protected onPointerUp(event: AdaptedEvent): void {
146147
super.onPointerUp(event);
147-
this.lastX = this.tracker.getLastAvgX();
148-
this.lastY = this.tracker.getLastAvgY();
148+
149+
const lastCoords = this.tracker.getAbsoluteCoordsAverage();
150+
this.lastX = lastCoords.x;
151+
this.lastY = lastCoords.y;
149152

150153
this.tracker.removeFromTracker(event.pointerId);
151154

@@ -159,8 +162,9 @@ export default class TapGestureHandler extends GestureHandler {
159162
this.offsetX += this.lastX - this.startX;
160163
this.offsetY += this.lastY = this.startY;
161164

162-
this.lastX = this.tracker.getLastAvgX();
163-
this.lastY = this.tracker.getLastAvgY();
165+
const lastCoords = this.tracker.getAbsoluteCoordsAverage();
166+
this.lastX = lastCoords.x;
167+
this.lastY = lastCoords.y;
164168

165169
this.startX = this.lastX;
166170
this.startY = this.lastY;
@@ -172,8 +176,9 @@ export default class TapGestureHandler extends GestureHandler {
172176
this.trySettingPosition(event);
173177
this.tracker.track(event);
174178

175-
this.lastX = this.tracker.getLastAvgX();
176-
this.lastY = this.tracker.getLastAvgY();
179+
const lastCoords = this.tracker.getAbsoluteCoordsAverage();
180+
this.lastX = lastCoords.x;
181+
this.lastY = lastCoords.y;
177182

178183
this.updateState(event);
179184

@@ -184,8 +189,9 @@ export default class TapGestureHandler extends GestureHandler {
184189
this.trySettingPosition(event);
185190
this.tracker.track(event);
186191

187-
this.lastX = this.tracker.getLastAvgX();
188-
this.lastY = this.tracker.getLastAvgY();
192+
const lastCoords = this.tracker.getAbsoluteCoordsAverage();
193+
this.lastX = lastCoords.x;
194+
this.lastY = lastCoords.y;
189195

190196
this.updateState(event);
191197

src/web/tools/GestureHandlerOrchestrator.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -335,13 +335,7 @@ export default class GestureHandlerOrchestrator {
335335
// TODO: Find better way to handle that issue, for example by activation order and handler cancelling
336336

337337
const isPointerWithinBothBounds = (pointer: number) => {
338-
const handlerX: number = handler.getTracker().getLastX(pointer);
339-
const handlerY: number = handler.getTracker().getLastY(pointer);
340-
341-
const point = {
342-
x: handlerX,
343-
y: handlerY,
344-
};
338+
const point = handler.getTracker().getLastAbsoluteCoords(pointer);
345339

346340
return (
347341
handler.getDelegate().isPointerInBounds(point) &&

0 commit comments

Comments
 (0)