From 9c9ac23818a111a7b99a34041ad572e29bc2efa6 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Fri, 7 Apr 2023 10:21:27 +0200 Subject: [PATCH] feat: Added a much more flexible popup system --- lib/libimhex/CMakeLists.txt | 55 +++++++++++----------- lib/libimhex/include/hex/ui/popup.hpp | 66 +++++++++++++++++++++++++++ lib/libimhex/source/ui/popup.cpp | 7 +++ main/source/window/window.cpp | 24 ++++++++++ plugins/builtin/romfs/lang/en_US.json | 2 +- plugins/builtin/romfs/lang/ja_JP.json | 2 +- plugins/builtin/romfs/lang/zh_TW.json | 2 +- 7 files changed, 128 insertions(+), 30 deletions(-) create mode 100644 lib/libimhex/include/hex/ui/popup.hpp create mode 100644 lib/libimhex/source/ui/popup.cpp diff --git a/lib/libimhex/CMakeLists.txt b/lib/libimhex/CMakeLists.txt index d622c2ed5..16420d780 100644 --- a/lib/libimhex/CMakeLists.txt +++ b/lib/libimhex/CMakeLists.txt @@ -5,37 +5,38 @@ set(CMAKE_CXX_STANDARD 23) set(CMAKE_SHARED_LIBRARY_PREFIX "") set(LIBIMHEX_SOURCES - source/api/event.cpp - source/api/imhex_api.cpp - source/api/content_registry.cpp - source/api/task.cpp - source/api/keybinding.cpp - source/api/plugin_manager.cpp - source/api/localization.cpp - source/api/project_file_manager.cpp - source/api/theme_manager.cpp + source/api/event.cpp + source/api/imhex_api.cpp + source/api/content_registry.cpp + source/api/task.cpp + source/api/keybinding.cpp + source/api/plugin_manager.cpp + source/api/localization.cpp + source/api/project_file_manager.cpp + source/api/theme_manager.cpp - source/data_processor/attribute.cpp - source/data_processor/link.cpp - source/data_processor/node.cpp + source/data_processor/attribute.cpp + source/data_processor/link.cpp + source/data_processor/node.cpp - source/helpers/utils.cpp - source/helpers/fs.cpp - source/helpers/magic.cpp - source/helpers/crypto.cpp - source/helpers/http_requests.cpp - source/helpers/opengl.cpp - source/helpers/patches.cpp - source/helpers/encoding_file.cpp - source/helpers/logger.cpp - source/helpers/stacktrace.cpp - source/helpers/tar.cpp + source/helpers/utils.cpp + source/helpers/fs.cpp + source/helpers/magic.cpp + source/helpers/crypto.cpp + source/helpers/http_requests.cpp + source/helpers/opengl.cpp + source/helpers/patches.cpp + source/helpers/encoding_file.cpp + source/helpers/logger.cpp + source/helpers/stacktrace.cpp + source/helpers/tar.cpp - source/providers/provider.cpp + source/providers/provider.cpp - source/ui/imgui_imhex_extensions.cpp - source/ui/view.cpp - ) + source/ui/imgui_imhex_extensions.cpp + source/ui/view.cpp + source/ui/popup.cpp +) if (APPLE) set(OSX_11_0_SDK_PATH /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk) diff --git a/lib/libimhex/include/hex/ui/popup.hpp b/lib/libimhex/include/hex/ui/popup.hpp new file mode 100644 index 000000000..01bf8898a --- /dev/null +++ b/lib/libimhex/include/hex/ui/popup.hpp @@ -0,0 +1,66 @@ +#pragma once + +#include + +#include +#include +#include + +#include + +namespace hex { + + class PopupBase { + public: + explicit PopupBase(std::string unlocalizedName, bool closeButton = true) + : m_unlocalizedName(std::move(unlocalizedName)), m_closeButton(closeButton) { } + + virtual ~PopupBase() = default; + + virtual void drawContent() = 0; + [[nodiscard]] virtual ImGuiPopupFlags getFlags() const { return ImGuiPopupFlags_None; } + + + [[nodiscard]] static std::vector> &getOpenPopups() { + return s_openPopups; + } + + [[nodiscard]] const std::string &getUnlocalizedName() const { + return this->m_unlocalizedName; + } + + [[nodiscard]] bool hasCloseButton() const { + return this->m_closeButton; + } + + protected: + static void close() { + ImGui::CloseCurrentPopup(); + s_openPopups.pop_back(); + } + + static std::vector> s_openPopups; + private: + + std::string m_unlocalizedName; + bool m_closeButton; + }; + + template + class Popup : public PopupBase { + protected: + explicit Popup(std::string unlocalizedName, bool closeButton = true) : PopupBase(std::move(unlocalizedName), closeButton) { } + + public: + virtual ~Popup() = default; + + template + static void open(Args && ... args) { + auto popup = std::make_unique(std::forward(args)...); + + s_openPopups.emplace_back(std::move(popup)); + } + + }; + +} \ No newline at end of file diff --git a/lib/libimhex/source/ui/popup.cpp b/lib/libimhex/source/ui/popup.cpp new file mode 100644 index 000000000..bc2a76515 --- /dev/null +++ b/lib/libimhex/source/ui/popup.cpp @@ -0,0 +1,7 @@ +#include + +namespace hex { + + std::vector> PopupBase::s_openPopups; + +} \ No newline at end of file diff --git a/main/source/window/window.cpp b/main/source/window/window.cpp index e8f7f1a9b..c8055e1d6 100644 --- a/main/source/window/window.cpp +++ b/main/source/window/window.cpp @@ -11,6 +11,9 @@ #include #include +#include +#include + #include #include #include @@ -520,6 +523,27 @@ namespace hex { }); } + // Draw popup stack + { + if (auto &popups = PopupBase::getOpenPopups(); !popups.empty()) { + auto &currPopup = popups.back(); + const auto &name = LangEntry(currPopup->getUnlocalizedName()); + + if (!ImGui::IsPopupOpen(ImGuiID(0), ImGuiPopupFlags_AnyPopupId)) + ImGui::OpenPopup(name); + + bool open = true; + if (ImGui::BeginPopupModal(name, currPopup->hasCloseButton() ? &open : nullptr, currPopup->getFlags())) { + currPopup->drawContent(); + + ImGui::EndPopup(); + } + + if (!open) + popups.pop_back(); + } + } + // Run all deferred calls TaskManager::runDeferredCalls(); diff --git a/plugins/builtin/romfs/lang/en_US.json b/plugins/builtin/romfs/lang/en_US.json index 6ed42aa7c..013803194 100644 --- a/plugins/builtin/romfs/lang/en_US.json +++ b/plugins/builtin/romfs/lang/en_US.json @@ -774,7 +774,7 @@ "hex.builtin.view.patches.remove": "Remove patch", "hex.builtin.view.pattern_data.name": "Pattern Data", "hex.builtin.view.pattern_editor.accept_pattern": "Accept pattern", - "hex.builtin.view.pattern_editor.accept_pattern.desc": "One or more pattern_language compatible with this data type has been found", + "hex.builtin.view.pattern_editor.accept_pattern.desc": "One or more Patterns compatible with this data type has been found", "hex.builtin.view.pattern_editor.accept_pattern.pattern_language": "Patterns", "hex.builtin.view.pattern_editor.accept_pattern.question": "Do you want to apply the selected pattern?", "hex.builtin.view.pattern_editor.auto": "Auto evaluate", diff --git a/plugins/builtin/romfs/lang/ja_JP.json b/plugins/builtin/romfs/lang/ja_JP.json index 7facdeba5..35da2f878 100644 --- a/plugins/builtin/romfs/lang/ja_JP.json +++ b/plugins/builtin/romfs/lang/ja_JP.json @@ -767,7 +767,7 @@ "hex.builtin.view.patches.remove": "パッチを削除", "hex.builtin.view.pattern_data.name": "パターンデータ", "hex.builtin.view.pattern_editor.accept_pattern": "使用できるパターン", - "hex.builtin.view.pattern_editor.accept_pattern.desc": "このデータ型と互換性のある pattern_language が1つ以上見つかりました", + "hex.builtin.view.pattern_editor.accept_pattern.desc": "このデータ型と互換性のある Patterns が1つ以上見つかりました", "hex.builtin.view.pattern_editor.accept_pattern.pattern_language": "パターン", "hex.builtin.view.pattern_editor.accept_pattern.question": "選択したパターンを反映してよろしいですか?", "hex.builtin.view.pattern_editor.auto": "常に実行", diff --git a/plugins/builtin/romfs/lang/zh_TW.json b/plugins/builtin/romfs/lang/zh_TW.json index 4d5e90e3c..8b7286797 100644 --- a/plugins/builtin/romfs/lang/zh_TW.json +++ b/plugins/builtin/romfs/lang/zh_TW.json @@ -767,7 +767,7 @@ "hex.builtin.view.patches.remove": "Remove patch", "hex.builtin.view.pattern_data.name": "模式資料", "hex.builtin.view.pattern_editor.accept_pattern": "接受模式", - "hex.builtin.view.pattern_editor.accept_pattern.desc": "已找到一或多個與此資料類型相容的 pattern_language", + "hex.builtin.view.pattern_editor.accept_pattern.desc": "已找到一或多個與此資料類型相容的 Patterns", "hex.builtin.view.pattern_editor.accept_pattern.pattern_language": "模式", "hex.builtin.view.pattern_editor.accept_pattern.question": "您要套用所選的模式嗎?", "hex.builtin.view.pattern_editor.auto": "自動評估",