On this page
  1. Run it
  2. Keys
  3. The code

Inventory

grid with a column count worked out from the space available, scroll with clipping, tooltip() for what each item is -- under a resting pointer or the keyboard's focus -- and wants_pointer() keeping the game's hands off the UI's clicks. One panel per file under src/panels/.

Inventory, as it starts

Run it

From a clone of the framework:

rmp example 05_inventory

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.

ActionKeys
useleft mouse button

The code

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

src/main.cpp 89 lines
// ---------------------------------------------------------------------------
// examples/ui/05_inventory/src/main.cpp
//
// grid and scroll: an inventory beside a scrolling list, both of them behaving
// when the window changes size -- which is the only reason either exists.
//
// The two ideas worth taking away:
//
//   * grid(0, ...) works out its own column count from the space it has, and
//     works it out again when that changes. A grid with a hard-coded 6 columns
//     is a grid that is wrong on a phone in portrait.
//   * scroll() clips and scrolls with the wheel or with a finger. Same gesture,
//     same code, no branch on platform.
//
// One panel per file under src/panels/; the item table is in include/.
// Run it: `rmp example 05_inventory`.
// ---------------------------------------------------------------------------

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

#include "inventory.h"

class InventoryScene : public rmp::Scene {
public:
    void _ready() override {
        // The whole point: resize the window.
        _icon = rmp::assets::load_texture("rabbit.png");
        // What a click means to the game, by name.
        rmp::input::action("use", MOUSE_BUTTON_LEFT);
    }

    void _update(float) override {
        // The UI is reading the pointer, so the game must not act on the same
        // click: picking an item must not also swing the sword. An action asked
        // for by name already knows -- it is quiet while the UI wants the
        // pointer -- so there is no `if (!rmp::ui::wants_pointer())` to
        // remember here.
        if (rmp::input::just_pressed("use")) {
            TraceLog(LOG_INFO, "GAME: click in the world");
        }
    }

    void _draw() override {
        // grow: the frame is the whole window, so the row below can fill it.
        // Without it begin() is a column that fits its content -- the right
        // thing for a menu -- and a growing row would fill that column and no
        // more.
        rmp::ui::begin({ .grow = true });
        rmp::ui::row({ .gap = 12, .grow_x = true, .grow_y = true }, [&] {
            inventory_grid(_icon);
            item_list();
        });
        rmp::ui::end();
    }

private:
    // An rmp::Texture and not a Texture2D: the handle owns the texture, and the
    // scene owns the handle, so it goes when the scene does -- before the
    // window closes. Copying it into a raw Texture2D would have destroyed the
    // only owner on the same line.
    rmp::Texture _icon;
};

RMP_GAME(InventoryScene);

// ---------------------------------------------------------------------------
// Notes
// ---------------------------------------------------------------------------
//
// WHY cell() EXISTS. The layout engine underneath wraps text, not elements, so
// a grid has to be built as real rows. cell() is what lets the grid count its
// items and start a row at the right moment. It is the same bargain as
// stack()/layer(): one extra call, in exchange for the layout knowing what you
// meant instead of guessing.
//
// SCROLL AND SIZE. A scroll area grows by default, so it takes the height its
// parent gives it and clips there: here, what the panel has left once its two
// lines of text are in. Its rows never push a parent past the window -- inside
// one that fits its contents, it is squeezed to the room that is left and
// clips there. Give it a height when you want a particular one.
//
// PERFORMANCE. Every item is rebuilt every frame, and that is fine: this is
// twelve, and the layout engine is measured in microseconds. A list long
// enough to matter would want to draw only the rows in view, and rmp::ui has
// no call yet that tells you where the area is or how far it has scrolled.
include/inventory.h 34 lines
#pragma once
#include <rmp/assets.h>

#include <array>
#include <string_view>

struct Item {
    std::string_view name;
    int count;
    bool equipped;
    std::string_view description; // the list's tooltip
};

inline constexpr int ITEM_COUNT = 12;
inline std::array<Item, ITEM_COUNT> items{ {
    { "Sword", 1, true, "Sharp at one end. Equipped." },
    { "Shield", 1, false, "Stops what the sword does not." },
    { "Potion", 12, false, "Twelve second chances." },
    { "Rope", 3, false, "Thirty feet, more or less." },
    { "Torch", 8, false, "Light for a cave, or for a fuse." },
    { "Map", 1, false, "Of somewhere else, unfortunately." },
    { "Key", 2, false, "Two keys, no doors yet." },
    { "Bread", 5, false, "Restores a little, tastes of a lot." },
    { "Coin", 240, false, "Spend it, or count it." },
    { "Gem", 4, false, "Worth a shop each." },
    { "Bow", 1, false, "Useless without the arrows." },
    { "Arrow", 60, false, "Useless without the bow." },
} };
inline int selected = 0;

// One panel per file under src/panels/. The grid draws each item with `icon`,
// which the scene owns.
void inventory_grid(const rmp::Texture &icon);
void item_list();
src/panels/grid.cpp 38 lines
// The grid. Every item goes in a cell(), which is what lets the grid count
// them and start a new row at the right moment.
#include "inventory.h"

#include <rmp/ui.h>

#include <string>

void inventory_grid(const rmp::Texture &icon) {
    rmp::ui::panel({ .box = { .grow_x = true, .grow_y = true } }, [&] {
        rmp::ui::text("Inventory");

        // columns = 0: fit as many 88-unit cells as the width allows, and at
        // least one. The grid measures the width it had last frame; the id
        // keeps it the same grid whatever else comes and goes on screen, where
        // an unnamed one is told apart by its order among the unnamed grids.
        rmp::ui::grid({ .columns = 0, .min_cell = 88, .id = "inv" }, [&] {
            for (int i = 0; i < ITEM_COUNT; ++i) {
                rmp::ui::cell([&] {
                    rmp::ui::panel(
                        { .box = { .padding = 6 },
                          .background = (i == selected)
                              ? rmp::ui::current_theme().surface_hover
                              : rmp::ui::current_theme().surface },
                        [&] {
                            rmp::ui::image(icon, { .width = 40, .height = 40 });
                            rmp::ui::text(items[i].name, { .size = 13 });
                            if (items[i].count > 1) {
                                rmp::ui::text(
                                    "x" + std::to_string(items[i].count),
                                    { .color = rmp::ui::ColorRole::MUTED, .size = 12 });
                            }
                        });
                });
            }
        });
    });
}
src/panels/list.cpp 35 lines
// 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 });
    });
}