From fdd2e1fcde1158164090aacf370b509ffd199593 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 23 Jul 2023 09:14:00 +0200 Subject: [PATCH] patterns: Added `[[hex::spec_name]]` --- .../include/hex/ui/imgui_imhex_extensions.h | 1 + .../source/ui/imgui_imhex_extensions.cpp | 20 +++++ plugins/builtin/include/ui/pattern_drawer.hpp | 7 ++ plugins/builtin/romfs/lang/en_US.json | 1 + plugins/builtin/source/ui/hex_editor.cpp | 80 +++---------------- plugins/builtin/source/ui/pattern_drawer.cpp | 40 ++++++---- 6 files changed, 65 insertions(+), 84 deletions(-) diff --git a/lib/libimhex/include/hex/ui/imgui_imhex_extensions.h b/lib/libimhex/include/hex/ui/imgui_imhex_extensions.h index 2893ed25a..9c086c6e2 100644 --- a/lib/libimhex/include/hex/ui/imgui_imhex_extensions.h +++ b/lib/libimhex/include/hex/ui/imgui_imhex_extensions.h @@ -191,5 +191,6 @@ namespace ImGui { bool DimmedButton(const char* label); bool DimmedIconButton(const char *symbol, ImVec4 color, ImVec2 size_arg = ImVec2(0, 0)); + bool DimmedIconToggle(const char *icon, bool *v); } \ No newline at end of file diff --git a/lib/libimhex/source/ui/imgui_imhex_extensions.cpp b/lib/libimhex/source/ui/imgui_imhex_extensions.cpp index 29fb1b1ef..8939d280b 100644 --- a/lib/libimhex/source/ui/imgui_imhex_extensions.cpp +++ b/lib/libimhex/source/ui/imgui_imhex_extensions.cpp @@ -788,4 +788,24 @@ namespace ImGui { return res; } + bool DimmedIconToggle(const char *icon, bool *v) { + bool pushed = false; + bool toggled = false; + + if (*v) { + ImGui::PushStyleColor(ImGuiCol_Border, ImGui::GetStyleColorVec4(ImGuiCol_ButtonActive)); + pushed = true; + } + + if (ImGui::DimmedIconButton(icon, ImGui::GetStyleColorVec4(ImGuiCol_Text))) { + *v = !*v; + toggled = true; + } + + if (pushed) + ImGui::PopStyleColor(); + + return toggled; + } + } \ No newline at end of file diff --git a/plugins/builtin/include/ui/pattern_drawer.hpp b/plugins/builtin/include/ui/pattern_drawer.hpp index 34fd2313a..9cc491fbf 100644 --- a/plugins/builtin/include/ui/pattern_drawer.hpp +++ b/plugins/builtin/include/ui/pattern_drawer.hpp @@ -10,6 +10,8 @@ #include +struct ImGuiTableSortSpecs; + namespace hex::plugin::builtin::ui { class PatternDrawer : public pl::PatternVisitor { @@ -65,14 +67,17 @@ namespace hex::plugin::builtin::ui { void drawVisualizer(const std::map &visualizers, const std::vector &arguments, pl::ptrn::Pattern &pattern, pl::ptrn::IIterable &iterable, bool reset); void drawFavoriteColumn(const pl::ptrn::Pattern& pattern); + bool beginPatternTable(const std::vector> &patterns, std::vector &sortedPatterns, float height); bool createTreeNode(const pl::ptrn::Pattern& pattern, bool leaf = false); void createDefaultEntry(pl::ptrn::Pattern &pattern); void closeTreeNode(bool inlined); + bool sortPatterns(const ImGuiTableSortSpecs* sortSpecs, const pl::ptrn::Pattern * left, const pl::ptrn::Pattern * right) const; bool isEditingPattern(const pl::ptrn::Pattern& pattern) const; void resetEditing(); bool matchesFilter(const std::vector &filterPath, const std::vector &patternPath, bool fullMatch); void traversePatternTree(pl::ptrn::Pattern &pattern, std::vector &patternPath, const std::function &callback); + std::string getDisplayName(const pl::ptrn::Pattern& pattern) const; private: std::map m_displayEnd; @@ -93,6 +98,8 @@ namespace hex::plugin::builtin::ui { std::map, std::unique_ptr> m_favorites; bool m_showFavoriteStars = false; bool m_favoritesUpdated = false; + bool m_showSpecName = false; + TaskHolder m_favoritesUpdateTask; std::function m_selectionCallback = [](Region) { }; diff --git a/plugins/builtin/romfs/lang/en_US.json b/plugins/builtin/romfs/lang/en_US.json index df788bc7e..59012b11e 100644 --- a/plugins/builtin/romfs/lang/en_US.json +++ b/plugins/builtin/romfs/lang/en_US.json @@ -374,6 +374,7 @@ "hex.builtin.pattern_drawer.favorites": "Favorites", "hex.builtin.pattern_drawer.local": "Local", "hex.builtin.pattern_drawer.size": "Size", + "hex.builtin.pattern_drawer.spec_name": "Display specification names", "hex.builtin.pattern_drawer.start": "Start", "hex.builtin.pattern_drawer.tree_style.tree": "Tree", "hex.builtin.pattern_drawer.tree_style.auto_expanded": "Auto Expanded Tree", diff --git a/plugins/builtin/source/ui/hex_editor.cpp b/plugins/builtin/source/ui/hex_editor.cpp index 92856da35..d62f1bfcc 100644 --- a/plugins/builtin/source/ui/hex_editor.cpp +++ b/plugins/builtin/source/ui/hex_editor.cpp @@ -725,89 +725,29 @@ namespace hex::plugin::builtin::ui { ImGui::SetCursorPosX(ImGui::GetCursorPosX() + 2_scaled); // Upper/lower case hex - { - bool pushed = false; - - if (this->m_upperCaseHex) { - ImGui::PushStyleColor(ImGuiCol_Border, ImGui::GetStyleColorVec4(ImGuiCol_ButtonActive)); - pushed = true; - } - - if (ImGui::DimmedIconButton(ICON_VS_CASE_SENSITIVE, ImGui::GetStyleColorVec4(ImGuiCol_Text))) { - this->m_upperCaseHex = !this->m_upperCaseHex; - } - - if (pushed) - ImGui::PopStyleColor(); - - ImGui::InfoTooltip("hex.builtin.hex_editor.uppercase_hex"_lang); - } + ImGui::DimmedIconToggle(ICON_VS_CASE_SENSITIVE, &this->m_upperCaseHex); + ImGui::InfoTooltip("hex.builtin.hex_editor.uppercase_hex"_lang); ImGui::SameLine(); // Grayed out zeros - { - bool pushed = false; - - if (this->m_grayOutZero) { - ImGui::PushStyleColor(ImGuiCol_Border, ImGui::GetStyleColorVec4(ImGuiCol_ButtonActive)); - pushed = true; - } - - if (ImGui::DimmedIconButton(ICON_VS_LIGHTBULB, ImGui::GetStyleColorVec4(ImGuiCol_Text))) { - this->m_grayOutZero = !this->m_grayOutZero; - EventManager::post(); - } - - if (pushed) - ImGui::PopStyleColor(); - - ImGui::InfoTooltip("hex.builtin.hex_editor.gray_out_zero"_lang); - } + ImGui::DimmedIconToggle(ICON_VS_LIGHTBULB, &this->m_grayOutZero); + ImGui::InfoTooltip("hex.builtin.hex_editor.gray_out_zero"_lang); ImGui::SameLine(); // ASCII view - { - bool pushed = false; - - if (this->m_showAscii) { - ImGui::PushStyleColor(ImGuiCol_Border, ImGui::GetStyleColorVec4(ImGuiCol_ButtonActive)); - pushed = true; - } - - if (ImGui::DimmedIconButton(ICON_VS_SYMBOL_KEY, ImGui::GetStyleColorVec4(ImGuiCol_Text))) { - this->m_showAscii = !this->m_showAscii; - } - - if (pushed) - ImGui::PopStyleColor(); - - ImGui::InfoTooltip("hex.builtin.hex_editor.ascii_view"_lang); - } + ImGui::DimmedIconToggle(ICON_VS_SYMBOL_KEY, &this->m_showAscii); + ImGui::InfoTooltip("hex.builtin.hex_editor.ascii_view"_lang); ImGui::SameLine(0, 1_scaled); // Custom encoding view - { - bool pushed = false; + ImGui::BeginDisabled(!this->m_currCustomEncoding.has_value()); + ImGui::DimmedIconToggle(ICON_VS_WHITESPACE, &this->m_showCustomEncoding); + ImGui::EndDisabled(); - if (this->m_showCustomEncoding) { - ImGui::PushStyleColor(ImGuiCol_Border, ImGui::GetStyleColorVec4(ImGuiCol_ButtonActive)); - pushed = true; - } - - ImGui::BeginDisabled(!this->m_currCustomEncoding.has_value()); - if (ImGui::DimmedIconButton(ICON_VS_WHITESPACE, ImGui::GetStyleColorVec4(ImGuiCol_Text))) { - this->m_showCustomEncoding = !this->m_showCustomEncoding; - } - ImGui::EndDisabled(); - - if (pushed) - ImGui::PopStyleColor(); - - ImGui::InfoTooltip("hex.builtin.hex_editor.custom_encoding_view"_lang); - } + ImGui::InfoTooltip("hex.builtin.hex_editor.custom_encoding_view"_lang); ImGui::TableNextColumn(); diff --git a/plugins/builtin/source/ui/pattern_drawer.cpp b/plugins/builtin/source/ui/pattern_drawer.cpp index 47f3400c6..1fcba93f0 100644 --- a/plugins/builtin/source/ui/pattern_drawer.cpp +++ b/plugins/builtin/source/ui/pattern_drawer.cpp @@ -285,12 +285,19 @@ namespace hex::plugin::builtin::ui { } } + std::string PatternDrawer::getDisplayName(const pl::ptrn::Pattern& pattern) const { + if (this->m_showSpecName && pattern.hasAttribute("hex::spec_name")) + return pattern.getAttributeArguments("hex::spec_name")[0].toString(true); + else + return pattern.getDisplayName(); + } + bool PatternDrawer::createTreeNode(const pl::ptrn::Pattern& pattern, bool leaf) { drawFavoriteColumn(pattern); if (pattern.isSealed() || leaf) { ImGui::Indent(); - highlightWhenSelected(pattern, [&]{ ImGui::TextUnformatted(pattern.getDisplayName().c_str()); }); + highlightWhenSelected(pattern, [&]{ ImGui::TextUnformatted(this->getDisplayName(pattern).c_str()); }); ImGui::Unindent(); return false; } @@ -300,11 +307,11 @@ namespace hex::plugin::builtin::ui { using enum TreeStyle; default: case Default: - return ImGui::TreeNodeEx(pattern.getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth); + return ImGui::TreeNodeEx(this->getDisplayName(pattern).c_str(), ImGuiTreeNodeFlags_SpanFullWidth); case AutoExpanded: - return ImGui::TreeNodeEx(pattern.getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_DefaultOpen); + return ImGui::TreeNodeEx(this->getDisplayName(pattern).c_str(), ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_DefaultOpen); case Flattened: - return ImGui::TreeNodeEx(pattern.getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen); + return ImGui::TreeNodeEx(this->getDisplayName(pattern).c_str(), ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen); } }); } @@ -847,7 +854,7 @@ namespace hex::plugin::builtin::ui { ImGui::TableNextColumn(); chunkOpen = highlightWhenSelected(startOffset, ((endOffset + endSize) - startOffset) - 1, [&]{ - return ImGui::TreeNodeEx(hex::format("{0}[{1} ... {2}]", this->m_treeStyle == TreeStyle::Flattened ? pattern.getDisplayName().c_str() : "", i, endIndex - 1).c_str(), ImGuiTreeNodeFlags_SpanFullWidth); + return ImGui::TreeNodeEx(hex::format("{0}[{1} ... {2}]", this->m_treeStyle == TreeStyle::Flattened ? this->getDisplayName(pattern).c_str() : "", i, endIndex - 1).c_str(), ImGuiTreeNodeFlags_SpanFullWidth); }); ImGui::TableNextColumn(); @@ -900,12 +907,12 @@ namespace hex::plugin::builtin::ui { return value->second; } - static bool sortPatterns(const ImGuiTableSortSpecs* sortSpecs, const pl::ptrn::Pattern * left, const pl::ptrn::Pattern * right) { + bool PatternDrawer::sortPatterns(const ImGuiTableSortSpecs* sortSpecs, const pl::ptrn::Pattern * left, const pl::ptrn::Pattern * right) const { if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("name")) { if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending) - return left->getDisplayName() < right->getDisplayName(); + return this->getDisplayName(*left) < this->getDisplayName(*right); else - return left->getDisplayName() > right->getDisplayName(); + return this->getDisplayName(*left) > this->getDisplayName(*right); } else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("start")) { if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending) return left->getOffsetForSorting() < right->getOffsetForSorting(); @@ -941,7 +948,7 @@ namespace hex::plugin::builtin::ui { return false; } - static bool beginPatternTable(const std::vector> &patterns, std::vector &sortedPatterns, float height) { + bool PatternDrawer::beginPatternTable(const std::vector> &patterns, std::vector &sortedPatterns, float height) { if (ImGui::BeginTable("##Patterntable", 8, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Sortable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollY, ImVec2(0, height))) { ImGui::TableSetupScrollFreeze(0, 1); ImGui::TableSetupColumn("##favorite", ImGuiTableColumnFlags_NoSort | ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_NoResize | ImGuiTableColumnFlags_NoReorder | ImGuiTableColumnFlags_NoHide | ImGuiTableColumnFlags_IndentDisable, ImGui::GetTextLineHeight(), ImGui::GetID("favorite")); @@ -964,13 +971,13 @@ namespace hex::plugin::builtin::ui { return pattern.get(); }); - std::sort(sortedPatterns.begin(), sortedPatterns.end(), [&sortSpecs](pl::ptrn::Pattern *left, pl::ptrn::Pattern *right) -> bool { - return sortPatterns(sortSpecs, left, right); + std::sort(sortedPatterns.begin(), sortedPatterns.end(), [this, &sortSpecs](pl::ptrn::Pattern *left, pl::ptrn::Pattern *right) -> bool { + return this->sortPatterns(sortSpecs, left, right); }); for (auto &pattern : sortedPatterns) - pattern->sort([&sortSpecs](const pl::ptrn::Pattern *left, const pl::ptrn::Pattern *right){ - return sortPatterns(sortSpecs, left, right); + pattern->sort([this, &sortSpecs](const pl::ptrn::Pattern *left, const pl::ptrn::Pattern *right){ + return this->sortPatterns(sortSpecs, left, right); }); sortSpecs->SpecsDirty = false; @@ -1016,7 +1023,7 @@ namespace hex::plugin::builtin::ui { this->resetEditing(); } - ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x - ImGui::GetTextLineHeightWithSpacing() * 7.5); + ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x - ImGui::GetTextLineHeightWithSpacing() * 9.5); if (ImGui::InputTextIcon("##Search", ICON_VS_FILTER, this->m_filterText)) { this->m_filter = parseRValueFilter(this->m_filterText); } @@ -1024,6 +1031,11 @@ namespace hex::plugin::builtin::ui { ImGui::SameLine(); + ImGui::DimmedIconToggle(ICON_VS_BOOK, &this->m_showSpecName); + ImGui::InfoTooltip("hex.builtin.pattern_drawer.spec_name"_lang); + + ImGui::SameLine(); + treeStyleButton(ICON_VS_SYMBOL_KEYWORD, TreeStyle::Default, "hex.builtin.pattern_drawer.tree_style.tree"_lang); ImGui::SameLine(0, 0); treeStyleButton(ICON_VS_LIST_TREE, TreeStyle::AutoExpanded, "hex.builtin.pattern_drawer.tree_style.auto_expanded"_lang);