@forward-software/reveal.js-joycontroller
Version:
Custom plugin for Reveal.js to control presentations using Nintendo Switch Joy-Cons
2 lines (1 loc) • 2.86 kB
JavaScript
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(e="undefined"!=typeof globalThis?globalThis:e||self).RevealJoyController=n()}(this,(function(){"use strict";const e={type:"right",cooldown:300,pointerSpeed:20},n={left:{RIGHT:3,LEFT:0,UP:2,DOWN:1,PREV:4,NEXT:5,QUIT_OVERVIEW_OR_NEXT:6,TOGGLE_OVERVIEW:8,TOGGLE_POINTING:10,TOGGLE_PAUSE:9,TOGGLE_HELP:16},right:{RIGHT:0,LEFT:3,UP:1,DOWN:2,PREV:4,NEXT:5,QUIT_OVERVIEW_OR_NEXT:7,TOGGLE_OVERVIEW:8,TOGGLE_POINTING:10,TOGGLE_PAUSE:9,TOGGLE_HELP:16}};return()=>({id:"joycontroller",init:function(o){const t=o.getConfig(),a=Object.assign({},e,t.joycon);console.info("🎮 JoyController plugin by ForWarD Software loaded",a);const i="GamepadEvent"in window,d="WebKitGamepadEvent"in window,s=window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||window.requestAnimationFrame,c={},r={};function g(e,t){if(i=`${e.id}-btn-${t}`,r[i]&&!(Date.now()-r[i]>=a.cooldown)||(r[i]=Date.now(),0))return;var i;console.debug("🎮 %s => Button %d pressed",e.id,t);const d=n[a.type];switch(t){case d.RIGHT:o.right();break;case d.DOWN:o.down();break;case d.UP:o.up();break;case d.LEFT:o.left();break;case d.PREV:o.prev();break;case d.NEXT:o.next();break;case d.TOGGLE_OVERVIEW:o.toggleOverview();break;case d.QUIT_OVERVIEW_OR_NEXT:o.isOverview()?o.toggleOverview():o.next();break;case d.TOGGLE_PAUSE:o.togglePause();break;case d.TOGGLE_POINTING:pointing=!pointing,pointer.style.display=pointing?"block":"none";break;case d.TOGGLE_HELP:o.toggleHelp();break;default:console.warn("🎮 %s => Button %d not mapped",e.id,t)}}function l(){return navigator.getGamepads?navigator.getGamepads():navigator.webkitGetGamepads?navigator.webkitGetGamepads():[]}function E(){const e=l();if(e){for(const n in e){const o=e[n];if(o)for(let e=0;e<o.buttons.length;++e){const n=o.buttons[e];let t=1===n,a=!1;"object"==typeof n&&(t=n.pressed,"touched"in n&&(a=n.touched)),(t||a)&&g(o,e)}}s(E)}}function p(e){c[e.index]=e,s(E)}function G(e){console.info("🎮 %s connected ⚡",e.gamepad.id,e.gamepad),p(e.gamepad)}function u(e){var n;n=e.gamepad,delete c[n.index],console.info(`🎮 ${e.gamepad.id} disconnected 🔌`,e.gamepad)}i?(window.addEventListener("gamepadconnected",G),window.addEventListener("gamepaddisconnected",u),console.debug("🎮 Standard Mode: Listening for standard Gamepad events")):d?(window.addEventListener("webkitgamepadconnected",G),window.addEventListener("webkitgamepaddisconnected",u),console.debug("🎮 WebKit Mode: Listening for WebKit Gamepad events")):(setInterval((function(){console.debug("🎮 scanning Gamepads list");const e=l();for(const n in e){const o=e[n];o&&(o.index in c?c[o.index]=o:p(o))}console.debug(`🎮 found ${c.length} Gamepads`,c)}),500),console.debug("🎮 Legacy Mode: Polling Gamepad status"))}})}));