# Menu

> A main menu in three lines, then an options screen, a confirm dialog and a HUD — one scene per screen under src/scenes/, and change<>() between them.

Source: https://omardev29.github.io/rmp-docs/examples/ui/01_menu.html

A main menu in three lines, then an options screen, a confirm dialog and a HUD — one scene per screen under `src/scenes/`, and `change<>()` between them. Variants, disabled controls, explicit ids, placement, the Theme. **Start here.**

## Run it

From a clone of the framework:

```shell
rmp example 01_menu
```

## Keys

Read from the code below: the actions it defines with `rmp::input::action`, the ones that come defined that it reads by name, and the keys it asks raylib about directly.

| Action | Keys |
|---|---|
| `ui_cancel` | `Esc`, `gamepad right face right` |

## The code

Every file of `examples/ui/01_menu`, as it is at the commit this site documents ([on GitHub](https://github.com/omardev29/raylib_multiplatform/tree/982bf2a1e307c49776bff84211260703b8de6895/examples/ui/01_menu)).

**`src/main.cpp` 49 lines**

[src/main.cpp](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/examples/ui/01_menu/src/main.cpp)

```cpp
// ---------------------------------------------------------------------------
// examples/ui/01_menu/src/main.cpp
//
// rmp::ui, from the three-line version to the parts you reach for later.
//
// The API is deliberately small: begin, end, button, text. Everything that
// would normally make a UI tedious -- coordinates, sizes, fonts, hitboxes,
// hover states, what happens when the window is resized -- is decided for you,
// and each of those decisions can be overridden individually when you need it.
//
// One scene per screen under src/scenes/; this file only says which comes first.
// Run it: `rmp example 01_menu`.
// ---------------------------------------------------------------------------

#include <rmp/app.h>

#include "scenes/screens.h"

RMP_GAME(MainMenuScene);

// ---------------------------------------------------------------------------
// Things worth knowing, none of which you need on day one
// ---------------------------------------------------------------------------
//
// THEME. Plain data. Copy, change, set back — usually once, in a _ready():
//
//     auto t = rmp::ui::current_theme();
//     t.primary       = GOLD;
//     t.corner_radius = 0;          // angular instead of rounded
//     rmp::ui::set_theme(t);
//
// SCALE. Every Theme metric is in design units, at the [window] resolution
// from raylib_multiplatform.toml, and multiplied by rmp::ui::scale() before it
// is drawn. That is why this menu looks right on a phone and on a 4K monitor
// without a single conditional. To pin it — an "interface Size" setting, say:
//
//     rmp::ui::set_scale(1.5f);     // and 0 puts it back to automatic
//
// FONT. [ui] font = "ui.ttf" in the toml and every widget picks it up. Left
// empty you get the font built into raylib: no asset, no licence, works
// everywhere, and its scale is rounded to whole numbers so it stays sharp.
//
// TOUCH. This file already works on Android and iOS. Buttons are never shorter
// than the theme's min_touch_size, and hover is suppressed when nothing is
// touching the screen, so nothing stays lit up after a tap.
//
// ONE FRAME BEHIND. A button cannot be clicked on the very first frame it
// appears — 16 ms at 60 fps. It is inherent to immediate mode and the
// alternative is worse; TECHNICAL.md explains why.
```

**`src/scenes/confirm.cpp` 32 lines**

[src/scenes/confirm.cpp](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/examples/ui/01_menu/src/scenes/confirm.cpp)

```cpp
// Two buttons with the same label, in different screens.
#include "screens.h"

#include <rmp/app.h>
#include <rmp/ui.h>

void ConfirmScene::_draw() {
    rmp::ui::begin();

    rmp::ui::text("Really quit?");
    rmp::ui::text("Progress since the last save will be lost.",
                  { .color = rmp::ui::ColorRole::MUTED });

    // rmp::app::quit(), never std::exit(): this lets the frame finish and
    // then ends the scenes and closes the window on the way out. On Android it also
    // finishes the Activity, so the app does not leave a dead task behind.
    //
    // On iOS it does nothing at all, on purpose -- Apple rejects apps that
    // terminate themselves. Guarding it out is optional; the call is safe
    // everywhere. Here it is guarded so the button is not dead on iPhone.
#if !defined(PLATFORM_IOS)
    if (rmp::ui::button("Quit", { .style = rmp::ui::Variant::DANGER })) rmp::app::quit();
#endif

    // This screen and the main menu both have a "Back"-ish button. Identical
    // labels inside ONE frame are told apart automatically; an explicit id is
    // for when the UI is conditional -- as it is here -- and you want an
    // element's hover state to stay its own across screen changes.
    if (rmp::ui::button("Cancel", { .id = "confirm.cancel" })) change<MainMenuScene>();

    rmp::ui::end();
}
```

**`src/scenes/main_menu.cpp` 18 lines**

[src/scenes/main_menu.cpp](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/examples/ui/01_menu/src/scenes/main_menu.cpp)

```cpp
// The whole point: a main menu.
#include "screens.h"

#include <rmp/ui.h>

void MainMenuScene::_draw() {
    // A centred column, sized and spaced by the theme, scaled to the window.
    // Three ifs and you have a menu.
    rmp::ui::begin();

    rmp::ui::text("MY GAME");

    if (rmp::ui::button("Play")) change<PlayingScene>();
    if (rmp::ui::button("Options")) change<OptionsScene>();
    if (rmp::ui::button("Quit")) change<ConfirmScene>();

    rmp::ui::end();
}
```

**`src/scenes/options.cpp` 29 lines**

[src/scenes/options.cpp](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/examples/ui/01_menu/src/scenes/options.cpp)

```cpp
// One step up: options on the widgets.
#include "screens.h"

#include <rmp/ui.h>

#include <string>

void OptionsScene::_draw() {
    rmp::ui::begin();

    rmp::ui::text("Options");

    // A button whose label is built this frame. std::string_view takes it, and
    // the text is copied immediately -- temporaries are safe here.
    if (rmp::ui::button(std::string("Music: ") + (game.music ? "on" : "off"))) {
        game.music = !game.music;
    }

    // Semantic variants: you say what the button MEANS, the theme decides what
    // that looks like. Restyling the game never means revisiting this line.
    rmp::ui::button("Reset progress", { .style = rmp::ui::Variant::DANGER });

    // Disabled controls still lay out, and still look deliberate.
    rmp::ui::button("Cloud saves", { .enabled = false });

    if (rmp::ui::button("Back")) change<MainMenuScene>();

    rmp::ui::end();
}
```

**`src/scenes/playing.cpp` 29 lines**

[src/scenes/playing.cpp](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/examples/ui/01_menu/src/scenes/playing.cpp)

```cpp
// A HUD: not everything is a centred menu.
#include "screens.h"

#include <rmp/input.h>
#include <rmp/ui.h>

#include <string>

void PlayingScene::_update(float) {
    game.score += 1;
    // "ui_cancel" is one of the actions that come defined: Escape, or the
    // gamepad's right face button. Through rmp::input rather than
    // IsKeyPressed(KEY_ESCAPE), so a controller can go back too.
    if (rmp::input::just_pressed("ui_cancel")) change<MainMenuScene>();
}

void PlayingScene::_draw() {
    // placement moves the whole thing. The nine Align values are the ones you
    // would guess: TOP_LEFT, TOP_CENTER, ..., BOTTOM_RIGHT.
    rmp::ui::begin({ .placement = rmp::ui::Align::TOP_LEFT, .gap = 4 });

    // Built fresh every frame, which is exactly how immediate mode is meant to
    // be used: no label object to update, no "setText" to remember.
    rmp::ui::text("Score: " + std::to_string(game.score));
    rmp::ui::text("Press ESC for the menu",
                  { .color = rmp::ui::ColorRole::MUTED, .size = 14 });

    rmp::ui::end();
}
```

**`src/scenes/screens.h` 36 lines**

[src/scenes/screens.h](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/examples/ui/01_menu/src/scenes/screens.h)

```cpp
#pragma once
// One scene per screen, one file per scene under src/scenes/. Going from one
// to the next is rmp::Scene::change<T>(): the scene on screen ends, the next
// one starts, at the end of the frame the button was pressed in.
#include <rmp/scene.h>

// What outlives a screen: the score of the run, and the music switch the
// options change. Plain data of YOURS -- the UI is immediate mode, so it holds
// no state, and there is nothing to keep in sync.
struct Progress {
    int score = 0;
    bool music = true;
};
inline Progress game;

class MainMenuScene : public rmp::Scene {
public:
    void _draw() override;
};

class OptionsScene : public rmp::Scene {
public:
    void _draw() override;
};

class ConfirmScene : public rmp::Scene {
public:
    void _draw() override;
};

// Not everything is a centred menu: the game being played, with its HUD.
class PlayingScene : public rmp::Scene {
public:
    void _update(float delta) override;
    void _draw() override;
};
```
