Supporting setting panel layout
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user