Supporting setting panel layout

This commit is contained in:
2026-09-19 19:41:39 -07:00
parent ace69f613e
commit da6bda7ca7
24 changed files with 257 additions and 37 deletions
+23
View File
@@ -0,0 +1,23 @@
#pragma once
#include <string>
#include <vector>
namespace sophia {
struct DockLayout {
float leftWidth = 0.2f;
float rightWidth = 0.25f;
float bottomHeight = 0.25f;
std::vector<std::string> left;
std::vector<std::string> right;
std::vector<std::string> bottom;
std::vector<std::string> center;
bool empty() const {
return left.empty() && right.empty() && bottom.empty() && center.empty();
}
};
} // namespace sophia
+24 -4
View File
@@ -1,5 +1,7 @@
#pragma once
#include <glm/glm.hpp>
#include <optional>
#include <sophia/frame_info.hpp>
#include <string>
#include <utility>
@@ -8,28 +10,46 @@ namespace sophia {
class Panel {
public:
struct Config {
std::string name;
// Locked panels can't be moved, resized, docked, collapsed or closed.
bool locked = false;
bool closable = true;
// A zero component lets ImGui size the window. Applied on first use, or
// every frame when locked.
glm::vec2 size{0.0f, 0.0f};
std::optional<glm::vec2> position;
};
Panel(const Panel&) = delete;
Panel& operator=(const Panel&) = delete;
Panel(Panel&&) noexcept = default;
Panel& operator=(Panel&&) noexcept = default;
explicit Panel(std::string name = {}) : name{std::move(name)} {}
explicit Panel(Config config) : config{std::move(config)} {}
virtual ~Panel() = default;
virtual void onUpdate(const FrameInfo& frameInfo) = 0;
// Owns the ImGui window: Begin, onUpdate, End.
void draw(const FrameInfo& frameInfo);
const std::string& getName() const { return name; }
const std::string& getName() const { return config.name; }
bool isLocked() const { return config.locked; }
bool isVisible() const { return this->visible; }
void setVisible(bool visible) { this->visible = visible; }
void toggleVisible() { this->visible = !this->visible; }
protected:
// Draw the panel's contents; the window is already open.
virtual void onUpdate(const FrameInfo& frameInfo) = 0;
bool visible = true;
private:
std::string name;
Config config;
};
} // namespace sophia
@@ -3,6 +3,7 @@
#include <memory>
#include <sophia/frame_info.hpp>
#include <sophia/handles.hpp>
#include <sophia/ui/dock_layout.hpp>
#include <sophia/ui/panel.hpp>
#include <sophia/util/assert.hpp>
#include <string_view>
@@ -94,6 +95,11 @@ class PanelRegistry {
void update(const FrameInfo& frameInfo);
void setDefaultLayout(DockLayout layout) {
defaultLayout = std::move(layout);
}
const DockLayout& getDefaultLayout() const { return defaultLayout; }
private:
struct Slot {
std::unique_ptr<Panel> panel;
@@ -102,6 +108,7 @@ class PanelRegistry {
std::vector<Slot> slots;
std::vector<u32> freeList;
DockLayout defaultLayout;
};
} // namespace sophia
+9
View File
@@ -57,7 +57,16 @@ class Application::Impl {
ImGuiRenderer imguiRenderer{window, device, renderContext.getImageFormat(),
renderContext.getImageCount()};
this->application.consoleCommandRegistry.registerCommand(
"reset_layout", "Restore the default panel layout",
[&imguiRenderer](const std::vector<std::string>&) {
imguiRenderer.resetLayout();
return std::string{"Layout reset"};
});
this->application.onCreate();
imguiRenderer.setDefaultLayout(
this->application.panelRegistry.getDefaultLayout());
auto startTime = std::chrono::high_resolution_clock::now();
auto currentTime = startTime;
+65 -2
View File
@@ -3,9 +3,21 @@
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_vulkan.h>
#include <imgui_internal.h> // DockBuilder API
namespace sophia {
namespace {
constexpr ImGuiDockNodeFlags DOCKSPACE_FLAGS =
ImGuiDockNodeFlags_PassthruCentralNode;
void dockAll(const std::vector<std::string>& windows, ImGuiID nodeId) {
for (const std::string& name : windows) {
ImGui::DockBuilderDockWindow(name.c_str(), nodeId);
}
}
} // namespace
ImGuiRenderer::ImGuiRenderer(Window& window, Device& device,
vk::Format colorAttachmentFormat,
u32 minImageCount) {
@@ -65,8 +77,59 @@ void ImGuiRenderer::beginFrame() {
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::DockSpaceOverViewport(0, ImGui::GetMainViewport(),
ImGuiDockNodeFlags_PassthruCentralNode);
ImGuiID dockspaceId = ImGui::DockSpaceOverViewport(
0, ImGui::GetMainViewport(), DOCKSPACE_FLAGS);
applyLayoutIfNeeded(dockspaceId, DOCKSPACE_FLAGS);
}
void ImGuiRenderer::setDefaultLayout(DockLayout layout) {
defaultLayout = std::move(layout);
seedIfUnsaved = true;
}
void ImGuiRenderer::resetLayout() { forceReset = true; }
void ImGuiRenderer::applyLayoutIfNeeded(ImGuiID dockspaceId,
ImGuiDockNodeFlags flags) {
if (defaultLayout.empty()) {
return;
}
bool apply = forceReset;
if (seedIfUnsaved) {
// A layout restored from imgui.ini shows up as a split root node.
ImGuiDockNode* root = ImGui::DockBuilderGetNode(dockspaceId);
apply = apply || root == nullptr || !root->IsSplitNode();
seedIfUnsaved = false;
}
if (!apply) {
return;
}
forceReset = false;
const ImVec2 size = ImGui::GetMainViewport()->Size;
ImGui::DockBuilderRemoveNode(dockspaceId);
ImGui::DockBuilderAddNode(dockspaceId, flags | ImGuiDockNodeFlags_DockSpace);
ImGui::DockBuilderSetNodeSize(dockspaceId, size);
ImGuiID center = dockspaceId;
const ImGuiID left = ImGui::DockBuilderSplitNode(
center, ImGuiDir_Left, defaultLayout.leftWidth, nullptr, &center);
// The right split is taken from what's left after the left split.
const float rightRatio =
defaultLayout.rightWidth / (1.0f - defaultLayout.leftWidth);
const ImGuiID right = ImGui::DockBuilderSplitNode(
center, ImGuiDir_Right, rightRatio, nullptr, &center);
const ImGuiID bottom = ImGui::DockBuilderSplitNode(
center, ImGuiDir_Down, defaultLayout.bottomHeight, nullptr, &center);
dockAll(defaultLayout.left, left);
dockAll(defaultLayout.right, right);
dockAll(defaultLayout.bottom, bottom);
dockAll(defaultLayout.center, center);
ImGui::DockBuilderFinish(dockspaceId);
}
void ImGuiRenderer::endFrame(vk::CommandBuffer commandBuffer) {
+16
View File
@@ -1,6 +1,9 @@
#pragma once
#include <imgui.h>
#include <sophia/types.hpp>
#include <sophia/ui/dock_layout.hpp>
#include "device.hpp"
#include "window.hpp"
@@ -18,6 +21,19 @@ class ImGuiRenderer {
void beginFrame();
void endFrame(vk::CommandBuffer commandBuffer);
// Seeds the layout on the next frame, but only if no saved layout exists.
void setDefaultLayout(DockLayout layout);
// Discards the current layout and re-applies the default on the next frame.
void resetLayout();
private:
void applyLayoutIfNeeded(ImGuiID dockspaceId, ImGuiDockNodeFlags flags);
DockLayout defaultLayout;
bool seedIfUnsaved = false;
bool forceReset = false;
};
} // namespace sophia
+35
View File
@@ -0,0 +1,35 @@
#include <imgui.h>
#include <sophia/ui/panel.hpp>
namespace sophia {
void Panel::draw(const FrameInfo& frameInfo) {
ImGuiWindowFlags flags = ImGuiWindowFlags_None;
if (config.locked) {
flags |= ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_NoCollapse;
}
const ImGuiCond cond =
config.locked ? ImGuiCond_Always : ImGuiCond_FirstUseEver;
if (config.size.x > 0.0f || config.size.y > 0.0f) {
ImGui::SetNextWindowSize(ImVec2(config.size.x, config.size.y), cond);
}
if (config.position) {
ImGui::SetNextWindowPos(ImVec2(config.position->x, config.position->y),
cond);
}
bool open = true;
const bool showCloseButton = config.closable && !config.locked;
ImGui::Begin(config.name.c_str(), showCloseButton ? &open : nullptr, flags);
onUpdate(frameInfo);
ImGui::End();
if (!open) {
visible = false;
}
}
} // namespace sophia
+1 -1
View File
@@ -80,7 +80,7 @@ u32 PanelRegistry::visibleCount() const {
void PanelRegistry::update(const FrameInfo& frameInfo) {
for (auto& slot : slots) {
if (slot.panel && slot.panel->isVisible()) {
slot.panel->onUpdate(frameInfo);
slot.panel->draw(frameInfo);
}
}
}
-4
View File
@@ -111,8 +111,6 @@ int ConsolePanel::textEditCallback(ImGuiInputTextCallbackData* data) {
}
void ConsolePanel::onUpdate(const FrameInfo&) {
ImGui::Begin("Console");
if (ImGui::SmallButton("Clear")) {
clear();
}
@@ -161,8 +159,6 @@ void ConsolePanel::onUpdate(const FrameInfo&) {
if (reclaimFocus) {
ImGui::SetKeyboardFocusHere(-1);
}
ImGui::End();
}
} // namespace sophia
+1 -1
View File
@@ -13,7 +13,7 @@ namespace sophia {
class ConsolePanel : public Panel {
public:
explicit ConsolePanel(ConsoleCommandRegistry& consoleCommandRegistry)
: Panel{"Console"}, consoleCommandRegistry{consoleCommandRegistry} {
: Panel{{.name = "Console"}}, consoleCommandRegistry{consoleCommandRegistry} {
log("Console ready. Type 'help' for a list of commands.");
}
@@ -7,8 +7,6 @@
namespace sophia {
void EcsInspectorPanel::onUpdate(const FrameInfo&) {
ImGui::Begin("ECS Inspector");
entityScratch.clear();
for (entt::entity entity : registry.storage<entt::entity>()) {
entityScratch.push_back(entity);
@@ -50,8 +48,6 @@ void EcsInspectorPanel::onUpdate(const FrameInfo&) {
} else {
ImGui::TextDisabled("No entity selected");
}
ImGui::End();
}
} // namespace sophia
+1 -1
View File
@@ -10,7 +10,7 @@ namespace sophia {
class EcsInspectorPanel : public Panel {
public:
explicit EcsInspectorPanel(entt::registry& registry)
: Panel{"ECS Inspector"}, registry{registry} {}
: Panel{{.name = "ECS Inspector"}}, registry{registry} {}
void onUpdate(const FrameInfo& frameInfo) override;
+1 -3
View File
@@ -9,7 +9,7 @@ namespace sophia {
class FrameStatsPanel : public Panel {
public:
FrameStatsPanel() : Panel{"Frame Stats"} {}
FrameStatsPanel() : Panel{{.name = "Frame Stats"}} {}
void onUpdate(const FrameInfo& frameInfo) override {
frameCount++;
@@ -25,10 +25,8 @@ class FrameStatsPanel : public Panel {
nextRefreshTime = frameInfo.elapsedTime + REFRESH_INTERVAL;
}
ImGui::Begin("Frame Stats");
ImGui::Text("FPS: %.1f", displayedFps);
ImGui::Text("Frame time: %.3f ms", displayedFrameTimeMs);
ImGui::End();
}
private:
@@ -5,7 +5,6 @@
namespace sophia {
void PanelRegistryPanel::onUpdate(const FrameInfo&) {
ImGui::Begin("PanelRegistry");
ImGui::Text("%u panels (%u visible)", registry.size(),
registry.visibleCount());
ImGui::Separator();
@@ -21,8 +20,6 @@ void PanelRegistryPanel::onUpdate(const FrameInfo&) {
panel->setVisible(visible);
}
}
ImGui::End();
}
} // namespace sophia
@@ -9,7 +9,7 @@ namespace sophia {
class PanelRegistryPanel : public Panel {
public:
explicit PanelRegistryPanel(PanelRegistry& registry)
: Panel{"PanelRegistry"}, registry{registry} {}
: Panel{{.name = "PanelRegistry"}}, registry{registry} {}
void onUpdate(const FrameInfo& frameInfo) override;