On this page
  1. Run it
  2. The code

Responsive

scale() versus current_breakpoint(), and which to reach for. A sidebar that becomes a top strip when the window is taller than it is wide. Resize it: it takes the whole window with set_stretch(Stretch::NONE), where the other examples letterbox.

Responsive, as it starts

Run it

From a clone of the framework:

rmp example 07_responsive

The code

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

src/main.cpp 173 lines
// ---------------------------------------------------------------------------
// examples/ui/07_responsive/src/main.cpp
//
// Two mechanisms, and knowing which one to reach for is most of the skill.
//
//   scale()             makes everything BIGGER or smaller, together. It is
//                       automatic, derived from [window] and the real window,
//                       and you almost never touch it.
//   current_breakpoint() changes the SHAPE of the layout. It is manual, and it
//                       is for the one thing scale cannot do: no combination of
//                       grow, fit and fixed turns a row into a column.
//
// The rule of thumb: if you are reaching for a breakpoint to pick a size, stop
// — scale already did that for you, and hard-coding around it is how a layout
// ends up looking right on exactly one machine. Reach for it when the layout
// has to become a different layout.
//
// Resize the window and watch the sidebar move under the content when the
// window becomes taller than it is wide. That is a phone held upright.
//
// Run it: `rmp example 07_responsive`.
// ---------------------------------------------------------------------------

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

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

namespace {

constexpr std::array SECTIONS{ "World", "Bestiary", "Journal", "Crafting", "Map" };
constexpr int SECTION_COUNT = static_cast<int>(SECTIONS.size());

std::string_view breakpoint_name() {
    switch (rmp::ui::current_breakpoint()) {
        case rmp::ui::Breakpoint::COMPACT:
            return "compact";
        case rmp::ui::Breakpoint::MEDIUM:
            return "medium";
        case rmp::ui::Breakpoint::EXPANDED:
            return "expanded";
    }
    return "?";
}

} // namespace

class ResponsiveScene : public rmp::Scene {
public:
    void _ready() override {
        // The whole window, whatever its shape -- resize it, that is the
        // point. The examples' .toml letterboxes the game's area to the
        // design's 16:9 ([window] stretch), and in that area the breakpoint
        // never changes -- which is this example's point.
        rmp::app::set_stretch(rmp::app::Stretch::NONE);
    }

    void _draw() override {
        // grow: the frame is the whole window, so the sidebar and the content
        // below can take all of it. Without it begin() fits its content, which is
        // a menu's shape and not a screen's.
        rmp::ui::begin(
            { .placement = rmp::ui::Align::TOP_CENTER, .padding = 12, .grow = true });

        // A status line, so the breakpoint is visible while you drag the window.
        // Nothing in a real game would draw this.
        rmp::ui::row({ .grow_x = true }, [] {
            rmp::ui::text(
                std::string("Breakpoint: ").append(breakpoint_name()),
                { .color = rmp::ui::ColorRole::PRIMARY, .size = rmp::ui::Size::SMALL });
            rmp::ui::spacer();
            rmp::ui::text(
                std::to_string(GetScreenWidth()) + "x" +
                    std::to_string(GetScreenHeight()) + "   scale " +
                    std::to_string(rmp::ui::scale()).substr(0, 4),
                { .color = rmp::ui::ColorRole::MUTED, .size = rmp::ui::Size::SMALL });
        });

        // THE WHOLE EXAMPLE IS THESE SEVEN LINES. Two arrangements of the same two
        // functions, and the only thing that picks between them is the shape of the
        // window — not its size, which scale() has already handled.
        if (rmp::ui::compact()) {
            rmp::ui::column({ .grow_x = true, .grow_y = true }, [this] {
                sidebar();
                content();
            });
        } else {
            rmp::ui::row({ .grow_x = true, .grow_y = true }, [this] {
                sidebar();
                content();
            });
        }

        rmp::ui::end();
    }

private:
    int _selected = 0;
    bool _show_grid = true;

    // -------------------------------------------------------------------
    // The two halves. Written once, arranged twice — which is the entire trick.
    // Neither of them knows anything about the window.
    // -------------------------------------------------------------------

    void sidebar() {
        // On a wide window this is a fixed-width column down the left. On a narrow
        // one it is a full-width strip along the top, and the buttons run across
        // instead of down. Same buttons, same state, same handlers.
        const bool narrow = rmp::ui::compact();

        auto items = [this] {
            for (int i = 0; i < SECTION_COUNT; ++i) {
                const bool active = (i == _selected);
                if (rmp::ui::button(SECTIONS[i],
                                    { .style = active ? rmp::ui::Variant::PRIMARY
                                                      : rmp::ui::Variant::GHOST,
                                      .size = rmp::ui::Size::SMALL })) {
                    _selected = i;
                }
            }
        };

        if (narrow) {
            rmp::ui::panel({ .box = { .grow_x = true } }, [&] {
                // A row that wraps would be better still, and "a row that wraps" is
                // spelled grid({ .columns = 0 }) — see the grid below and
                // ui/05_inventory. Kept as a plain row here so the shape change is
                // the only thing this example is demonstrating.
                rmp::ui::row({ .grow_x = true }, items);
            });
        } else {
            rmp::ui::panel({ .box = { .width = 200, .grow_y = true } }, [&] {
                rmp::ui::text(
                    "Sections",
                    { .color = rmp::ui::ColorRole::MUTED, .size = rmp::ui::Size::SMALL });
                items();
            });
        }
    }

    void content() {
        rmp::ui::panel({ .box = { .grow_x = true, .grow_y = true } }, [this] {
            rmp::ui::text(SECTIONS[_selected], { .size = rmp::ui::Size::LARGE });
            rmp::ui::checkbox("Show the grid", _show_grid);

            if (_show_grid) {
                // Nothing responsive to write here: columns = 0 works out how many
                // fit in the width it is given and works it out again every frame.
                // This is the composition answer to "a row that wraps", and it is
                // why there is no wrap flag on row().
                rmp::ui::scroll([] {
                    rmp::ui::grid({ .columns = 0, .min_cell = 110, .id = "tiles" }, [] {
                        for (int i = 0; i < 24; ++i) {
                            rmp::ui::cell([&] {
                                rmp::ui::panel({ .box = { .padding = 10 } }, [&] {
                                    rmp::ui::text("Item " + std::to_string(i + 1),
                                                  { .size = rmp::ui::Size::SMALL });
                                    rmp::ui::progress(static_cast<float>(i % 10) / 9.0f);
                                });
                            });
                        }
                    });
                });
            }
        });
    }
};

RMP_GAME(ResponsiveScene);