# Pointer picking

> usePointer is one input for the whole scene, and every event says what is under the pointer.

Example `pointer-pick`, running at https://nacatamalon.com/en/examples/pointer-pick. The whole catalogue: https://nacatamalon.com/llms/examples.md.

`usePointer()` is one input for the whole scene, and every event says what is under the pointer. A single `onDown` reads `info.target`, the sprite on top, to highlight whatever was clicked and reset the rest. Click empty space and `target` is `null`, so everything deselects.

Rotated, scaled and anchored sprites are hit where they are drawn, and a camera is taken into account.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { Field } from './scenes/demo';
import { WIDTH, HEIGHT } from './settings';

const game = createGame('#app', {
    width: WIDTH,
    height: HEIGHT,
    background: '#10141d'
});

// Declare Scenes
game({
    Field
});
```

`components/pick_grid.ts`:

```ts
import { createSprite, getColor, useLoadTexture, usePointer, type TSprite } from 'nacatamalon';
import { GRID, TEXTURE } from '../settings';

export const PickGrid = () => {
    const texture = useLoadTexture(TEXTURE);

    // See-through when resting, solid when picked: the alpha of the tint.
    const resting = getColor('rgba(255, 255, 255, 0.55)');
    const picked = getColor('#ffffff');

    const cells: TSprite[] = [];

    for (let row = 0; row < GRID.rows; row++) {
        for (let column = 0; column < GRID.columns; column++) {
            cells.push(createSprite({
                texture,
                tint: resting,
                width: GRID.cellSize,
                height: GRID.cellSize,
                transform: {
                    x: GRID.x + column * GRID.gapX,
                    y: GRID.y + row * GRID.gapY,
                    rotation: 0,
                    scaleX: 1,
                    scaleY: 1
                },
            }));
        }
    }

    const pointer = usePointer();

    pointer.onDown((info) => {
        for (const cell of cells) {
            const isPicked = cell === info.target;

            cell.tint = isPicked ? picked : resting;
            cell.transform.scaleX = isPicked ? GRID.selectedScale : 1;
            cell.transform.scaleY = isPicked ? GRID.selectedScale : 1;
        }

        // Over empty space there is no target, which deselects everything above. worldX and
        // worldY are where that was; with no camera they are the screen position.
        console.log(info.target
            ? 'picked ' + info.target.id
            : 'clicked empty space at ' + Math.round(info.worldX) + ',' + Math.round(info.worldY));
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useSpawn } from 'nacatamalon';
import { PickGrid } from '../components/pick_grid';

export const Field = () => {
    useSpawn(PickGrid)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
export const GRID = {
    rows: 4,
    columns: 6,
    x: 82,
    y: 72,
    gapX: 68,
    gapY: 62,
    cellSize: 44,
    selectedScale: 1.4
};
export const TEXTURE = {
    src: '/example-assets/assets/gammix.png',
    key: 'gammix'
};
```
