| | const MathUtil = require('../util/math-util'); |
| | const { translateScreenPos } = require('../util/pos-math'); |
| |
|
| | const roundToThreeDecimals = number => Math.round(number * 1000) / 1000; |
| |
|
| | class Mouse { |
| | constructor (runtime) { |
| | this._clientX = 0; |
| | this._clientY = 0; |
| | this._scratchX = 0; |
| | this._scratchY = 0; |
| |
|
| | this._buttons = new Set(); |
| | this._isDown = false; |
| | |
| | this.usesRightClickDown = false; |
| |
|
| | |
| | this._isClicked = false; |
| | this._clickOnStep = -1; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | this.runtime = runtime; |
| | this.cameraBound = null; |
| |
|
| | |
| | this.runtime.on("RUNTIME_STEP_END", () => { |
| | if (this.runtime.frameLoop._stepCounter > this._clickOnStep) { |
| | this._isClicked = false; |
| | } |
| | }); |
| | } |
| |
|
| | bindToCamera(screen) { |
| | this.cameraBound = screen; |
| | } |
| |
|
| | removeCameraBinding() { |
| | this.cameraBound = null; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | _activateClickHats (target) { |
| | |
| | |
| | |
| | |
| | this.runtime.startHats('event_whenthisspriteclicked', null, target); |
| | this.runtime.startHats('event_whenstageclicked', null, target); |
| | if (target.isStage) { |
| | this.runtime.startHats('pmEventsExpansion_whenSpriteClicked', { SPRITE: '_stage_' }); |
| | return; |
| | } |
| | if (target.sprite) { |
| | this.runtime.startHats('pmEventsExpansion_whenSpriteClicked', { SPRITE: target.sprite.name }); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | _pickTarget (x, y) { |
| | if (this.runtime.renderer) { |
| | const drawableID = this.runtime.renderer.pick(x, y); |
| | for (let i = 0; i < this.runtime.targets.length; i++) { |
| | const target = this.runtime.targets[i]; |
| | if (target.hasOwnProperty('drawableID') && |
| | target.drawableID === drawableID) { |
| | return target; |
| | } |
| | } |
| | } |
| | |
| | return this.runtime.getTargetForStage(); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | postData (data) { |
| | if (typeof data.x === 'number') { |
| | this._clientX = data.x; |
| | this._scratchX = MathUtil.clamp( |
| | this.runtime.stageWidth * ((data.x / data.canvasWidth) - 0.5), |
| | -(this.runtime.stageWidth / 2), |
| | (this.runtime.stageWidth / 2) |
| | ); |
| | } |
| | if (typeof data.y === 'number') { |
| | this._clientY = data.y; |
| | this._scratchY = MathUtil.clamp( |
| | -this.runtime.stageHeight * ((data.y / data.canvasHeight) - 0.5), |
| | -(this.runtime.stageHeight / 2), |
| | (this.runtime.stageHeight / 2) |
| | ); |
| | } |
| | if (typeof data.isDown !== 'undefined') { |
| | |
| | const button = typeof data.button === 'undefined' ? 0 : data.button; |
| | if (data.isDown) { |
| | this._buttons.add(button); |
| | } else { |
| | this._buttons.delete(button); |
| | } |
| |
|
| | const previousDownState = this._isDown; |
| | this._isDown = data.isDown; |
| | if (data.isDown) { |
| | this._isClicked = true; |
| | this._clickOnStep = this.runtime.frameLoop._stepCounter; |
| | } |
| |
|
| | |
| | if (previousDownState === this._isDown) return; |
| |
|
| | |
| | if (data.wasDragged) return; |
| |
|
| | |
| | if (!(data.x > 0 && data.x < data.canvasWidth && |
| | data.y > 0 && data.y < data.canvasHeight)) return; |
| |
|
| | const target = this._pickTarget(data.x, data.y); |
| | const isNewMouseDown = !previousDownState && this._isDown; |
| | const isNewMouseUp = previousDownState && !this._isDown; |
| |
|
| | |
| | |
| | if (target.draggable && isNewMouseUp) { |
| | this._activateClickHats(target); |
| | } else if (!target.draggable && isNewMouseDown) { |
| | this._activateClickHats(target); |
| | } |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | getClientX () { |
| | return this._clientX; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | getClientY () { |
| | return this._clientY; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | getScratchX () { |
| | const mouseX = this.cameraBound |
| | ? translateScreenPos(this.runtime, this.cameraBound, this._scratchX, this._scratchY)[0] |
| | |
| | : this._scratchX; |
| | if (this.runtime.runtimeOptions.miscLimits) { |
| | return Math.round(mouseX); |
| | } |
| | return roundToThreeDecimals(mouseX); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | getScratchY () { |
| | const mouseY = this.cameraBound |
| | ? translateScreenPos(this.runtime, this.cameraBound, this._scratchX, this._scratchY)[1] |
| | |
| | : this._scratchY; |
| | if (this.runtime.runtimeOptions.miscLimits) { |
| | return Math.round(mouseY); |
| | } |
| | return roundToThreeDecimals(mouseY); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | getIsDown () { |
| | return this._isDown; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | getIsClicked () { |
| | return this._isClicked; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | getButtonIsDown (button) { |
| | if (button === 2) { |
| | this.usesRightClickDown = true; |
| | } |
| | return this._buttons.has(button); |
| | } |
| | } |
| |
|
| | module.exports = Mouse; |
| |
|