On this page
  1. Run it
  2. The code

Skinned

The other way to write rmp::ui: a game's shop screen designed rather than written. Every button and panel a nine-slice from one Aseprite file, icons as its named slices (slice()), a title in its own font with an outline, a soft shadow, sounds on hover and click, buttons that grow and shrink, a panel that slides in, a button that shake()s -- all of it one Theme with a Style per kind of widget -- and widgets you keep (Button, Label, Image, Panel, Checkbox, Slider, ProgressBar) as members of the scene, each with its callbacks; image_button() and on_event(). Its art is generated by tools/make_skinned_art.py.

Skinned, as it starts

Run it

From a clone of the framework:

rmp example 09_skinned

The code

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

src/main.cpp 32 lines
// ---------------------------------------------------------------------------
// examples/ui/09_skinned/src/main.cpp
//
// A game's shop screen, skinned: the other way to write rmp::ui.
//
// The examples before this one write the interface a call at a time, and a
// main menu is three of them. This one is what a screen DESIGNED by an artist
// looks like: every button and panel a picture from one Aseprite file, every
// icon a named slice of it, a title in a font of its own with an outline, a
// soft shadow under the shop, sounds on hover and click, buttons that grow
// under the pointer and shrink when pressed, a panel that slides in, a button
// that shakes when there is not enough money -- and widgets you KEEP, members
// of the scene, each with its look and its callbacks, the way a Godot scene
// keeps its nodes.
//
// Where each piece is:
//
//   the look of everything    ShopScene::_ready(): one Theme, with a Style for
//                             every button and every panel, sounds and motion
//   the widgets               src/scenes/shop.h: kept, with their callbacks
//   the screen                ShopScene::_draw(): where each one is shown
//
// The art is generated, as the art of every example is: tools/make_skinned_art.py.
//
// Run it: `rmp example 09_skinned`.
// ---------------------------------------------------------------------------

#include <rmp/app.h>

#include "scenes/shop.h"

RMP_GAME(ShopScene);
src/scenes/shop.cpp 126 lines
#include "shop.h"

#include <rmp/audio.h>

#include <string>

namespace {

constexpr int STAR_PRICE = 3;
constexpr int STARS_TO_COLLECT = 5;

} // namespace

void ShopScene::_ready() {
    _skin = rmp::assets::load_sheet("skin.aseprite");

    // The look of the whole game, in one place: every button a wooden frame
    // that stretches by its middle, a darker one while it is pressed; every
    // panel the same frame, with a soft shadow, sliding in when it appears.
    // Sounds on hover and click, and a "no" for a shake.
    rmp::ui::Theme look = rmp::ui::theme_dark();
    look.sounds = { .hover = "hover", .click = "click", .shake = "nope" };
    look.motion = { .hover_scale = 1.04f, .press_scale = 0.96f };
    look.styles.button = {
        .skin = rmp::ui::Skin{ .normal = rmp::ui::slice(_skin, "button"),
                               .press = rmp::ui::slice(_skin, "button_down") },
        .padding_x = 18,
        .padding_y = 10,
        .text = RAYWHITE,
        .text_shadow = rmp::ui::TextShadow{},
    };
    look.styles.panel = {
        .skin = rmp::ui::Skin{ .normal = rmp::ui::slice(_skin, "panel") },
        .shadow = rmp::ui::Shadow{ .offset = { 0, 6 }, .blur = 10 },
        .padding_x = 22,
        .motion = rmp::ui::Motion{ .appear = 0.25f,
                                   .appear_offset = 16,
                                   .appear_scale = 0.96f },
    };
    rmp::ui::set_theme(look);

    _title.style = { .text = GOLD,
                     .font = rmp::assets::load_font("title.ttf", 40),
                     .text_outline = rmp::ui::TextOutline{ .width = 2, .color = BLACK } };
    _spinner.sprite.sheet = rmp::assets::load_sheet("spinner.aseprite");
    _spinner.sprite.play("spin");
    _buy.icon = rmp::ui::slice(_skin, "coin");
    _sell.icon = rmp::ui::slice(_skin, "star");
    _collection.fill_picture = rmp::ui::slice(_skin, "bar");

    // Every click of every control, the immediate ones too: one hook.
    rmp::ui::on_event([this](const rmp::ui::Event &e) {
        if (e.kind == rmp::ui::EventKind::CLICK) ++_clicks;
    });
}

void ShopScene::buy_star() {
    if (_coins < STAR_PRICE) {
        _buy.shake(); // and the theme plays its "nope"
        rmp::ui::notify("Not enough coins", { .color = rmp::ui::ColorRole::DANGER });
        return;
    }
    _coins -= STAR_PRICE;
    ++_stars;
}

void ShopScene::sell_star() {
    if (_stars == 0) {
        _sell.shake();
        return;
    }
    --_stars;
    _coins += STAR_PRICE - 1;
}

void ShopScene::show_shop() {
    _shop.show([&] {
        rmp::ui::row([&] {
            _spinner.show();
            rmp::ui::column([&] {
                rmp::ui::row([&] {
                    rmp::ui::image(rmp::ui::slice(_skin, "coin"),
                                   { .width = 24, .height = 24 });
                    rmp::ui::text("Coins: " + std::to_string(_coins));
                    rmp::ui::image(rmp::ui::slice(_skin, "star"),
                                   { .width = 24, .height = 24 });
                    rmp::ui::text("Stars: " + std::to_string(_stars));
                });
                rmp::ui::text("Three coins a star. No refunds, café prices.");
            });
        });
        rmp::ui::row([&] {
            _buy.show();
            _sell.show();
        });
        _collection.value = static_cast<float>(_stars) / STARS_TO_COLLECT;
        _collection.show();
    });
}

void ShopScene::show_settings() {
    rmp::ui::overlay({ .dim = 0.5f }, [&] {
        rmp::ui::panel([&] {
            rmp::ui::text("Settings");
            _sound.show();
            _volume.show();
            _close.show();
        });
    });
}

void ShopScene::_draw() {
    rmp::ui::begin();
    _title.show();
    show_shop();
    rmp::ui::row([&] {
        if (rmp::ui::image_button(rmp::ui::slice(_skin, "gear"),
                                  { .width = 32, .height = 32, .id = "settings" }))
            _settings = true;
        rmp::ui::text("Clicks: " + std::to_string(_clicks),
                      { .color = rmp::ui::ColorRole::MUTED });
    });
    rmp::ui::end();

    if (_settings) show_settings();
}
src/scenes/shop.h 52 lines
#pragma once
#include <rmp/assets.h>
#include <rmp/audio.h>
#include <rmp/scene.h>
#include <rmp/ui.h>

// The shop: coins to spend on stars, a settings panel behind the gear.
class ShopScene : public rmp::Scene {
public:
    void _ready() override;
    void _draw() override;

private:
    void buy_star();
    void sell_star();
    void show_shop();
    void show_settings();

    rmp::SpriteSheet _skin; // skin.aseprite: the frames, the panel, the icons
    int _coins = 5;
    int _stars = 0;
    int _clicks = 0; // every click of every control, counted by on_event()
    bool _settings = false;

    // A title in a font of its own, with an outline, so it reads on anything.
    rmp::ui::Label _title{ .text = "The Star Shop", .size = rmp::ui::Size::LARGE };
    // The star, turning: an animated sprite a kept Image plays by itself.
    rmp::ui::Image _spinner{ .width = 48, .height = 48, .id = "spinner" };
    // The shop: a panel whose own look comes from the theme, and whose
    // children all write in gold.
    rmp::ui::Panel _shop{ .id = "shop", .children = { .text = GOLD } };
    rmp::ui::Button _buy{ .text = "Buy a star", .id = "buy", .on_click = [this] {
                             buy_star();
                         } };
    rmp::ui::Button _sell{ .text = "Sell a star", .id = "sell", .on_click = [this] {
                              sell_star();
                          } };
    rmp::ui::ProgressBar _collection{ .width = 300, .height = 18, .id = "collection" };
    // The settings, which keep their own values.
    rmp::ui::Checkbox _sound{ .text = "Sound", .checked = true, .on_change = [](bool on) {
                                 rmp::audio::set_volume(rmp::audio::Bus::SFX,
                                                        on ? 1.0f : 0.0f);
                             } };
    rmp::ui::Slider _volume{ .text = "Volume",
                             .value = 1.0f,
                             .step = 0.1f,
                             .width = 160,
                             .on_change = [](float v) {
                                 rmp::audio::set_volume(rmp::audio::Bus::MASTER, v);
                             } };
    rmp::ui::Button _close{ .text = "Close", .on_click = [this] { _settings = false; } };
};