On this page
  1. Run it
  2. The code

Style

The two themes, the five variants, the three sizes and the transition. Nothing in it names a colour — that is the point. Plus copy-modify-set for a Theme of your own.

Style, as it starts

Run it

From a clone of the framework:

rmp example 06_style

The code

Every file of examples/ui/06_style, as it is at the commit this site documents (on GitHub).

src/main.cpp 190 lines
// ---------------------------------------------------------------------------
// examples/ui/06_style/src/main.cpp
//
// The two themes, the five variants, the three sizes, and the transition that
// ties them together.
//
// The idea worth taking away is that none of this is a colour parameter. You
// say what a button MEANS — primary, danger, ghost — and how important it is —
// small, medium, large — and the theme decides what that looks like. Which is
// why the light/dark switch at the top of this file is one line and does not
// need a single call site revisited:
//
//     rmp::ui::set_theme(light ? rmp::ui::theme_light() : rmp::ui::theme_dark());
//
// If you had passed colours to widgets instead, that line would be a rewrite.
//
// Run it: `rmp example 06_style`.
// ---------------------------------------------------------------------------

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

#include <string>

class StyleScene : public rmp::Scene {
public:
    void _ready() override { apply_style(); }

    void _draw() override {
        rmp::ui::begin({ .placement = rmp::ui::Align::TOP_CENTER });

        rmp::ui::panel({ .box = { .grow_x = true } }, [this] {
            rmp::ui::row({ .grow_x = true }, [this] {
                rmp::ui::text("Style", { .size = rmp::ui::Size::LARGE });
                rmp::ui::spacer();
                // Both of these rebuild the theme and hand it back. Nothing else in
                // the file knows or cares which one is active.
                if (rmp::ui::checkbox("Light Theme", _light)) apply_style();
                if (rmp::ui::checkbox("Reduce motion", _reduce)) apply_style();
            });
        });

        // The rest is taller than the window, so it scrolls: the wheel, a finger,
        // or the keyboard and the gamepad -- moving the focus to a control the
        // area has clipped away brings it into view.
        rmp::ui::scroll({ .id = "page" }, [this] {
            variants();
            sizes();
            custom_theme_demo();

            // The transition is on colour only — never on position or size. A
            // control is always exactly where it was drawn, so an animation can
            // never make you miss what you were aiming at. Drag the slider and
            // watch the fill above it keep up.
            rmp::ui::panel({ .box = { .grow_x = true } }, [this] {
                rmp::ui::progress(_excitement);
                rmp::ui::slider("Excitement", _excitement, 0.0f, 1.0f);
            });
        });

        rmp::ui::end();
    }

private:
    bool _light = false;
    bool _reduce = false; // the accessibility switch, see apply_style()
    float _excitement = 0.35f;
    int _pressed_count = 0;

    // One place decides what the interface looks like, and it is not spread across
    // the widgets. Everything else in this file just says what things mean.
    void apply_style() {
        rmp::ui::Theme t = _light ? rmp::ui::theme_light() : rmp::ui::theme_dark();

        // "Reduce motion" is a theme field, not a global switch, because the
        // transition is a property of the look and not of the machinery. 0 means
        // every control snaps to its new colour on the frame it changes.
        if (_reduce) t.transition = 0.0f;

        rmp::ui::set_theme(t);
    }

    // -------------------------------------------------------------------
    // The five variants. Read the call sites, not the colours: nothing here names
    // a colour, and that is the point of the whole enum.
    // -------------------------------------------------------------------
    void variants() {
        rmp::ui::panel({ .box = { .grow_x = true } }, [this] {
            rmp::ui::text("Variants", { .size = rmp::ui::Size::LARGE });
            rmp::ui::text(
                "What the button means, not what colour it is.",
                { .color = rmp::ui::ColorRole::MUTED, .size = rmp::ui::Size::SMALL });

            rmp::ui::row({ .grow_x = true }, [this] {
                if (rmp::ui::button("Start game", { .style = rmp::ui::Variant::PRIMARY }))
                    ++_pressed_count;
                if (rmp::ui::button("Load")) ++_pressed_count;
                if (rmp::ui::button("Settings", { .style = rmp::ui::Variant::OUTLINE }))
                    ++_pressed_count;
                if (rmp::ui::button("Back", { .style = rmp::ui::Variant::GHOST }))
                    ++_pressed_count;
                if (rmp::ui::button("Delete", { .style = rmp::ui::Variant::DANGER }))
                    ++_pressed_count;
            });

            // Disabled is a state, not a variant: it can happen to any of them, so
            // it is a flag rather than a sixth entry in the enum.
            rmp::ui::row({ .grow_x = true }, [] {
                rmp::ui::button("Continue",
                                { .style = rmp::ui::Variant::PRIMARY, .enabled = false });
                rmp::ui::button("No save file yet", { .enabled = false });
            });

            rmp::ui::text(
                "Buttons pressed: " + std::to_string(_pressed_count),
                { .color = rmp::ui::ColorRole::MUTED, .size = rmp::ui::Size::SMALL });
        });
    }

    // -------------------------------------------------------------------
    // The three sizes. They are steps in the theme's type scale, so they move
    // together when someone changes the scale and stay in proportion at every
    // window Size — which a hand-picked number would not.
    // -------------------------------------------------------------------
    static void sizes() {
        rmp::ui::panel({ .box = { .grow_x = true } }, [] {
            rmp::ui::text("Sizes", { .size = rmp::ui::Size::LARGE });

            rmp::ui::row({ .grow_x = true }, [] {
                rmp::ui::button("Small", { .size = rmp::ui::Size::SMALL });
                rmp::ui::button("Medium", { .size = rmp::ui::Size::MEDIUM });
                rmp::ui::button("Large", { .size = rmp::ui::Size::LARGE });
            });

            // The padding and the minimum touch height follow the type Size, so a
            // large button is large all over rather than a normal one with bigger
            // letters in it. On a touch screen the small one still never drops
            // below the 44-unit touch target: there, a small button is still a
            // button you hit with a thumb.
            rmp::ui::text(
                "A Size step moves the type, the padding and the minimum height "
                "together.",
                { .color = rmp::ui::ColorRole::MUTED, .size = rmp::ui::Size::SMALL });

            // The same field takes an exact number when you genuinely need one.
            // Reach for it for a title, not for a button.
            rmp::ui::text("CHAPTER ONE", { .size = 44 });
        });
    }

    // -------------------------------------------------------------------
    // Copy-modify-set. A Theme is data, so customising it is not an API — it is
    // assignment. There is no cascade, no selector and no stylesheet to learn.
    // -------------------------------------------------------------------
    void custom_theme_demo() {
        rmp::ui::panel({ .box = { .grow_x = true } }, [this] {
            rmp::ui::text("Your own Theme", { .size = rmp::ui::Size::LARGE });
            rmp::ui::text(
                "Start from one of ours, change what you want, set it back.",
                { .color = rmp::ui::ColorRole::MUTED, .size = rmp::ui::Size::SMALL });

            rmp::ui::row({ .grow_x = true }, [this] {
                if (rmp::ui::button("Gold accent")) {
                    rmp::ui::Theme t = rmp::ui::current_theme();
                    t.primary = GOLD;
                    t.primary_hover = YELLOW;
                    t.primary_press = ORANGE;
                    rmp::ui::set_theme(t);
                }
                if (rmp::ui::button("Sharp corners")) {
                    rmp::ui::Theme t = rmp::ui::current_theme();
                    t.corner_radius = 0;
                    rmp::ui::set_theme(t);
                }
                if (rmp::ui::button("Bigger everything")) {
                    rmp::ui::Theme t = rmp::ui::current_theme();
                    t.font_size *= 1.15f;
                    t.font_size_small *= 1.15f;
                    t.font_size_large *= 1.15f;
                    rmp::ui::set_theme(t);
                }
                if (rmp::ui::button("Reset", { .style = rmp::ui::Variant::GHOST }))
                    apply_style();
            });
        });
    }
};

RMP_GAME(StyleScene);