From cfde9939b45cf5cb8292bb65843c3980a73dfad0 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 7 Sep 2022 23:11:13 +0200 Subject: [PATCH] sys: Refactor and cleanup pattern drawer --- plugins/builtin/CMakeLists.txt | 2 +- .../{ => content/helpers}/pattern_drawer.hpp | 13 -- .../content/views/view_pattern_data.hpp | 7 +- .../{ => content/helpers}/pattern_drawer.cpp | 214 +++++++++--------- .../content/views/view_pattern_editor.cpp | 26 +-- 5 files changed, 122 insertions(+), 140 deletions(-) rename plugins/builtin/include/{ => content/helpers}/pattern_drawer.hpp (76%) rename plugins/builtin/source/{ => content/helpers}/pattern_drawer.cpp (65%) diff --git a/plugins/builtin/CMakeLists.txt b/plugins/builtin/CMakeLists.txt index 9d71cd892..11b5941d1 100644 --- a/plugins/builtin/CMakeLists.txt +++ b/plugins/builtin/CMakeLists.txt @@ -53,7 +53,7 @@ add_library(${PROJECT_NAME} SHARED source/content/views/view_find.cpp - source/math_evaluator.cpp + source/content/helpers/pattern_drawer.cpp source/pattern_drawer.cpp source/lang/de_DE.cpp diff --git a/plugins/builtin/include/pattern_drawer.hpp b/plugins/builtin/include/content/helpers/pattern_drawer.hpp similarity index 76% rename from plugins/builtin/include/pattern_drawer.hpp rename to plugins/builtin/include/content/helpers/pattern_drawer.hpp index d65acc0c2..2451141c4 100644 --- a/plugins/builtin/include/pattern_drawer.hpp +++ b/plugins/builtin/include/content/helpers/pattern_drawer.hpp @@ -34,12 +34,6 @@ namespace hex { void visit(pl::ptrn::PatternWideString& pattern) override; private: - void createDefaultEntry(const pl::ptrn::Pattern &pattern, const std::string &value, pl::core::Token::Literal &&literal) const; - void createLeafNode(const pl::ptrn::Pattern& pattern) const; - bool createTreeNode(const pl::ptrn::Pattern& pattern) const; - - void makeSelectable(const pl::ptrn::Pattern &pattern) const; - void draw(pl::ptrn::Pattern& pattern); template @@ -60,13 +54,6 @@ namespace hex { void drawArrayNode(u64 idx, u64& displayEnd, pl::ptrn::Pattern& pattern); void drawArrayEnd(pl::ptrn::Pattern& pattern, bool opened, bool inlined); - void drawCommentTooltip(const pl::ptrn::Pattern &pattern) const; - void drawTypenameColumn(const pl::ptrn::Pattern& pattern, const std::string& pattern_name) const; - void drawNameColumn(const pl::ptrn::Pattern& pattern) const; - void drawColorColumn(const pl::ptrn::Pattern& pattern) const; - void drawOffsetColumn(const pl::ptrn::Pattern& pattern) const; - void drawSizeColumn(const pl::ptrn::Pattern& pattern) const; - u64& getDisplayEnd(const pl::ptrn::Pattern& pattern); private: diff --git a/plugins/builtin/include/content/views/view_pattern_data.hpp b/plugins/builtin/include/content/views/view_pattern_data.hpp index a7ac7dfab..8265d9e27 100644 --- a/plugins/builtin/include/content/views/view_pattern_data.hpp +++ b/plugins/builtin/include/content/views/view_pattern_data.hpp @@ -1,15 +1,12 @@ #pragma once -#include "pattern_drawer.hpp" - #include - -#include #include +#include + #include #include -#include namespace hex::plugin::builtin { diff --git a/plugins/builtin/source/pattern_drawer.cpp b/plugins/builtin/source/content/helpers/pattern_drawer.cpp similarity index 65% rename from plugins/builtin/source/pattern_drawer.cpp rename to plugins/builtin/source/content/helpers/pattern_drawer.cpp index 7b254b650..04ede97d0 100644 --- a/plugins/builtin/source/pattern_drawer.cpp +++ b/plugins/builtin/source/content/helpers/pattern_drawer.cpp @@ -1,4 +1,4 @@ -#include "pattern_drawer.hpp" +#include #include #include @@ -25,15 +25,100 @@ #include #include -namespace { - constexpr static auto DisplayEndDefault = 50u; - constexpr static auto DisplayEndStep = 50u; - - using namespace ::std::literals::string_literals; -}; - namespace hex { + namespace { + + constexpr auto DisplayEndDefault = 50u; + constexpr auto DisplayEndStep = 50u; + + using namespace ::std::literals::string_literals; + + void createLeafNode(const pl::ptrn::Pattern& pattern) { + ImGui::TreeNodeEx(pattern.getDisplayName().c_str(), ImGuiTreeNodeFlags_Leaf | + ImGuiTreeNodeFlags_NoTreePushOnOpen | + ImGuiTreeNodeFlags_SpanFullWidth | + ImGuiTreeNodeFlags_AllowItemOverlap); + } + + bool createTreeNode(const pl::ptrn::Pattern& pattern) { + if (pattern.isSealed()) { + ImGui::Indent(); + ImGui::TextUnformatted(pattern.getDisplayName().c_str()); + ImGui::Unindent(); + return false; + } + else + return ImGui::TreeNodeEx(pattern.getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth); + } + + void drawTypenameColumn(const pl::ptrn::Pattern& pattern, const std::string& pattern_name) { + ImGui::TextFormattedColored(ImColor(0xFFD69C56), pattern_name); + ImGui::SameLine(); + ImGui::TextUnformatted(pattern.getTypeName().c_str()); + ImGui::TableNextColumn(); + } + + void drawNameColumn(const pl::ptrn::Pattern& pattern) { + ImGui::TextUnformatted(pattern.getDisplayName().c_str()); + ImGui::TableNextColumn(); + } + + void drawColorColumn(const pl::ptrn::Pattern& pattern) { + ImGui::ColorButton("color", ImColor(pattern.getColor()), ImGuiColorEditFlags_NoTooltip, ImVec2(ImGui::GetColumnWidth(), ImGui::GetTextLineHeight())); + ImGui::TableNextColumn(); + } + + void drawOffsetColumn(const pl::ptrn::Pattern& pattern) { + ImGui::TextFormatted("0x{0:08X} : 0x{1:08X}", pattern.getOffset(), pattern.getOffset() + pattern.getSize() - (pattern.getSize() == 0 ? 0 : 1)); + ImGui::TableNextColumn(); + } + + void drawSizeColumn(const pl::ptrn::Pattern& pattern) { + ImGui::TextFormatted("0x{0:04X}", pattern.getSize()); + ImGui::TableNextColumn(); + } + + void drawCommentTooltip(const pl::ptrn::Pattern &pattern) { + if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem) && pattern.getComment() != nullptr) { + ImGui::BeginTooltip(); + ImGui::TextUnformatted(pattern.getComment()->c_str()); + ImGui::EndTooltip(); + } + } + + void makeSelectable(const pl::ptrn::Pattern &pattern) { + ImGui::PushID(static_cast(pattern.getOffset())); + ImGui::PushID(pattern.getVariableName().c_str()); + + if (ImGui::Selectable("##PatternLine", false, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap)) { + ImHexApi::HexEditor::setSelection(pattern.getOffset(), pattern.getSize()); + } + + ImGui::SameLine(0, 0); + + ImGui::PopID(); + ImGui::PopID(); + } + + void createDefaultEntry(pl::ptrn::Pattern &pattern) { + ImGui::TableNextRow(); + createLeafNode(pattern); + ImGui::TableNextColumn(); + makeSelectable(pattern); + drawCommentTooltip(pattern); + ImGui::SameLine(); + drawNameColumn(pattern); + drawColorColumn(pattern); + drawOffsetColumn(pattern); + drawSizeColumn(pattern); + ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "{}", pattern.getFormattedName().empty() ? pattern.getTypeName() : pattern.getFormattedName()); + ImGui::TableNextColumn(); + ImGui::TextFormatted("{}", pattern.getFormattedValue()); + } + + } + void PatternDrawer::visit(pl::ptrn::PatternArrayDynamic& pattern) { this->drawArray(pattern); } @@ -100,11 +185,11 @@ namespace hex { } void PatternDrawer::visit(pl::ptrn::PatternBoolean& pattern) { - this->createDefaultEntry(pattern, pattern.getFormattedValue(), bool(hex::get_or(pattern.getValue(), 0x00))); + createDefaultEntry(pattern); } void PatternDrawer::visit(pl::ptrn::PatternCharacter& pattern) { - this->createDefaultEntry(pattern, pattern.getFormattedValue(), pattern.getValue()); + createDefaultEntry(pattern); } void PatternDrawer::visit(pl::ptrn::PatternEnum& pattern) { @@ -123,11 +208,7 @@ namespace hex { } void PatternDrawer::visit(pl::ptrn::PatternFloat& pattern) { - if (pattern.getSize() == 4) { - this->createDefaultEntry(pattern, pattern.getFormattedValue(), pattern.getValue()); - } else if (pattern.getSize() == 8) { - this->createDefaultEntry(pattern, pattern.getFormattedValue(), pattern.getValue()); - } + createDefaultEntry(pattern); } void PatternDrawer::visit(pl::ptrn::PatternPadding& pattern) { @@ -162,12 +243,12 @@ namespace hex { } void PatternDrawer::visit(pl::ptrn::PatternSigned& pattern) { - this->createDefaultEntry(pattern, pattern.getFormattedValue(), pattern.getValue()); + createDefaultEntry(pattern); } void PatternDrawer::visit(pl::ptrn::PatternString& pattern) { if (pattern.getSize() > 0) - this->createDefaultEntry(pattern, pattern.getFormattedValue(), pattern.getValue()); + createDefaultEntry(pattern); } void PatternDrawer::visit(pl::ptrn::PatternStruct& pattern) { @@ -231,54 +312,16 @@ namespace hex { } void PatternDrawer::visit(pl::ptrn::PatternUnsigned& pattern) { - this->createDefaultEntry(pattern, pattern.getFormattedValue(), pattern.getValue()); + createDefaultEntry(pattern); } void PatternDrawer::visit(pl::ptrn::PatternWideCharacter& pattern) { - this->createDefaultEntry(pattern, pattern.getFormattedValue(), pattern.getValue()); + createDefaultEntry(pattern); } void PatternDrawer::visit(pl::ptrn::PatternWideString& pattern) { if (pattern.getSize() > 0) - this->createDefaultEntry(pattern, pattern.getFormattedValue(), pattern.getValue()); - } - - void PatternDrawer::createDefaultEntry(const pl::ptrn::Pattern &pattern, const std::string &value, pl::core::Token::Literal &&literal) const { - ImGui::TableNextRow(); - createLeafNode(pattern); - ImGui::TableNextColumn(); - - makeSelectable(pattern); - - drawCommentTooltip(pattern); - ImGui::SameLine(); - drawNameColumn(pattern); - drawColorColumn(pattern); - drawOffsetColumn(pattern); - drawSizeColumn(pattern); - ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "{}", pattern.getFormattedName().empty() ? pattern.getTypeName() : pattern.getFormattedName()); - ImGui::TableNextColumn(); - ImGui::TextFormatted("{}", pattern.formatDisplayValue(value, literal)); - } - - void PatternDrawer::makeSelectable(const pl::ptrn::Pattern &pattern) const { - ImGui::PushID(static_cast(pattern.getOffset())); - ImGui::PushID(pattern.getVariableName().c_str()); - if (ImGui::Selectable("##PatternLine", false, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap)) { - ImHexApi::HexEditor::setSelection(pattern.getOffset(), pattern.getSize()); - } - ImGui::SameLine(); - ImGui::PopID(); - ImGui::PopID(); - } - - - void PatternDrawer::drawCommentTooltip(const pl::ptrn::Pattern &pattern) const { - if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem) && pattern.getComment() != nullptr) { - ImGui::BeginTooltip(); - ImGui::TextUnformatted(pattern.getComment()->c_str()); - ImGui::EndTooltip(); - } + createDefaultEntry(pattern); } void PatternDrawer::draw(pl::ptrn::Pattern& pattern) { @@ -325,7 +368,7 @@ namespace hex { void PatternDrawer::drawArrayNode(u64 idx, u64& displayEnd, pl::ptrn::Pattern& pattern) { u64 lastVisible = displayEnd - 1; - ImGui::PushID(pattern.getOffset()); + ImGui::PushID(&pattern); if (idx < lastVisible) { this->draw(pattern); @@ -351,58 +394,13 @@ namespace hex { } } - void PatternDrawer::createLeafNode(const pl::ptrn::Pattern& pattern) const { - ImGui::TreeNodeEx(pattern.getDisplayName().c_str(), ImGuiTreeNodeFlags_Leaf | - ImGuiTreeNodeFlags_NoTreePushOnOpen | - ImGuiTreeNodeFlags_SpanFullWidth | - ImGuiTreeNodeFlags_AllowItemOverlap); - } - - bool PatternDrawer::createTreeNode(const pl::ptrn::Pattern& pattern) const { - if (pattern.isSealed()) { - ImGui::Indent(); - ImGui::TextUnformatted(pattern.getDisplayName().c_str()); - ImGui::Unindent(); - return false; - } - else - return ImGui::TreeNodeEx(pattern.getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth); - } - - void PatternDrawer::drawTypenameColumn(const pl::ptrn::Pattern& pattern, const std::string& pattern_name) const { - ImGui::TextFormattedColored(ImColor(0xFFD69C56), pattern_name); - ImGui::SameLine(); - ImGui::TextUnformatted(pattern.getTypeName().c_str()); - ImGui::TableNextColumn(); - } - - void PatternDrawer::drawNameColumn(const pl::ptrn::Pattern& pattern) const { - ImGui::TextUnformatted(pattern.getDisplayName().c_str()); - ImGui::TableNextColumn(); - } - - void PatternDrawer::drawColorColumn(const pl::ptrn::Pattern& pattern) const { - ImGui::ColorButton("color", ImColor(pattern.getColor()), ImGuiColorEditFlags_NoTooltip, ImVec2(ImGui::GetColumnWidth(), ImGui::GetTextLineHeight())); - ImGui::TableNextColumn(); - } - - void PatternDrawer::drawOffsetColumn(const pl::ptrn::Pattern& pattern) const { - ImGui::TextFormatted("0x{0:08X} : 0x{1:08X}", pattern.getOffset(), pattern.getOffset() + pattern.getSize() - (pattern.getSize() == 0 ? 0 : 1)); - ImGui::TableNextColumn(); - } - - void PatternDrawer::drawSizeColumn(const pl::ptrn::Pattern& pattern) const { - ImGui::TextFormatted("0x{0:04X}", pattern.getSize()); - ImGui::TableNextColumn(); - } - u64& PatternDrawer::getDisplayEnd(const pl::ptrn::Pattern& pattern) { - auto it = m_displayEnd.find(&pattern); - if (it != m_displayEnd.end()) { + auto it = this->m_displayEnd.find(&pattern); + if (it != this->m_displayEnd.end()) { return it->second; } - auto [inserted, success] = m_displayEnd.emplace(&pattern, DisplayEndDefault); - return inserted->second; + auto [value, success] = this->m_displayEnd.emplace(&pattern, DisplayEndDefault); + return value->second; } }; diff --git a/plugins/builtin/source/content/views/view_pattern_editor.cpp b/plugins/builtin/source/content/views/view_pattern_editor.cpp index a8b33f22e..2edb6c0bc 100644 --- a/plugins/builtin/source/content/views/view_pattern_editor.cpp +++ b/plugins/builtin/source/content/views/view_pattern_editor.cpp @@ -704,51 +704,51 @@ namespace hex::plugin::builtin { { ImGui::ColorButton(pattern->getVariableName().c_str(), ImColor(pattern->getColor())); ImGui::SameLine(0, 10); - ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "{}", pattern->getFormattedName()); + ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "{} ", pattern->getFormattedName()); ImGui::SameLine(0, 5); ImGui::TextFormatted("{}", pattern->getDisplayName()); ImGui::SameLine(); ImGui::SeparatorEx(ImGuiSeparatorFlags_Vertical); ImGui::SameLine(); - ImGui::TextFormatted("{}", pattern->getFormattedValue()); + ImGui::TextFormatted("{} ", pattern->getFormattedValue()); if (ImGui::GetIO().KeyShift) { ImGui::Indent(); - if (ImGui::BeginTable("##extra_info", 2, ImGuiTableFlags_RowBg | ImGuiTableFlags_NoClip)) { + if (ImGui::BeginTable("##extra_info", 2, ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersInnerV | ImGuiTableFlags_NoClip)) { ImGui::TableNextRow(); ImGui::TableNextColumn(); ImGui::TableNextRow(); ImGui::TableNextColumn(); - ImGui::TextFormatted("{}: ", "hex.builtin.common.type"_lang); + ImGui::TextFormatted("{} ", "hex.builtin.common.type"_lang); ImGui::TableNextColumn(); - ImGui::TextFormatted("{}", pattern->getTypeName()); + ImGui::TextFormatted(" {}", pattern->getTypeName()); ImGui::TableNextRow(); ImGui::TableNextColumn(); - ImGui::TextFormatted("{}: ", "hex.builtin.common.address"_lang); + ImGui::TextFormatted("{} ", "hex.builtin.common.address"_lang); ImGui::TableNextColumn(); - ImGui::TextFormatted("0x{:08X}", pattern->getOffset()); + ImGui::TextFormatted(" 0x{:08X}", pattern->getOffset()); ImGui::TableNextRow(); ImGui::TableNextColumn(); - ImGui::TextFormatted("{}: ", "hex.builtin.common.size"_lang); + ImGui::TextFormatted("{} ", "hex.builtin.common.size"_lang); ImGui::TableNextColumn(); - ImGui::TextFormatted("{}", hex::toByteString(pattern->getSize())); + ImGui::TextFormatted(" {}", hex::toByteString(pattern->getSize())); ImGui::TableNextRow(); ImGui::TableNextColumn(); - ImGui::TextFormatted("{}: ", "hex.builtin.common.endian"_lang); + ImGui::TextFormatted("{} ", "hex.builtin.common.endian"_lang); ImGui::TableNextColumn(); - ImGui::TextFormatted("{}", pattern->getEndian() == std::endian::little ? "hex.builtin.common.little"_lang : "hex.builtin.common.big"_lang); + ImGui::TextFormatted(" {}", pattern->getEndian() == std::endian::little ? "hex.builtin.common.little"_lang : "hex.builtin.common.big"_lang); if (const auto &comment = pattern->getComment(); comment != nullptr) { ImGui::TableNextRow(); ImGui::TableNextColumn(); - ImGui::TextFormatted("{}: ", "hex.builtin.common.comment"_lang); + ImGui::TextFormatted("{} ", "hex.builtin.common.comment"_lang); ImGui::TableNextColumn(); - ImGui::TextWrapped("\"%s\"", comment->c_str()); + ImGui::TextWrapped(" \"%s\"", comment->c_str()); } ImGui::EndTable();