On this page
  1. The state model
  2. What they have in common
  3. Checkbox
  4. Slider
  5. Dropdown
  6. Text field
    1. Typing is having the keyboard
  7. A settings screen
  8. Widgets you keep
    1. Button
    2. Label
    3. Image
    4. Panel
    5. Checkbox, Slider, Dropdown, TextInput
    6. ProgressBar
  9. Events

Interface controls

Four controls own a value: a checkbox, a slider, a dropdown and a text field. Each one takes your variable, by reference, and writes to it, and that is the whole of their state model — there is no widget object holding a copy, and nothing to keep in sync. For a screen designed widget by widget there are also widgets you keep, each with its own style and callbacks, and every control sends events.

A settings panel: Fullscreen and VSync checked, Subtitles disabled, three volume sliders with percentages, and the Quality dropdown open on Low, Medium, High and Ultra, with Revert and Apply below
Checkboxes, sliders, a dropdown and a text field — examples/ui/04_settings, a frame its e2e script compares exactly.
rmp::ui::begin();
rmp::ui::checkbox("Fullscreen", settings.fullscreen);
rmp::ui::slider("Music", settings.music, 0.0f, 1.0f);
rmp::ui::dropdown("Quality", settings.quality, { "Low", "Medium", "High" });
rmp::ui::text_input("Name", settings.name);
rmp::ui::end();

The state model#

Every control reads your variable and writes it back in the same call, so what is on screen is what is in your struct because it was read this frame. Delete the screen and the settings still exist: they were never the interface's. The struct stays plain data, which is also what makes it easy to save:

#pragma once
// Plain data. This is the whole model, and it is the point of the example: the
// controls take a field of this, by reference, and write to it. Delete the UI
// and your settings still exist; they were never ours.

#include <array>
#include <string>
#include <string_view>

struct Settings {
    bool fullscreen = false;
    bool vsync = true;
    bool subtitles = false;
    float master = 0.8f;
    float music = 0.5f;
    float sensitivity = 0.35f;
    int quality = 1;
    int language = 0;
    std::string player; // "Player" until the player says otherwise: see _ready()
};

// What the two dropdowns offer: a std::array the dropdown takes as a span, so
// nobody has to tell it how long the list is.
inline constexpr std::array<std::string_view, 4> QUALITY{ "Low", "Medium", "High",
                                                          "Ultra" };
inline constexpr std::array<std::string_view, 3> LANGUAGE{ "English", "Espanol",
                                                           "Francais" };

Each control returns true on the frame its value changed, so "do something when it changes" reads the way it sounds:

if (rmp::ui::checkbox("Fullscreen", cfg.fullscreen)) apply(cfg);

The variable stays yours: the control reads it and writes it during the call, and keeps nothing of it afterwards.

What the interface does keep is the state that means nothing to your game — whether a dropdown is open, whether a slider is being dragged — and it keeps it by the control's id, which is its label unless you give it an id. The rules are the ones for buttons, in labels are ids.

What they have in common#

Each control is a row: a slider, a dropdown and a text field put their label on the left and the control after it; a checkbox puts its box first and the label after it. The row fills the width it is offered and is focusable as one thing, so the focus ring goes around the whole row and the keyboard and gamepad reach it like a button (see focus, keyboard and gamepad). The label is drawn at the theme's font_size.

A checkbox, a dropdown and a text field count a click the way a button does: the press has to start on the control and the release end on it. A release alone is not a click, so a drag that starts in the game and ends on a settings panel ticks no box, opens no list and starts no typing. The items of an open dropdown are clicked the same way.

All four options structs have the two fields buttons have:

  • enabled — false draws the control in the theme's disabled colours: its label in disabled_text, and a checkbox's box, a slider's filled part and a text field's field in disabled. A dropdown greys only its label and value; its field keeps the theme's surface. A disabled control cannot be changed, and the focus skips it. A control that is unavailable right now reads better disabled than missing: a menu whose items appear and disappear is a menu nobody can learn.
  • id — a name that is the same control whatever its label says, for two controls that share a label on a screen that changes.

Checkbox#

rmp::ui::checkbox flips a bool when its row is clicked, or when it is pressed — Enter, Space, the bottom face button — while it has the focus. It returns true on the frame it flipped:

if (rmp::ui::checkbox("Light theme", light))
    rmp::ui::set_theme(light ? rmp::ui::theme_light()
                             : rmp::ui::theme_dark());
Simple: a call each frame
rmp::ui::Checkbox light{
    .text = "Light theme",
    .on_change = [](bool on) {
        rmp::ui::set_theme(on ? rmp::ui::theme_light()
                              : rmp::ui::theme_dark());
    },
};

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

The box is filled with the theme's primary and carries a dot when the value is true, and is the theme's surface inside a border when it is false — a difference that reads at a glance without a tick, which the built-in font does not have. Its size is the theme's control_size. CheckboxOptions is enabled and id.

Slider#

rmp::ui::slider sets a float between a minimum and a maximum:

if (rmp::ui::slider("Music", music, 0.0f, 1.0f))
    rmp::audio::set_volume(rmp::audio::Bus::MUSIC, music);
rmp::ui::slider("Sensitivity", sensitivity, 0.0f, 1.0f,
                { .step = 0.05f });
Simple: a call each frame
rmp::ui::Slider music{
    .text = "Music",
    .value = 0.8f,
    .on_change = [](float v) {
        rmp::audio::set_volume(rmp::audio::Bus::MUSIC, v);
    },
};
rmp::ui::Slider sensitivity{ .text = "Sensitivity",
                             .value = 0.35f, .step = 0.05f };

void _draw() override {
    rmp::ui::begin();
    music.show();
    sensitivity.show();
    rmp::ui::end();
}
Full: a widget you keep
  • With the pointer, press on the rail and drag: the value follows where the pointer is along it. The area that takes the press is a little taller than the rail, which is thinner than a finger. While a drag lasts the slider holds the pointer — wants_pointer() stays true — even when the pointer leaves the rail.
  • With the keyboard or a gamepad, while it has the focus, Left and Right — or the d-pad, or the left stick — move it one step per press, then twelve steps a second while held. A step is step, or 5 % of the range when there is none, so a controller can move a slider of any range.

The value is clamped to the range, and snapped to step when there is one, on every call — not only when it is touched. A variable that starts outside the range is corrected on the first frame, and the slider returns true for that frame like for any other change. Nothing is drawn when max is not above min.

SliderOptions:

FieldDefaultMeans
width0Of the rail, in design units; 0 fills the space available.
step00 is continuous; otherwise the value snaps to min plus a multiple of it.
enabledtrueAs above.
show_valuetrueDraw the position after the rail, as a percentage of the range.
idnoneAs above.

The number show_value draws is where the handle is, not the value: a slider from 0 to 60 standing at 30 shows 50%. A screen that wants the value itself turns it off and draws it with text().

rmp::ui::dropdown picks one string of a list. The value it writes is an index into that list:

if (rmp::ui::dropdown("Quality", quality,
                      { "Low", "Medium", "High", "Ultra" }))
    apply_quality(quality);
Simple: a call each frame
rmp::ui::Dropdown quality{
    .text = "Quality",
    .items = { "Low", "Medium", "High", "Ultra" },
    .selected = 1,
    .on_change = [](int index) { apply_quality(index); },
};

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

The list can be written in place, as there, or kept somewhere — a std::array, a std::vector or a plain array of std::string_view — and handed over as it is, because the dropdown takes a std::span of them and the span knows its length. settings.h above keeps its two lists that way.

A click on it opens the list, which floats over whatever is below instead of pushing it down the screen. A click on an item picks it and closes the list; a click anywhere else closes it. The return value is true when a different item was picked. While the list is open it is in front of everything else in the scene: wants_pointer() is true wherever the pointer is, and a click on the list does not reach what is behind it.

A controller has to be able to pick from a list it can open, so the keyboard and the gamepad work the whole of it. While the dropdown has the focus, Enter, Space or the bottom face button opens the list. Open and focused, the list takes the keys that would move the focus — Up and Down, the d-pad, the stick — for itself: they walk a highlight that starts on the current value and stops at the first and the last item, and once the player has used a key it lights up the way an item under the pointer does. Enter, Space or the bottom face button picks the highlighted item and closes the list, returning true if it is a different one; Escape or the right face button closes it and changes nothing.

The index is clamped into the list on every call, so a value read from an old save cannot point past the end of a shorter list. Nothing is drawn for an empty list. The strings are copied as they are drawn, so they only have to live through the call — which is what lets a list written in place be a temporary. DropdownOptions is width — of the field, in design units, 0 filling the space available — enabled and id. Two dropdowns that both have a "Low" in them are still two lists: an item belongs to its dropdown, not to its text.

Text field#

rmp::ui::text_input edits a std::string of yours, in UTF-8:

static std::string name;
rmp::ui::text_input("Name", name,
                    { .placeholder = "your name",
                      .max_length = 16 });
Simple: a call each frame
rmp::ui::TextInput name{ .text = "Name",
                         .placeholder = "your name",
                         .max_length = 16 };

void _draw() override {
    rmp::ui::begin();
    name.show(); // what is typed is name.value
    rmp::ui::end();
}
Full: a widget you keep

While the field is typing — which is not the same as having the focus; see below — what the player types is added at the end, any character but a control character, and Backspace removes the last character, never half of one, repeating while it is held. A blinking _ marks the end of the text. There is no caret to move, no selection and no clipboard. Typing arrives through raylib's GetCharPressed(); the field does not open an on-screen keyboard on Android or iOS.

The return value is true on the frames the text changed. TextInputOptions:

FieldDefaultMeans
width0Of the field, in design units; 0 fills the space available.
enabledtrueAs above.
placeholdernoneShown in the theme's text_muted while the field is empty and not typing. It is copied, so a temporary is fine.
idnoneAs above.
max_length0The most characters it holds; 0 is no limit.

A max_length counts characters, which is what a player counts: an é is one, not the two bytes it takes. Typing stops at it. A string longer than that — one you hand in, or one left by a max_length that came down — is cut to it when the field is drawn, at the end of a character, and the field returns true for that frame like for any other change. An empty label draws the field alone.

Typing is having the keyboard#

A field types only while it has the keyboard, and the keyboard is not the focus. The focus says which control Enter would press; the keyboard says which field the keys are typed into. The focus can rest on a field that is not typing, and a player who reaches a field with a controller can always leave it again.

  • It takes the keyboard when it is clicked, when the focus is moved onto it — by Tab, Up or Down, the d-pad, or rmp::ui::focus() — and when Enter, Space or the bottom face button is pressed while it has the focus. The focus a screen gives its first control by itself is not one of those: a screen whose first control is a name field opens with the field focused and not typing.
  • It gives the keyboard back on Enter or the bottom face button, on Escape or the right face button, on a press of the pointer anywhere else, and when the focus moves on. Space is typed, not taken as Enter. The focus stays on the field, so Enter again starts typing again.
  • Tab, Up and Down leave a field like any other control, and the keyboard goes with the focus: off this field, and into the next one if that is a field too.

While a field is typing, rmp::ui::wants_keyboard() is true, and every rmp::input action reads its keys, gamepad buttons and sticks as not pressed; only its mouse buttons still count. Typing "Wolf" into a name field does not walk the player across the level. A field that has the focus and not the keyboard leaves them alone, so once the player presses Enter the game hears its keys again.

A settings screen#

The settings example puts all four on one screen over the Settings struct above: toggles, two volume sliders that drive the audio buses, a stepped slider, two dropdowns and a name field, with Revert and Apply buttons that are disabled until something changes, and the struct saved with rmp::save when it is applied.

rmp example 04_settings

Widgets you keep#

Everything above is immediate: a call each frame, and nothing to keep. That is the simple way, and the three-function menu stays exactly that. A screen that is designed rather than written — each button with its own look, an icon, its sounds, and something to run when it is clicked, hovered or focused — is easier to read as objects you keep, the way a Godot scene keeps its nodes: members of your scene, set up once with designated initialisers, and shown each frame where they go.

rmp::ui::Button play{
    .text = "Play",
    .variant = rmp::ui::Variant::PRIMARY,
    .style = { .shadow = rmp::ui::Shadow{} },
    .on_click = [this] { change<GameScene>(); },
};
rmp::ui::Button quit{ .text = "Quit", .on_click = [] { rmp::app::quit(); } };

void _draw() override {
    rmp::ui::begin();
    play.show();
    quit.show();
    rmp::ui::end();
}

show() puts the widget in the layout like the immediate call of the same name — between begin() and end(), in a row, a column, a panel — and is drawn by the same code, so it lays out, navigates and is hit-tested exactly as that call is. The callbacks run inside show(), on the frame the thing happened, in the order it happened. A widget that is kept and not shown is not on screen and hears nothing.

  • Every field can change between frames. play.text = "Continue"; and the next show() draws it.
  • It cannot be copied. Its callbacks are rmp::Callbacks, which hold lambdas that capture what they like, a std::unique_ptr too, and cannot be copied; keep the widget where it lives.
  • It remembers what it is doing in ours, a KeptState — hovered, pressed, focused and the shakes not yet started. It is public only so the widget stays a struct you can write with designated initialisers; read it with the methods hovered(), pressed() and focused().
  • shake() shakes it side to side by its style's Motion, plays its shake sound and sends EventKind::SHAKE: "that did not work". It starts at the next show().
  • style is its own style, over everything around it, and id is the same id the immediate calls take.

The callbacks, each one optional — an empty one is not called:

CallbackRuns whenOn
on_clickit was clicked: released on it after a press on it, or Enter or the gamepad's A with the focusButton, Label, Image, Panel
on_hoverthe pointer arrived on itall nine
on_unhover…and leftall nine
on_pressthe pointer went down on itButton, Image, Panel
on_release…and came up, on it or notButton, Image, Panel
on_focusthe keyboard or gamepad focus arrivedButton and the four value controls
on_blur…and leftButton and the four value controls
on_changeits value changed, with the new valueCheckbox (bool), Slider (float), Dropdown (int, the index), TextInput (std::string_view, the text)

A pointer that arrives on a button gives it the focus too, so moving onto one and clicking it runs on_hover, on_focus, on_press, on_release and on_click, over as many frames as the player took. show() also returns what the immediate call would — true on the frame it was clicked, or changed — so if (play.show()) works as well as on_click.

Button#

rmp::ui::Button: text, an icon, or both.

rmp::ui::Button settings{ .text = "Settings",
                          .icon = gear_icon,
                          .icon_size = 24,
                          .on_click = [] { open_settings(); } };
rmp::ui::Button tool{ .icon = gear_icon, .icon_above = true, .id = "tool" };
FieldDefaultIs
text""The label; empty for a button that is only its icon.
iconnoneA picture before the label, or alone.
icon_size0The icon's height in design units; 0 is the label's type size, or the picture's own height when there is no label.
icon_abovefalsetrue puts the icon over the label, as a toolbar tile.
variantNORMALWhat it means, as ButtonOptions::style.
sizethe theme'sA step or design units, as ButtonOptions::size.
width, height0Above 0, a fixed size in design units; 0, sized as button() is.
enabledtruefalse: disabled colours, no press, no focus.
id""Only for two buttons with one label.
styleemptyIts look.
on_click, on_hover, on_unhover, on_press, on_release, on_focus, on_bluremptySee the table above.

show() is true on the frame it was clicked; hovered(), pressed() and focused() say what it is doing now. An icon-only button has no padding and is the size of its picture, as image_button() is.

Label#

rmp::ui::Label is text as text() draws it, with a style of its own — a title in another font, with an outline — and clickable when it has an on_click:

rmp::ui::Label credits{ .text = "Credits",
                        .color = rmp::ui::ColorRole::MUTED,
                        .on_click = [] { rmp::ui::notify("Made with raylib"); } };

Its fields: text; color, a ColorRole used when the style sets no text colour; size and wrap, as TextOptions has them; id; style; and on_click, on_hover, on_unhover. show() is true on the frame it was clicked, and hovered() says whether the pointer is on it.

Image#

rmp::ui::Image is a picture with a style — a frame behind it, a shadow, a tint — that can be clicked. With a sprite that has a sheet, it plays the sprite's animation, one step of the game's clock every show():

rmp::ui::Image coin{ .sprite = rmp::Sprite{ .sheet = rmp::assets::load_sheet("coin.aseprite") },
                     .width = 32,
                     .height = 32 };
coin.sprite.play("spin");
FieldDefaultIs
picturenoneWhat it shows, unless sprite has a texture or a sheet.
spritenoneAn animated picture, advanced every show().
growfalseFill the space available, as ImageOptions::grow.
width, height0Above 0, a fixed size in design units; 0, the picture's own.
enabledtruefalse: never clicked.
id""Its id; images have no label to go by.
styleemptyTint, a skin behind it, a shadow, a border.
on_click, on_hover, on_unhover, on_press, on_releaseemptySee above.

show() is true on the frame it was clicked; hovered() and pressed() say what it is doing.

Panel#

rmp::ui::Panel is a container with a look of its own, style, and a look for everything inside it, children — see a look for everything inside a container. Its show() takes what goes inside it, as panel() does, and it is clickable as a whole when it has an on_click: an inventory slot, a card.

rmp::ui::Panel slot{ .box = { .width = 64, .height = 64 },
                     .id = "slot-1",
                     .style = { .background = DARKGRAY },
                     .on_click = [] { equip(1); } };
slot.show([] { rmp::ui::text("Sword"); });
FieldDefaultIs
rowfalseIts children left to right; false, top to bottom.
boxfitIts layout — sizing, padding, gap, alignment — as panel() takes it.
backgroundtruefalse: a bare row or column, with no fill unless its style gives one, that still styles its children.
id""Its id: one per clickable panel, so each is told apart.
style, childrenemptyIts look, and the look of everything inside it.
on_click, on_hover, on_unhover, on_press, on_releaseemptySee above.

show(body) lays it out with body inside it and is true on the frame it was clicked; hovered() and pressed() say what it is doing.

Checkbox, Slider, Dropdown, TextInput#

The four value controls, each holding its own value instead of writing to yours. Their fields are the immediate control's options, plus the value and the callbacks:

rmp::ui::Checkbox fullscreen{ .text = "Fullscreen", .on_change = [](bool on) { (void)on; } };
rmp::ui::Slider music{ .text = "Music",
                       .value = 0.5f,
                       .step = 0.1f,
                       .on_change = [](float v) {
                           rmp::audio::set_volume(rmp::audio::Bus::MUSIC, v);
                       } };
rmp::ui::Dropdown quality{ .text = "Quality",
                           .items = { "Low", "Medium", "High" },
                           .selected = 1 };
rmp::ui::TextInput name{ .text = "Name", .placeholder = "Your name", .max_length = 16 };
WidgetIts valueIts other fields
Checkboxchecked (false)text, enabled, id, style
Slidervalue (0), kept from min (0) to max (1)text, step, width, show_value, enabled, id, style
Dropdownselected (0), an index into itemstext, width, enabled, id, style
TextInputvalue (""), what is typedtext (its label), placeholder, max_length, width, enabled, id, style

Each has on_change with the new value, on_hover, on_unhover, on_focus and on_blur. Each one's show() is true on a frame its value changed, and hovered() and focused() say what it is doing. What every field means is what the immediate control's option of the same name means, above.

ProgressBar#

rmp::ui::ProgressBar is a bar with a look of its own: the style is the track's — its background, its skin — and fill and fill_picture are the bar's. A nine-slice as the fill stretches with the bar and keeps its ends:

rmp::ui::ProgressBar hp{ .value = health, .width = 200, .fill_picture = bar_art };
hp.show();
FieldDefaultIs
value0From 0 to 1, clamped.
width0In design units; 0 fills the space available.
height-1-1 is from the type size.
radius-1-1 is round ends.
fillnoneThe bar's colour; none is the theme's primary.
fill_picturenoneThe filled part as a picture, tinted by the style's tint.
id, styleAs every other.
on_hover, on_unhoveremptyA bar is never pressed.

show() returns nothing, and hovered() says whether the pointer is on it: what a bar's tooltip needs.

Events#

Everything a control does, as it happens, is an Event: the pointer arriving and leaving, a press, a click, the focus coming and going, a value changing. The sounds of the theme and of a style play on these; on_event() is one hook for everything else — a vibration on a phone, a particle, a counter for achievements. Every control sends them, the ones written as rmp::ui::button("Play") too:

rmp::ui::on_event([](const rmp::ui::Event &e) {
    if (e.kind == rmp::ui::EventKind::CLICK) ++clicks;
});

An event has three fields: kind, what happened; widget, a WidgetKind — BUTTON, LABEL, IMAGE, PANEL, CHECKBOX, SLIDER, DROPDOWN, TEXT_INPUT or PROGRESS; and name, the control's label, or its id when it has one, valid during the call only.

EventKindWhen
HOVERthe pointer arrived on it
UNHOVER…and left
PRESSthe pointer went down on it
RELEASEthe pointer came up after a PRESS, on it or not
CLICKit was pressed: released on it, or Enter or the A button with the focus
FOCUSthe keyboard or gamepad focus arrived
BLUR…and left
CHANGEa checkbox, slider, dropdown or text field changed its value
OPENa dropdown opened its list
CLOSE…and closed it
SHAKEa kept widget's shake() started

An event's sound plays first, then the hook. There is one hook: setting it again replaces it, and rmp::ui::on_event({}) turns it off. A kept widget's own callbacks run in its show(), after the hook has heard the same thing.