UNPKG

@forward-software/reveal.js-joycontroller

Version:

Custom plugin for Reveal.js to control presentations using Nintendo Switch Joy-Cons

2 lines (1 loc) 2.23 kB
!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(t){const o=t.getConfig(),i=Object.assign({},e,o.joycon),a="GamepadEvent"in window,d="WebKitGamepadEvent"in window,s=window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||window.requestAnimationFrame,c={},r={};function E(e,o){if(a=`${e.id}-btn-${o}`,r[a]&&!(Date.now()-r[a]>=i.cooldown)||(r[a]=Date.now(),0))return;var a;const d=n[i.type];switch(o){case d.RIGHT:t.right();break;case d.DOWN:t.down();break;case d.UP:t.up();break;case d.LEFT:t.left();break;case d.PREV:t.prev();break;case d.NEXT:t.next();break;case d.TOGGLE_OVERVIEW:t.toggleOverview();break;case d.QUIT_OVERVIEW_OR_NEXT:t.isOverview()?t.toggleOverview():t.next();break;case d.TOGGLE_PAUSE:t.togglePause();break;case d.TOGGLE_POINTING:pointing=!pointing,pointer.style.display=pointing?"block":"none";break;case d.TOGGLE_HELP:t.toggleHelp()}}function G(){return navigator.getGamepads?navigator.getGamepads():navigator.webkitGetGamepads?navigator.webkitGetGamepads():[]}function g(){const e=G();if(e){for(const n in e){const t=e[n];if(t)for(let e=0;e<t.buttons.length;++e){const n=t.buttons[e];let o=1===n,i=!1;"object"==typeof n&&(o=n.pressed,"touched"in n&&(i=n.touched)),(o||i)&&E(t,e)}}s(g)}}function p(e){c[e.index]=e,s(g)}function w(e){p(e.gamepad)}function T(e){var n;n=e.gamepad,delete c[n.index]}a?(window.addEventListener("gamepadconnected",w),window.addEventListener("gamepaddisconnected",T)):d?(window.addEventListener("webkitgamepadconnected",w),window.addEventListener("webkitgamepaddisconnected",T)):setInterval((function(){const e=G();for(const n in e){const t=e[n];t&&(t.index in c?c[t.index]=t:p(t))}}),500)}})}));