Inspector + Settings (Example 2)

Example 2: Inspector + Settings

SampleInspector.cpp
#include "ScriptRuntime.h"
#include "SceneObject.h"
#include "ThirdParty/imgui/imgui.h"

namespace {
    // -- references and Variables --
    bool autoRotate = false;
    glm::vec3 spinSpeed = glm::vec3(0.0f, 45.0f, 0.0f);
    glm::vec3 offset = glm::vec3(0.0f, 1.0f, 0.0f);
    char targetName[128] = "MyTarget";
    // functions area
    static void ApplyAutoRotate(ScriptContext& ctx, float deltaTime) {
        if (!autoRotate || !ctx.object) return;
        ctx.SetRotation(ctx.object->rotation + spinSpeed * deltaTime);
    }
}

extern "C" void Script_OnInspector(ScriptContext& ctx) {
    // -- AutoSetting and IMGUI Variable/UI settings --
    // (Since this is for IMGUI, go crazy with it i guess lmfao.)
    ctx.AutoSetting("autoRotate", autoRotate);
    ctx.AutoSetting("spinSpeed",  spinSpeed);
    ctx.AutoSetting("offset",     offset);
    ctx.AutoSetting("targetName", targetName, sizeof(targetName));
    ImGui::TextUnformatted("SampleInspector");
    ImGui::Separator();
    bool changed = false;
    changed |= ImGui::Checkbox("Auto Rotate", &autoRotate);
    changed |= ImGui::DragFloat3("Spin Speed (deg/s)", &spinSpeed.x, 1.0f, -360.0f, 360.0f);
    changed |= ImGui::DragFloat3("Offset", &offset.x, 0.1f);
    changed |= ImGui::InputText("Target Name", targetName, sizeof(targetName));
    if (changed) {
        ctx.SaveAutoSettings();
    }
    if (ctx.object) {
        ImGui::TextDisabled("Attached to: %s (id=%d)", ctx.object->name.c_str(), ctx.object->id);

        if (ImGui::Button("Apply Offset To Self")) {
            ctx.SetPosition(ctx.object->position + offset);
        }
    }
    if (ImGui::Button("Nudge Target")) {
        if (SceneObject* target = ctx.FindObjectByName(targetName)) {
            target->position += offset;
        }
    }
}

void Spec(ScriptContext& ctx, float deltaTime) {
    ApplyAutoRotate(ctx, deltaTime);
}
void TestEditor(ScriptContext& ctx, float deltaTime) {
    ApplyAutoRotate(ctx, deltaTime);
}
void TickUpdate(ScriptContext& ctx, float deltaTime) {
    ApplyAutoRotate(ctx, deltaTime);
}

This example demonstrates:

  • Inspector UI

  • Per-object settings

  • Spec/Test hooks

  • Safe state caching

Last updated

Was this helpful?