UNPKG

@aurigma/design-atoms

Version:

Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.

71 lines 2.78 kB
export class PointerInputHelper { constructor(settings) { this._clicks = []; this.isInDownUpThresholdArea = (point, isTouch) => { const threshold = this._getThreshold(isTouch, false); return this._isInThresholdArea(point, this._lastDownPoint, threshold); }; this._isInClickThresholdArea = (point, isTouch) => { const threshold = this._getThreshold(isTouch, true); const prevClick = this._getLastClick(); return this._isInThresholdArea(point, prevClick === null || prevClick === void 0 ? void 0 : prevClick.point, threshold); }; this._isInClickTimeSpan = (time0, time1) => { return Math.abs(time1 - time0) < this._settings.doubleTapTime; }; this._isInThresholdArea = (point0, point1, threshold) => { if (point1 == null) return false; const dx = Math.abs(point1.x - point0.x); const dy = Math.abs(point1.y - point0.y); return dx < threshold && dy < threshold; }; this._settings = settings; } get lastDownPoint() { return this._lastDownPoint; } onButtonDown(point) { this._lastDownPoint = point; } onButtonUp(point, isTouch, button) { if (this._lastDownPoint == null) return this._reset(); if (this.isInDownUpThresholdArea(point, isTouch) == false) return this._reset(); const clickCount = this._addClick(this._lastDownPoint, Date.now(), isTouch); this._reset(false); return clickCount; } isButtonDown() { return this._lastDownPoint != null; } _getThreshold(isTouch, isClick) { const threshold = isTouch ? (isClick ? this._settings.doubleTapThreshold : this._settings.tapThreshold) : this._settings.clickThreshold; return threshold; } _addClick(point, time, isTouch) { const prevClick = this._getLastClick(); if (this._isInClickThresholdArea(point, isTouch) == false || this._isInClickTimeSpan(prevClick === null || prevClick === void 0 ? void 0 : prevClick.time, time) == false) { this._clicks = []; } const click = { point: point, time: time }; this._clicks.push(click); return this._clicks.length; } _getLastClick() { if (this._clicks.length == 0) return null; return this._clicks[this._clicks.length - 1]; } _reset(resetClicks = true) { this._lastDownPoint = null; if (resetClicks) this._clicks = []; return 0; } } //# sourceMappingURL=PointerInputHelper.js.map