making config editable
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <imgui.h>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <optional>
|
||||
#include <sophia/frame_info.hpp>
|
||||
@@ -11,9 +13,11 @@ namespace sophia {
|
||||
class Panel {
|
||||
public:
|
||||
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 closable = true;
|
||||
|
||||
@@ -21,20 +25,27 @@ class Panel {
|
||||
// every frame when locked.
|
||||
glm::vec2 size{0.0f, 0.0f};
|
||||
std::optional<glm::vec2> position;
|
||||
|
||||
ImGuiWindowFlags getWindowFlags() const;
|
||||
ImGuiDockNodeFlags getDockNodeFlags() const;
|
||||
bool hasCloseButton() const;
|
||||
ImGuiCond getPlacementCond() const;
|
||||
};
|
||||
|
||||
Panel(const Panel&) = delete;
|
||||
Panel& operator=(const Panel&) = delete;
|
||||
|
||||
Panel(Panel&&) noexcept = default;
|
||||
Panel& operator=(Panel&&) noexcept = default;
|
||||
Panel& operator=(Panel&&) = delete;
|
||||
|
||||
explicit Panel(Config config) : config{std::move(config)} {}
|
||||
virtual ~Panel() = default;
|
||||
|
||||
// Owns the ImGui window: Begin, onUpdate, End.
|
||||
void draw(const FrameInfo& frameInfo);
|
||||
|
||||
Config& getConfig() { return config; }
|
||||
const Config& getConfig() const { return config; }
|
||||
|
||||
const std::string& getName() const { return config.name; }
|
||||
bool isLocked() const { return config.locked; }
|
||||
|
||||
@@ -43,7 +54,6 @@ class Panel {
|
||||
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;
|
||||
|
||||
+33
-8
@@ -1,18 +1,43 @@
|
||||
#include <imgui.h>
|
||||
#include <imgui_internal.h>
|
||||
|
||||
#include <sophia/ui/panel.hpp>
|
||||
|
||||
namespace sophia {
|
||||
|
||||
void Panel::draw(const FrameInfo& frameInfo) {
|
||||
ImGuiWindowFlags flags = ImGuiWindowFlags_None;
|
||||
if (config.locked) {
|
||||
ImGuiWindowFlags Panel::Config::getWindowFlags() const {
|
||||
ImGuiWindowFlags flags = windowFlags;
|
||||
if (locked) {
|
||||
flags |= ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize |
|
||||
ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_NoCollapse;
|
||||
ImGuiWindowFlags_NoCollapse;
|
||||
}
|
||||
return flags;
|
||||
}
|
||||
|
||||
const ImGuiCond cond =
|
||||
config.locked ? ImGuiCond_Always : ImGuiCond_FirstUseEver;
|
||||
ImGuiDockNodeFlags Panel::Config::getDockNodeFlags() const {
|
||||
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) {
|
||||
ImGui::SetNextWindowSize(ImVec2(config.size.x, config.size.y), cond);
|
||||
}
|
||||
@@ -22,8 +47,8 @@ void Panel::draw(const FrameInfo& frameInfo) {
|
||||
}
|
||||
|
||||
bool open = true;
|
||||
const bool showCloseButton = config.closable && !config.locked;
|
||||
ImGui::Begin(config.name.c_str(), showCloseButton ? &open : nullptr, flags);
|
||||
ImGui::Begin(config.name.c_str(), config.hasCloseButton() ? &open : nullptr,
|
||||
config.getWindowFlags());
|
||||
onUpdate(frameInfo);
|
||||
ImGui::End();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user