UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 3.16 kB
import{EventHandler as e}from"../../core/event-handler.js";import{platform as t}from"../../core/platform.js";import{XrInputSource as s}from"./xr-input-source.js";class r extends e{constructor(e){var s;super(),this.manager=void 0,this._inputSources=[],this._onInputSourcesChangeEvt=void 0,this.velocitiesSupported=!1,this.manager=e,this.velocitiesSupported=!(!t.browser||null==(s=window.XRPose)||null==(s=s.prototype)||!s.hasOwnProperty("linearVelocity")),this._onInputSourcesChangeEvt=e=>{this._onInputSourcesChange(e)},this.manager.on("start",this._onSessionStart,this),this.manager.on("end",this._onSessionEnd,this)}_onSessionStart(){const e=this.manager.session;e.addEventListener("inputsourceschange",this._onInputSourcesChangeEvt),e.addEventListener("select",(e=>{const t=this._getByInputSource(e.inputSource);t.update(e.frame),t.fire("select",e),this.fire("select",t,e)})),e.addEventListener("selectstart",(e=>{const t=this._getByInputSource(e.inputSource);t.update(e.frame),t._selecting=!0,t.fire("selectstart",e),this.fire("selectstart",t,e)})),e.addEventListener("selectend",(e=>{const t=this._getByInputSource(e.inputSource);t.update(e.frame),t._selecting=!1,t.fire("selectend",e),this.fire("selectend",t,e)})),e.addEventListener("squeeze",(e=>{const t=this._getByInputSource(e.inputSource);t.update(e.frame),t.fire("squeeze",e),this.fire("squeeze",t,e)})),e.addEventListener("squeezestart",(e=>{const t=this._getByInputSource(e.inputSource);t.update(e.frame),t._squeezing=!0,t.fire("squeezestart",e),this.fire("squeezestart",t,e)})),e.addEventListener("squeezeend",(e=>{const t=this._getByInputSource(e.inputSource);t.update(e.frame),t._squeezing=!1,t.fire("squeezeend",e),this.fire("squeezeend",t,e)}));const t=e.inputSources;for(let e=0;e<t.length;e++)this._addInputSource(t[e])}_onSessionEnd(){let e=this._inputSources.length;for(;e--;){const t=this._inputSources[e];this._inputSources.splice(e,1),t.fire("remove"),this.fire("remove",t)}this.manager.session.removeEventListener("inputsourceschange",this._onInputSourcesChangeEvt)}_onInputSourcesChange(e){for(let t=0;t<e.removed.length;t++)this._removeInputSource(e.removed[t]);for(let t=0;t<e.added.length;t++)this._addInputSource(e.added[t])}_getByInputSource(e){for(let t=0;t<this._inputSources.length;t++)if(this._inputSources[t].inputSource===e)return this._inputSources[t];return null}_addInputSource(e){if(this._getByInputSource(e))return;const t=new s(this.manager,e);this._inputSources.push(t),this.fire("add",t)}_removeInputSource(e){for(let t=0;t<this._inputSources.length;t++){if(this._inputSources[t].inputSource!==e)continue;const s=this._inputSources[t];this._inputSources.splice(t,1);let r=s.hitTestSources.length;for(;r--;)s.hitTestSources[r].remove();return s.fire("remove"),void this.fire("remove",s)}}update(e){for(let t=0;t<this._inputSources.length;t++)this._inputSources[t].update(e)}get inputSources(){return this._inputSources}}r.EVENT_ADD="add",r.EVENT_REMOVE="remove",r.EVENT_SELECT="select",r.EVENT_SELECTSTART="selectstart",r.EVENT_SELECTEND="selectend",r.EVENT_SQUEEZE="squeeze",r.EVENT_SQUEEZESTART="squeezestart",r.EVENT_SQUEEZEEND="squeezeend";export{r as XrInput};