changes for ImGui

This commit is contained in:
2026-09-15 02:23:22 -07:00
parent 12a1e6eca9
commit 05fe3c2ee8
14 changed files with 593 additions and 6 deletions
+14 -6
View File
@@ -3,10 +3,13 @@
#include <cstdlib>
#include <iostream>
#include <memory>
#include <sophia/asset_registry.hpp>
#include <sophia/camera_2d.hpp>
#include <sophia/canvas.hpp>
#include <sophia/frame_info.hpp>
#include <sophia/types.hpp>
#include <sophia/ui/debug_ui.hpp>
#include <sophia/ui/panel_registry.hpp>
#include <string>
#include <vector>
@@ -27,7 +30,7 @@ class Application {
Application(const Application&) = delete;
Application& operator=(const Application&) = delete;
Application(const Config& config);
explicit Application(const Config& config);
virtual ~Application();
void run();
@@ -40,15 +43,15 @@ class Application {
virtual void onRender() {}
virtual void onDestroy() {}
AssetRegistry& assets() { return this->assetRegistry; }
PanelRegistry& panels() { return this->panelRegistry; }
DebugUI& debugUI() { return this->_debugUI; }
Canvas canvas;
Camera2D camera;
std::vector<std::string> args;
// Creates a mesh that persists across frames, for callers that want to
// draw the same geometry many times with different transforms (e.g.
// instancing) instead of resubmitting vertices every frame via Canvas.
MeshHandle createMesh(std::vector<CanvasVertex> vertices,
std::vector<u32> indices);
void drawMesh(MeshHandle handle, const glm::mat4& transform);
private:
@@ -59,6 +62,11 @@ class Application {
class Impl;
std::unique_ptr<Impl> impl;
AssetRegistry assetRegistry;
PanelRegistry panelRegistry;
DebugUI _debugUI;
};
} // namespace sophia
+29
View File
@@ -0,0 +1,29 @@
#pragma once
#include <sophia/handles.hpp>
#include <sophia/ui/panel_registry.hpp>
namespace sophia {
class DebugUI {
public:
DebugUI(const DebugUI&) = delete;
DebugUI& operator=(const DebugUI&) = delete;
explicit DebugUI(PanelRegistry& registry);
~DebugUI() = default;
void show();
void hide();
void showPanelRegistry(bool show = true);
void showFrameStats(bool show = true);
private:
PanelRegistry& registry;
PanelHandle panelRegistryHandle;
PanelHandle frameStatsHandle;
};
} // namespace sophia
+35
View File
@@ -0,0 +1,35 @@
#pragma once
#include <sophia/frame_info.hpp>
#include <string>
#include <utility>
namespace sophia {
class Panel {
public:
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)} {}
virtual ~Panel() = default;
virtual void onUpdate(const FrameInfo& frameInfo) = 0;
const std::string& getName() const { return name; }
bool isVisible() const { return this->visible; }
void setVisible(bool visible) { this->visible = visible; }
void toggleVisible() { this->visible = !this->visible; }
protected:
bool visible = true;
private:
std::string name;
};
} // namespace sophia
+107
View File
@@ -0,0 +1,107 @@
#pragma once
#include <memory>
#include <sophia/frame_info.hpp>
#include <sophia/handles.hpp>
#include <sophia/ui/panel.hpp>
#include <sophia/util/assert.hpp>
#include <string_view>
#include <typeinfo>
#include <utility>
#include <vector>
namespace sophia {
class PanelRegistry {
public:
PanelRegistry(const PanelRegistry&) = delete;
PanelRegistry& operator=(const PanelRegistry&) = delete;
PanelRegistry() = default;
~PanelRegistry() = default;
PanelHandle add(std::unique_ptr<Panel> panel);
template <typename T, typename... Args>
PanelHandle emplace(Args&&... args) {
return add(std::make_unique<T>(std::forward<Args>(args)...));
}
const Panel& get(PanelHandle handle) const;
Panel& get(PanelHandle handle) {
return const_cast<Panel&>(std::as_const(*this).get(handle));
}
template <typename T>
const T& get(PanelHandle handle) const {
const Panel& panel = get(handle);
SOPHIA_ASSERT(dynamic_cast<const T*>(&panel) != nullptr,
"PanelHandle does not refer to a", typeid(T).name());
return static_cast<const T&>(panel);
}
template <typename T>
T& get(PanelHandle handle) {
return const_cast<T&>(std::as_const(*this).template get<T>(handle));
}
void remove(PanelHandle handle);
bool isValid(PanelHandle handle) const;
template <typename T>
const T* find() const {
for (const Slot& slot : slots) {
if (slot.panel) {
if (const T* panel = dynamic_cast<const T*>(slot.panel.get())) {
return panel;
}
}
}
return nullptr;
}
template <typename T>
T* find() {
return const_cast<T*>(std::as_const(*this).template find<T>());
}
const Panel* find(std::string_view name) const {
for (const Slot& slot : slots) {
if (slot.panel && slot.panel->getName() == name) {
return slot.panel.get();
}
}
return nullptr;
}
Panel* find(std::string_view name) {
return const_cast<Panel*>(std::as_const(*this).find(name));
}
u32 size() const;
u32 visibleCount() const;
std::vector<std::string_view> names() const {
std::vector<std::string_view> result;
result.reserve(slots.size());
for (const Slot& slot : slots) {
if (slot.panel) {
result.push_back(slot.panel->getName());
}
}
return result;
}
void update(const FrameInfo& frameInfo);
private:
struct Slot {
std::unique_ptr<Panel> panel;
u32 generation = 0;
};
std::vector<Slot> slots;
std::vector<u32> freeList;
};
} // namespace sophia