UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 3.15 kB
import{platform as t}from"../../core/platform.js";import{EventHandler as e}from"../../core/event-handler.js";import{EVENT_MOUSEUP as n,EVENT_MOUSEDOWN as s,EVENT_MOUSEMOVE as i,EVENT_MOUSEWHEEL as o}from"./constants.js";import{isMousePointerLocked as r,MouseEvent as h}from"./mouse-event.js";class a extends e{constructor(t){super(),this._lastX=0,this._lastY=0,this._buttons=[!1,!1,!1],this._lastbuttons=[!1,!1,!1],this._target=null,this._attached=!1,this._upHandler=this._handleUp.bind(this),this._downHandler=this._handleDown.bind(this),this._moveHandler=this._handleMove.bind(this),this._wheelHandler=this._handleWheel.bind(this),this._contextMenuHandler=t=>{t.preventDefault()},this.attach(t)}static isPointerLocked(){return r()}attach(e){if(this._target=e,this._attached)return;this._attached=!0;const n=!!t.passiveEvents&&{passive:!1};window.addEventListener("mouseup",this._upHandler,n),window.addEventListener("mousedown",this._downHandler,n),window.addEventListener("mousemove",this._moveHandler,n),window.addEventListener("wheel",this._wheelHandler,n)}detach(){if(!this._attached)return;this._attached=!1,this._target=null;const e=!!t.passiveEvents&&{passive:!1};window.removeEventListener("mouseup",this._upHandler,e),window.removeEventListener("mousedown",this._downHandler,e),window.removeEventListener("mousemove",this._moveHandler,e),window.removeEventListener("wheel",this._wheelHandler,e)}disableContextMenu(){this._target&&this._target.addEventListener("contextmenu",this._contextMenuHandler)}enableContextMenu(){this._target&&this._target.removeEventListener("contextmenu",this._contextMenuHandler)}enablePointerLock(t,e){if(!document.body.requestPointerLock)return void(e&&e());const n=()=>{t(),document.removeEventListener("pointerlockchange",n)},s=()=>{e(),document.removeEventListener("pointerlockerror",s)};t&&document.addEventListener("pointerlockchange",n,!1),e&&document.addEventListener("pointerlockerror",s,!1),document.body.requestPointerLock()}disablePointerLock(t){if(!document.exitPointerLock)return;const e=()=>{t(),document.removeEventListener("pointerlockchange",e)};t&&document.addEventListener("pointerlockchange",e,!1),document.exitPointerLock()}update(){this._lastbuttons[0]=this._buttons[0],this._lastbuttons[1]=this._buttons[1],this._lastbuttons[2]=this._buttons[2]}isPressed(t){return this._buttons[t]}wasPressed(t){return this._buttons[t]&&!this._lastbuttons[t]}wasReleased(t){return!this._buttons[t]&&this._lastbuttons[t]}_handleUp(t){this._buttons[t.button]=!1;const e=new h(this,t);e.event&&this.fire(n,e)}_handleDown(t){this._buttons[t.button]=!0;const e=new h(this,t);e.event&&this.fire(s,e)}_handleMove(t){const e=new h(this,t);e.event&&(this.fire(i,e),this._lastX=e.x,this._lastY=e.y)}_handleWheel(t){const e=new h(this,t);e.event&&this.fire(o,e)}_getTargetCoords(t){const e=this._target.getBoundingClientRect(),n=Math.floor(e.left),s=Math.floor(e.top);return t.clientX<n||t.clientX>=n+this._target.clientWidth||t.clientY<s||t.clientY>=s+this._target.clientHeight?null:{x:t.clientX-n,y:t.clientY-s}}}a.EVENT_MOUSEMOVE=i,a.EVENT_MOUSEDOWN=s,a.EVENT_MOUSEUP=n,a.EVENT_MOUSEWHEEL=o;export{a as Mouse};