| | const MathUtil = require('../util/math-util'); |
| |
|
| | const roundToThreeDecimals = number => Math.round(number * 1000) / 1000; |
| |
|
| | class Touch { |
| | constructor (runtime) { |
| | this.fingers = Array.from(Array(5).keys()).map(() => { |
| | return { |
| | _clientX: 0, |
| | _clientY: 0, |
| | _scratchX: 0, |
| | _scratchY: 0, |
| | _isDown: false, |
| |
|
| | |
| | _isTapped: false, |
| | _tapOnStep: -1 |
| | } |
| | }); |
| |
|
| | |
| | |
| | |
| | |
| | |
| | this.runtime = runtime; |
| | |
| | |
| | this.runtime.on("RUNTIME_STEP_END", () => { |
| | for (const finger of this.fingers) { |
| | if (this.runtime.frameLoop._stepCounter > finger._tapOnStep) { |
| | finger._isTapped = false; |
| | } |
| | } |
| | }); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | postData (data) { |
| | data.changedTouches.forEach(touch => { |
| | const finger = this.fingers[touch.identifier]; |
| | if (!finger) return; |
| | if (typeof touch.x === 'number') { |
| | finger._clientX = touch.x; |
| | finger._scratchX = MathUtil.clamp( |
| | this.runtime.stageWidth * ((touch.x / data.canvasWidth) - 0.5), |
| | -(this.runtime.stageWidth / 2), |
| | (this.runtime.stageWidth / 2) |
| | ); |
| | } |
| | if (typeof touch.y === 'number') { |
| | finger._clientY = touch.y; |
| | finger._scratchY = MathUtil.clamp( |
| | -this.runtime.stageHeight * ((touch.y / data.canvasHeight) - 0.5), |
| | -(this.runtime.stageHeight / 2), |
| | (this.runtime.stageHeight / 2) |
| | ); |
| | } |
| | if (typeof data.isDown !== "undefined") { |
| | finger._isDown = data.isDown; |
| | } |
| | if (data.isDown === true) { |
| | finger._isTapped = true; |
| | finger._tapOnStep = this.runtime.frameLoop._stepCounter; |
| | } |
| | }) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | getClientX (finger) { |
| | const f = this.fingers[finger]; |
| | if (!f) return 0; |
| | return f._clientX; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | getClientY (finger) { |
| | const f = this.fingers[finger]; |
| | if (!f) return 0; |
| | return f._clientY; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | getScratchX (finger) { |
| | const f = this.fingers[finger]; |
| | if (!f) return 0; |
| | if (this.runtime.runtimeOptions.miscLimits) { |
| | return Math.round(f._scratchX); |
| | } |
| | return roundToThreeDecimals(f._scratchX); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | getScratchY (finger) { |
| | const f = this.fingers[finger]; |
| | if (!f) return 0; |
| | if (this.runtime.runtimeOptions.miscLimits) { |
| | return Math.round(f._scratchY); |
| | } |
| | return roundToThreeDecimals(f._scratchY); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | getIsDown (finger) { |
| | const f = this.fingers[finger]; |
| | if (!f) return false; |
| | return f._isDown; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | getIsTapped (finger) { |
| | const f = this.fingers[finger]; |
| | if (!f) return false; |
| | return f._isTapped; |
| | } |
| | } |
| |
|
| | module.exports = Touch; |
| |
|