UNPKG

coherent-gameface-interaction-manager

Version:
3 lines (2 loc) 4.57 kB
var keyboard=(()=>{var s=(o,e)=>()=>(o&&(e=o(o=0)),e);var m=(o,e)=>()=>(e||o((e={exports:{}}).exports,e),e.exports);var c,i,a=s(()=>{c=class o{constructor(){this.actions=[],this.keyboardFunctions=[],this.gamepadFunctions=[]}init(){window._IM||(window._IM=new o)}getKeys(e){return _IM.keyboardFunctions.filter(t=>t.keys.every(n=>e.includes(n)))}getKeysIndex(e){return _IM.keyboardFunctions.findIndex(t=>t.keys.every(n=>e.includes(n)))}getGamepadAction({actions:e,type:t}){return _IM.gamepadFunctions.find(n=>n.actions.every(r=>e.includes(r))&&n.type===t&&n.actions.length===e.length)}getGamepadActions(e){return _IM.gamepadFunctions.filter(t=>t.actions.every(n=>e.includes(n))&&t.actions.length===e.length)}getGamepadActionIndex(e){return _IM.gamepadFunctions.findIndex(t=>t.actions.every(n=>e.includes(n)))}getAction(e){return _IM.actions.find(t=>t.name===e)}getActionIndex(e){return _IM.actions.findIndex(t=>t.name===e)}},i=new c});var d,u=s(()=>{d={ALT:18,ARROW_DOWN:40,ARROW_LEFT:37,ARROW_RIGHT:39,ARROW_UP:38,BACKSPACE:8,CAPS_LOCK:20,CTRL:17,DELETE:46,END:35,ENTER:13,ESC:27,F1:112,F2:113,F3:114,F4:115,F5:116,F6:117,F7:118,F8:119,F9:120,F10:121,F11:122,F12:123,HOME:36,INSERT:45,NUM_LOCK:144,NUMPAD_ENTER:13,NUMPAD_DASH:109,NUMPAD_STAR:106,NUMPAD_DOT:110,NUMPAD_FORWARD_SLASH:111,NUMPAD_PLUS:107,NUMPAD_0:96,NUMPAD_1:97,NUMPAD_2:98,NUMPAD_3:99,NUMPAD_4:100,NUMPAD_5:101,NUMPAD_6:102,NUMPAD_7:103,NUMPAD_8:104,NUMPAD_9:105,PAGE_DOWN:34,PAGE_UP:33,PAUSE:19,PRINT_SCRN:44,SCROLL_LOCK:145,SHIFT:16,SPACEBAR:32,TAB:9,A:65,B:66,C:67,D:68,E:69,F:70,G:71,H:72,I:73,J:74,K:75,L:76,M:77,N:78,O:79,P:80,Q:81,R:82,S:83,T:84,U:85,V:86,W:87,X:88,Y:89,Z:90,1:49,2:50,3:51,4:52,5:53,6:54,7:55,8:56,9:57,0:48,QUOTE:222,DASH:189,COMMA:188,DOT:190,FORWARD_SLASH:191,SEMI_COLON:186,SQUARE_BRACKET_LEFT:219,SQUARE_BRACKET_RIGHT:221,BACKWARD_SLASH:220,BACKTICK:192,EQUAL:187,SYSTEM:91}});var l,A,h=s(()=>{a();l=class{register(e,t){if(i.getAction(e))return console.error(`The following action "${e}" is already registered!`);_IM.actions.push({name:e,callback:t})}remove(e){let t=i.getActionIndex(e);if(t===-1)return console.error(`${e} is not a registered action!`);_IM.actions.splice(t,1)}execute(e,t){let n=i.getAction(e);if(!n)return console.error(`${e} is not a registered action!`);n.callback(t)}},A=new l});var y,f,g=s(()=>{a();u();h();y=class{constructor(){this.mappings=d,this.eventListenerAttached=!1,this.keysPressed=new Set,this.onKeyDown=this.onKeyDown.bind(this),this.onKeyUp=this.onKeyUp.bind(this),window.KEYS||(window.KEYS=d)}on(e){e.keys=[...new Set(e.keys.map(n=>(n=typeof n=="number"?this.keyCodeToString(n):n,n.toUpperCase())))];let t=e.keys.filter(n=>!this.mappings[n]);if(t.length>0)return console.error(`The following keys [${t.join(", ")}] you have entered are incorrect! `);this.eventListenerAttached||(document.addEventListener("keydown",this.onKeyDown),document.addEventListener("keyup",this.onKeyUp),this.eventListenerAttached=!0),Array.isArray(e.type)||(e.type=[e.type]),e.type.forEach(n=>{let r=i.getKeys(e.keys);if(r.length>0&&r.some(_=>_.type===n))return console.error("You are trying to overwrite an existing key combination! To do that, first remove it with .off([keys]) then add it again");if(n==="lift"&&e.keys.length>1)return console.error("You can only have a single key trigger an action on lift");_IM.keyboardFunctions.push({...e,type:n})})}off(e){e=[...new Set(e.map(n=>(n=typeof n=="number"?this.keyCodeToString(n):n,n.toUpperCase())))];let t=i.getKeys(e).length;if(t===0)return console.error("You are trying to remove a non-existent key combination!");for(;t>0;){let n=i.getKeysIndex(e);_IM.keyboardFunctions.splice(n,1),t--}_IM.keyboardFunctions.length===0&&(document.removeEventListener("keydown",this.onKeyDown),document.removeEventListener("keyup",this.onKeyUp),this.eventListenerAttached=!1)}onKeyDown(e){let t=this.keyCodeToString(e.keyCode);this.keysPressed.add(t);let n=i.getKeys([...this.keysPressed]);n.length!==0&&n.forEach(r=>{r.type==="press"&&e.repeat||r.type!=="press"&&r.type!=="hold"||r.type==="hold"&&!e.repeat||this.executeCallback(e,r)})}onKeyUp(e){let t=this.keyCodeToString(e.keyCode);this.keysPressed.delete(t);let n=i.getKeys(t);n.length!==0&&n.forEach(r=>{r.type==="lift"&&r.keys.indexOf(t)!==-1&&this.executeCallback(e,r)})}keyCodeToString(e){return Object.keys(this.mappings).find(t=>this.mappings[t]===e)}executeCallback(e,t){if(typeof t.callback=="string")return A.execute(t.callback,e);t.callback(e)}},f=new y});var M=m((w,p)=>{g();a();i.init();p.exports=f});return M();})(); //# sourceMappingURL=keyboard.min.js.map