# Text menu

> Texts take the same pointer events as sprites: a menu that lights up on hover and changes scene.

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

Texts take the same pointer events as sprites: `onPointerOver`, `onPointerOut`, `onClick` and the rest, in `createText`'s options or connected later with `listen()` (that is how EXIT gets its). The whole block reacts, gaps between letters and spaces included, so an option does not flicker as the mouse crosses it.

SOUND rewrites its own `.text` and the area that reacts follows the new width. START changes scene, and BACK comes back.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { Menu } from './scenes/menu';
import { Level } from './scenes/level';
import { WIDTH, HEIGHT } from './settings';

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

// Declare Scenes
game({
    Menu,
    Level
});
```

`components/exit_option.ts`:

```ts
import { createText, listen, useLoadBitmapFont } from 'nacatamalon';
import { ARCADE, WHITE, WIDTH, CENTRED } from '../settings';
import { useHighlight } from '../hooks/use_highlight';

export const ExitOption = ({ onExit }: { onExit: () => void }) => {
    const font = useLoadBitmapFont(ARCADE);
    const highlight = useHighlight();

    const exit = createText({
        text: 'EXIT',
        font,
        style: {
            fontSize: 16
        },
        tint: WHITE,
        anchor: CENTRED,
        transform: {
            x: WIDTH / 2,
            y: 200
        }
    });

    // Same events connected after creation: listen works on text too.
    listen(exit, {
        onPointerOver: () => highlight(exit, true),
        onPointerOut: () => highlight(exit, false),
        onClick: onExit
    });
};
```

`components/menu_option.ts`:

```ts
import { createText, useLoadBitmapFont } from 'nacatamalon';
import { ARCADE, WHITE, CENTRED, WIDTH } from '../settings';
import { useHighlight } from '../hooks/use_highlight';

type TOptionProps = { text: string; y: number; onClick: () => void };
export const MenuOption = ({ text, y, onClick }: TOptionProps) => {
    const font = useLoadBitmapFont(ARCADE);
    const highlight = useHighlight();

    const option = createText({
        text,
        font,
        style: {
            fontSize: 16
        },
        tint: WHITE,
        anchor: CENTRED,
        transform: {
            x: WIDTH / 2,
            y
        },
        onPointerOver: () => highlight(option, true),
        onPointerOut: () => highlight(option, false),
        onClick
    });

    return option;
};
```

`components/sound_option.ts`:

```ts
import { useSpawn } from 'nacatamalon';
import { MenuOption } from './menu_option';

export const SoundOption = () => {
    let soundOn = true;

    const sound = useSpawn(MenuOption)({
        text: 'SOUND ON',
        y: 160,
        onClick: () => {
            soundOn = !soundOn;
            sound.api.text = soundOn ? 'SOUND ON' : 'SOUND OFF';
        }
    });
};
```

`hooks/use_highlight.ts`:

```ts
import { useTween, type TText, type TTweenHandle } from 'nacatamalon';
import { easeOutBack } from 'nacatamalon/math';
import { WHITE, YELLOW, HOVER } from '../settings';

// Every menu option owns its current tween; a new hover continues from the current scale.
export const useHighlight = () => {
    const tween = useTween();
    let growing: TTweenHandle | null = null;

    return (option: TText, on: boolean) => {
        option.tint = on ? YELLOW : WHITE;
        growing?.stop();

        const scale = on ? HOVER.scale : 1;

        growing = tween({
            target: option,
            to: {
                transform: {
                    scaleX: scale,
                    scaleY: scale
                }
            },
            duration: HOVER.duration,
            ease: easeOutBack
        });
        growing.play();
    };
};
```

`scenes/level.ts`:

```ts
import { createScene, createText, useScene, useLoadBitmapFont } from 'nacatamalon';
import { ARCADE, WIDTH, CENTRED, YELLOW, WHITE } from '../settings';

export const Level = () => {
    const font = useLoadBitmapFont(ARCADE);
    const scene = useScene();

    createText({
        text: 'LEVEL 1',
        font,
        style: {
            fontSize: 32
        },
        tint: YELLOW,
        anchor: CENTRED,
        transform: {
            x: WIDTH / 2,
            y: 90
        }
    });

    const back = createText({
        text: 'BACK',
        font,
        style: {
            fontSize: 16
        },
        tint: WHITE,
        anchor: CENTRED,
        transform: {
            x: WIDTH / 2,
            y: 200
        },
        onPointerOver: () => {
            back.tint = YELLOW;
        },
        onPointerOut: () => {
            back.tint = WHITE;
        },
        onClick: () => scene.change('Menu'),
    });

    return createScene();
};
```

`scenes/menu.ts`:

```ts
import { createScene, createText, useScene, useLoadBitmapFont, useSpawn } from 'nacatamalon';
import { ARCADE, WIDTH, YELLOW, CENTRED } from '../settings';
import { MenuOption } from '../components/menu_option';
import { SoundOption } from '../components/sound_option';
import { ExitOption } from '../components/exit_option';

export const Menu = () => {
    const font = useLoadBitmapFont(ARCADE);
    const scene = useScene();

    const title = createText({
        text: 'NACATAMAL ON',
        font,
        style: {
            fontSize: 24
        },
        tint: YELLOW,
        anchor: CENTRED,
        transform: {
            x: WIDTH / 2,
            y: 40
        }
    });

    useSpawn(MenuOption)({
        text: 'START',
        y: 120,
        onClick: () => scene.change('Level')
    });
    useSpawn(SoundOption)();
    useSpawn(ExitOption)({
        onExit: () => {
            title.text = 'SEE YOU SOON';
        }
    });

    return createScene();
};
```

`settings.ts`:

```ts
import { getColor } from 'nacatamalon';

export const WIDTH = 480;
export const HEIGHT = 280;
export const WHITE = getColor('#ffffff');
export const YELLOW = getColor('#f5c518');
export const CENTRED = {
    x: 0.5,
    y: 0
};
export const HOVER = {
    scale: 1.2,
    duration: 0.15
};
export const ARCADE = {
    json: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.json',
    atlas: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.png'
};
```
