#include "InspectorView.h" #include "ComponentRegistry.h" #include "Graphics/Graphics.h" #include "UI/Element/Button.h" #include "UI/Element/Div.h" #include "UI/Element/Text.h" #include "UI/Style/Fonts.h" // Font_Arial tag type (Text.h does not pull this in) #include "UI/System.h" using namespace Seele; using namespace Seele::Editor; using namespace Seele::UI; namespace { // Split a multi-line string into trimmed, non-empty lines (for the field display). std::vector splitLines(const std::string& text) { std::vector out; std::string cur; for (char c : text) { if (c == '\n') { if (!cur.empty()) { out.push_back(cur); cur.clear(); } } else { cur.push_back(c); } } if (!cur.empty()) { out.push_back(cur); } return out; } } // namespace InspectorView::InspectorView(Gfx::PGraphics graphics, PWindow owner, const ViewportCreateInfo& createInfo, PScene scene) : View(graphics, owner, createInfo, "InspectorView"), scene(scene) { // ------------------------------------------------------------------ // Build the ENTIRE fixed widget tree exactly once, here, in the constructor. // // "Children fixed at construction": the UI tree's structure is immutable at // runtime. We create every widget as a named local (so we can keep a raw pointer // for setText()), then reference those same objects in the tree's initializer-list. // The initializer-list's Element* -> OElement implicit conversion stores an OWNING // ref in each parent's children array (Array) for the element's whole // lifetime, so the raw pointers below remain valid for the life of the view. // ------------------------------------------------------------------ // ---- Leaf widgets (named locals; also referenced by the tree below) ---------- // Entity-list row labels + select buttons (one row per pool slot). Text* rowLabels[kMaxEntities]; Button<>* rowButtons[kMaxEntities]; for (int i = 0; i < kMaxEntities; i++) { // Text has no default ctor (text is a required arg); start with an empty string // and refresh() fills it each frame. Button's default ctor is deleted (Div() // = delete) so we must pass explicit Attributes. rowLabels[i] = new Text(""); rowButtons[i] = new Button<>(UI::Attributes{}); // The button's onClick is the ONLY sanctioned way selection state changes; // refresh() (called from update() next frame) reads it back and re-renders the // component pane. We do not markDirty() here - the next frame's refresh() is // the sanctioned per-frame mutation path, and markDirty() is an Element method // (not callable on this View). rowButtons[i]->attr.onClick = [this, i](MouseEvent&) { selectedEntity = i; return true; }; // PElement is a NON-OWNING wrapper (no operator=(T*)); the tree's owning // OElement refs (stored in the parent Div's children below) are what keep these // alive. We construct an explicit PElement temporary to copy-assign the handle. entityPool[i].label = PElement(rowLabels[i]); entityPool[i].button = PElement(rowButtons[i]); } // Component-inspector pane: a header line + a fixed pool of field lines. // Text has no default ctor - pass an initial string (refresh() overwrites it). Text* header = new Text("(no entity selected)"); Text* lines[kMaxComponentLines]; for (int i = 0; i < kMaxComponentLines; i++) { lines[i] = new Text(""); } componentHeader = PElement(header); for (int i = 0; i < kMaxComponentLines; i++) { componentLines[i] = PElement(lines[i]); } // ---- Compose the tree (structure fixed at construction) ---------------------- // Element's only children ctor is (Attributes, initializer_list) - there is // no variadic form and initializer_list cannot be built from an Array, so the child // list must be a braced-init at the call site. A braced list MAY contain function-call // expressions, so a small lambda builds each identical row (returning the owning // OElement of the row Div) and we reference makeRow(0..7) explicitly. The label/button // handles for refresh() were already stashed in entityPool[] above (non-owning PElement); // the row Div's children array holds the OWNING refs that keep the widgets alive. auto makeRow = [this, &rowLabels, &rowButtons](int i) -> OElement { return OElement(new Div( UI::Attributes{}, std::initializer_list{OElement(rowLabels[i]), OElement(rowButtons[i])})); }; // Left pane: a vertical (FlexCol) stack of the 8 fixed entity rows. The child list is // a braced-init of makeRow(0..7) - each call returns the owning OElement of one row. Div* leftPane = new Div( UI::Attributes{}, std::initializer_list{ makeRow(0), makeRow(1), makeRow(2), makeRow(3), makeRow(4), makeRow(5), makeRow(6), makeRow(7), }); // Right pane: a vertical (FlexCol) stack of the header line + the fixed field-line pool. // Same braced-init pattern; makeLine(i) returns the owning OElement of one field Text. auto makeLine = [this, &lines](int i) -> OElement { return OElement(lines[i]); }; Div* rightPane = new Div( UI::Attributes{}, std::initializer_list{ OElement(header), makeLine(0), makeLine(1), makeLine(2), makeLine(3), makeLine(4), makeLine(5), makeLine(6), makeLine(7), makeLine(8), makeLine(9), makeLine(10), makeLine(11), makeLine(12), makeLine(13), makeLine(14), makeLine(15), }); // Root: a horizontal (FlexRow) split - entity list on the left, inspector on the right. Div* root = new Div( UI::Attributes{}, std::initializer_list{OElement(leftPane), OElement(rightPane)}); // Hand the fully-built root tree to the UI system. The system is owned by the view // (uiSystem is an OwningPtr); construct the owning handle in place. uiSystem = UI::OSystem(new UI::System(OElement(root))); // Register a UIPass to render the UI. Keep a raw pointer to it so the window input // callbacks can forward events (RenderGraph exposes only resources, not passes). uiPass = new UIPass(graphics, uiSystem); renderGraph.addPass(ORenderPass(uiPass)); renderGraph.setViewport(viewport); renderGraph.createRenderPass(); } InspectorView::~InspectorView() {} void InspectorView::beginUpdate() {} void InspectorView::update() { refresh(); } void InspectorView::commitUpdate() {} void InspectorView::prepareRender() {} void InspectorView::render() { // Drives every pass in the graph, including the UIPass (which runs // system->processInput() + system->beginFrame() internally before rendering). // The UI system's per-frame update is performed by the pass, not by us. renderGraph.render(Component::Camera(), Component::Transform()); } void InspectorView::applyArea(URect) { // The Viewport was already resized by View::resize before this is called. Recreate // the render graph's passes at the new size so the UI (and everything) re-layouts. renderGraph.setViewport(viewport); renderGraph.createRenderPass(); } void InspectorView::keyCallback(KeyCode code, InputAction action, uint32_t modifier) { if (uiPass != nullptr) { uiPass->key(code, action, KeyModifierFlags(modifier), 0); } } void InspectorView::mouseMoveCallback(double xPos, double yPos) { if (uiPass != nullptr) { uiPass->mouseMove(Vector2(xPos, yPos)); } } void InspectorView::mouseButtonCallback(MouseButton button, InputAction action, uint32_t modifier) { if (uiPass != nullptr) { uiPass->mouseButton(button, action, KeyModifierFlags(modifier), Vector2(0, 0)); } } void InspectorView::scrollCallback(double xOffset, double yOffset) { if (uiPass != nullptr) { uiPass->scroll(Vector2(xOffset, yOffset)); } } void InspectorView::fileCallback(int, const char**) {} void InspectorView::refresh() { // Re-read the scene's live entity roster + the selected entity's components, and // push the results into the fixed widget pool via Text::setText. This is the ONLY // per-frame mutation of the UI tree - children are never added or removed. // // The pool handles are UI::PElement (the Element base ref) because Text/Button are // class templates (can't be member types). We downcast to the concrete type at the // use site. Every downcast target is guaranteed valid: we built each widget with the // exact concrete type below and the tree's owning OElement refs keep it alive. auto setTextOn = [](UI::PElement handle, const std::string& text) { if (handle == nullptr) { return; } if (auto* t = dynamic_cast*>(handle.getHandle())) { t->setText(text); } }; auto entities = enumerateEntities(scene); // --- Entity list pane: fill the fixed row pool from the live roster ----------- for (int i = 0; i < kMaxEntities; i++) { if (i < (int)entities.size()) { setTextOn(entityPool[i].label, entities[i].label + " [" + entities[i].componentSummary + "]"); } else { setTextOn(entityPool[i].label, ""); } } // --- Component inspector pane: describe the selected entity ------------------- if (selectedEntity >= 0 && selectedEntity < (int)entities.size()) { PEntity ent = entities[selectedEntity].entity; setTextOn(componentHeader, "Components: " + entities[selectedEntity].label); auto components = describeEntityComponents(scene, ent); int line = 0; for (auto& c : components) { if (line >= kMaxComponentLines) { break; // fixed pool: truncate beyond it } setTextOn(componentLines[line], " " + c.name); line++; // Multi-line field text is folded into the subsequent pool lines (clamped). auto fields = splitLines(c.fieldText); for (auto& f : fields) { if (line >= kMaxComponentLines) { break; } setTextOn(componentLines[line], " " + f); line++; } } // Clear leftover pool lines so stale data never shows (fixed pool must be clean). for (; line < kMaxComponentLines; line++) { setTextOn(componentLines[line], ""); } } else { setTextOn(componentHeader, "(no entity selected)"); for (int i = 0; i < kMaxComponentLines; i++) { setTextOn(componentLines[i], ""); } } }