From 77550d902c76f6f766bbd5d33778f0a7ab121991 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 26 Feb 2024 21:41:43 +0100 Subject: [PATCH] feat: Added option to disable annotations in byte type graph --- .../include/content/helpers/diagrams.hpp | 59 +++++++++++-------- plugins/builtin/romfs/lang/en_US.json | 1 + .../content/data_information_sections.cpp | 11 +++- 3 files changed, 44 insertions(+), 27 deletions(-) diff --git a/plugins/builtin/include/content/helpers/diagrams.hpp b/plugins/builtin/include/content/helpers/diagrams.hpp index a29607689..f24c1e7b6 100644 --- a/plugins/builtin/include/content/helpers/diagrams.hpp +++ b/plugins/builtin/include/content/helpers/diagrams.hpp @@ -791,35 +791,37 @@ namespace hex { ImPlot::PlotLine(Names[i], m_xBlockTypeDistributions.data(), m_yBlockTypeDistributionsSampled[i].data(), m_xBlockTypeDistributions.size()); } - u32 id = 1; - for (const auto &annotation : m_annotationRegions) { - const auto ®ion = annotation.region; - double xMin = region.getStartAddress(); - double xMax = region.getEndAddress(); - double yMin = 0.0F; - double yMax = 100.0F; + if (m_showAnnotations) { + u32 id = 1; + for (const auto &annotation : m_annotationRegions) { + const auto ®ion = annotation.region; + double xMin = region.getStartAddress(); + double xMax = region.getEndAddress(); + double yMin = 0.0F; + double yMax = 100.0F; - ImPlot::DragRect(id, &xMin, &yMin, &xMax, &yMax, annotation.color, ImPlotDragToolFlags_NoFit | ImPlotDragToolFlags_NoInputs); + ImPlot::DragRect(id, &xMin, &yMin, &xMax, &yMax, annotation.color, ImPlotDragToolFlags_NoFit | ImPlotDragToolFlags_NoInputs); - const auto min = ImPlot::PlotToPixels(xMin, yMax); - const auto max = ImPlot::PlotToPixels(xMax, yMin); - const auto mousePos = ImPlot::PixelsToPlot(ImGui::GetMousePos()); - if (ImGui::IsMouseHoveringRect(min, max)) { - ImPlot::Annotation(xMin + (xMax - xMin) / 2, mousePos.y, annotation.color, ImVec2(), false, Lang(annotation.unlocalizedName)); + const auto min = ImPlot::PlotToPixels(xMin, yMax); + const auto max = ImPlot::PlotToPixels(xMax, yMin); + const auto mousePos = ImPlot::PixelsToPlot(ImGui::GetMousePos()); + if (ImGui::IsMouseHoveringRect(min, max)) { + ImPlot::Annotation(xMin + (xMax - xMin) / 2, mousePos.y, annotation.color, ImVec2(), false, Lang(annotation.unlocalizedName)); - if (ImGui::IsMouseClicked(ImGuiMouseButton_Left)) { - ImHexApi::HexEditor::setSelection(annotation.region); + if (ImGui::IsMouseClicked(ImGuiMouseButton_Left)) { + ImHexApi::HexEditor::setSelection(annotation.region); + } } + + id += 1; } - id += 1; - } - - for (const auto &tag : m_tags) { - if (tag.axis == ImAxis_X1) - ImPlot::TagX(tag.value, ImGui::GetStyleColorVec4(tag.color), Lang(tag.unlocalizedName)); - else if (tag.axis == ImAxis_Y1) - ImPlot::TagY(tag.value, ImGui::GetStyleColorVec4(tag.color), Lang(tag.unlocalizedName)); + for (const auto &tag : m_tags) { + if (tag.axis == ImAxis_X1) + ImPlot::TagX(tag.value, ImGui::GetStyleColorVec4(tag.color), Lang(tag.unlocalizedName)); + else if (tag.axis == ImAxis_Y1) + ImPlot::TagY(tag.value, ImGui::GetStyleColorVec4(tag.color), Lang(tag.unlocalizedName)); + } } // The parameter updateHandle is used when using the pattern language since we don't have a provider @@ -951,6 +953,10 @@ namespace hex { m_handlePosition = filePosition; } + void enableAnnotations(bool enabled) { + m_showAnnotations = enabled; + } + private: static std::array calculateTypeDistribution(const std::array &valueCounts, size_t blockSize) { std::array counts = {}; @@ -1042,8 +1048,9 @@ namespace hex { } void addRegion(const UnlocalizedString &name, Region region, ImColor color) { - auto existingRegion = std::ranges::find_if(m_annotationRegions, [®ion](const AnnotationRegion &annotation) { - return annotation.region.getEndAddress() + 1 == region.getStartAddress(); + const auto existingRegion = std::ranges::find_if(m_annotationRegions, [this, ®ion](const AnnotationRegion &annotation) { + auto difference = i64(region.getEndAddress()) - i64(annotation.region.getEndAddress()); + return difference > 0 && difference < i64(m_blockSize * 32); }); if (existingRegion != m_annotationRegions.end()) { @@ -1096,5 +1103,7 @@ namespace hex { std::vector m_annotationRegions; std::vector m_tags; + + bool m_showAnnotations = true; }; } diff --git a/plugins/builtin/romfs/lang/en_US.json b/plugins/builtin/romfs/lang/en_US.json index 3ed1a05ab..08f15b06b 100644 --- a/plugins/builtin/romfs/lang/en_US.json +++ b/plugins/builtin/romfs/lang/en_US.json @@ -842,6 +842,7 @@ "hex.builtin.information_section.info_analysis.highest_entropy": "Highest block entropy", "hex.builtin.information_section.info_analysis.lowest_entropy": "Lowest block entropy", "hex.builtin.information_section.info_analysis": "Information analysis", + "hex.builtin.information_section.info_analysis.show_annotations": "Show annotations", "hex.builtin.information_section.relationship_analysis": "Byte Relationship", "hex.builtin.information_section.relationship_analysis.sample_size": "Sample size", "hex.builtin.information_section.relationship_analysis.brightness": "Brightness", diff --git a/plugins/builtin/source/content/data_information_sections.cpp b/plugins/builtin/source/content/data_information_sections.cpp index db43ca14b..108ccdd37 100644 --- a/plugins/builtin/source/content/data_information_sections.cpp +++ b/plugins/builtin/source/content/data_information_sections.cpp @@ -164,6 +164,8 @@ namespace hex::plugin::builtin { m_chunkBasedEntropy.reset(m_inputChunkSize, region.getStartAddress(), region.getEndAddress(), provider->getBaseAddress(), provider->getActualSize()); + m_byteTypesDistribution.enableAnnotations(m_showAnnotations); + // Create a handle to the file auto reader = prv::ProviderReader(provider); reader.seek(region.getStartAddress()); @@ -194,6 +196,7 @@ namespace hex::plugin::builtin { void drawSettings() override { ImGuiExt::InputHexadecimal("hex.builtin.information_section.info_analysis.block_size"_lang, &m_inputChunkSize); + ImGui::Checkbox("hex.builtin.information_section.info_analysis.show_annotations"_lang, &m_showAnnotations); } void drawContent() override { @@ -319,12 +322,14 @@ namespace hex::plugin::builtin { void load(const nlohmann::json &data) override { InformationSection::load(data); - m_inputChunkSize = data.value("block_size", 0); + m_inputChunkSize = data.value("block_size", 0); + m_showAnnotations = data.value("annotations", true); } nlohmann::json store() override { auto result = InformationSection::store(); - result["block_size"] = m_inputChunkSize; + result["block_size"] = m_inputChunkSize; + result["annotations"] = m_showAnnotations; return result; } @@ -341,6 +346,8 @@ namespace hex::plugin::builtin { u64 m_lowestBlockEntropyAddress = 0x00; double m_plainTextCharacterPercentage = -1.0; + bool m_showAnnotations = true; + DiagramByteDistribution m_byteDistribution; DiagramByteTypesDistribution m_byteTypesDistribution; DiagramChunkBasedEntropyAnalysis m_chunkBasedEntropy;