feat: Added option to disable annotations in byte type graph

This commit is contained in:
WerWolv 2024-02-26 21:41:43 +01:00
parent 41935781fb
commit 77550d902c
3 changed files with 44 additions and 27 deletions

View file

@ -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 &region = 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 &region = 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<float, 12> calculateTypeDistribution(const std::array<ImU64, 256> &valueCounts, size_t blockSize) {
std::array<ImU64, 12> counts = {};
@ -1042,8 +1048,9 @@ namespace hex {
}
void addRegion(const UnlocalizedString &name, Region region, ImColor color) {
auto existingRegion = std::ranges::find_if(m_annotationRegions, [&region](const AnnotationRegion &annotation) {
return annotation.region.getEndAddress() + 1 == region.getStartAddress();
const auto existingRegion = std::ranges::find_if(m_annotationRegions, [this, &region](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<AnnotationRegion> m_annotationRegions;
std::vector<Tag> m_tags;
bool m_showAnnotations = true;
};
}

View file

@ -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",

View file

@ -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;