coherent-gameface-interaction-manager
Version:
Library for the most common UI interactions
7 lines (6 loc) • 2.17 kB
JavaScript
(function(o,i){typeof exports=="object"&&typeof module<"u"?module.exports=i():typeof define=="function"&&define.amd?define(i):(o=typeof globalThis<"u"?globalThis:o||self,o.actions=i())})(this,(function(){"use strict";class o{constructor(){this.actions=[],this.keyboardFunctions=[],this.gamepadFunctions=[]}init(){window._IM||(window._IM=new o)}getKeys(e){return _IM.keyboardFunctions.filter(n=>n.keys.every(t=>e.includes(t)))}getKeysIndex(e){return _IM.keyboardFunctions.findIndex(n=>n.keys.every(t=>e.includes(t)))}getGamepadAction({actions:e,type:n}){return _IM.gamepadFunctions.find(t=>t.actions.every(s=>e.includes(s))&&t.type===n&&t.actions.length===e.length)}getGamepadActions(e,n=!0){return _IM.gamepadFunctions.filter(t=>t.actions.every(s=>e.includes(s))&&(n?t.actions.length===e.length:!0))}getGamepadActionIndex(e){return _IM.gamepadFunctions.findIndex(n=>n.actions.every(t=>e.includes(t)))}getAction(e){return _IM.actions.find(n=>n.name===e)}getActionIndex(e){return _IM.actions.findIndex(n=>n.name===e)}hasDuplicateCallback(e,n){return e.callbacks.some(t=>t===n)}addCallbackToEntry(e,n,t){if(this.hasDuplicateCallback(e,n)){const s=typeof n=="string"?"action":"function",a=typeof n=="string"?n:"(anonymous function)";return console.error(`Duplicate callback detected!
${t.identifier}
Type: '${t.type}'
Callback: ${a}
This ${s} is already registered for this combination and type. To update it, first remove with the .off() method.`)}return e.callbacks.push(n)}removeKeyboardFunction(e){const n=_IM.keyboardFunctions.indexOf(e);n!==-1&&_IM.keyboardFunctions.splice(n,1)}removeGamepadFunction(e){const n=_IM.gamepadFunctions.indexOf(e);n!==-1&&_IM.gamepadFunctions.splice(n,1)}}const i=new o;class r{register(e,n){if(i.getAction(e))return console.error(`The following action "${e}" is already registered!`);_IM.actions.push({name:e,callback:n})}remove(e){const n=i.getActionIndex(e);if(n===-1)return console.error(`${e} is not a registered action!`);_IM.actions.splice(n,1)}execute(e,n){const t=i.getAction(e);if(!t)return console.error(`${e} is not a registered action!`);t.callback(n)}}const c=new r;return i.init(),c}));
//# sourceMappingURL=actions.min.js.map