coherent-gameface-interaction-manager
Version:
Library for the most common UI interactions
3 lines (2 loc) • 7.13 kB
JavaScript
var gamepad=(()=>{var a=(s,t)=>()=>(s&&(t=s(s=0)),t);var O=(s,t)=>()=>(t||s((t={exports:{}}).exports,t),t.exports);var _,h=a(()=>{_={FACE_BUTTON_DOWN:0,FACE_BUTTON_RIGHT:1,FACE_BUTTON_LEFT:2,FACE_BUTTON_TOP:3,LEFT_SHOULDER:4,RIGHT_SHOULDER:5,LEFT_SHOULDER_BOTTOM:6,RIGHT_SHOULDER_BOTTOM:7,SELECT:8,START:9,LEFT_ANALOGUE_STICK:10,RIGHT_ANALOGUE_STICK:11,PAD_UP:12,PAD_DOWN:13,PAD_LEFT:14,PAD_RIGHT:15,CENTER_BUTTON:16,aliases:{"face-button-down":"FACE_BUTTON_DOWN","face-button-left":"FACE_BUTTON_LEFT","face-button-right":"FACE_BUTTON_RIGHT","face-button-top":"FACE_BUTTON_TOP","left-sholder":"LEFT_SHOULDER","right-sholder":"RIGHT_SHOULDER","left-sholder-bottom":"LEFT_SHOULDER_BOTTOM","right-sholder-bottom":"RIGHT_SHOULDER_BOTTOM",select:"SELECT",start:"START","left-analogue-stick":"LEFT_ANALOGUE_STICK","right-analogue-stick":"RIGHT_ANALOGUE_STICK","pad-up":"PAD_UP","pad-down":"PAD_DOWN","pad-left":"PAD_LEFT","pad-right":"PAD_RIGHT","center-button":"CENTER_BUTTON","playstation.x":"FACE_BUTTON_DOWN","playstation.square":"FACE_BUTTON_LEFT","playstation.circle":"FACE_BUTTON_RIGHT","playstation.triangle":"FACE_BUTTON_TOP","playstation.l1":"LEFT_SHOULDER","playstation.r1":"RIGHT_SHOULDER","playstation.l2":"LEFT_SHOULDER_BOTTOM","playstation.r2":"RIGHT_SHOULDER_BOTTOM","playstation.share":"SELECT","playstation.options":"START","playstation.l3":"LEFT_ANALOGUE_STICK","playstation.r3":"RIGHT_ANALOGUE_STICK","playstation.d-pad-up":"PAD_UP","playstation.d-pad-down":"PAD_DOWN","playstation.d-pad-left":"PAD_LEFT","playstation.d-pad-right":"PAD_RIGHT","playstation.center":"CENTER_BUTTON","xbox.a":"FACE_BUTTON_DOWN","xbox.x":"FACE_BUTTON_LEFT","xbox.b":"FACE_BUTTON_RIGHT","xbox.y":"FACE_BUTTON_TOP","xbox.lb":"LEFT_SHOULDER","xbox.rb":"RIGHT_SHOULDER","xbox.lt":"LEFT_SHOULDER_BOTTOM","xbox.rt":"RIGHT_SHOULDER_BOTTOM","xbox.view":"SELECT","xbox.menu":"START","xbox.left-thumbstick":"LEFT_ANALOGUE_STICK","xbox.right-thumbstick":"RIGHT_ANALOGUE_STICK","xbox.d-pad-up":"PAD_UP","xbox.d-pad-down":"PAD_DOWN","xbox.d-pad-left":"PAD_LEFT","xbox.d-pad-right":"PAD_RIGHT","xbox.center":"CENTER_BUTTON"},axisAliases:["right.joystick","left.joystick","left.joystick.down","left.joystick.up","left.joystick.left","left.joystick.right","right.joystick.down","right.joystick.up","right.joystick.left","right.joystick.right"]}});var l,n,c=a(()=>{l=class s{constructor(){this.actions=[],this.keyboardFunctions=[],this.gamepadFunctions=[]}init(){window._IM||(window._IM=new s)}getKeys(t){return _IM.keyboardFunctions.filter(e=>e.keys.every(i=>t.includes(i)))}getKeysIndex(t){return _IM.keyboardFunctions.findIndex(e=>e.keys.every(i=>t.includes(i)))}getGamepadAction({actions:t,type:e}){return _IM.gamepadFunctions.find(i=>i.actions.every(o=>t.includes(o))&&i.type===e&&i.actions.length===t.length)}getGamepadActions(t){return _IM.gamepadFunctions.filter(e=>e.actions.every(i=>t.includes(i))&&e.actions.length===t.length)}getGamepadActionIndex(t){return _IM.gamepadFunctions.findIndex(e=>e.actions.every(i=>t.includes(i)))}getAction(t){return _IM.actions.find(e=>e.name===t)}getActionIndex(t){return _IM.actions.findIndex(e=>e.name===t)}},n=new l});var d,p,f=a(()=>{c();d=class{register(t,e){if(n.getAction(t))return console.error(`The following action "${t}" is already registered!`);_IM.actions.push({name:t,callback:e})}remove(t){let e=n.getActionIndex(t);if(e===-1)return console.error(`${t} is not a registered action!`);_IM.actions.splice(e,1)}execute(t,e){let i=n.getAction(t);if(!i)return console.error(`${t} is not a registered action!`);i.callback(e)}},p=new d});var r,y,T,E,g=a(()=>{h();c();f();r=.9,y=["press","hold"],T=class{constructor(){this.mappings=_,this.pollingStarted=!1,this.gamepadEnabled=!1,this.onGamepadConnected=this.onGamepadConnected.bind(this),this.sanitizeAction=this.sanitizeAction.bind(this),this.pollingInterval=200,this._pressedAction=null}set enabled(t){this.gamepadEnabled=t,this.gamepadEnabled?this.init():this.deinit()}init(){window.addEventListener("gamepadconnected",this.onGamepadConnected)}deinit(){window.removeEventListener("gamepadconnected",this.onGamepadConnected)}onGamepadConnected(){this.pollingStarted||(this.pollingStarted=!0,this.startPolling())}on(t){t.actions=t.actions.map(this.sanitizeAction);let e=this.mappings.axisAliases.some(i=>t.actions.includes(i));if((!t.type||!y.includes(t.type))&&(t.type="hold"),t.type==="press"&&e)return console.error("You can't use an axis action with a 'press' type!");if(t.actions.length>1&&e)return console.error("You can't use an axis action in a combination with a button action");if(n.getGamepadAction(t))return console.error("You have already registered a callback for this action. If you want to overwrite it, remove it first with .off([actions])");_IM.gamepadFunctions.push(t)}off(t){let e=n.getGamepadActionIndex(t.map(this.sanitizeAction));if(e===-1)return console.error("You are trying to remove a non-existent action!");_IM.gamepadFunctions.splice(e,1)}startPolling(){let t=navigator.getGamepads();if(t.length===0){this.pollingStarted=!1;return}t.forEach((e,i)=>{e&&(this.handleButtons(e.buttons,i),this.handleJoysticks(e.axes))}),setTimeout(this.startPolling.bind(this),this.pollingInterval)}handleButtons(t){let e=t.reduce((o,u,A)=>(u.pressed&&(o.buttonIndexes.push(A),o.buttons.push(u)),o),{buttonIndexes:[],buttons:[]}),i=n.getGamepadActions(e.buttonIndexes);!i.length!==0&&(this._pressedAction&&(i.includes(this._pressedAction)||this.executeCallback(this._pressedAction,this._pressedAction.actions),this._pressedAction=null),i.forEach(o=>{if(o.type==="press"){this._pressedAction=o;return}this.executeCallback(o,e.buttons)}))}handleJoysticks(t){this.getJoystickActions().forEach(i=>{switch(i.actions[0]){case"left.joystick":return this.executeCallback(i,[t[0],t[1]]);case"right.joystick":return this.executeCallback(i,[t[2],t[3]]);case"left.joystick.down":if(t[1]>r)return this.executeCallback(i,[t[0],t[1]]);break;case"left.joystick.up":if(t[1]<-r)return this.executeCallback(i,[t[0],t[1]]);break;case"left.joystick.left":if(t[0]<-r)return this.executeCallback(i,[t[0],t[1]]);break;case"left.joystick.right":if(t[0]>r)return this.executeCallback(i,[t[0],t[1]]);break;case"right.joystick.down":if(t[3]>r)return this.executeCallback(i,[t[2],t[3]]);break;case"right.joystick.up":if(t[3]<-r)return this.executeCallback(i,[t[2],t[3]]);break;case"right.joystick.left":if(t[2]<-r)return this.executeCallback(i,[t[2],t[3]]);break;case"right.joystick.right":if(t[2]>r)return this.executeCallback(i,[t[2],t[3]]);break}})}sanitizeAction(t){if(typeof t=="number")return t;if(this.mappings.axisAliases.includes(t.toLowerCase()))return t.toLowerCase();if(typeof t=="string"){let e=this.mappings.aliases[t.toLowerCase()];return e?this.mappings[e]:console.error(`You have entered a non-supported button alias ${t}`)}return t}getJoystickActions(){return _IM.gamepadFunctions.filter(t=>this.mappings.axisAliases.includes(t.actions[0]))}executeCallback(t,e){if(typeof t.callback=="string")return p.execute(t.callback,e);t.callback(e)}},E=new T});var k=O((S,b)=>{g();c();n.init();b.exports=E});return k();})();
//# sourceMappingURL=gamepad.min.js.map