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

Make FlingHandler use velocity as the activation metric. #2796

Merged
merged 30 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
aaa4c14
fling now detects movement by velocity, first working version tested …
latekvo Mar 8, 2024
618e1e3
fix commit requirement error
latekvo Mar 8, 2024
38e736a
- applied fling changes from web to native android devices
latekvo Mar 8, 2024
a8f0ed1
add missing function,
latekvo Mar 8, 2024
17d7b22
Update android/src/main/java/com/swmansion/gesturehandler/core/FlingG…
latekvo Mar 11, 2024
fba0a25
Applied changes from review.
latekvo Mar 11, 2024
53ec2f3
resolved merge conflict
latekvo Mar 11, 2024
0e94b6f
Applied review suggestions.
latekvo Mar 11, 2024
adb8546
Fix pointer not being detected out of bounds on web.
latekvo Mar 11, 2024
555862e
Simplify code.
latekvo Mar 11, 2024
6284b70
Merge branch 'main' into @latekvo/fix_fling
latekvo Mar 11, 2024
5344a7a
Apply review suggestions.
latekvo Mar 11, 2024
92e7191
Optimization
latekvo Mar 12, 2024
dacb57f
Merge branch '@latekvo/fix_fling' of https://github.com/software-mans…
latekvo Mar 12, 2024
dd34b38
Apply changes to kotlin code
latekvo Mar 12, 2024
13138be
Move code into a separate Vector class. Decrease sensitivity.
latekvo Mar 12, 2024
63007f4
Apply changes from JS to KT: separate logic into a separate class
latekvo Mar 12, 2024
bf8693a
Minor cleanup.
latekvo Mar 12, 2024
774bdb1
- moved Vector class to separate file
latekvo Mar 13, 2024
d3cb42c
Applied cosmetic review suggestions.
latekvo Mar 13, 2024
534050e
Fling and Vector code cleanup.
latekvo Mar 13, 2024
19fc039
Merge branch 'main' into @latekvo/fix_fling
latekvo Mar 14, 2024
b4711c6
- Velocity tracker now tracks the entire user movement.
latekvo Mar 14, 2024
ec8d55f
Merge branch '@latekvo/fix_fling' of https://github.com/software-mans…
latekvo Mar 14, 2024
93a238d
Create alternative static constructors for Vector, simplified code, r…
latekvo Mar 14, 2024
4e2c455
Merge branch 'main' into @latekvo/fix_fling
latekvo Mar 15, 2024
1af78a7
Remove potential edge-case crash
latekvo Mar 15, 2024
6f70fed
Merge branch 'main' into @latekvo/fix_fling
latekvo Mar 15, 2024
b84adae
Make Vector members private readonly, make magnitude a getter of priv…
latekvo Mar 15, 2024
06f38f3
Merge branch '@latekvo/fix_fling' of https://github.com/software-mans…
latekvo Mar 15, 2024
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
13 changes: 6 additions & 7 deletions src/web/handlers/FlingGestureHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Directions } from '../../Directions';
import { AdaptedEvent, Config } from '../interfaces';

import GestureHandler from './GestureHandler';
import Vector, { DirectionToVectorMappings } from '../tools/Vector';
import Vector from '../tools/Vector';

const DEFAULT_MAX_DURATION_MS = 800;
const DEFAULT_MIN_VELOCITY = 700;
Expand Down Expand Up @@ -48,16 +48,15 @@ export default class FlingGestureHandler extends GestureHandler {
}

private tryEndFling(): boolean {
const velocityVector = new Vector().fromVelocity(
this.tracker,
this.keyPointer
);
const velocityVector = Vector.fromVelocity(this.tracker, this.keyPointer);

const getAlignment = (direction: Directions) => {
const directionVector = DirectionToVectorMappings.get(direction)!;
return (
direction & this.direction &&
velocityVector.isSimilar(directionVector, this.minDirectionalAlignment)
velocityVector.isSimilar(
Vector.fromDirection(direction),
this.minDirectionalAlignment
)
);
};

Expand Down
55 changes: 24 additions & 31 deletions src/web/tools/Vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,32 @@ import { MINIMAL_FLING_VELOCITY } from '../constants';
import PointerTracker from './PointerTracker';

export default class Vector {
x: number = 0;
y: number = 0;
unitX: number = 0;
unitY: number = 0;
x = 0;
y = 0;
unitX = 0;
unitY = 0;
magnitude = 0;
m-bert marked this conversation as resolved.
Show resolved Hide resolved

fromDirection(direction: Directions) {
[this.x, this.y] = [this.unitX, this.unitY] =
DirectionMappings.get(direction)!;

return this;
}

fromVelocity(tracker: PointerTracker, pointerId: number) {
this.x = tracker.getVelocityX(pointerId);
this.y = tracker.getVelocityY(pointerId);
constructor(x: number, y: number) {
this.x = x;
this.y = y;

this.magnitude = Math.hypot(this.x, this.y);
const isMagnitudeSufficient = this.magnitude > MINIMAL_FLING_VELOCITY;

this.unitX = isMagnitudeSufficient ? this.x / this.magnitude : 0;
this.unitY = isMagnitudeSufficient ? this.y / this.magnitude : 0;
}

return this;
static fromDirection(direction: Directions) {
return DirectionToVectorMappings.get(direction)!;
}

static fromVelocity(tracker: PointerTracker, pointerId: number) {
return new Vector(
tracker.getVelocityX(pointerId),
tracker.getVelocityY(pointerId)
);
}

computeSimilarity(vector: Vector) {
Expand All @@ -34,22 +38,11 @@ export default class Vector {
isSimilar(vector: Vector, threshold: number) {
return this.computeSimilarity(vector) > threshold;
}

get magnitude() {
return Math.hypot(this.x, this.y);
}
}

const DirectionMappings = new Map<Directions, number[]>([
[Directions.LEFT, [-1, 0]],
[Directions.RIGHT, [1, 0]],
[Directions.UP, [0, -1]],
[Directions.DOWN, [0, 1]],
const DirectionToVectorMappings = new Map<Directions, Vector>([
[Directions.LEFT, new Vector(-1, 0)],
[Directions.RIGHT, new Vector(1, 0)],
[Directions.UP, new Vector(0, -1)],
[Directions.DOWN, new Vector(0, 1)],
]);

export const DirectionToVectorMappings = new Map<Directions, Vector>(
Object.values(Directions).map((direction) => [
direction,
new Vector().fromDirection(direction),
])
);
Loading