# Pack: a UI kit

> A pack offering several pieces (a panel, a button, a badge): three copies of one button, each with its own label, reporting to the game.

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

A pack that offers **several things**: a panel, a button and a badge, with the icon and font they use. The game names which one each copy is (`box: 'Button'`) and takes as many as it wants.

The button's label is a setting, so one button in the pack is three different buttons here. Clicking one is a **report**: the button sends `pressed` up to whoever placed it, and the game hears it in one of two ways: with `on` when creating the copy (PLAY and OPTIONS), or with `onReport(copy, 'pressed', …)` on what `createPack` gave back (QUIT). The button does not know what pressing it means: that is the game's to decide.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { WIDTH, HEIGHT } from './settings';
import { Menu } from './scenes/demo';
import './packs/ui-kit/ui-kit.pack';

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

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

`components/content.ts`:

```ts
import { createPack, createText, onReport, useLoadBitmapFont, useLoadPack } from 'nacatamalon';

export const Content = () => {
    const ui = useLoadPack({
        src: '/example-assets/packs/ui-kit'
    });

    const font = useLoadBitmapFont({
        json: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.json',
        atlas: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.png'
    });

    const status = createText({
        text: 'CLICK A BUTTON',
        font,
        style: {
            fontSize: 8
        },
        anchor: {
            x: 0.5,
            y: 0
        },
        transform: {
            x: 160,
            y: 196
        }
    });

    // One handler for the menu: each copy says which it was, with the label it was given.
    const said = (payload: unknown) => {
        if (!payload || typeof payload !== 'object' || !('label' in payload) || typeof payload.label !== 'string') {
            return;
        }

        const { label } = payload;

        status.text = label + ' PRESSED';
    };

    // Two ways to listen, the same two a sprite's click has (createSprite({ onClick }) and listen):

    createPack({
        pack: ui,
        box: 'Panel',
        transform: {
            x: 160,
            y: 112,
            scaleX: 1.4,
            scaleY: 1.9
        }
    });
    // 1. When creating it, with on.
    createPack({
        pack: ui,
        box: 'Button',
        transform: {
            x: 160,
            y: 80
        },
        props: {
            label: 'PLAY'
        },
        on: {
            pressed: said
        }
    });
    createPack({
        pack: ui,
        box: 'Button',
        transform: {
            x: 160,
            y: 112
        },
        props: {
            label: 'OPTIONS'
        },
        on: {
            pressed: said
        }
    });

    // 2. Afterwards, on what createPack gave back: onReport(copy, name, handler). For when what to do
    //    is decided later, or somewhere else (a script that is handed the copy). This one does its own thing.
    const quit = createPack({
        pack: ui,
        box: 'Button',
        transform: {
            x: 160,
            y: 144
        },
        props: {
            label: 'QUIT'
        }
    });

    onReport(quit, 'pressed', () => {
        status.text = 'SEE YOU';
    });

    createPack({
        pack: ui,
        box: 'Badge',
        transform: {
            x: 24,
            y: 24
        }
    });
    createPack({
        pack: ui,
        box: 'Badge',
        transform: {
            x: 296,
            y: 24
        }
    });
};
```

`packs/ui-kit/scripts/ui_button.ts`:

```ts
import { listen, useReport, useUpdate, type TGameObject } from 'nacatamalon';

/**
 * A button of the `ui-kit` pack: shows its own label, presses in when clicked, and tells whoever
 * placed it.
 *
 * The label is a **setting**, so every copy of the same button says something different
 * (`createPack({ pack, box: 'Button', props: { label: 'QUIT' } })`) while the pack stays one file.
 *
 * The click is a **report**: the button sends `pressed` with its label, and a game hears it with
 * `createPack({ ..., on: { pressed } })`. The button does not know what pressing it means; that is
 * the game's to decide.
 */
export default (self: TGameObject, props: {
    /** @label Label @default OK */
    label: string;
}) => {
    const pressed = useReport<{ label: string; }>('pressed');
    const text = self.drawables.find((drawable) => drawable.type === 'text');

    if (text !== undefined && text.type === 'text') {
        text.text = props.label;
    }

    let hover = false;
    let squash = 0;

    // The whole button reacts as one thing: its border, its fill and its label.
    listen(self, {
        onPointerOver: () => {
            hover = true;
        },
        onPointerOut: () => {
            hover = false;
        },
        onClick: () => {
            squash = 0.15;
            pressed({
                label: props.label
            });
        },
    });

    useUpdate((delta) => {
        if (!self.transform) {
            return;
        }

        squash = Math.max(0, squash - delta);

        const scale = squash > 0 ? 0.9 : hover ? 1.06 : 1;

        self.transform.scaleX = scale;
        self.transform.scaleY = scale;
    });
};
```

`packs/ui-kit/ui-kit.pack.ts`:

```ts
// Registers the scripts of the "ui-kit" pack. Import it once in a game written in code:
//   import './packs/ui-kit/ui-kit.pack';
// Written when the pack was made. The editor and an exported game do not need it.
import { registerScript } from 'nacatamalon';
import script_ui_button from './scripts/ui_button';

registerScript('ui-kit:ui_button', script_ui_button);
```

`scenes/demo.ts`:

```ts
import { createScene, useSpawn } from 'nacatamalon';
import { Content } from '../components/content';

export const Menu = () => {
    useSpawn(Content)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 320;
export const HEIGHT = 224;
```
