# Clay direct

> The escape hatch. Clay's own macros in the same frame as rmp::ui, for anything the small API does not expose yet.

Source: https://omardev29.github.io/rmp-docs/examples/ui/03_clay_direct.html

**The escape hatch.** Clay's own macros in the same frame as `rmp::ui`, for anything the small API does not expose yet.

## Run it

From a clone of the framework:

```shell
rmp example 03_clay_direct
```

## The code

Every file of `examples/ui/03_clay_direct`, as it is at the commit this site documents ([on GitHub](https://github.com/omardev29/raylib_multiplatform/tree/982bf2a1e307c49776bff84211260703b8de6895/examples/ui/03_clay_direct)).

**`src/main.cpp` 140 lines**

[src/main.cpp](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/examples/ui/03_clay_direct/src/main.cpp)

```cpp
// ---------------------------------------------------------------------------
// examples/ui/03_clay_direct/src/main.cpp
//
// THE ESCAPE HATCH: using Clay's own API directly, alongside rmp::ui.
//
// rmp::ui is a small, opinionated surface over Clay — enough for menus, HUDs
// and dialogs without ever mentioning a coordinate. When you want something it
// does not expose yet (floating elements placed where you choose, aspect
// ratios, per-corner radii, a border on one side, an explicit z-index), you do
// not have to wait for us and you do not have to give up rmp::ui to get it.
// (Clipping and scrolling it does expose: rmp::ui::scroll().)
//
// It is the same bargain as the rest of the framework: rmp::assets does not stop
// you calling LoadTexture, and rmp::ui does not stop you calling Clay. Or
// rlgl. Or raw OpenGL, for that matter.
//
//   #include <clay.h>
//
// and write Clay between rmp::ui::begin() and rmp::ui::end(). Your elements go
// into the same tree, get laid out in the same pass and drawn by the same
// renderer.
//
// Two things to know and then you are on your own:
//
//   * begin() has already opened two elements — a root that fills the screen
//     and a column that centres its contents. Yours become children of that
//     column. If you want to escape it, use Clay's floating elements.
//   * Clay does NOT copy strings. CLAY_STRING() on a literal is fine forever;
//     anything built at runtime must stay alive until end() has returned. This
//     is the one footgun rmp::ui::text() removes for you by copying.
//
// Run it: `rmp example 03_clay_direct`.
// ---------------------------------------------------------------------------

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

#include <clay.h> // the escape hatch: not included for you on purpose

class ClayScene : public rmp::Scene {
public:
    void _ready() override { _logo = rmp::assets::load_texture("rabbit.png"); }

    void _draw() override {
        // A copy of the raylib struct, alive for this frame. The handle below
        // is the owner; this is what Clay is allowed to point at.
        Texture2D logo_tex = _logo;

        rmp::ui::begin();

        // --- the normal API ---------------------------------------------------
        rmp::ui::text("Inventory");

        // --- and Clay, in the same frame --------------------------------------
        //
        // A horizontal strip of slots, which rmp::ui has no primitive for yet.
        // CLAY_AUTO_ID is the version without an explicit id; CLAY(CLAY_ID("x"),
        // ...) is the one to use when you want to ask Clay about the element later
        // with Clay_PointerOver or Clay_GetElementData.
        //
        // It is shaped like a tab, which rmp::ui cannot do either: each corner has
        // its own radius, and the border is on one side only.
        CLAY_AUTO_ID(
            { .layout = { .sizing = { .width = CLAY_SIZING_FIT(0),
                                      .height = CLAY_SIZING_FIT(0) },
                          .padding = CLAY_PADDING_ALL(10),
                          .childGap = 10,
                          .layoutDirection = CLAY_LEFT_TO_RIGHT },
              .backgroundColor = { 30, 30, 38, 255 },
              .cornerRadius = { .topLeft = 18,
                                .topRight = 18,
                                .bottomLeft = 0,
                                .bottomRight = 0 },
              .border = { .color = { 88, 120, 245, 255 }, .width = { .bottom = 3 } } }) {
            for (int i = 0; i < 4; ++i) {
                // CLAY_IDI gives each slot in a loop its own identity without
                // building a string per element.
                CLAY(CLAY_IDI("slot", i),
                     { .layout = { .sizing = { .width = CLAY_SIZING_FIXED(64),
                                               .height = CLAY_SIZING_FIXED(64) } },
                       .backgroundColor = { 44, 44, 56, 255 },
                       .cornerRadius = CLAY_CORNER_RADIUS(6) }) {
                    // Images: point imageData at a Texture2D you own and keep
                    // alive for the frame. Our renderer draws it stretched to the
                    // element's box, untinted. A tint is a Color* in userData; a
                    // backgroundColor is NOT a tint -- Clay draws it as a rectangle
                    // after the image, which paints a flat square over it.
                    if (i == 0) {
                        CLAY_AUTO_ID(
                            { .layout = { .sizing = { .width = CLAY_SIZING_GROW(0),
                                                      .height = CLAY_SIZING_GROW(0) } },
                              .image = { .imageData = &logo_tex } }) {}
                    }
                }
            }
        }

        // Hover, straight from Clay. Note it reads the PREVIOUS frame's geometry,
        // exactly like rmp::ui::button() does — the layout for this frame does not
        // exist until end().
        if (Clay_PointerOver(CLAY_IDI("slot", 0))) {
            rmp::ui::text("slot 1", { .color = rmp::ui::ColorRole::PRIMARY });
        }

        // Back to the normal API, still the same frame.
        if (rmp::ui::button("Close")) rmp::app::quit();

        rmp::ui::end();
    }

private:
    // The owning handle. Clay wants a Texture2D* that stays valid for the
    // frame, so _draw() copies the raylib struct out for exactly that long.
    rmp::Texture _logo;
};

RMP_GAME(ClayScene);

// ---------------------------------------------------------------------------
// What our renderer understands
// ---------------------------------------------------------------------------
//
//   RECTANGLE      background colour + each corner's own radius
//   BORDER         colour + each side's own width + each corner's own radius
//   TEXT           drawn with the UI font; fontId is ignored, there is one font
//   IMAGE          imageData as a Texture2D*; userData as a Color* tint, or
//                  null for none (a backgroundColor covers the image: see above)
//   SCISSOR_START  clipping, so Clay's clip/scroll containers work
//   SCISSOR_END
//
// CUSTOM commands are not handled. If you need them, src/rmp/ui/render.cpp is
// one short file, and adding a case is the intended way to extend it.
//
// AND THE LIMITS. Doing this ties your code to a specific version of Clay
// (0.14, pinned in thirdparty/FROZEN_VERSIONS.md). rmp::ui exists partly so
// that most code does not carry that dependency — Clay is pre-1.0 and its API
// has moved between minor versions. That is a fair trade for a feature you need
// today; it is a bad trade for a button.
```
