1
0
mirror of synced 2025-01-31 12:03:46 +01:00

sys: Refactor and cleanup pattern drawer

This commit is contained in:
WerWolv 2022-09-07 23:11:13 +02:00
parent a22725bb67
commit cfde9939b4
5 changed files with 122 additions and 140 deletions

View File

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

View File

@ -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<ArrayPattern T>
@ -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:

View File

@ -1,15 +1,12 @@
#pragma once
#include "pattern_drawer.hpp"
#include <hex.hpp>
#include <imgui.h>
#include <hex/ui/view.hpp>
#include <content/helpers/pattern_drawer.hpp>
#include <vector>
#include <tuple>
#include <cstdio>
namespace hex::plugin::builtin {

View File

@ -1,4 +1,4 @@
#include "pattern_drawer.hpp"
#include <content/helpers/pattern_drawer.hpp>
#include <pl/patterns/pattern_array_dynamic.hpp>
#include <pl/patterns/pattern_array_static.hpp>
@ -25,14 +25,99 @@
#include <imgui.h>
#include <hex/ui/imgui_imhex_extensions.h>
namespace hex {
namespace {
constexpr static auto DisplayEndDefault = 50u;
constexpr static auto DisplayEndStep = 50u;
constexpr auto DisplayEndDefault = 50u;
constexpr auto DisplayEndStep = 50u;
using namespace ::std::literals::string_literals;
};
namespace hex {
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<int>(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<u128>(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<int>(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;
}
};

View File

@ -714,39 +714,39 @@ namespace hex::plugin::builtin {
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::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::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::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);
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());
}