From 5dfd8c89a37faf5b19934a24c8d0650d86d5d6b1 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sat, 3 Aug 2024 22:00:47 +0200 Subject: [PATCH] feat: Display pattern descriptions in file chooser and pattern popup --- .../content/views/view_data_processor.hpp | 2 - .../content/views/view_pattern_editor.hpp | 36 ++++++++++++--- .../ui/include/popups/popup_file_chooser.hpp | 45 ++++++++++++------- 3 files changed, 60 insertions(+), 23 deletions(-) diff --git a/plugins/builtin/include/content/views/view_data_processor.hpp b/plugins/builtin/include/content/views/view_data_processor.hpp index b7abbf15d..08225f8f6 100644 --- a/plugins/builtin/include/content/views/view_data_processor.hpp +++ b/plugins/builtin/include/content/views/view_data_processor.hpp @@ -18,8 +18,6 @@ namespace hex::plugin::builtin { struct Workspace { Workspace() = default; - - std::unique_ptr context = { []{ ImNodesContext *ctx = ImNodes::CreateContext(); ctx->Style = ImNodes::GetStyle(); diff --git a/plugins/builtin/include/content/views/view_pattern_editor.hpp b/plugins/builtin/include/content/views/view_pattern_editor.hpp index 0554e4c8f..fbc6d9c11 100644 --- a/plugins/builtin/include/content/views/view_pattern_editor.hpp +++ b/plugins/builtin/include/content/views/view_pattern_editor.hpp @@ -261,6 +261,8 @@ namespace hex::plugin::builtin { bool m_parentHighlightingEnabled = true; bool m_replaceMode = false; + std::map m_patternNames; + static inline std::array m_findHistory; static inline u32 m_findHistorySize = 0; @@ -281,7 +283,7 @@ namespace hex::plugin::builtin { void drawFindReplaceDialog(std::string &findWord, bool &requestFocus, u64 &position, u64 &count, bool &updateCount); - void historyInsert(std::array &history,u32 &size, u32 &index, const std::string &value); + void historyInsert(std::array &history, u32 &size, u32 &index, const std::string &value); void loadPatternFile(const std::fs::path &path, prv::Provider *provider); @@ -306,12 +308,34 @@ namespace hex::plugin::builtin { paths.push_back(entry.path()); } } - ui::PopupFileChooser::open( - basePaths, paths, std::vector{ { "Pattern File", "hexpat" } }, false, - [this, provider](const std::fs::path &path) { - this->loadPatternFile(path, provider); - AchievementManager::unlockAchievement("hex.builtin.achievement.patterns", "hex.builtin.achievement.patterns.load_existing.name"); + + ui::PopupNamedFileChooser::open( + basePaths, paths, std::vector{ { "Pattern File", "hexpat" } }, false, + [this, provider](const std::fs::path &path, const std::fs::path &adjustedPath) mutable -> std::string { + auto it = m_patternNames.find(path); + if (it != m_patternNames.end()) { + return it->second; } + + const auto fileName = wolv::util::toUTF8String(adjustedPath.filename()); + m_patternNames[path] = fileName; + + pl::PatternLanguage runtime; + ContentRegistry::PatternLanguage::configureRuntime(runtime, provider); + runtime.addPragma("description", [&](pl::PatternLanguage &, const std::string &value) -> bool { + m_patternNames[path] = hex::format("{} ({})", value, fileName); + return true; + }); + + wolv::io::File file(path, wolv::io::File::Mode::Read); + hex::unused(runtime.preprocessString(file.readString(), pl::api::Source::DefaultSource)); + + return m_patternNames[path]; + }, + [this, provider](const std::fs::path &path) { + this->loadPatternFile(path, provider); + AchievementManager::unlockAchievement("hex.builtin.achievement.patterns", "hex.builtin.achievement.patterns.load_existing.name"); + } ); }; diff --git a/plugins/ui/include/popups/popup_file_chooser.hpp b/plugins/ui/include/popups/popup_file_chooser.hpp index ad58cd2cc..d9701f252 100644 --- a/plugins/ui/include/popups/popup_file_chooser.hpp +++ b/plugins/ui/include/popups/popup_file_chooser.hpp @@ -17,10 +17,10 @@ namespace hex::ui { template class PopupNamedFileChooserBase : public Popup { public: - PopupNamedFileChooserBase(const std::vector &basePaths, const std::vector &files, const std::vector &validExtensions, bool multiple, const std::function &nameCallback, const std::function &callback) + PopupNamedFileChooserBase(const std::vector &basePaths, const std::vector &files, const std::vector &validExtensions, bool multiple, const std::function &callback) : hex::Popup("hex.ui.common.choose_file"), + m_files(files), m_selectedFiles({ }), - m_nameCallback(nameCallback), m_openCallback(callback), m_validExtensions(validExtensions), m_multiple(multiple) { @@ -37,11 +37,11 @@ namespace hex::ui { if (adjustedPath.empty()) adjustedPath = path.filename(); - m_files.push_back({ path, adjustedPath }); + m_adjustedPaths[path] = adjustedPath; } std::sort(m_files.begin(), m_files.end(), [](const auto &a, const auto &b) { - return a.first < b.first; + return a < b; }); } @@ -59,9 +59,9 @@ namespace hex::ui { if (ImGui::BeginListBox("##files", scaled(ImVec2(500, 400)))) { for (auto fileIt = m_files.begin(); fileIt != m_files.end(); ++fileIt) { - const auto &[path, pathName] = *fileIt; + const auto &path = *fileIt; - const auto &pathNameString = m_nameCallback(pathName); + const auto &pathNameString = getEntryName(path); if (!m_filter.empty() && !pathNameString.contains(m_filter)) continue; @@ -94,7 +94,7 @@ namespace hex::ui { if (ImGui::Button("hex.ui.common.open"_lang) || doubleClicked) { for (const auto &it : m_selectedFiles) - m_openCallback(it->first); + m_openCallback(*it); Popup::close(); } @@ -115,6 +115,14 @@ namespace hex::ui { return ImGuiWindowFlags_AlwaysAutoResize; } + + protected: + const std::fs::path& getAdjustedPath(const std::fs::path &path) const { + return m_adjustedPaths.at(path); + } + + virtual std::string getEntryName(const std::fs::path &path) = 0; + private: static bool isSubpath(const std::fs::path &basePath, const std::fs::path &path) { auto relativePath = std::fs::relative(path, basePath); @@ -124,9 +132,9 @@ namespace hex::ui { private: std::string m_filter; - std::vector> m_files; - std::set>::const_iterator> m_selectedFiles; - std::function m_nameCallback; + std::vector m_files; + std::map m_adjustedPaths; + std::set::const_iterator> m_selectedFiles; std::function m_openCallback; std::vector m_validExtensions; bool m_multiple = false; @@ -135,17 +143,24 @@ namespace hex::ui { class PopupNamedFileChooser : public PopupNamedFileChooserBase { public: - PopupNamedFileChooser(const std::vector &basePaths, const std::vector &files, const std::vector &validExtensions, bool multiple, const std::function &nameCallback, const std::function &callback) - : PopupNamedFileChooserBase(basePaths, files, validExtensions, multiple, nameCallback, callback) { } + PopupNamedFileChooser(const std::vector &basePaths, const std::vector &files, const std::vector &validExtensions, bool multiple, const std::function &nameCallback, const std::function &callback) + : PopupNamedFileChooserBase(basePaths, files, validExtensions, multiple, callback), m_nameCallback(nameCallback) { } + + std::string getEntryName(const std::fs::path &path) override { + return m_nameCallback(path, getAdjustedPath(path)); + } + + private: + std::function m_nameCallback; }; class PopupFileChooser : public PopupNamedFileChooserBase { public: PopupFileChooser(const std::vector &basePaths, const std::vector &files, const std::vector &validExtensions, bool multiple, const std::function &callback) - : PopupNamedFileChooserBase(basePaths, files, validExtensions, multiple, nameCallback, callback) { } + : PopupNamedFileChooserBase(basePaths, files, validExtensions, multiple, callback) { } - static std::string nameCallback(const std::fs::path &path) { - return wolv::util::toUTF8String(path); + std::string getEntryName(const std::fs::path &path) override { + return wolv::util::toUTF8String(getAdjustedPath(path)); } };