From 49610f59ea18ddaccc3c5a391da50813cf7965c1 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 23 Jan 2022 21:52:43 +0100 Subject: [PATCH] ux: Added interface to choose encoding files more easily --- .../include/hex/helpers/shared_data.hpp | 5 +++ lib/libimhex/include/hex/views/view.hpp | 2 + lib/libimhex/source/helpers/shared_data.cpp | 5 +++ lib/libimhex/source/views/view.cpp | 42 +++++++++++++++++++ .../source/content/views/view_hexeditor.cpp | 11 ++++- .../content/views/view_pattern_editor.cpp | 42 +++---------------- plugins/builtin/source/lang/de_DE.cpp | 1 + plugins/builtin/source/lang/en_US.cpp | 1 + plugins/builtin/source/lang/it_IT.cpp | 1 + plugins/builtin/source/lang/zh_CN.cpp | 1 + 10 files changed, 73 insertions(+), 38 deletions(-) diff --git a/lib/libimhex/include/hex/helpers/shared_data.hpp b/lib/libimhex/include/hex/helpers/shared_data.hpp index 83dc41663..ea1bf98d8 100644 --- a/lib/libimhex/include/hex/helpers/shared_data.hpp +++ b/lib/libimhex/include/hex/helpers/shared_data.hpp @@ -71,6 +71,11 @@ namespace hex { static std::list bookmarkEntries; static std::vector patternData; + static u32 selectableFileIndex; + static std::vector selectableFiles; + static std::function selectableFileOpenCallback; + static std::vector selectableFilesValidExtensions; + static std::map languageNames; static std::map> languageDefinitions; static std::map loadedLanguageStrings; diff --git a/lib/libimhex/include/hex/views/view.hpp b/lib/libimhex/include/hex/views/view.hpp index 9ce7e9a7f..9b626b706 100644 --- a/lib/libimhex/include/hex/views/view.hpp +++ b/lib/libimhex/include/hex/views/view.hpp @@ -44,6 +44,8 @@ namespace hex { static void showErrorPopup(const std::string &errorMessage); static void showFatalPopup(const std::string &errorMessage); + static void showFileChooserPopup(const std::vector &paths, const std::vector &validExtensions, const std::function &callback); + virtual bool hasViewMenuItemEntry() const; virtual ImVec2 getMinSize() const; virtual ImVec2 getMaxSize() const; diff --git a/lib/libimhex/source/helpers/shared_data.cpp b/lib/libimhex/source/helpers/shared_data.cpp index 39ed6b8ff..5038c0e0d 100644 --- a/lib/libimhex/source/helpers/shared_data.cpp +++ b/lib/libimhex/source/helpers/shared_data.cpp @@ -21,6 +21,11 @@ namespace hex { std::list SharedData::bookmarkEntries; std::vector SharedData::patternData; + u32 SharedData::selectableFileIndex; + std::vector SharedData::selectableFiles; + std::function SharedData::selectableFileOpenCallback; + std::vector SharedData::selectableFilesValidExtensions; + std::map SharedData::languageNames; std::map> SharedData::languageDefinitions; std::map SharedData::loadedLanguageStrings; diff --git a/lib/libimhex/source/views/view.cpp b/lib/libimhex/source/views/view.cpp index ceeceebc7..e55a7285a 100644 --- a/lib/libimhex/source/views/view.cpp +++ b/lib/libimhex/source/views/view.cpp @@ -58,6 +58,39 @@ namespace hex { ImGui::SetWindowPos((SharedData::windowSize - ImGui::GetWindowSize()) / 2, ImGuiCond_Appearing); ImGui::EndPopup(); } + + bool opened = true; + ImGui::SetNextWindowPos(ImGui::GetMainViewport()->GetCenter(), ImGuiCond_Appearing, ImVec2(0.5F, 0.5F)); + if (ImGui::BeginPopupModal("hex.common.choose_file"_lang, &opened, ImGuiWindowFlags_AlwaysAutoResize)) { + + if (ImGui::BeginListBox("##files", ImVec2(300_scaled, 0))) { + + u32 index = 0; + for (auto &path : SharedData::selectableFiles) { + if (ImGui::Selectable(path.filename().string().c_str(), index == SharedData::selectableFileIndex)) + SharedData::selectableFileIndex = index; + index++; + } + + ImGui::EndListBox(); + } + + if (ImGui::Button("hex.common.open"_lang)) { + SharedData::selectableFileOpenCallback(SharedData::selectableFiles[SharedData::selectableFileIndex]); + ImGui::CloseCurrentPopup(); + } + + ImGui::SameLine(); + + if (ImGui::Button("hex.common.browse"_lang)) { + hex::openFileBrowser("hex.common.open"_lang, DialogMode::Open, SharedData::selectableFilesValidExtensions, [](const auto &path) { + SharedData::selectableFileOpenCallback(path); + ImGui::CloseCurrentPopup(); + }); + } + + ImGui::EndPopup(); + } } void View::showMessagePopup(const std::string &message) { @@ -78,6 +111,15 @@ namespace hex { View::doLater([] { ImGui::OpenPopup("hex.common.fatal"_lang); }); } + void View::showFileChooserPopup(const std::vector &paths, const std::vector &validExtensions, const std::function &callback) { + SharedData::selectableFileIndex = 0; + SharedData::selectableFiles = paths; + SharedData::selectableFilesValidExtensions = validExtensions; + SharedData::selectableFileOpenCallback = callback; + + View::doLater([] { ImGui::OpenPopup("hex.common.choose_file"_lang); }); + } + bool View::hasViewMenuItemEntry() const { return true; } diff --git a/plugins/builtin/source/content/views/view_hexeditor.cpp b/plugins/builtin/source/content/views/view_hexeditor.cpp index 76925eee9..9474921b3 100644 --- a/plugins/builtin/source/content/views/view_hexeditor.cpp +++ b/plugins/builtin/source/content/views/view_hexeditor.cpp @@ -1048,7 +1048,16 @@ namespace hex::plugin::builtin { } if (ImGui::MenuItem("hex.builtin.view.hexeditor.menu.file.load_encoding_file"_lang)) { - hex::openFileBrowser("hex.builtin.view.hexeditor.load_enconding_file"_lang, DialogMode::Open, { { "Thingy Table File", "tbl" } }, [this](const auto &path) { + std::vector paths; + for (const auto &path : hex::getPath(ImHexPath::Encodings)) { + for (const auto &entry : fs::recursive_directory_iterator(path)) { + if (!entry.is_regular_file()) continue; + + paths.push_back(entry); + } + } + + View::showFileChooserPopup(paths, { { "Thingy Table File", "tbl" } }, [this](const auto &path) { this->m_currEncodingFile = EncodingFile(EncodingFile::Type::Thingy, path); }); } diff --git a/plugins/builtin/source/content/views/view_pattern_editor.cpp b/plugins/builtin/source/content/views/view_pattern_editor.cpp index e1f53d735..b44e9ff09 100644 --- a/plugins/builtin/source/content/views/view_pattern_editor.cpp +++ b/plugins/builtin/source/content/views/view_pattern_editor.cpp @@ -198,20 +198,21 @@ namespace hex::plugin::builtin { ContentRegistry::Interface::addMenuItem("hex.builtin.menu.file", 2000, [&, this] { if (ImGui::MenuItem("hex.builtin.view.pattern_editor.menu.file.load_pattern"_lang)) { - this->m_selectedPatternFile = 0; - this->m_possiblePatternFiles.clear(); + std::vector paths; for (auto &imhexPath : hex::getPath(ImHexPath::Patterns)) { if (!fs::exists(imhexPath)) continue; for (auto &entry: fs::recursive_directory_iterator(imhexPath)) { if (entry.is_regular_file() && entry.path().extension() == ".hexpat") { - this->m_possiblePatternFiles.push_back(entry.path()); + paths.push_back(entry.path()); } } } - View::doLater([]{ ImGui::OpenPopup("hex.builtin.view.pattern_editor.menu.file.load_pattern"_lang); }); + View::showFileChooserPopup(paths, { { "Pattern File", "hexpat" } }, [this](const fs::path &path){ + this->loadPatternFile(path); + }); } if (ImGui::MenuItem("hex.builtin.view.pattern_editor.menu.file.save_pattern"_lang)) { @@ -554,39 +555,6 @@ namespace hex::plugin::builtin { ImGui::EndPopup(); } - - bool opened = true; - ImGui::SetNextWindowPos(ImGui::GetMainViewport()->GetCenter(), ImGuiCond_Appearing, ImVec2(0.5F, 0.5F)); - if (ImGui::BeginPopupModal("hex.builtin.view.pattern_editor.menu.file.load_pattern"_lang, &opened, ImGuiWindowFlags_AlwaysAutoResize)) { - - if (ImGui::BeginListBox("##patterns", ImVec2(300_scaled, 0))) { - - u32 index = 0; - for (auto &path : this->m_possiblePatternFiles) { - if (ImGui::Selectable(path.filename().string().c_str(), index == this->m_selectedPatternFile)) - this->m_selectedPatternFile = index; - index++; - } - - ImGui::EndListBox(); - } - - if (ImGui::Button("hex.common.open"_lang)) { - this->loadPatternFile(this->m_possiblePatternFiles[this->m_selectedPatternFile].string()); - ImGui::CloseCurrentPopup(); - } - - ImGui::SameLine(); - - if (ImGui::Button("hex.common.browse"_lang)) { - hex::openFileBrowser("hex.builtin.view.pattern_editor.open_pattern"_lang, DialogMode::Open, { { "Pattern File", "hexpat" } }, [this](const auto &path) { - this->loadPatternFile(path); - ImGui::CloseCurrentPopup(); - }); - } - - ImGui::EndPopup(); - } } diff --git a/plugins/builtin/source/lang/de_DE.cpp b/plugins/builtin/source/lang/de_DE.cpp index b4cb43d1f..7f8872cd2 100644 --- a/plugins/builtin/source/lang/de_DE.cpp +++ b/plugins/builtin/source/lang/de_DE.cpp @@ -79,6 +79,7 @@ namespace hex::plugin::builtin { { "hex.common.file", "Datei" }, { "hex.common.open", "Öffnen" }, { "hex.common.browse", "Druchsuchen..." }, + { "hex.common.choose_file", "Datei auswählen" }, { "hex.message.file_handler_failed", "Datei konnte nicht mit registriertem Dateihandler geöffnet werden." }, diff --git a/plugins/builtin/source/lang/en_US.cpp b/plugins/builtin/source/lang/en_US.cpp index f64b49439..04c1c26f7 100644 --- a/plugins/builtin/source/lang/en_US.cpp +++ b/plugins/builtin/source/lang/en_US.cpp @@ -79,6 +79,7 @@ namespace hex::plugin::builtin { { "hex.common.file", "File" }, { "hex.common.open", "Open" }, { "hex.common.browse", "Browse..." }, + { "hex.common.choose_file", "Choose file" }, { "hex.message.file_handler_failed", "Failed to open file with registered file handler." }, diff --git a/plugins/builtin/source/lang/it_IT.cpp b/plugins/builtin/source/lang/it_IT.cpp index 58c17ee30..1d12aaa1b 100644 --- a/plugins/builtin/source/lang/it_IT.cpp +++ b/plugins/builtin/source/lang/it_IT.cpp @@ -78,6 +78,7 @@ namespace hex::plugin::builtin { { "hex.common.file", "File" }, //{ "hex.common.open", "Open" }, //{ "hex.common.browse", "Browse..." }, + //{ "hex.common.choose_file", "Choose file" }, //{ "hex.message.file_handler_failed", "Failed to open file with registered file handler." }, diff --git a/plugins/builtin/source/lang/zh_CN.cpp b/plugins/builtin/source/lang/zh_CN.cpp index 8170decb6..74dd4adb1 100644 --- a/plugins/builtin/source/lang/zh_CN.cpp +++ b/plugins/builtin/source/lang/zh_CN.cpp @@ -79,6 +79,7 @@ namespace hex::plugin::builtin { { "hex.common.file", "文件" }, { "hex.common.open", "打开" }, { "hex.common.browse", "浏览..." }, + //{ "hex.common.choose_file", "Choose file" }, //{ "hex.message.file_handler_failed", "Failed to open file with registered file handler." },