# gizgame/input

Pointer, touch, pinch, wheel, gamepad stick, and mouse-look input state machine.

```gizlang
import * as Input from "gizgame/input";
```

## Types

### `Pointer`

```gizlang
type Pointer = { x: f64; y: f64; width: f64; height: f64; held: bool; pressed: bool; seen: bool; }
```

The pointer in surface pixels, the surface's size, its main button, and whether it has moved over the surface yet.

- `x`: `f64`
- `y`: `f64`
- `width`: `f64`
- `height`: `f64`
- `held`: `bool`
- `pressed`: `bool`
- `seen`: `bool`

### `Stick`

```gizlang
type Stick = { x: f64; y: f64; }
```

A stick's direction, each axis in [-1, 1], +Y down as on the screen.

- `x`: `f64`
- `y`: `f64`

### `March`

```gizlang
type March = { forward: i64; aside: i64; }
```

A walk's order of a stick (Game.march): forward and aside, each -1, 0 or 1.

- `forward`: `i64`
- `aside`: `i64`

### `StickKeys`

```gizlang
type StickKeys = { left: Vec<string>; right: Vec<string>; up: Vec<string>; down: Vec<string>; pad: i64; }
```

The keys and gamepad axes of a stick. PAD is the gamepad's stick: 0 for the left (axes 0 and 1), 1 for the right.

- `left`: `Vec<string>`
- `right`: `Vec<string>`
- `up`: `Vec<string>`
- `down`: `Vec<string>`
- `pad`: `i64`

### `Look`

```gizlang
type Look = { speed?: f64; invert?: bool; pad?: i64; turn?: f64; keys?: StickKeys; lock?: bool; }
```

A mouse look (Game.mouseLook): while the game plays, the host holds the pointer (a page locks it at the first click), and
the mouse turns the view by SPEED radians a count of its raw motion (0.0007, about 0.04 degrees: a shooter's usual
default: a full turn in about 29 cm of a mouse of 800 counts an inch); INVERT turns the vertical the other way. The gamepad's
stick PAD (1, the right) and the KEYS (none) turn it TURN radians a second (3) at full tilt.

- `speed`: `_gizlang_sdk_core.Option<f64>`
- `invert`: `_gizlang_sdk_core.Option<bool>`
- `pad`: `_gizlang_sdk_core.Option<i64>`
- `turn`: `_gizlang_sdk_core.Option<f64>`
- `keys`: `_gizlang_sdk_core.Option<M234.StickKeys>`
- `lock`: `_gizlang_sdk_core.Option<bool>`

### `Target`

```gizlang
type Target = { code: string; x: f64; y: f64; halfWidth: f64; halfHeight: f64; }
```

A place on the screen that a pointer presses as the key CODE: its middle and its half sizes, as fractions of the
surface.

- `code`: `string`
- `x`: `f64`
- `y`: `f64`
- `halfWidth`: `f64`
- `halfHeight`: `f64`

### `Thumb`

```gizlang
type Thumb = { from: Point2; at: Point2; }
```

The thumb stick a finger holds: where the stick stands and where its knob is, as fractions of the surface.

- `from`: `gizlang_geometry.Point2`
- `at`: `gizlang_geometry.Point2`

### `Swipe`

```gizlang
type Swipe = { x: f64; y: f64; by: f64; }
```

A slide of a finger on a target (Game.swipe).

- `x`: `f64`
- `y`: `f64`
- `by`: `f64`

### `Finger`

```gizlang
type Finger = { id: i64; role: i64; code: string; from: Point2; at: Point2; }
```

A pointer that is down: its host ID, what it does, the key of its target, and where it went down and is, in CSS pixels.

- `id`: `i64`
- `role`: `i64`
- `code`: `string`
- `from`: `gizlang_geometry.Point2`
- `at`: `gizlang_geometry.Point2`

### `PointerState`

```gizlang
type PointerState = { x: f64; y: f64; width: f64; height: f64; seen: bool; buttons: u64; zoom: f64; }
```

Mutable surface/pointer state passed into TouchHand.point().

- `x`: `f64`
- `y`: `f64`
- `width`: `f64`
- `height`: `f64`
- `seen`: `bool`
- `buttons`: `u64`
- `zoom`: `f64`

### `TouchHand`

```gizlang
class TouchHand { fingers: Vec<Finger> = []; touched: bool = false; clicked: bool = false; ratio: f64 = 1; buzz: i64 = 0; features: Vec<string> = []; private wheeled: f64 = 0; private swiped: f64 = 0; private swipeAt: Point2 = point2(0, 0); private wheelTaken: i64 = 0; turnWheel(notches: f64, zoom: f64): f64 { if (this.wheelTaken> 0) { this.wheeled + = notches; return zoom; } return Math.max(ZOOM_MIN, Math.min(ZOOM_MAX, zoom * Math.pow(1.12, notches))); } private pinch(wasAt: Point2, nowAt: Point2, anchor: Point2, zoom: f64): f64 { const was = length2(sub2(wasAt, anchor)); const now = length2(sub2(nowAt, anchor)); return was> 1 && now> 1 ? this.turnWheel(Math.log(was / now) / Math.log(1.12), zoom): zoom; } wheel(): f64 { const n = this.wheeled; this.wheeled = 0; this.wheelTaken = 8; return n; } tickWheel(): void { if (this.wheelTaken> 0) { this.wheelTaken - = 1; if (this.wheelTaken === 0) { this.wheeled = 0; } } } swipe(): Swipe { const by = this.swiped; this.swiped = 0; return { x: this.swipeAt.x, y: this.swipeAt.y, by }; } pushing(code: string): bool { return this.fingers.some(f => f.role === ON_TARGET && f.code === code); } onMenu(): bool { return this.fingers.some(f => f.role === ON_MENU); } mouseDragging(): bool { return this.fingers.some(f => f.role === ON_LOOK && (f.code === "ML" || f.code === "MR")); } thumb(width: f64, height: f64): Thumb | undefined { const f = this.fingers.find(f => f.role === ON_STICK); if (f === undefined) { return undefined; } const wide = Math.max(1, width / this.ratio); const tall = Math.max(1, height / this.ratio); return { from: point2(f!.from.x / wide, f!.from.y / tall), at: point2(f!.at.x / wide, f!.at.y / tall) }; } thumbAxes(): Point2 | undefined { const f = this.fingers.find(f => f.role === ON_STICK); return f === undefined ? undefined: scale2(sub2(f!.at, f!.from), 1 / THUMB_REACH); } drainCues(out: mut Vec<HostCue>): void { if (this.buzz> 0) { out.push({ kind: "haptic", ms: this.buzz }); this.buzz = 0; } } point(state: mut PointerState, event: HostEvent, playing: bool, captured: bool, hasLook: bool, bare: bool, covered: bool, targets: Vec<Target>, post: Point2 | undefined, tapped: mut Vec<string>): Point2 { const touch = event.pointer === "touch"; const down = event.kind === "pointerdown"; const lifted = !down && event.kind !== "pointermove"; const at = point2(event.x * state.width / this.ratio, event.y * state.height / this.ratio); const none = point2(0, 0); if (touch) { this.touched = true; } const knob = (from: Point2): Point2 => { const pull = sub2(at, from); const far = length2(pull); return far> THUMB_REACH ? add2(from, scale2(pull, THUMB_REACH / far)): at; }; const found = this.fingers.findIndex(f => f.id === event.id); if (down) { if (!touch && found> = 0 && this.fingers[found].role === ON_TARGET && (event.buttons & (event.buttons - 1)) !== 0) { return none; } this.fingers = this.fingers.filter(f => f.id !== event.id); let target: Target | undefined = undefined; if (!captured) { for (let ti = targets.length - 1; ti> = 0; ti - = 1) { const t = targets[ti]; if (Math.abs(event.x - t.x) <= t.halfWidth && Math.abs(event.y - t.y) <= t.halfHeight) { target = t; break; } } } const cast = target !== undefined && touch && playing && target!.code.startsWith("Hero"); if (target !== undefined && !cast) { this.fingers.push({ id: event.id, role: ON_TARGET, code: target!.code, from: at, at }); tapped.push(target!.code); state.x = event.x * state.width; state.y = event.y * state.height; state.seen = true; return none; } if (touch) { const postPx = post === undefined ? undefined: point2(post!.x * state.width / this.ratio, post!.y * state.height / this.ratio); const nearPost = postPx !== undefined && length2(sub2(at, postPx!)) <= THUMB_REACH * 2; const stick = !bare && (postPx === undefined ? event.x <0.48: nearPost); const looking = this.fingers.some(f => f.role === ON_LOOK); const role = !playing || covered ? ON_MENU: stick ? ON_STICK: looking ? ON_PINCH: ON_LOOK; if (role !== ON_MENU && this.fingers.some(f => f.role === role)) { return none; } const from = role === ON_STICK && nearPost ? postPx!: at; this.fingers.push({ id: event.id, role, code: cast && role !== ON_STICK ? target!.code: "", from, at: role === ON_STICK ? knob(from): at }); if (role !== ON_MENU) { return none; } } else if (!captured && playing && hasLook && (event.buttons & 7) !== 0) { const mCode = (event.buttons & 1) !== 0 ? "ML": "MR"; this.fingers.push({ id: event.id, role: ON_LOOK, code: mCode, from: at, at }); state.x = event.x * state.width; state.y = event.y * state.height; state.seen = true; state.buttons = u64(Math.maxI64(0, event.buttons)); return none; } } else if (found> = 0) { const finger = this.fingers[found]; if (!touch && finger.role === ON_LOOK && (finger.code === "ML" || finger.code === "MR")) { state.x = event.x * state.width; state.y = event.y * state.height; state.seen = true; state.buttons = u64(Math.maxI64(0, event.buttons)); if (lifted) { this.fingers = this.fingers.filter(f => f.id !== event.id); if (finger.code === "ML" && length2(sub2(at, finger.from)) <6.0) { this.clicked = true; } return none; } this.fingers[found] = { id: finger.id, role: ON_LOOK, code: finger.code, from: finger.from, at }; return (event.buttons & 7) !== 0 ? point2(event.dx * 1.35, event.dy * 1.35): none; } if (lifted && finger.code !== "" && finger.role === ON_LOOK && length2(sub2(at, finger.from)) <12) { tapped.push(finger.code); } if (lifted) { this.fingers = this.fingers.filter(f => f.id !== event.id).map((f): Finger => f.role === ON_PINCH ? { ...f, role: ON_LOOK }: f); } if (finger.role === ON_TARGET) { const second = this.fingers.find(f => !lifted && f.id !== finger.id && f.role === ON_TARGET); if (second !== undefined) { state.zoom = this.pinch(finger.at, at, second!.at, state.zoom); this.fingers[found] = { ...finger, at }; } else if (!lifted) { this.swiped + = (at.y - finger.at.y) * this.ratio / state.height; this.swipeAt = point2(event.x, event.y); this.fingers[found] = { ...finger, at }; } state.x = event.x * state.width; state.y = event.y * state.height; state.seen = true; return none; } if (finger.role !== ON_MENU) { if (lifted) { return none; } const keepCode = finger.code !== "" && length2(sub2(at, finger.from)) <12 ? finger.code: ""; this.fingers[found] = { id: finger.id, role: finger.role, code: keepCode, from: finger.from, at: finger.role === ON_STICK ? knob(finger.from): at }; const other = finger.role === ON_LOOK || finger.role === ON_PINCH ? this.fingers.find(f => f.id !== finger.id && (f.role === ON_LOOK || f.role === ON_PINCH)): undefined; if (other !== undefined) { state.zoom = this.pinch(finger.at, at, other!.at, state.zoom); this.fingers = this.fingers.map((f): Finger => f.role === ON_LOOK || f.role === ON_PINCH ? { ...f, code: "" }: f); return none; } return finger.role === ON_LOOK ? scale2(sub2(at, finger.at), FINGER_LOOK): none; } } else if (touch) { return none; } state.x = event.x * state.width; state.y = event.y * state.height; state.seen = true; if (!touch) { state.buttons = u64(Math.maxI64(0, event.buttons)); } if (down) { this.clicked = this.clicked || !touch; } return captured ? point2(event.dx, event.dy): none; } }
```

Tracks active touch/mouse pointers, virtual thumb stick, pinch-zoom, wheel, swipe, and haptics.

- `fingers`: `Vec<M234.Finger>`
- `touched`: `bool`
- `clicked`: `bool`
- `ratio`: `f64`
- `buzz`: `i64`
- `features`: `Vec<string>`
- `wheeled`: `f64`
- `swiped`: `f64`
- `swipeAt`: `gizlang_geometry.Point2`
- `wheelTaken`: `i64`

## Functions

### `keyLabel`

```gizlang
function keyLabel(code: string): string
```

The key CODE as it is printed on the key: KeyR is R, Digit1 is 1.

Effects: fail, alloc.

### `axis`

```gizlang
function axis(keys: Vec<string>, negative: Vec<string>, positive: Vec<string>): f64
```

Evaluates a digital axis from pressed key CODES against NEGATIVE and POSITIVE bindings.

Effects: fail.

### `wasd`

```gizlang
function wasd(): StickKeys
```

WASD and the arrow keys, and the gamepad's left stick.

Effects: fail, alloc.

### `pad`

```gizlang
function pad(stick: i64): StickKeys
```

Only the gamepad's stick PAD.

Effects: fail, alloc.

## Methods

### `TouchHand.turnWheel`

```gizlang
turnWheel(notches: f64, zoom: f64): f64
```

Applies wheel or pinch notches to ZOOM unless a panel owns the wheel this frame.

Effects: fail, read, write.

### `TouchHand.wheel`

```gizlang
wheel(): f64
```

Returns wheel/pinch notches since the last call and claims the wheel for panels for 8 ticks.

Effects: fail, read, write.

### `TouchHand.tickWheel`

```gizlang
tickWheel(): void
```

Counts down panel wheel ownership so the camera regains wheel zoom when panels close.

Effects: fail, read, write.

### `TouchHand.swipe`

```gizlang
swipe(): Swipe
```

Returns vertical swipe delta on a target since the last call.

Effects: fail, read, write.

### `TouchHand.pushing`

```gizlang
pushing(code: string): bool
```

Whether a pointer is currently holding a HUD target of CODE.

Effects: fail, read.

### `TouchHand.onMenu`

```gizlang
onMenu(): bool
```

Whether any finger is pressing a menu surface.

Effects: fail, read.

### `TouchHand.mouseDragging`

```gizlang
mouseDragging(): bool
```

Whether the mouse is currently orbiting the camera in free-cursor mode.

Effects: fail, read.

### `TouchHand.thumb`

```gizlang
thumb(width: f64, height: f64): Thumb | undefined
```

Active virtual thumb stick in surface fractions, if held.

Effects: fail, read.

### `TouchHand.thumbAxes`

```gizlang
thumbAxes(): Point2 | undefined
```

Active virtual thumb stick axes in [-1, 1], if held.

Effects: fail, read.

### `TouchHand.drainCues`

```gizlang
drainCues(out: mut Vec<HostCue>): void
```

Appends pending haptic cue to OUT.

Effects: fail, read, write, alloc.

### `TouchHand.point`

```gizlang
point(state: mut PointerState, event: HostEvent, playing: bool, captured: bool, hasLook: bool, bare: bool, covered: bool, targets: Vec<Target>, post: Point2 | undefined, tapped: mut Vec<string>): Point2
```

Processes one pointer event into STATE, returning camera look delta in mouse counts.

Effects: fail, read, write, alloc.

## Constants

### `THUMB_REACH`

```gizlang
const THUMB_REACH: f64 = 56
```

How far a thumb moves from the stick's middle to push it all the way, in CSS pixels: the knob goes no farther.

### `ON_TARGET`

```gizlang
const ON_TARGET: i64 = 0
```

A pointer that is down holds a target's key.

### `ON_STICK`

```gizlang
const ON_STICK: i64 = 1
```

A pointer that is down pushes the thumb stick.

### `ON_LOOK`

```gizlang
const ON_LOOK: i64 = 2
```

A pointer that is down turns the view.

### `ON_MENU`

```gizlang
const ON_MENU: i64 = 3
```

A pointer that is down points (a menu).

### `ON_PINCH`

```gizlang
const ON_PINCH: i64 = 4
```

A second finger beside one that turns the view: the two pinch, their spread zooming the camera (Game.zoom).

