Supporting setting panel layout #7
@@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
class SceneHierarchyPanel : public sophia::Panel {
|
class SceneHierarchyPanel : public sophia::Panel {
|
||||||
public:
|
public:
|
||||||
SceneHierarchyPanel() : Panel{{.name = "Scene Hierarchy"}} {}
|
SceneHierarchyPanel() : Panel{{.name = "Scene Hierarchy", .locked = true}} {}
|
||||||
|
|
||||||
void onUpdate(const sophia::FrameInfo& frameInfo) override;
|
void onUpdate(const sophia::FrameInfo& frameInfo) override;
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <imgui.h>
|
||||||
|
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <sophia/frame_info.hpp>
|
#include <sophia/frame_info.hpp>
|
||||||
@@ -11,9 +13,11 @@ namespace sophia {
|
|||||||
class Panel {
|
class Panel {
|
||||||
public:
|
public:
|
||||||
struct Config {
|
struct Config {
|
||||||
std::string name;
|
const std::string name;
|
||||||
|
|
||||||
|
ImGuiWindowFlags windowFlags = ImGuiWindowFlags_None;
|
||||||
|
ImGuiDockNodeFlags dockNodeFlags = ImGuiDockNodeFlags_None;
|
||||||
|
|
||||||
// Locked panels can't be moved, resized, docked, collapsed or closed.
|
|
||||||
bool locked = false;
|
bool locked = false;
|
||||||
bool closable = true;
|
bool closable = true;
|
||||||
|
|
||||||
@@ -21,20 +25,27 @@ class Panel {
|
|||||||
// every frame when locked.
|
// every frame when locked.
|
||||||
glm::vec2 size{0.0f, 0.0f};
|
glm::vec2 size{0.0f, 0.0f};
|
||||||
std::optional<glm::vec2> position;
|
std::optional<glm::vec2> position;
|
||||||
|
|
||||||
|
ImGuiWindowFlags getWindowFlags() const;
|
||||||
|
ImGuiDockNodeFlags getDockNodeFlags() const;
|
||||||
|
bool hasCloseButton() const;
|
||||||
|
ImGuiCond getPlacementCond() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
Panel(const Panel&) = delete;
|
Panel(const Panel&) = delete;
|
||||||
Panel& operator=(const Panel&) = delete;
|
Panel& operator=(const Panel&) = delete;
|
||||||
|
|
||||||
Panel(Panel&&) noexcept = default;
|
Panel(Panel&&) noexcept = default;
|
||||||
Panel& operator=(Panel&&) noexcept = default;
|
Panel& operator=(Panel&&) = delete;
|
||||||
|
|
||||||
explicit Panel(Config config) : config{std::move(config)} {}
|
explicit Panel(Config config) : config{std::move(config)} {}
|
||||||
virtual ~Panel() = default;
|
virtual ~Panel() = default;
|
||||||
|
|
||||||
// Owns the ImGui window: Begin, onUpdate, End.
|
|
||||||
void draw(const FrameInfo& frameInfo);
|
void draw(const FrameInfo& frameInfo);
|
||||||
|
|
||||||
|
Config& getConfig() { return config; }
|
||||||
|
const Config& getConfig() const { return config; }
|
||||||
|
|
||||||
const std::string& getName() const { return config.name; }
|
const std::string& getName() const { return config.name; }
|
||||||
bool isLocked() const { return config.locked; }
|
bool isLocked() const { return config.locked; }
|
||||||
|
|
||||||
@@ -43,7 +54,6 @@ class Panel {
|
|||||||
void toggleVisible() { this->visible = !this->visible; }
|
void toggleVisible() { this->visible = !this->visible; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// Draw the panel's contents; the window is already open.
|
|
||||||
virtual void onUpdate(const FrameInfo& frameInfo) = 0;
|
virtual void onUpdate(const FrameInfo& frameInfo) = 0;
|
||||||
|
|
||||||
bool visible = true;
|
bool visible = true;
|
||||||
|
|||||||
+33
-8
@@ -1,18 +1,43 @@
|
|||||||
#include <imgui.h>
|
#include <imgui.h>
|
||||||
|
#include <imgui_internal.h>
|
||||||
|
|
||||||
#include <sophia/ui/panel.hpp>
|
#include <sophia/ui/panel.hpp>
|
||||||
|
|
||||||
namespace sophia {
|
namespace sophia {
|
||||||
|
|
||||||
void Panel::draw(const FrameInfo& frameInfo) {
|
ImGuiWindowFlags Panel::Config::getWindowFlags() const {
|
||||||
ImGuiWindowFlags flags = ImGuiWindowFlags_None;
|
ImGuiWindowFlags flags = windowFlags;
|
||||||
if (config.locked) {
|
if (locked) {
|
||||||
flags |= ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize |
|
flags |= ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize |
|
||||||
ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_NoCollapse;
|
ImGuiWindowFlags_NoCollapse;
|
||||||
}
|
}
|
||||||
|
return flags;
|
||||||
|
}
|
||||||
|
|
||||||
const ImGuiCond cond =
|
ImGuiDockNodeFlags Panel::Config::getDockNodeFlags() const {
|
||||||
config.locked ? ImGuiCond_Always : ImGuiCond_FirstUseEver;
|
ImGuiDockNodeFlags flags = dockNodeFlags;
|
||||||
|
if (locked) {
|
||||||
|
flags |= static_cast<int>(ImGuiDockNodeFlags_NoUndocking) |
|
||||||
|
static_cast<int>(ImGuiDockNodeFlags_NoResize) |
|
||||||
|
static_cast<int>(ImGuiDockNodeFlags_NoCloseButton) |
|
||||||
|
static_cast<int>(ImGuiDockNodeFlags_NoDockingOverMe) |
|
||||||
|
static_cast<int>(ImGuiDockNodeFlags_NoDockingSplit);
|
||||||
|
}
|
||||||
|
return flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Panel::Config::hasCloseButton() const { return closable && !locked; }
|
||||||
|
|
||||||
|
ImGuiCond Panel::Config::getPlacementCond() const {
|
||||||
|
return locked ? ImGuiCond_Always : ImGuiCond_FirstUseEver;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Panel::draw(const FrameInfo& frameInfo) {
|
||||||
|
ImGuiWindowClass windowClass;
|
||||||
|
windowClass.DockNodeFlagsOverrideSet = config.getDockNodeFlags();
|
||||||
|
ImGui::SetNextWindowClass(&windowClass);
|
||||||
|
|
||||||
|
const ImGuiCond cond = config.getPlacementCond();
|
||||||
if (config.size.x > 0.0f || config.size.y > 0.0f) {
|
if (config.size.x > 0.0f || config.size.y > 0.0f) {
|
||||||
ImGui::SetNextWindowSize(ImVec2(config.size.x, config.size.y), cond);
|
ImGui::SetNextWindowSize(ImVec2(config.size.x, config.size.y), cond);
|
||||||
}
|
}
|
||||||
@@ -22,8 +47,8 @@ void Panel::draw(const FrameInfo& frameInfo) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool open = true;
|
bool open = true;
|
||||||
const bool showCloseButton = config.closable && !config.locked;
|
ImGui::Begin(config.name.c_str(), config.hasCloseButton() ? &open : nullptr,
|
||||||
ImGui::Begin(config.name.c_str(), showCloseButton ? &open : nullptr, flags);
|
config.getWindowFlags());
|
||||||
onUpdate(frameInfo);
|
onUpdate(frameInfo);
|
||||||
ImGui::End();
|
ImGui::End();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user