UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 8.88 kB
import{EventHandler as e}from"../../core/event-handler.js";import{EVENT_GAMEPADCONNECTED as t,EVENT_GAMEPADDISCONNECTED as s,PAD_FACE_1 as n,PAD_FACE_2 as i,PAD_FACE_3 as _,PAD_FACE_4 as a,PAD_L_SHOULDER_1 as r,PAD_R_SHOULDER_1 as d,PAD_L_SHOULDER_2 as o,PAD_R_SHOULDER_2 as h,PAD_SELECT as u,PAD_START as p,PAD_L_STICK_BUTTON as A,PAD_R_STICK_BUTTON as D,PAD_UP as c,PAD_DOWN as P,PAD_LEFT as l,PAD_RIGHT as T,PAD_VENDOR as m,XRPAD_TRIGGER as g,XRPAD_SQUEEZE as R,XRPAD_TOUCHPAD_BUTTON as E,XRPAD_STICK_BUTTON as C,XRPAD_A as S,XRPAD_B as x,PAD_L_STICK_X as L,PAD_L_STICK_Y as f,PAD_R_STICK_X as v,PAD_R_STICK_Y as O,XRPAD_TOUCHPAD_X as U,XRPAD_TOUCHPAD_Y as w,XRPAD_STICK_X as I,XRPAD_STICK_Y as b}from"./constants.js";import{math as X}from"../../core/math/math.js";import{platform as H}from"../../core/platform.js";const K=Object.freeze([]);let B=function(){return K};"undefined"!=typeof navigator&&(B=(navigator.getGamepads||navigator.webkitGetGamepads||B).bind(navigator));const F={buttons:{PAD_FACE_1:n,PAD_FACE_2:i,PAD_FACE_3:_,PAD_FACE_4:a,PAD_L_SHOULDER_1:r,PAD_R_SHOULDER_1:d,PAD_L_SHOULDER_2:o,PAD_R_SHOULDER_2:h,PAD_SELECT:u,PAD_START:p,PAD_L_STICK_BUTTON:A,PAD_R_STICK_BUTTON:D,PAD_UP:c,PAD_DOWN:P,PAD_LEFT:l,PAD_RIGHT:T,PAD_VENDOR:m,XRPAD_TRIGGER:g,XRPAD_SQUEEZE:R,XRPAD_TOUCHPAD_BUTTON:E,XRPAD_STICK_BUTTON:C,XRPAD_A:S,XRPAD_B:x},axes:{PAD_L_STICK_X:L,PAD_L_STICK_Y:f,PAD_R_STICK_X:v,PAD_R_STICK_Y:O,XRPAD_TOUCHPAD_X:U,XRPAD_TOUCHPAD_Y:w,XRPAD_STICK_X:I,XRPAD_STICK_Y:b}},N={DEFAULT:{buttons:["PAD_FACE_1","PAD_FACE_2","PAD_FACE_3","PAD_FACE_4","PAD_L_SHOULDER_1","PAD_R_SHOULDER_1","PAD_L_SHOULDER_2","PAD_R_SHOULDER_2","PAD_SELECT","PAD_START","PAD_L_STICK_BUTTON","PAD_R_STICK_BUTTON","PAD_UP","PAD_DOWN","PAD_LEFT","PAD_RIGHT","PAD_VENDOR"],axes:["PAD_L_STICK_X","PAD_L_STICK_Y","PAD_R_STICK_X","PAD_R_STICK_Y"]},DEFAULT_DUAL:{buttons:["PAD_FACE_1","PAD_FACE_2","PAD_FACE_3","PAD_FACE_4","PAD_L_SHOULDER_1","PAD_R_SHOULDER_1","PAD_L_SHOULDER_2","PAD_R_SHOULDER_2","PAD_SELECT","PAD_START","PAD_L_STICK_BUTTON","PAD_R_STICK_BUTTON","PAD_VENDOR"],axes:["PAD_L_STICK_X","PAD_L_STICK_Y","PAD_R_STICK_X","PAD_R_STICK_Y"],synthesizedButtons:{PAD_UP:{axis:0,min:0,max:1},PAD_DOWN:{axis:0,min:-1,max:0},PAD_LEFT:{axis:0,min:-1,max:0},PAD_RIGHT:{axis:0,min:0,max:1}}},PS3:{buttons:["PAD_FACE_1","PAD_FACE_2","PAD_FACE_4","PAD_FACE_3","PAD_L_SHOULDER_1","PAD_R_SHOULDER_1","PAD_L_SHOULDER_2","PAD_R_SHOULDER_2","PAD_SELECT","PAD_START","PAD_L_STICK_BUTTON","PAD_R_STICK_BUTTON","PAD_UP","PAD_DOWN","PAD_LEFT","PAD_RIGHT","PAD_VENDOR"],axes:["PAD_L_STICK_X","PAD_L_STICK_Y","PAD_R_STICK_X","PAD_R_STICK_Y"],mapping:"standard"},DEFAULT_XR:{buttons:["XRPAD_TRIGGER","XRPAD_SQUEEZE","XRPAD_TOUCHPAD_BUTTON","XRPAD_STICK_BUTTON","XRPAD_A","XRPAD_B"],axes:["XRPAD_TOUCHPAD_X","XRPAD_TOUCHPAD_Y","XRPAD_STICK_X","XRPAD_STICK_Y"],mapping:"xr-standard"}},M={"Product: 0268":"PS3"},y={};function G(e){const t=y[e.id];if(t)return t;for(const t in M)if(-1!==e.id.indexOf(t)){const s=M[t];if(!e.mapping){const e=N[`RAW_${s}`];if(e)return e}return N[s]}if("xr-standard"===e.mapping)return N.DEFAULT_XR;const s=N.DEFAULT,n=e.buttons.length<s.buttons.length?N.DEFAULT_DUAL:s;return n.mapping=e.mapping,n}let Y=.25;class j{constructor(e,t){var s,n;(this.value=0,this.pressed=!1,this.touched=!1,this.wasPressed=!1,this.wasReleased=!1,this.wasTouched=!1,"number"==typeof e)?(this.value=e,this.pressed=1===e,this.touched=e>0):(this.value=e.value,this.pressed=e.pressed,this.touched=null!=(s=e.touched)?s:e.value>0);t&&("number"==typeof t?(this.wasPressed=1!==t&&this.pressed,this.wasReleased=1===t&&!this.pressed,this.wasTouched=0===t&&this.touched):(this.wasPressed=!t.pressed&&this.pressed,this.wasReleased=t.pressed&&!this.pressed,this.wasTouched=!(null!=(n=t.touched)?n:t.value>0)&&this.touched))}update(e){var t;const{value:s,pressed:n}=e,i=null!=(t=e.touched)?t:s>0;this.wasPressed=!this.pressed&&n,this.wasReleased=this.pressed&&!n,this.wasTouched=!this.touched&&i,this.value=s,this.pressed=n,this.touched=i}}const V=Object.freeze(new j(0));class W{constructor(e,t){this._compiledMapping={buttons:[],axes:[]},this.id=e.id,this.index=e.index,this._buttons=e.buttons.map((e=>new j(e))),this._axes=[...e.axes],this._previousAxes=[...e.axes],this.mapping=t.mapping,this.map=t,this.hand=e.hand||"none",this.pad=e,this._compileMapping()}get connected(){return this.pad.connected}_compileMapping(){const{axes:e,buttons:t}=this._compiledMapping,s=F.axes,n=F.buttons;e.length=0,t.length=0;this.map.axes&&this.map.axes.forEach(((t,n)=>{e[s[t]]=()=>this.pad.axes[n]||0}));for(let t=0,s=e.length;t<s;t++)e[t]||(e[t]=()=>0);const i=this.map.buttons;i&&i.forEach(((e,s)=>{t[n[e]]=()=>this._buttons[s]||V}));const _=this.map.synthesizedButtons;_&&Object.entries(_).forEach((e=>{const{axis:s,max:i,min:_}=e[1];t[n[e[0]]]=()=>{var e,t;return new j(Math.abs(X.clamp(null!=(e=this._axes[s])?e:0,_,i)),Math.abs(X.clamp(null!=(t=this._previousAxes[s])?t:0,_,i)))}}));for(let e=0,s=t.length;e<s;e++)t[e]||(t[e]=()=>V)}update(e){this.pad=e;const t=this._previousAxes,s=this._axes;t.length=0,t.push(...s),s.length=0,s.push(...e.axes);const n=this._buttons;for(let t=0,s=n.length;t<s;t++)n[t].update(e.buttons[t]);return this}updateMap(e){e.mapping="custom",y[this.id]=e,this.map=e,this.mapping="custom",this._compileMapping()}resetMap(){if(y[this.id]){delete y[this.id];const e=G(this.pad);this.map=e,this.mapping=e.mapping,this._compileMapping()}}get axes(){return this._compiledMapping.axes.map((e=>e()))}get buttons(){return this._compiledMapping.buttons.map((e=>e()))}async pulse(e,t,s){const n=this.pad.vibrationActuator?[this.pad.vibrationActuator]:this.pad.hapticActuators||K;if(n.length){var i,_,a;const r=null!=(i=null==s?void 0:s.startDelay)?i:0,d=null!=(_=null==s?void 0:s.strongMagnitude)?_:e,o=null!=(a=null==s?void 0:s.weakMagnitude)?a:e;return(await Promise.all(n.map((async function(s){return!s||(s.playEffect?s.playEffect(s.type,{duration:t,startDelay:r,strongMagnitude:d,weakMagnitude:o}):!!s.pulse&&(await(n=r,new Promise((e=>{setTimeout(e,n)}))),s.pulse(e,t)));var n})))).some((e=>!0===e||"complete"===e))}return!1}getButton(e){const t=this._compiledMapping.buttons[e];return t?t():V}isPressed(e){return this.getButton(e).pressed}wasPressed(e){return this.getButton(e).wasPressed}wasReleased(e){return this.getButton(e).wasReleased}isTouched(e){return this.getButton(e).touched}wasTouched(e){return this.getButton(e).wasTouched}getValue(e){return this.getButton(e).value}getAxis(e){const t=this.axes[e];return t&&Math.abs(t)>Y?t:0}}class z extends e{constructor(){super(),this.gamepadsSupported=H.gamepads,this.current=[],this._previous=[],this._ongamepadconnectedHandler=this._ongamepadconnected.bind(this),this._ongamepaddisconnectedHandler=this._ongamepaddisconnected.bind(this),window.addEventListener("gamepadconnected",this._ongamepadconnectedHandler,!1),window.addEventListener("gamepaddisconnected",this._ongamepaddisconnectedHandler,!1),this.poll()}set deadZone(e){Y=e}get deadZone(){return Y}get previous(){const e=this.current;for(let t=0,s=e.length;t<s;t++){const s=e[t]._buttons;this._previous[t]||(this._previous[t]=[]);for(let e=0,n=s.length;e<n;e++){const n=s[t];this.previous[t][e]=!!n&&(!n.wasPressed&&n.pressed||n.wasReleased)}}return this._previous.length=this.current.length,this._previous}_ongamepadconnected(e){const s=new W(e.gamepad,this.getMap(e.gamepad)),n=this.current;let i=n.findIndex((e=>e.index===s.index));for(;-1!==i;)n.splice(i,1),i=n.findIndex((e=>e.index===s.index));n.push(s),this.fire(t,s)}_ongamepaddisconnected(e){const t=this.current,n=t.findIndex((t=>t.index===e.gamepad.index));-1!==n&&(this.fire(s,t[n]),t.splice(n,1))}update(){this.poll()}poll(e=[]){e.length>0&&(e.length=0);const t=B();for(let s=0,n=t.length;s<n;s++)if(t[s]){const n=this.findByIndex(t[s].index);if(n)e.push(n.update(t[s]));else{const n=new W(t[s],this.getMap(t[s]));this.current.push(n),e.push(n)}}return e}destroy(){window.removeEventListener("gamepadconnected",this._ongamepadconnectedHandler,!1),window.removeEventListener("gamepaddisconnected",this._ongamepaddisconnectedHandler,!1)}getMap(e){return G(e)}isPressed(e,t){var s;return(null==(s=this.current[e])?void 0:s.isPressed(t))||!1}wasPressed(e,t){var s;return(null==(s=this.current[e])?void 0:s.wasPressed(t))||!1}wasReleased(e,t){var s;return(null==(s=this.current[e])?void 0:s.wasReleased(t))||!1}getAxis(e,t){var s;return(null==(s=this.current[e])?void 0:s.getAxis(t))||0}pulse(e,t,s,n){const i=this.current[e];return i?i.pulse(t,s,n):Promise.resolve(!1)}pulseAll(e,t,s){return Promise.all(this.current.map((n=>n.pulse(e,t,s))))}findById(e){return this.current.find((t=>t&&t.id===e))||null}findByIndex(e){return this.current.find((t=>t&&t.index===e))||null}}z.EVENT_GAMEPADCONNECTED="gamepadconnected",z.EVENT_GAMEPADDISCONNECTED="gamepaddisconnected";export{W as GamePad,j as GamePadButton,z as GamePads};