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

Unify Vector-related constant naming scheme across Web and Android implementations #3052

Merged
merged 5 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Vector(val x: Double, val y: Double) {
val magnitude = hypot(x, y)

init {
val isMagnitudeSufficient = magnitude > MINIMAL_MAGNITUDE
val isMagnitudeSufficient = magnitude > MINIMAL_RECOGNIZABLE_MAGNITUDE

unitX = if (isMagnitudeSufficient) x / magnitude else 0.0
unitY = if (isMagnitudeSufficient) y / magnitude else 0.0
Expand All @@ -39,7 +39,7 @@ class Vector(val x: Double, val y: Double) {
private val VECTOR_LEFT_DOWN: Vector = Vector(-1.0, 1.0)

private val VECTOR_ZERO: Vector = Vector(0.0, 0.0)
private const val MINIMAL_MAGNITUDE = 0.1
private const val MINIMAL_RECOGNIZABLE_MAGNITUDE = 0.1

fun fromDirection(direction: Int): Vector =
when (direction) {
Expand Down
2 changes: 1 addition & 1 deletion src/web/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const DEFAULT_TOUCH_SLOP = 15;
export const MINIMAL_FLING_VELOCITY = 0.1;
export const MINIMAL_RECOGNIZABLE_MAGNITUDE = 0.1;
7 changes: 4 additions & 3 deletions src/web/tools/Vector.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DiagonalDirections, Directions } from '../../Directions';
import { MINIMAL_FLING_VELOCITY } from '../constants';
import { MINIMAL_RECOGNIZABLE_MAGNITUDE } from '../constants';
import PointerTracker from './PointerTracker';

export default class Vector {
Expand All @@ -14,14 +14,15 @@ export default class Vector {
this.y = y;

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

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

static fromDirection(direction: Directions | DiagonalDirections): Vector {
return DirectionToVectorMappings.get(direction)!;
return DirectionToVectorMappings.get(direction) ?? new Vector(0, 0);
}

static fromVelocity(tracker: PointerTracker, pointerId: number) {
Expand Down
Loading