UNPKG

virtual-gamepad-lib

Version:

Emulate and display virtual and real gamepads on the web

3 lines (2 loc) 3.82 kB
"use strict";var p=Object.defineProperty;var f=(r,i,o)=>i in r?p(r,i,{enumerable:!0,configurable:!0,writable:!0,value:o}):r[i]=o;var g=(r,i,o)=>f(r,typeof i!="symbol"?i+"":i,o);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const m=require("./GamepadApiWrapper.js"),l=require("./enums.js");class u{constructor(i,o){g(this,"config");g(this,"apiWrapper");g(this,"DefaultJoystickDisplayFunction",(i,o,a)=>{const h=i.movementRange;if(i.joystickElement.style.transform=`translate(${o*h}px,${a*h}px)`,i.highlights&&this.config.moveDirectionHighlightClass){const s=i.highlights[l.gamepadDirection.up],t=i.highlights[l.gamepadDirection.down],e=i.highlights[l.gamepadDirection.left],n=i.highlights[l.gamepadDirection.right];s&&a<-.1?s.classList.add(this.config.moveDirectionHighlightClass||""):s&&s.classList.remove(this.config.moveDirectionHighlightClass||""),t&&a>.1?t.classList.add(this.config.moveDirectionHighlightClass||""):t&&t.classList.remove(this.config.moveDirectionHighlightClass||""),e&&o<-.1?e.classList.add(this.config.moveDirectionHighlightClass||""):e&&e.classList.remove(this.config.moveDirectionHighlightClass||""),n&&o>.1?n.classList.add(this.config.moveDirectionHighlightClass||""):n&&n.classList.remove(this.config.moveDirectionHighlightClass||"")}});g(this,"DefaultButtonDisplayFunction",(i,o,a,h,s,t)=>{const e=i.highlight;if(this.config.touchedHighlightClass&&e&&(s.touchDown?e.classList.add(this.config.touchedHighlightClass):s.touchUp&&e.classList.remove(this.config.touchedHighlightClass)),this.config.pressedHighlightClass&&e&&(s.pressed?e.classList.add(this.config.pressedHighlightClass):s.released&&e.classList.remove(this.config.pressedHighlightClass)),i.type==l.gamepadButtonType.variable){const n=i.directionHighlight;if(this.config.moveDirectionHighlightClass&&n&&(s.pressed?n.classList.add(this.config.moveDirectionHighlightClass):s.released&&n.classList.remove(this.config.moveDirectionHighlightClass)),i.buttonElement){const c=i.direction==l.gamepadDirection.left||i.direction==l.gamepadDirection.right,d=i.direction==l.gamepadDirection.right||i.direction==l.gamepadDirection.down;i.buttonElement.style.transform=`translate${c?"X":"Y"}(${d?"":"-"}${o*i.movementRange}px)`}}});g(this,"displayJoystickChanges",(i,o,a)=>{if(i!=this.config.gamepadIndex)return;const h=this.config.sticks;for(let s=0;s<h.length;s++){const t=h[s];if(t&&(t.xAxisIndex!==void 0&&a[t.xAxisIndex]||t.yAxisIndex!==void 0&&a[t.yAxisIndex])){const e=o.axes,n=t.xAxisIndex!==void 0&&e[t.xAxisIndex]||0,c=t.yAxisIndex!==void 0&&e[t.yAxisIndex]||0;this.config.joystickDisplayFunction?this.config.joystickDisplayFunction(t,n,c):this.DefaultJoystickDisplayFunction(t,n,c)}}});g(this,"displayButtonChanges",(i,o,a)=>{if(i!=this.config.gamepadIndex)return;const h=this.config.buttons;for(let s=0;s<h.length;s++){const t=h[s],e=a[s];if(!t||Object.keys(t).length==0||!e||Object.keys(e).length==0)continue;const n=o.buttons[s].value,c=o.buttons[s].touched,d=o.buttons[s].pressed;this.config.buttonDisplayFunction?this.config.buttonDisplayFunction(t,n,c,d,e,s):this.DefaultButtonDisplayFunction(t,n,c,d,e,s)}});var a,h;this.config=Object.assign({gamepadIndex:0,buttons:[],sticks:[],touchedHighlightClass:"touched",pressedHighlightClass:"pressed",moveDirectionHighlightClass:"active"},i),this.apiWrapper=o||new m.GamepadApiWrapper({buttonConfigs:[],updateDelay:0}),((a=this.config.buttons)==null?void 0:a.length)!==0&&this.apiWrapper.onGamepadButtonChange(this.displayButtonChanges),((h=this.config.sticks)==null?void 0:h.length)!==0&&this.apiWrapper.onGamepadAxisChange(this.displayJoystickChanges)}Cleanup(){this.apiWrapper.offGamepadButtonChange(this.displayButtonChanges),this.apiWrapper.offGamepadAxisChange(this.displayJoystickChanges)}}exports.GamepadDisplay=u; //# sourceMappingURL=GamepadDisplay.js.map