()
| 601 | } |
| 602 | |
| 603 | function inputUpdate() |
| 604 | { |
| 605 | if (headlessMode) return; |
| 606 | |
| 607 | // clear input when lost focus (prevent stuck keys) |
| 608 | if (!(touchInputEnable && isTouchDevice) && !document.hasFocus()) |
| 609 | inputClear(); |
| 610 | |
| 611 | // update mouse world space position and delta |
| 612 | mousePos = screenToWorld(mousePosScreen); |
| 613 | mouseDelta = screenToWorldDelta(mouseDeltaScreen); |
| 614 | |
| 615 | // update gamepads if enabled |
| 616 | gamepadsUpdate(); |
| 617 | |
| 618 | // gamepads are updated by engine every frame automatically |
| 619 | function gamepadsUpdate() |
| 620 | { |
| 621 | const applyDeadZones = (v)=> |
| 622 | { |
| 623 | const min=.3, max=.8; |
| 624 | const deadZone = (v)=> |
| 625 | v > min ? percent(v, min, max) : |
| 626 | v < -min ? -percent(-v, min, max) : 0; |
| 627 | return vec2(deadZone(v.x), deadZone(-v.y)).clampLength(); |
| 628 | }; |
| 629 | |
| 630 | // update touch gamepad if enabled |
| 631 | if (touchGamepadEnable && isTouchDevice) |
| 632 | { |
| 633 | if (debugGamepads) |
| 634 | { |
| 635 | const stickCenter = vec2(touchGamepadSize, mainCanvasSize.y-touchGamepadSize); |
| 636 | const buttonCenter = touchGamepadButtonCenter(); |
| 637 | const startCenter = mainCanvasSize.scale(.5); |
| 638 | |
| 639 | debugCircle(stickCenter, 2*touchGamepadSize, 'cyan', 0, false, true); |
| 640 | debugCircle(buttonCenter, 2*touchGamepadSize, 'cyan', 0, false, true); |
| 641 | if (touchGamepadCenterButtonSize) |
| 642 | { |
| 643 | debugCircle(startCenter, 2*touchGamepadCenterButtonSize, 'cyan', 0, false, true); |
| 644 | // exclusion bubbles around controls (where start is blocked) |
| 645 | debugCircle(stickCenter, 4*touchGamepadSize, 'magenta', 0, false, true); |
| 646 | debugCircle(buttonCenter, 4*touchGamepadSize, 'magenta', 0, false, true); |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | if (!touchGamepadTimer.isSet()) return; |
| 651 | |
| 652 | // read virtual analog stick |
| 653 | gamepadPrimary = 0; // touch gamepad uses index 0 |
| 654 | const sticks = gamepadStickData[0] ?? (gamepadStickData[0] = []); |
| 655 | const dpad = gamepadDpadData[0] ?? (gamepadDpadData[0] = vec2()); |
| 656 | sticks[0] = vec2(); |
| 657 | dpad.set(); |
| 658 | const leftTouchStick = touchGamepadSticks[0] ?? vec2(); |
| 659 | if (touchGamepadAnalog) |
| 660 | sticks[0] = applyDeadZones(leftTouchStick); |
no test coverage detected