From dbbaac5c8e3e115975d44ec67021c8d2ac54ddff Mon Sep 17 00:00:00 2001 From: falsycat Date: Fri, 19 Aug 2022 22:35:42 +0900 Subject: [PATCH] rename System/ImGuiConfig -> System/ImGui and Add root window to use docking feature easily --- CMakeLists.txt | 2 +- common/gui_window.hh | 6 ++- file/system_imgui.cc | 82 +++++++++++++++++++++++++++++++++++++ file/system_imgui_config.cc | 67 ------------------------------ tool/init.cc | 2 +- 5 files changed, 89 insertions(+), 70 deletions(-) create mode 100644 file/system_imgui.cc delete mode 100644 file/system_imgui_config.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 3f2d27e..46fb5ab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 ) diff --git a/common/gui_window.hh b/common/gui_window.hh index 7aaaa0f..23e3aa9 100644 --- a/common/gui_window.hh +++ b/common/gui_window.hh @@ -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_; } diff --git a/file/system_imgui.cc b/file/system_imgui.cc new file mode 100644 index 0000000..148c95d --- /dev/null +++ b/file/system_imgui.cc @@ -0,0 +1,82 @@ +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + +#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 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 Clone(nf7::Env& env) const noexcept override { + return std::make_unique(env); + } + + void Update() noexcept override; + + nf7::File::Interface* interface(const std::type_info& t) noexcept override { + return nf7::InterfaceSelector(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 diff --git a/file/system_imgui_config.cc b/file/system_imgui_config.cc deleted file mode 100644 index 8455cec..0000000 --- a/file/system_imgui_config.cc +++ /dev/null @@ -1,67 +0,0 @@ -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#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 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(skip_save_), false)) { - ar(""s); - } else { - size_t n; - const char* ini = ImGui::SaveIniSettingsToMemory(&n); - ar(std::string_view(ini, n)); - } - } - std::unique_ptr Clone(Env& env) const noexcept override { - return std::make_unique(env); - } - - void UpdateMenu() noexcept override; - - File::Interface* interface(const std::type_info& t) noexcept override { - return InterfaceSelector(t).Select(this); - } - - private: - bool skip_save_ = false; -}; - -void ImGuiConfig::UpdateMenu() noexcept { - ImGui::MenuItem("skip next serialization", nullptr, &skip_save_); -} - -} -} // namespace nf7 diff --git a/tool/init.cc b/tool/init.cc index 7264d72..4e13ada 100644 --- a/tool/init.cc +++ b/tool/init.cc @@ -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,