|
| 1 | +// Uses only web APIs, so it runs unchanged in a browser and in NativeScript |
| 2 | +// with @nativescript/canvas-polyfill and @nativescript/canvas-gamepad installed. |
| 3 | + |
| 4 | +const WIDTH = 480; |
| 5 | +const HEIGHT = 270; |
| 6 | + |
| 7 | +const BACKGROUND = '#0f141c'; |
| 8 | +const IDLE = '#273142'; |
| 9 | +const OUTLINE = '#4b5563'; |
| 10 | +const TEXT = '#e5e7eb'; |
| 11 | +const MUTED = '#94a3b8'; |
| 12 | +const ACTIVE = '#f75930'; |
| 13 | + |
| 14 | +export function startGamepadTester(canvas: HTMLCanvasElement): () => void { |
| 15 | + const ctx = canvas.getContext('2d')!; |
| 16 | + let frame = 0; |
| 17 | + |
| 18 | + const onConnected = (e: GamepadEvent) => console.log(`gamepad ${e.gamepad.index} connected: ${e.gamepad.id}`); |
| 19 | + const onDisconnected = (e: GamepadEvent) => console.log(`gamepad ${e.gamepad.index} disconnected`); |
| 20 | + window.addEventListener('gamepadconnected', onConnected); |
| 21 | + window.addEventListener('gamepaddisconnected', onDisconnected); |
| 22 | + |
| 23 | + const draw = () => { |
| 24 | + // Fit a 480x270 layout into whatever size the canvas has. |
| 25 | + const scale = Math.min(canvas.width / WIDTH, canvas.height / HEIGHT); |
| 26 | + ctx.setTransform(1, 0, 0, 1, 0, 0); |
| 27 | + ctx.fillStyle = BACKGROUND; |
| 28 | + ctx.fillRect(0, 0, canvas.width, canvas.height); |
| 29 | + ctx.setTransform(scale, 0, 0, scale, (canvas.width - WIDTH * scale) / 2, (canvas.height - HEIGHT * scale) / 2); |
| 30 | + |
| 31 | + // Poll every frame: this is how the Gamepad API is meant to be read. |
| 32 | + const pads = navigator.getGamepads().filter((pad): pad is Gamepad => pad !== null && pad.connected); |
| 33 | + if (pads.length > 0) { |
| 34 | + drawPad(ctx, pads[0], pads.length - 1); |
| 35 | + } else { |
| 36 | + drawWaiting(ctx); |
| 37 | + } |
| 38 | + |
| 39 | + frame = requestAnimationFrame(draw); |
| 40 | + }; |
| 41 | + frame = requestAnimationFrame(draw); |
| 42 | + |
| 43 | + return () => { |
| 44 | + cancelAnimationFrame(frame); |
| 45 | + window.removeEventListener('gamepadconnected', onConnected); |
| 46 | + window.removeEventListener('gamepaddisconnected', onDisconnected); |
| 47 | + }; |
| 48 | +} |
| 49 | + |
| 50 | +function drawWaiting(ctx: CanvasRenderingContext2D) { |
| 51 | + ctx.textAlign = 'center'; |
| 52 | + ctx.textBaseline = 'middle'; |
| 53 | + ctx.fillStyle = TEXT; |
| 54 | + ctx.font = 'bold 16px sans-serif'; |
| 55 | + ctx.fillText('Connect a controller and press any button', WIDTH / 2, HEIGHT / 2 - 10); |
| 56 | + ctx.fillStyle = MUTED; |
| 57 | + ctx.font = '12px sans-serif'; |
| 58 | + ctx.fillText('navigator.getGamepads() has no connected pads yet', WIDTH / 2, HEIGHT / 2 + 16); |
| 59 | +} |
| 60 | + |
| 61 | +function drawPad(ctx: CanvasRenderingContext2D, pad: Gamepad, others: number) { |
| 62 | + const b = pad.buttons; |
| 63 | + |
| 64 | + ctx.textAlign = 'left'; |
| 65 | + ctx.textBaseline = 'middle'; |
| 66 | + ctx.fillStyle = TEXT; |
| 67 | + ctx.font = 'bold 12px sans-serif'; |
| 68 | + ctx.fillText(pad.id.length > 60 ? pad.id.slice(0, 59) + '…' : pad.id, 16, 18); |
| 69 | + |
| 70 | + // Standard mapping: https://w3c.github.io/gamepad/#remapping |
| 71 | + trigger(ctx, b[6], 40, 40, 'LT'); |
| 72 | + trigger(ctx, b[7], 320, 40, 'RT'); |
| 73 | + shoulder(ctx, b[4], 40, 62, 'LB'); |
| 74 | + shoulder(ctx, b[5], 320, 62, 'RB'); |
| 75 | + |
| 76 | + stick(ctx, pad.axes[0], pad.axes[1], b[10], 110, 140, 'L'); |
| 77 | + stick(ctx, pad.axes[2], pad.axes[3], b[11], 300, 188, 'R'); |
| 78 | + |
| 79 | + dpad(ctx, b[12], b[13], b[14], b[15], 180, 192); |
| 80 | + |
| 81 | + button(ctx, b[3], 370, 110, 'Y'); |
| 82 | + button(ctx, b[2], 345, 135, 'X'); |
| 83 | + button(ctx, b[1], 395, 135, 'B'); |
| 84 | + button(ctx, b[0], 370, 160, 'A'); |
| 85 | + |
| 86 | + pill(ctx, b[8], 205, 110, 'Select'); |
| 87 | + pill(ctx, b[9], 275, 110, 'Start'); |
| 88 | + if (b.length > 16) { |
| 89 | + button(ctx, b[16], 240, 145, 'Home'); |
| 90 | + } |
| 91 | + |
| 92 | + ctx.textAlign = 'left'; |
| 93 | + ctx.fillStyle = MUTED; |
| 94 | + ctx.font = '11px sans-serif'; |
| 95 | + const summary = `index ${pad.index} · mapping "${pad.mapping}" · ${pad.axes.length} axes · ${b.length} buttons`; |
| 96 | + ctx.fillText(others > 0 ? `${summary} · ${others} more connected` : summary, 16, HEIGHT - 14); |
| 97 | +} |
| 98 | + |
| 99 | +function trigger(ctx: CanvasRenderingContext2D, input: GamepadButton | undefined, x: number, y: number, label: string) { |
| 100 | + const value = input?.value ?? 0; |
| 101 | + ctx.fillStyle = IDLE; |
| 102 | + ctx.fillRect(x, y - 7, 100, 14); |
| 103 | + ctx.fillStyle = ACTIVE; |
| 104 | + ctx.fillRect(x, y - 7, 100 * value, 14); |
| 105 | + ctx.textAlign = 'left'; |
| 106 | + ctx.fillStyle = TEXT; |
| 107 | + ctx.font = '11px sans-serif'; |
| 108 | + ctx.fillText(`${label} ${value.toFixed(2)}`, x + 106, y); |
| 109 | +} |
| 110 | + |
| 111 | +function shoulder(ctx: CanvasRenderingContext2D, input: GamepadButton | undefined, x: number, y: number, label: string) { |
| 112 | + ctx.fillStyle = input?.pressed ? ACTIVE : IDLE; |
| 113 | + ctx.fillRect(x, y - 8, 100, 16); |
| 114 | + ctx.textAlign = 'center'; |
| 115 | + ctx.fillStyle = TEXT; |
| 116 | + ctx.font = '11px sans-serif'; |
| 117 | + ctx.fillText(label, x + 50, y); |
| 118 | +} |
| 119 | + |
| 120 | +function stick(ctx: CanvasRenderingContext2D, ax = 0, ay = 0, press: GamepadButton | undefined, x: number, y: number, label: string) { |
| 121 | + const radius = 34; |
| 122 | + ctx.fillStyle = IDLE; |
| 123 | + ctx.beginPath(); |
| 124 | + ctx.arc(x, y, radius, 0, Math.PI * 2); |
| 125 | + ctx.fill(); |
| 126 | + ctx.lineWidth = 2; |
| 127 | + ctx.strokeStyle = press?.pressed ? ACTIVE : OUTLINE; |
| 128 | + ctx.stroke(); |
| 129 | + |
| 130 | + // Axes run from -1 to 1, with +y pointing down. |
| 131 | + ctx.fillStyle = ACTIVE; |
| 132 | + ctx.beginPath(); |
| 133 | + ctx.arc(x + ax * (radius - 8), y + ay * (radius - 8), 8, 0, Math.PI * 2); |
| 134 | + ctx.fill(); |
| 135 | + |
| 136 | + ctx.textAlign = 'center'; |
| 137 | + ctx.fillStyle = MUTED; |
| 138 | + ctx.font = '10px sans-serif'; |
| 139 | + ctx.fillText(`${label} ${ax.toFixed(2)}, ${ay.toFixed(2)}`, x, y + radius + 12); |
| 140 | +} |
| 141 | + |
| 142 | +function dpad( |
| 143 | + ctx: CanvasRenderingContext2D, |
| 144 | + up: GamepadButton | undefined, |
| 145 | + down: GamepadButton | undefined, |
| 146 | + left: GamepadButton | undefined, |
| 147 | + right: GamepadButton | undefined, |
| 148 | + x: number, |
| 149 | + y: number, |
| 150 | +) { |
| 151 | + const size = 18; |
| 152 | + const cells: [GamepadButton | undefined, number, number][] = [ |
| 153 | + [up, 0, -1], |
| 154 | + [down, 0, 1], |
| 155 | + [left, -1, 0], |
| 156 | + [right, 1, 0], |
| 157 | + ]; |
| 158 | + for (const [input, dx, dy] of cells) { |
| 159 | + ctx.fillStyle = input?.pressed ? ACTIVE : IDLE; |
| 160 | + ctx.fillRect(x + dx * size - size / 2, y + dy * size - size / 2, size, size); |
| 161 | + } |
| 162 | + ctx.fillStyle = IDLE; |
| 163 | + ctx.fillRect(x - size / 2, y - size / 2, size, size); |
| 164 | +} |
| 165 | + |
| 166 | +function button(ctx: CanvasRenderingContext2D, input: GamepadButton | undefined, x: number, y: number, label: string) { |
| 167 | + ctx.fillStyle = input?.pressed ? ACTIVE : IDLE; |
| 168 | + ctx.beginPath(); |
| 169 | + ctx.arc(x, y, label.length > 1 ? 16 : 12, 0, Math.PI * 2); |
| 170 | + ctx.fill(); |
| 171 | + ctx.textAlign = 'center'; |
| 172 | + ctx.fillStyle = TEXT; |
| 173 | + ctx.font = label.length > 1 ? '10px sans-serif' : 'bold 12px sans-serif'; |
| 174 | + ctx.fillText(label, x, y); |
| 175 | +} |
| 176 | + |
| 177 | +function pill(ctx: CanvasRenderingContext2D, input: GamepadButton | undefined, x: number, y: number, label: string) { |
| 178 | + ctx.fillStyle = input?.pressed ? ACTIVE : IDLE; |
| 179 | + ctx.fillRect(x - 24, y - 8, 48, 16); |
| 180 | + ctx.textAlign = 'center'; |
| 181 | + ctx.fillStyle = TEXT; |
| 182 | + ctx.font = '10px sans-serif'; |
| 183 | + ctx.fillText(label, x, y); |
| 184 | +} |
0 commit comments