> For the complete documentation index, see [llms.txt](https://viridian-games.gitbook.io/pixel-palette/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://viridian-games.gitbook.io/pixel-palette/core-components/input-system.md).

# Input System

Two components work together: **CanvasInputHandler** reads input devices, and **CanvasViewController** applies zoom/pan to the canvas RectTransform.

#### Input Priority (Update order)

Each frame, `CanvasInputHandler.Update()` checks input in this order:

1. **Keyboard** — brush cycling (E), undo (Ctrl+Z), redo (Ctrl+Y), quick-shape toggle (Ctrl), delete selection (Del)
2. **Touch pinch/zoom** — two-finger pan + pinch zoom. Tracks `hasSeenMultiTouch` to suppress single-touch drawing during pinch
3. **Mouse scroll zoom** — scroll wheel zooms at the cursor position (1.1x factor)
4. **Mouse pan** — right-click or middle-click drag pans the canvas. Sets a 2-frame cooldown to avoid drawing immediately after panning
5. **Drawing** — if nothing else consumed the frame, process pen/touch/mouse strokes

#### Drawing Flow

```
TryGetDrawingInput()  →  gets screen position + pressure from Pen, Touch, or Mouse (priority order)
       ↓
TryScreenToPixel()    →  converts screen coords to pixel coords on the canvas
       ↓
HandleDrawing()       →  starts, continues, or ends a stroke via canvas.StartStroke/ContinueStroke/EndStroke
```

**Stroke start** — records the pixel, sets up quick-shape timers, starts undo capture

**Stroke move** — for Pen tool: if the pointer stays still (<4px movement for 0.25s), activates **quick-shape mode** (detects if stroke looks like a line or ellipse and switches to shape preview). Otherwise, draws Bresenham-interpolated pixels between the last and current position.

**Stroke end** — finalizes undo capture, cleans up quick-shape state

**Selection move** — if you click inside an existing selection with a non-select tool, it starts dragging the selection content instead of drawing.

**Double-tap** — within 0.3s / 20px of the last tap, toggles quick-shape between ellipse and rectangle.

#### UI Detection

`IsOverUI` = `IsOverUITK || IsOverUGUI`. When true, drawing is suppressed. The UITK controller tracks pointer enter/leave with a counter, and the uGUI controller runs `EventSystem.RaycastAll` each frame to set `IsOverUGUI`.

#### Canvas Input Utilities

* **`TryScreenToPixel(camera, screenPos, out pixelPos)`** — uses `RectTransformUtility.ScreenPointToLocalPointInRectangle` on the inputRect, then normalizes to pixel coords. Returns false if outside canvas bounds.
* **`ResolveEventCamera()`** — returns null for ScreenSpace-Overlay canvases, the world camera for ScreenSpace-Camera, or Camera.main as fallback.

#### Canvas View Controller

* **Zoom(factor, screenPivot, camera)** — multiplies current zoom (clamped 0.25x–16x), applies to `canvasRect.localScale`, adjusts `anchoredPosition` so the pivot point stays fixed on screen. Fires `OnZoomChanged`.
* **Pan(delta)** — adds delta to `canvasRect.anchoredPosition`. Fires `OnPanChanged`.
* **ResetView()** — resets zoom to 1x and position to center.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://viridian-games.gitbook.io/pixel-palette/core-components/input-system.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
