rename System/ImGuiConfig -> System/ImGui

and Add root window to use docking feature easily
This commit is contained in:
falsycat 2022-08-19 22:35:42 +09:00
parent 2c3a5fe182
commit dbbaac5c8e
5 changed files with 89 additions and 70 deletions

View File

@ -120,7 +120,7 @@ target_sources(nf7
file/sequencer_call.cc
file/sequencer_timeline.cc
file/system_dir.cc
file/system_imgui_config.cc
file/system_imgui.cc
file/system_logger.cc
file/system_native_file.cc
)

View File

@ -15,6 +15,10 @@ namespace nf7::gui {
class Window {
public:
static std::string ConcatId(nf7::File& f, const std::string& name) noexcept {
return f.abspath().Stringify() + " | " + std::string {name};
}
Window() = delete;
Window(File& owner, std::string_view title, const gui::Window* src = nullptr) noexcept :
owner_(&owner), title_(title),
@ -54,7 +58,7 @@ class Window {
}
std::string id() const noexcept {
return owner_->abspath().Stringify() + " | " + title_;
return ConcatId(*owner_, title_);
}
bool shown() const noexcept { return shown_; }

82
file/system_imgui.cc Normal file
View File

@ -0,0 +1,82 @@
#include <memory>
#include <string>
#include <string_view>
#include <typeinfo>
#include <utility>
#include <imgui.h>
#include <yas/serialize.hpp>
#include <yas/types/std/string.hpp>
#include <yas/types/std/string_view.hpp>
#include "nf7.hh"
#include "common/dir_item.hh"
#include "common/generic_type_info.hh"
#include "common/gui_window.hh"
#include "common/ptr_selector.hh"
using namespace std::literals;
namespace nf7 {
namespace {
class ImGui_ final : public nf7::File, public nf7::DirItem {
public:
static inline const nf7::GenericTypeInfo<ImGui_> kType = {"System/ImGui", {}};
ImGui_(nf7::Env& env) noexcept :
nf7::File(kType, env), nf7::DirItem(nf7::DirItem::kNone) {
}
ImGui_(nf7::Env& env, Deserializer& ar) noexcept : ImGui_(env) {
std::string config;
ar(config);
if (config.size() > 0) {
ImGui::LoadIniSettingsFromMemory(config.data(), config.size());
}
}
void Serialize(Serializer& ar) const noexcept override {
size_t n;
const char* config = ImGui::SaveIniSettingsToMemory(&n);
ar(std::string_view(config, n));
}
std::unique_ptr<nf7::File> Clone(nf7::Env& env) const noexcept override {
return std::make_unique<ImGui_>(env);
}
void Update() noexcept override;
nf7::File::Interface* interface(const std::type_info& t) noexcept override {
return nf7::InterfaceSelector<nf7::DirItem>(t).Select(this);
}
};
void ImGui_::Update() noexcept {
constexpr auto kFlags =
ImGuiWindowFlags_NoBackground |
ImGuiWindowFlags_NoBringToFrontOnFocus |
ImGuiWindowFlags_NoDecoration |
ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoNavFocus;
const auto id = nf7::gui::Window::ConcatId(*this, "Docking Root");
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {0, 0});
ImGui::SetNextWindowBgAlpha(0.f);
if (ImGui::Begin(id.c_str(), nullptr, kFlags)) {
const auto vp = ImGui::GetMainViewport();
ImGui::SetWindowPos({0, 0}, ImGuiCond_Always);
ImGui::SetWindowSize(vp->Size, ImGuiCond_Always);
ImGui::DockSpace(ImGui::GetID("DockSpace"), {0, 0},
ImGuiDockNodeFlags_PassthruCentralNode);
}
ImGui::End();
ImGui::PopStyleVar(1);
}
}
} // namespace nf7

View File

@ -1,67 +0,0 @@
#include <memory>
#include <string>
#include <string_view>
#include <typeinfo>
#include <utility>
#include <imgui.h>
#include <yas/serialize.hpp>
#include <yas/types/std/string.hpp>
#include <yas/types/std/string_view.hpp>
#include "nf7.hh"
#include "common/dir_item.hh"
#include "common/generic_type_info.hh"
#include "common/ptr_selector.hh"
using namespace std::literals;
namespace nf7 {
namespace {
class ImGuiConfig final : public File, public nf7::DirItem {
public:
static inline const GenericTypeInfo<ImGuiConfig> kType = {"System/ImGuiConfig", {}};
ImGuiConfig(Env& env) noexcept :
File(kType, env), DirItem(DirItem::kMenu) {
}
ImGuiConfig(Env& env, Deserializer& ar) noexcept : ImGuiConfig(env) {
std::string buf;
ar(buf);
if (buf.empty()) return;
ImGui::LoadIniSettingsFromMemory(buf.data(), buf.size());
}
void Serialize(Serializer& ar) const noexcept override {
if (std::exchange(const_cast<bool&>(skip_save_), false)) {
ar(""s);
} else {
size_t n;
const char* ini = ImGui::SaveIniSettingsToMemory(&n);
ar(std::string_view(ini, n));
}
}
std::unique_ptr<File> Clone(Env& env) const noexcept override {
return std::make_unique<ImGuiConfig>(env);
}
void UpdateMenu() noexcept override;
File::Interface* interface(const std::type_info& t) noexcept override {
return InterfaceSelector<nf7::DirItem>(t).Select(this);
}
private:
bool skip_save_ = false;
};
void ImGuiConfig::UpdateMenu() noexcept {
ImGui::MenuItem("skip next serialization", nullptr, &skip_save_);
}
}
} // namespace nf7

View File

@ -47,7 +47,7 @@ int main(void) {
{ "_audio"s,
Write(ar, "Audio/Context"s) },
{ "_imgui"s,
Write(ar, "System/ImGuiConfig"s, ""s) },
Write(ar, "System/ImGui"s, ""s) },
{ "_logger"s,
Write(ar, "System/Logger"s, WINDOW_(true), 1024, false, false) },
{ "_luajit"s,