virtual-gamepad-lib
Version:
Emulate and display virtual and real gamepads on the web
3 lines (2 loc) • 3.85 kB
JavaScript
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./enums.js"),G=require("./GamepadDisplay.js"),p=require("./GamepadEmulator.js"),A=require("./GamepadApiWrapper.js"),c=require("./utilities.js"),y=(r,a)=>{a=Object.assign({GpadDisplayConfig:{gamepadIndex:0},EmulatedGamepadIndex:0,EmulatedGamepadOverlayMode:!0,AllowDpadDiagonals:!1,VariableTriggers:!0,ClickableJoysticks:!0,JoystickDragDistance:50,TriggerDragDistance:50},a),r.classList.add("gpad-container");const o=a.GpadEmulator??new p.GamepadEmulator(.1),u=a.GpadApiWrapper??new A.GamepadApiWrapper({updateDelay:0,axisDeadZone:.05,buttonConfigs:[]}),d=a.EmulatedGamepadIndex;o.AddEmulatedGamepad(d,a.EmulatedGamepadOverlayMode,p.DEFAULT_GPAD_BUTTON_COUNT,p.DEFAULT_GPAD_AXIS_COUNT);const n=[];for(let t=0;t<e.PRESET_SVG_GPAD_BTN_TAP_TARGET_IDS.length;t++){const i=e.PRESET_SVG_GPAD_BTN_TAP_TARGET_IDS[t],s=r.querySelector("#"+i),m=i.includes("trigger"),g=i.includes("stick");if(s)m&&a.VariableTriggers?n.push({buttonIndex:t,type:e.gamepadButtonType.variable,tapTarget:s,dragDistance:a.TriggerDragDistance,lockTargetWhilePressed:!0,directions:{[e.gamepadDirection.down]:!0}}):(!g||a.ClickableJoysticks)&&n.push({buttonIndex:t,tapTarget:s,type:e.gamepadButtonType.onOff,lockTargetWhilePressed:g===!0});else{console.error("Could not find gamepad tap target with id "+i+". Make sure to insert the full gamepad SVG into the DOM before calling setupPremadeInteractiveGamepad().");continue}}if(a.AllowDpadDiagonals)for(const[t,i]of Object.entries(e.PRESET_SVG_GPAD_DPAD_DIAGONAL_TAP_TARGET_IDS_TO_BTN_INDEXES)){const s=r.querySelector("#"+t);if(s)n.push({buttonIndexes:i,type:e.gamepadButtonType.onOff,tapTarget:s,lockTargetWhilePressed:!1});else{console.error("Could not find gamepad tap target with id "+t+". Make sure to insert the full gamepad SVG into the DOM before calling setupPremadeInteractiveGamepad().");continue}}o.AddButtonTouchEventListeners(d,n),o.AddJoystickTouchEventListeners(d,[{tapTarget:r.querySelector("#stick_button_left_tap_target"),dragDistance:a.JoystickDragDistance,xAxisIndex:0,yAxisIndex:1,lockTargetWhilePressed:!0,directions:{[e.gamepadDirection.up]:!0,[e.gamepadDirection.down]:!0,[e.gamepadDirection.left]:!0,[e.gamepadDirection.right]:!0}},{tapTarget:r.querySelector("#stick_button_right_tap_target"),dragDistance:a.JoystickDragDistance,xAxisIndex:2,yAxisIndex:3,lockTargetWhilePressed:!0,directions:{[e.gamepadDirection.up]:!0,[e.gamepadDirection.down]:!0,[e.gamepadDirection.left]:!0,[e.gamepadDirection.right]:!0}}]);const l=document.querySelector(`#${e.PRESET_SVG_GPAD_BTN_IDS[e.standardGpadButtonMap.LStick]}`),D=document.querySelector(`#${e.PRESET_SVG_GPAD_BTN_IDS[e.standardGpadButtonMap.RStick]}`);c.CenterTransformOrigin(l),c.CenterTransformOrigin(D);const _=e.PRESET_SVG_GPAD_BTN_IDS.map(t=>t.includes("trigger")&&a.VariableTriggers?{type:e.gamepadButtonType.variable,direction:e.gamepadDirection.down,buttonElement:r.querySelector(`#${t}`),highlight:r.querySelector(`#${t} .${e.PRESET_SVG_GPAD_CLASS.ButtonHighlight}`),directionHighlight:r.querySelector(`#${t} .${e.PRESET_SVG_GPAD_CLASS.DirectionHighlight}`),movementRange:10}:{type:e.gamepadButtonType.onOff,highlight:r.querySelector(`#${t} .${e.PRESET_SVG_GPAD_CLASS.ButtonHighlight}`),extraData:{myCustomData:"onOff btn name is "+t}}),T=[{joystickElement:r.querySelector(`#${e.PRESET_SVG_GPAD_BTN_IDS[e.standardGpadButtonMap.LStick]}`),xAxisIndex:0,yAxisIndex:1,movementRange:10},{joystickElement:r.querySelector(`#${e.PRESET_SVG_GPAD_BTN_IDS[e.standardGpadButtonMap.RStick]}`),xAxisIndex:2,yAxisIndex:3,movementRange:10}],S=new G.GamepadDisplay(Object.assign({gamepadIndex:d,buttons:_,sticks:T},a.GpadDisplayConfig),u);return{gpadEmulator:o,gpadApiWrapper:u,gpadDisplay:S}};exports.setupPresetInteractiveGamepad=y;
//# sourceMappingURL=helpers.js.map