On this page
  1. The frame begin() opens
  2. Sizing: fit, grow, fixed
    1. Filling the window
  3. Gap, padding and where children sit
    1. Spacers
  4. Panels
  5. Centring and layers
  6. Grids
  7. Scrolling
  8. Images and progress bars
  9. Naming a container
  10. A whole layout

Interface layout

Everything inside begin() and end() arranges itself in a centred column, which is the right answer for a menu and the wrong one for anything with structure. Containers are how you get structure: they nest, they take their contents as a lambda, and every size they take is in design units, so a layout drawn for the [window] resolution keeps its proportions on any screen.

A column of cards: an inventory header reading 24 / 40, a rabbit card with two bars, a card that says PAUSED, one that says nothing here, and a Done button
Rows, columns and cards — examples/ui/02_layout
A wide layout: a sidebar of sections with Bestiary selected, and a grid of sixteen items with progress bars beside it
One layout that changes with the window — examples/ui/07_responsive
rmp::ui::begin();
rmp::ui::panel([&] {
    rmp::ui::text("Really quit?");
    rmp::ui::row({ .grow_x = true }, [&] {
        if (rmp::ui::button("Yes")) quit();
        rmp::ui::spacer();
        rmp::ui::button("No");
    });
});
rmp::ui::end();

The contents are a lambda, and that is the reason there is no closing call to forget: the container closes when the lambda returns. Capture with [&] when the body needs your variables, which is most of the time.

ContainerWhat it does
rowIts children left to right.
columnIts children top to bottom.
panelA column with a background, padding and rounded corners: a dialog, a card.
centerFills the space it is given and puts its contents in the middle of it.
stack and layerLayers in the same box, the later ones on top.
grid and cellEqual columns, as many rows as it takes.
scrollClips its contents and scrolls them.
spacerNot a container: it eats the space left over, or leaves a gap of a given size.

The frame begin() opens#

begin() opens two boxes before anything of yours: a root that fills the screen, and one column inside it that everything you declare goes into. The column is as big as its contents, and the root places it — in the middle, unless you say otherwise. FrameOptions says otherwise:

FieldDefaultMeans
placementAlign::CENTERWhere the column sits on the screen.
gapthe theme's gapBetween the things in the column.
paddingthe theme's panel_paddingKept clear around the edge of the screen.
growfalsetrue: the column fills the window, less the padding, instead of fitting what is in it. See filling the window.
focus_firstunsetWhether the first control takes the focus by itself. Unset, the framework decides: yes, unless the scene drawing it is a game being played. See a button in the HUD.

The screen, here, is the game's area. Under [window] stretch "letterbox" that is the design inside its bars, and what the frame lays out keeps clear of them as it keeps clear of a phone's notch; see scale and design units.

Align has the nine values you would guess, a three-by-three grid of the vertical (TOP, CENTER, BOTTOM) crossed with the horizontal (LEFT, CENTER, RIGHT): TOP_LEFT, TOP_CENTER, TOP_RIGHT, CENTER_LEFT, CENTER, CENTER_RIGHT, BOTTOM_LEFT, BOTTOM_CENTER and BOTTOM_RIGHT. A HUD in the corner is a placement and a smaller gap:

rmp::ui::begin({ .placement = rmp::ui::Align::TOP_LEFT, .gap = 4 });
rmp::ui::text("Score " + std::to_string(score));
rmp::ui::text("Lives " + std::to_string(lives), { .color = rmp::ui::ColorRole::MUTED });
rmp::ui::end();

On Android and iOS the root also keeps 24 design units clear at every edge, for the status bar and the notch.

Inside a scene, _draw() can hold more than one pair — the score in one corner, a button in another — and they are drawn as parts of the same frame. In a game that runs its own loop, keep to one pair per frame.

Sizing: fit, grow, fixed#

Each axis of a box is sized one of three ways, and there is no fourth:

SizingWrittenMeans
Fitnothing: the defaultAs big as its contents need.
Growgrow_x, grow_yFill the space its parent offers on that axis — along a row or a column, shared with the siblings that grow too.
Fixedwidth, height above 0That many design units, whatever else is set.

A box that fits its contents, holding children that grow into it, is the pairing that makes the buttons of a menu the same width: the column is as wide as its widest button, and every other button grows to match.

The fields are interleaved by axis — grow_x, width, grow_y, height — and not grouped by kind, because designated initialisers have to be written in declaration order. This way round the most ordinary thing anyone writes, a sidebar of a fixed width that fills the height, is legal:

rmp::ui::panel({ .box = { .width = 240, .grow_y = true } }, [&] {
    rmp::ui::text("Sections");
});

Filling the window#

Grow fills the parent, and the column that begin() opens fits its contents. So grow_x on something directly inside it fills the width of that column — the width of the widest thing next to it — and not the width of the screen. A menu wants exactly that, which is why it is the default.

A screen laid out edge to edge — a sidebar beside a content area, a list as tall as the window — asks for the other kind of frame. begin({ .grow = true }) makes the column fill the window, less the padding, on both axes, so whatever grows inside it reaches the edges:

rmp::ui::begin({ .grow = true });
rmp::ui::row({ .grow_x = true, .grow_y = true }, [&] {
    rmp::ui::panel({ .box = { .width = 200, .grow_y = true } }, [&] {
        rmp::ui::text("Sections");
    });
    rmp::ui::panel({ .box = { .grow_x = true, .grow_y = true } }, [&] {
        rmp::ui::text("World");
    });
});
rmp::ui::end();

What does not grow is still placed by placement: in a frame opened with { .placement = rmp::ui::Align::TOP_CENTER, .grow = true }, a line of text sits at the top in the middle, and a row that grows beneath it takes the rest. Without grow, the frame is the one the three-function menu is drawn in. The inventory and responsive examples are both laid out this way.

Inside a frame that fits, a bar that has to be a given size gets that size: a fixed width or height, in design units, which the scale turns into pixels with everything else. Everything that grows inside it then has something to grow into:

rmp::ui::begin({ .placement = rmp::ui::Align::TOP_CENTER });
rmp::ui::row({ .width = 760 }, [&] {
    rmp::ui::text("Inventory");
    rmp::ui::spacer();
    rmp::ui::text("24 / 40", { .color = rmp::ui::ColorRole::MUTED });
});
rmp::ui::end();

A fixed width is a fixed number of design units: on a window wider than the [window] resolution's shape, there is room left at the sides, because the scale follows the tighter axis.

Gap, padding and where children sit#

Rows, columns and panels take BoxOptions — a panel inside its .box — and three of its fields decide the space around things:

FieldDefaultMeans
gapthe theme's gap, 12Between children, in design units.
paddingnone; a panel's is the theme's panel_padding, 20Inside the container, the same on all four sides.
itemsAlign::CENTERWhere the children sit in the space left over.

items is the one that makes a card look like a card: a column of text is centred unless it is told to hug the left.

rmp::ui::column({ .gap = 6, .items = rmp::ui::Align::CENTER_LEFT }, [&] {
    rmp::ui::text("Rabbit", { .color = rmp::ui::ColorRole::PRIMARY });
    rmp::ui::text("Level 7", { .color = rmp::ui::ColorRole::MUTED, .size = 14 });
});

Spacers#

spacer() grows on both axes and eats the space left over, which is how two things are pushed to opposite ends of a row. The row has to have space left over for it to eat, so it is given a width or set to grow:

rmp::ui::panel({ .box = { .width = 300 } }, [&] {
    rmp::ui::row({ .grow_x = true }, [&] {
        rmp::ui::text("Health");
        rmp::ui::spacer(); // shoves what follows to the right
        rmp::ui::text("999");
    });
});

spacer(n) is the other one: a fixed box of n by n design units, for breathing room in one place without changing the container's gap.

Panels#

A panel is a column with a background, which is what makes it visible, padding of the theme's panel_padding, and the theme's corner_radius. PanelOptions holds the layout in .box and the look after it:

FieldDefaultMeans
box{}The layout, as a column takes it, except that its padding defaults to the theme's panel_padding.
backgroundthe theme's panelThe fill. { 0, 0, 0, 0 } means the theme's.
radiusthe theme's corner_radiusOf the corners, in design units; 0 is square.
bordernoneAn outline colour. Left unset, the theme's border is drawn only when the theme's border_width is above 0 — in the light theme, not in the dark one.
border_width1The width of an outline you set, in design units.
rmp::ui::panel({ .box = { .width = 260, .height = 70 },
                 .border = rmp::ui::current_theme().border },
               [&] {
                   rmp::ui::center([&] {
                       rmp::ui::text("nothing here",
                                     { .color = rmp::ui::ColorRole::MUTED });
                   });
               });
Simple: a call each frame
rmp::ui::Panel empty{
    .box = { .width = 260, .height = 70 },
    .style = { .border = GRAY, .border_width = 1 },
};

void _draw() override {
    rmp::ui::begin();
    empty.show([&] {
        rmp::ui::center([&] {
            rmp::ui::text("nothing here",
                          { .color = rmp::ui::ColorRole::MUTED });
        });
    });
    rmp::ui::end();
}
Full: a widget you keep

In the framework's themes a panel sits on a soft shadow, and fades and rises into place the first frame it is drawn — see styles for that look and how to take it away. A kept Panel can also be clicked, and styles everything inside it: Panel.

Centring and layers#

center() grows on both axes and puts its contents in the middle of what it grew into — so it centres within its parent, and its parent decides how much room that is: a panel of a fixed size, a grid cell.

stack() puts things on top of each other. Every child of a stack has to be a layer(); each layer fills the stack, centres what is in it, and is drawn over the layers written before it:

rmp::ui::panel({ .box = { .width = 260, .height = 90 } }, [&] {
    rmp::ui::stack([&] {
        rmp::ui::layer([&] {
            rmp::ui::image(portrait, { .grow = true, .tint = CLITERAL(Color){ 255, 255, 255, 60 } });
        });
        rmp::ui::layer([&] {
            rmp::ui::text("PAUSED", { .color = rmp::ui::ColorRole::DANGER });
        });
    });
});

The explicit layer() is not ceremony: it is what tells the layout which things are meant to overlap and which are ordinary children. And because layers overlap, they take no room of their own — a stack is as big as the space its parent gives it, so it goes inside something with a size, like the panel above. In a box that only fits its contents, a stack has no size at all and its layers spill over whatever comes next.

Grids#

A grid lays its items out in equal columns and starts a new row when one fills. Each item goes in a cell(), which is what lets the grid count them; the layout engine underneath wraps text, not boxes, so the rows are built as real rows:

rmp::ui::grid(4, [&] {
    for (auto &item : inventory)
        rmp::ui::cell([&] { rmp::ui::image(item.icon); });
});

Every cell in a row is the same width, its share of the row, and what is in it is centred. A cell() outside a grid is a plain box, so the mistake shows on screen instead of breaking the frame.

With columns at 0 the grid works out how many cells of min_cell design units, each with its gap, fit in the width it had last frame, and works it out again as the window changes. An inventory then reflows on a phone held upright instead of staying at the number someone typed on a desktop. This is also the row that wraps: there is no wrap flag on row(), because grid({ .columns = 0 }) is one.

rmp::ui::panel({ .box = { .width = 480 } }, [&] {
    rmp::ui::grid({ .columns = 0, .min_cell = 88, .id = "inventory" }, [&] {
        for (const std::string_view name : names)
            rmp::ui::cell([&] { rmp::ui::text(name, { .size = 13 }); });
    });
});

It is never fewer than one column and never more than fit. A grid too narrow for even one cell of min_cell is one column, so the narrower it gets, the fewer columns it has, down to one. Its first frame, with no width measured yet, is one column too, and it has its real count from the second.

GridOptions:

FieldDefaultMeans
columns0How many; 0 is as many as fit, and at least one.
min_cell96The width a column needs, when columns is 0.
gapthe theme's gapBetween cells and between rows.
paddingnoneInside the grid.
grow_xtrueTake the width it is offered.
grow_yfalseFill the parent's height instead of fitting the rows.
idnoneWhich grid it is, from frame to frame. See below.

To work out its columns, a grid finds its own width from last frame, with or without a name. A grid with an id finds itself by that name; one without is found by its order among the unnamed grids of the same begin()/end(), and a grid that comes and goes before it can make it borrow another's width. Name the ones that share a screen with others that come and go.

Scrolling#

scroll() clips its contents to its own box and scrolls them: with the mouse wheel, or by dragging inside it — with a finger on a phone, with the mouse on a desktop. It stops at the end of the contents. Neither gesture needs a line of code:

// The list. scroll() clips its contents; the wheel and a dragging finger both
// move it, and neither needs a line of code here. Each item says what it is
// with tooltip().
#include "inventory.h"

#include <rmp/ui.h>

#include <string>

void item_list() {
    rmp::ui::panel({ .box = { .width = 240, .grow_y = true } }, [&] {
        rmp::ui::text("All items");

        rmp::ui::scroll({ .gap = 4, .id = "list" }, [&] {
            for (int i = 0; i < ITEM_COUNT; ++i) {
                // Two buttons could share a label across a long list, so the
                // ids are made explicit. Identical labels in one frame are told
                // apart automatically; this is for when you want the identity
                // to survive the list being reordered.
                const std::string id = "item" + std::to_string(i);
                if (rmp::ui::button(items[i].name, { .id = id })) {
                    selected = i;
                }
                // About the button just above, and only that: shown when the
                // pointer rests on it for half a second, or at once when the
                // keyboard or a gamepad walks onto it. To the left, over the
                // grid, where it hides no other item of the list.
                rmp::ui::tooltip(items[i].description,
                                 { .side = rmp::ui::Align::CENTER_LEFT });
            }
        });

        rmp::ui::text("scroll me", { .color = rmp::ui::ColorRole::MUTED, .size = 12 });
    });
}

How big the area is follows the sizing rules. By default it grows on both axes: it takes the room its parent gives it and clips there. The list above is in a panel that grows to the height of the example's frame, which fills the window, and the area takes what the panel has left once its two lines of text are in. Its rows never push a parent past the window: where everything around it only fits its contents, the area is squeezed to the room the window has left, and clips there. height makes it a window of a fixed size onto a longer list:

rmp::ui::scroll({ .height = 200, .id = "levels" }, [&] {
    for (int i = 1; i <= level_count; ++i) {
        if (rmp::ui::button("Level " + std::to_string(i))) play_level(i);
    }
});

ScrollOptions:

FieldDefaultMeans
horizontalfalseClip and scroll left to right.
verticaltrueClip and scroll top to bottom.
gapthe theme's gapBetween children.
paddingnoneInside the area.
grow_x, widthtrue, 0Fill the parent's width, or a fixed one.
grow_y, heighttrue, 0Fill the parent's height, or a fixed one.
idnoneWhich area it is, from frame to frame.

Inside the area, children are laid out top to bottom whichever way it scrolls; a strip that scrolls sideways is a row() inside a horizontal area. How far an area has scrolled is remembered by the area, and an area without an id is told apart by its order among the unnamed ones, as a grid is — so one that shares a screen with areas that come and go gets a name.

What is scrolled out of view cannot be clicked: only the part of a control inside the area takes the pointer, and the area itself — its empty part included — is the interface's, so a drag inside it scrolls the list instead of reaching the game.

The keyboard and the gamepad scroll it too, by moving the focus. When the focus lands on a control the area has clipped away, and its ring is showing, the area moves just far enough to show the control — and a list inside a scrolling page shows its row first, then the page shows the list. It happens once for each move of the focus, so the wheel or a finger can take the list somewhere else afterwards without being pulled back to it.

Images and progress bars#

rmp::ui::image draws a texture. With no options it is drawn at its own size, its pixels taken as design units, so an icon grows with the rest of the interface instead of shrinking into nothing on a big screen. ImageOptions changes that:

FieldDefaultMeans
growfalseFill the space available on both axes; the picture is stretched to it.
width, height0A size in design units; 0 is the texture's own.
tintWHITEMultiplies the picture's colours; WHITE leaves them as they are.
rmp::ui::image(portrait, { .width = 96, .height = 96 });
Simple: a call each frame
rmp::ui::Image face{ .picture = portrait,
                     .width = 96, .height = 96,
                     .on_click = [] { TraceLog(LOG_INFO, "hi"); } };

void _draw() override {
    rmp::ui::begin();
    face.show();
    rmp::ui::end();
}
Full: a widget you keep

What it draws is any rmp::ui::Picture: an rmp::Texture, a part of one, the frame an rmp::Sprite shows, a slice of an Aseprite file, a nine-slice. Pictures says how each is made. A picture holds counted handles, so the texture stays loaded until end() has drawn the frame, and a temporary is safe:

rmp::ui::image(rmp::assets::load_texture("icon.png"), { .width = 40, .height = 40 });

A raylib Texture2D is taken too, for a texture the game made itself — a render texture, an image it generated. It is copied on the way in, but what is copied is a handle to something on the GPU, so that texture has to stay loaded until end() has drawn the frame:

rmp::ui::image(minimap.texture, { .width = 160, .height = 160 });

rmp::ui::progress is a bar, filled to a fraction from 0 to 1 — clamped, so a health of -5 draws empty rather than going wrong:

rmp::ui::progress(health, { .width = 180 });
rmp::ui::progress(mana, { .width = 180, .height = 8,
                          .fill = SKYBLUE });
Simple: a call each frame
rmp::ui::ProgressBar health_bar{ .width = 180 };
rmp::ui::ProgressBar mana_bar{ .width = 180, .height = 8,
                               .fill = SKYBLUE };

void _draw() override {
    health_bar.value = health;
    mana_bar.value = mana;
    rmp::ui::begin();
    health_bar.show();
    mana_bar.show();
    rmp::ui::end();
}
Full: a widget you keep

ProgressOptions: width (0 fills the space available), height (by default 0.6 of the theme's type size), radius (by default half the height, for round ends), fill and track (the theme's primary and surface unless set), and id.

A bar drawn by the artist — a filled part that is a picture, its ends kept round by a nine-slice — is a kept rmp::ui::ProgressBar with a fill_picture; see widgets you keep.

Naming a container#

Containers are anonymous unless you give them an id — BoxOptions::id, and the same field on a panel's .box, a grid, a scroll area and a progress bar. A named one is the same element from frame to frame, whatever else is on screen. Nothing needs it for a row, a column or a panel; it is what a test uses to find one, and for grids and scroll areas it is their identity, as above.

rmp::ui::panel({ .box = { .id = "inventory" } }, [&] {
    rmp::ui::text("Inventory");
});

A whole layout#

examples/ui/02_layout puts most of this page on one screen — a toolbar with a spacer, a card of a row and a column, a stack, a centred placeholder — with bars that move so it is clear the layout is rebuilt every frame:

// ---------------------------------------------------------------------------
// examples/ui/02_layout/src/main.cpp
//
// Containers: row, column, panel, center, stack, spacer — plus image and
// progress, the two widgets that only make sense once you have somewhere to
// put them.
//
// The rule that shapes all of it: a container takes its contents as a lambda.
// There is no closing call to forget, because the compiler closes it for you.
// Capture with [&] when the body needs your variables, which is most of the
// time.
//
// Every measurement below is in DESIGN UNITS, at the [window] resolution from
// raylib_multiplatform.toml. rmp::ui multiplies them by the current scale, so
// `width = 200` is 200 at your design Size, 400 at twice it, and never a
// hard-coded pixel count that looks right on exactly one monitor.
//
// Run it: `rmp example 02_layout`.
// ---------------------------------------------------------------------------

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

#include <string>

class LayoutScene : public rmp::Scene {
public:
    void _ready() override {
        // Resize the window and watch everything follow.
        _portrait = rmp::assets::load_texture("rabbit.png");
    }

    void _update(float delta) override {
        // Bars that move, so the layout is doing something rather than sitting
        // still -- and so it is obvious the numbers are read fresh every frame.
        _health -= delta * 0.05f;
        if (_health < 0) _health = 1.0f;
        _mana += delta * 0.08f;
        if (_mana > 1) _mana = 0.0f;
    }

    void _draw() override {
        // begin() is a centred column. placement moves the whole thing: this one
        // hugs the top-left corner, like a HUD would.
        rmp::ui::begin({ .placement = rmp::ui::Align::TOP_LEFT, .gap = 14 });

        toolbar();
        character_card();
        banner();
        empty_slot();

        if (rmp::ui::button("Done")) rmp::app::quit();

        rmp::ui::end();
    }

private:
    // An rmp::Texture, not a Texture2D. The handle OWNS the texture; a raw
    // Texture2D copied out of the temporary would have been unloaded on the
    // same line, before the next statement ran.
    rmp::Texture _portrait;
    float _health = 0.72f;
    float _mana = 0.35f;

    // -------------------------------------------------------------------
    // row and column: the two directions
    // -------------------------------------------------------------------
    void character_card() {
        // panel = a column with a background and padding. It is what you put
        // a dialog, a card or a tooltip inside.
        rmp::ui::panel([&] {
            // row lays out left to right.
            rmp::ui::row({ .gap = 16 }, [&] {
                rmp::ui::image(_portrait, { .width = 72, .height = 72 });

                // column lays out top to bottom. items decides where children
                // sit across the other axis -- here, hard against the left
                // instead of centred, which is what makes a card look like a
                // card.
                rmp::ui::column({ .gap = 6, .items = rmp::ui::Align::CENTER_LEFT }, [&] {
                    rmp::ui::text("Rabbit", { .color = rmp::ui::ColorRole::PRIMARY });
                    rmp::ui::progress(_health, { .width = 180 });
                    rmp::ui::progress(_mana,
                                      { .width = 180, .height = 8, .fill = SKYBLUE });
                    rmp::ui::text("Level 7",
                                  { .color = rmp::ui::ColorRole::MUTED, .size = 14 });
                });
            });
        });
    }

    // -------------------------------------------------------------------
    // spacer: how you push things apart
    // -------------------------------------------------------------------
    static void toolbar() {
        // grow_x makes the row take the full width it is offered; without it
        // the row would shrink to fit its contents and the spacer would have
        // nothing to eat.
        rmp::ui::row({ .grow_x = true }, [&] {
            rmp::ui::text("Inventory");
            rmp::ui::spacer(); // <- takes everything left over
            rmp::ui::text("24 / 40", { .color = rmp::ui::ColorRole::MUTED });
        });

        // spacer(n) is the other one: a fixed gap, when you want breathing
        // room in one specific place rather than changing the container's gap.
        rmp::ui::spacer(8);
    }

    // -------------------------------------------------------------------
    // stack and layer: things on top of each other
    // -------------------------------------------------------------------
    void banner() {
        rmp::ui::panel({ .box = { .width = 260, .height = 90 } }, [&] {
            // Every child of a stack has to be a layer(). That is not
            // ceremony: it is what says "these are meant to overlap" instead
            // of "these are ordinary children in a column". Later layers draw
            // on top.
            rmp::ui::stack([&] {
                rmp::ui::layer([&] {
                    rmp::ui::image(
                        _portrait,
                        { .grow = true, .tint = CLITERAL(Color){ 255, 255, 255, 60 } });
                });
                rmp::ui::layer([&] {
                    rmp::ui::text("PAUSED", { .color = rmp::ui::ColorRole::DANGER });
                });
            });
        });
    }

    // -------------------------------------------------------------------
    // center: fill what you were given, put the contents in the middle
    // -------------------------------------------------------------------
    static void empty_slot() {
        rmp::ui::panel({ .box = { .width = 260, .height = 70 },
                         .border = rmp::ui::current_theme().border },
                       [&] {
                           rmp::ui::center([&] {
                               rmp::ui::text("nothing here",
                                             { .color = rmp::ui::ColorRole::MUTED });
                           });
                       });
    }
};

RMP_GAME(LayoutScene);

// ---------------------------------------------------------------------------
// The Sizing model, in three lines
// ---------------------------------------------------------------------------
//
//   default        fit — as big as the contents need
//   grow_x/grow_y  fill the space the parent is offering
//   width/height   a fixed number of design units, overriding both
//
// A container fits its contents; a child with grow expands to fill whatever
// the container ended up being. That pairing is what makes the buttons in a
// menu come out the same width without anyone measuring anything.
//
// NAMING A CONTAINER. Every container takes an optional `.id`: a stable name,
// the same element from frame to frame whatever else is on screen.
//
//     rmp::ui::panel({ .box = { .id = "inventory" } }, [&]{ ... });
//
// Unnamed containers are anonymous, which is right for almost all of them.