2022-11-08 21:43:22 +01:00
|
|
|
#include <ui/pattern_drawer.hpp>
|
2022-04-08 16:08:02 +02:00
|
|
|
|
2022-04-17 16:57:30 +02:00
|
|
|
#include <pl/patterns/pattern_array_dynamic.hpp>
|
|
|
|
#include <pl/patterns/pattern_array_static.hpp>
|
|
|
|
#include <pl/patterns/pattern_bitfield.hpp>
|
|
|
|
#include <pl/patterns/pattern_boolean.hpp>
|
|
|
|
#include <pl/patterns/pattern_character.hpp>
|
|
|
|
#include <pl/patterns/pattern_enum.hpp>
|
|
|
|
#include <pl/patterns/pattern_float.hpp>
|
|
|
|
#include <pl/patterns/pattern_padding.hpp>
|
|
|
|
#include <pl/patterns/pattern_pointer.hpp>
|
|
|
|
#include <pl/patterns/pattern_signed.hpp>
|
|
|
|
#include <pl/patterns/pattern_string.hpp>
|
|
|
|
#include <pl/patterns/pattern_struct.hpp>
|
|
|
|
#include <pl/patterns/pattern_union.hpp>
|
|
|
|
#include <pl/patterns/pattern_unsigned.hpp>
|
|
|
|
#include <pl/patterns/pattern_wide_character.hpp>
|
|
|
|
#include <pl/patterns/pattern_wide_string.hpp>
|
2022-04-08 16:08:02 +02:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2022-04-17 16:57:30 +02:00
|
|
|
#include <hex/api/imhex_api.hpp>
|
|
|
|
#include <hex/helpers/utils.hpp>
|
2022-11-08 21:43:22 +01:00
|
|
|
#include <hex/api/localization.hpp>
|
2022-04-17 16:57:30 +02:00
|
|
|
|
|
|
|
#include <imgui.h>
|
|
|
|
#include <hex/ui/imgui_imhex_extensions.h>
|
|
|
|
|
2022-11-08 21:43:22 +01:00
|
|
|
namespace hex::plugin::builtin::ui {
|
2022-04-08 16:08:02 +02:00
|
|
|
|
2022-09-07 23:11:13 +02:00
|
|
|
namespace {
|
2022-04-08 16:08:02 +02:00
|
|
|
|
2022-09-12 21:28:02 +02:00
|
|
|
constexpr auto DisplayEndDefault = 50U;
|
|
|
|
constexpr auto DisplayEndStep = 50U;
|
2022-09-07 23:11:13 +02:00
|
|
|
|
|
|
|
using namespace ::std::literals::string_literals;
|
|
|
|
|
2022-10-09 15:56:26 +02:00
|
|
|
bool isPatternSelected(u64 address, u64 size) {
|
|
|
|
auto currSelection = ImHexApi::HexEditor::getSelection();
|
|
|
|
if (!currSelection.has_value())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return Region{ address, size }.overlaps(*currSelection);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
auto highlightWhenSelected(u64 address, u64 size, const T &callback) {
|
|
|
|
constexpr bool HasReturn = !requires(T t) { { t() } -> std::same_as<void>; };
|
|
|
|
|
|
|
|
auto selected = isPatternSelected(address, size);
|
|
|
|
|
|
|
|
if (selected)
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_Text, ImGui::GetStyleColorVec4(ImGuiCol_HeaderActive));
|
|
|
|
|
|
|
|
if constexpr (HasReturn) {
|
|
|
|
auto result = callback();
|
|
|
|
|
|
|
|
if (selected)
|
|
|
|
ImGui::PopStyleColor();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
|
|
|
|
if (selected)
|
|
|
|
ImGui::PopStyleColor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
auto highlightWhenSelected(const pl::ptrn::Pattern& pattern, const T &callback) {
|
|
|
|
return highlightWhenSelected(pattern.getOffset(), pattern.getSize(), callback);
|
|
|
|
}
|
|
|
|
|
2022-09-07 23:11:13 +02:00
|
|
|
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();
|
2022-10-10 14:40:49 +02:00
|
|
|
highlightWhenSelected(pattern, [&]{ ImGui::TextUnformatted(pattern.getDisplayName().c_str()); });
|
2022-09-07 23:11:13 +02:00
|
|
|
ImGui::Unindent();
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-09 15:56:26 +02:00
|
|
|
else {
|
|
|
|
return highlightWhenSelected(pattern, [&]{ return ImGui::TreeNodeEx(pattern.getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth);});
|
|
|
|
}
|
2022-09-07 23:11:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2022-10-09 15:56:26 +02:00
|
|
|
highlightWhenSelected(pattern, [&]{ ImGui::TextUnformatted(pattern.getDisplayName().c_str()); });
|
2022-09-07 23:11:13 +02:00
|
|
|
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) {
|
2022-11-07 00:04:47 +01:00
|
|
|
if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem)) {
|
|
|
|
if (auto comment = pattern.getComment(); !comment.empty()) {
|
|
|
|
ImGui::BeginTooltip();
|
|
|
|
ImGui::TextUnformatted(pattern.getComment().c_str());
|
|
|
|
ImGui::EndTooltip();
|
|
|
|
}
|
2022-09-07 23:11:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-04-08 16:08:02 +02:00
|
|
|
|
2022-09-19 14:16:23 +02:00
|
|
|
|
|
|
|
|
2022-08-06 21:38:09 +02:00
|
|
|
void PatternDrawer::visit(pl::ptrn::PatternArrayDynamic& pattern) {
|
2022-09-19 14:16:23 +02:00
|
|
|
drawArray(pattern, pattern, pattern.isInlined());
|
2022-04-08 16:08:02 +02:00
|
|
|
}
|
|
|
|
|
2022-08-06 21:38:09 +02:00
|
|
|
void PatternDrawer::visit(pl::ptrn::PatternArrayStatic& pattern) {
|
2022-09-19 14:16:23 +02:00
|
|
|
drawArray(pattern, pattern, pattern.isInlined());
|
2022-04-08 16:08:02 +02:00
|
|
|
}
|
|
|
|
|
2022-08-06 21:38:09 +02:00
|
|
|
void PatternDrawer::visit(pl::ptrn::PatternBitfieldField& pattern) {
|
2022-04-08 16:08:02 +02:00
|
|
|
ImGui::TableNextRow();
|
2022-07-25 12:09:28 +02:00
|
|
|
createLeafNode(pattern);
|
2022-04-08 16:08:02 +02:00
|
|
|
ImGui::TableNextColumn();
|
2022-07-25 12:09:28 +02:00
|
|
|
|
2022-04-08 16:08:02 +02:00
|
|
|
makeSelectable(pattern);
|
2022-07-25 12:09:28 +02:00
|
|
|
drawCommentTooltip(pattern);
|
|
|
|
ImGui::SameLine();
|
|
|
|
drawNameColumn(pattern);
|
2022-04-08 16:08:02 +02:00
|
|
|
drawColorColumn(pattern);
|
|
|
|
|
|
|
|
auto byteAddr = pattern.getOffset() + pattern.getBitOffset() / 8;
|
|
|
|
auto firstBitIdx = pattern.getBitOffset() % 8;
|
2022-08-09 08:38:41 +02:00
|
|
|
auto lastBitIdx = firstBitIdx + (pattern.getBitSize() - 1);
|
2022-04-08 16:08:02 +02:00
|
|
|
if (firstBitIdx == lastBitIdx)
|
|
|
|
ImGui::TextFormatted("0x{0:08X} bit {1}", byteAddr, firstBitIdx);
|
|
|
|
else
|
|
|
|
ImGui::TextFormatted("0x{0:08X} bits {1} - {2}", byteAddr, firstBitIdx, lastBitIdx);
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
if (pattern.getBitSize() == 1)
|
|
|
|
ImGui::TextFormatted("{0} bit", pattern.getBitSize());
|
|
|
|
else
|
|
|
|
ImGui::TextFormatted("{0} bits", pattern.getBitSize());
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "bits");
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
ImGui::TextFormatted("{}", pattern.getFormattedValue());
|
2022-04-08 16:08:02 +02:00
|
|
|
}
|
|
|
|
|
2022-08-06 21:38:09 +02:00
|
|
|
void PatternDrawer::visit(pl::ptrn::PatternBitfield& pattern) {
|
2022-04-08 16:08:02 +02:00
|
|
|
bool open = true;
|
|
|
|
if (!pattern.isInlined()) {
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
open = createTreeNode(pattern);
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
makeSelectable(pattern);
|
|
|
|
drawCommentTooltip(pattern);
|
2022-09-05 23:34:20 +02:00
|
|
|
drawColorColumn(pattern);
|
2022-04-08 16:08:02 +02:00
|
|
|
drawOffsetColumn(pattern);
|
|
|
|
drawSizeColumn(pattern);
|
|
|
|
drawTypenameColumn(pattern, "bitfield");
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
ImGui::TextFormatted("{}", pattern.getFormattedValue());
|
2022-04-08 16:08:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (open) {
|
|
|
|
pattern.forEachMember([&] (auto &field) {
|
|
|
|
this->draw(field);
|
|
|
|
});
|
|
|
|
|
2022-09-06 20:04:55 +02:00
|
|
|
if (!pattern.isInlined())
|
|
|
|
ImGui::TreePop();
|
2022-04-08 16:08:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-06 21:38:09 +02:00
|
|
|
void PatternDrawer::visit(pl::ptrn::PatternBoolean& pattern) {
|
2022-09-07 23:11:13 +02:00
|
|
|
createDefaultEntry(pattern);
|
2022-04-08 16:08:02 +02:00
|
|
|
}
|
|
|
|
|
2022-08-06 21:38:09 +02:00
|
|
|
void PatternDrawer::visit(pl::ptrn::PatternCharacter& pattern) {
|
2022-09-07 23:11:13 +02:00
|
|
|
createDefaultEntry(pattern);
|
2022-04-08 16:08:02 +02:00
|
|
|
}
|
|
|
|
|
2022-08-06 21:38:09 +02:00
|
|
|
void PatternDrawer::visit(pl::ptrn::PatternEnum& pattern) {
|
2022-04-08 16:08:02 +02:00
|
|
|
ImGui::TableNextRow();
|
|
|
|
createLeafNode(pattern);
|
|
|
|
drawCommentTooltip(pattern);
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
makeSelectable(pattern);
|
|
|
|
ImGui::SameLine();
|
|
|
|
drawNameColumn(pattern);
|
|
|
|
drawColorColumn(pattern);
|
|
|
|
drawOffsetColumn(pattern);
|
|
|
|
drawSizeColumn(pattern);
|
|
|
|
drawTypenameColumn(pattern, "enum");
|
2022-05-27 20:42:07 +02:00
|
|
|
ImGui::TextFormatted("{}", pattern.getFormattedValue());
|
2022-04-08 16:08:02 +02:00
|
|
|
}
|
|
|
|
|
2022-08-06 21:38:09 +02:00
|
|
|
void PatternDrawer::visit(pl::ptrn::PatternFloat& pattern) {
|
2022-09-07 23:11:13 +02:00
|
|
|
createDefaultEntry(pattern);
|
2022-04-08 16:08:02 +02:00
|
|
|
}
|
|
|
|
|
2022-08-06 21:38:09 +02:00
|
|
|
void PatternDrawer::visit(pl::ptrn::PatternPadding& pattern) {
|
2022-04-08 16:08:02 +02:00
|
|
|
// Do nothing
|
|
|
|
hex::unused(pattern);
|
|
|
|
}
|
|
|
|
|
2022-08-06 21:38:09 +02:00
|
|
|
void PatternDrawer::visit(pl::ptrn::PatternPointer& pattern) {
|
2022-04-08 16:08:02 +02:00
|
|
|
bool open = true;
|
|
|
|
|
|
|
|
if (!pattern.isInlined()) {
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
open = createTreeNode(pattern);
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
makeSelectable(pattern);
|
|
|
|
drawCommentTooltip(pattern);
|
|
|
|
drawColorColumn(pattern);
|
|
|
|
drawOffsetColumn(pattern);
|
|
|
|
drawSizeColumn(pattern);
|
|
|
|
ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "{}", pattern.getFormattedName());
|
|
|
|
ImGui::TableNextColumn();
|
2022-05-27 20:42:07 +02:00
|
|
|
ImGui::TextFormatted("{}", pattern.getFormattedValue());
|
2022-04-08 16:08:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (open) {
|
|
|
|
pattern.getPointedAtPattern()->accept(*this);
|
|
|
|
|
2022-09-06 20:04:55 +02:00
|
|
|
if (!pattern.isInlined())
|
|
|
|
ImGui::TreePop();
|
2022-04-08 16:08:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-06 21:38:09 +02:00
|
|
|
void PatternDrawer::visit(pl::ptrn::PatternSigned& pattern) {
|
2022-09-07 23:11:13 +02:00
|
|
|
createDefaultEntry(pattern);
|
2022-04-08 16:08:02 +02:00
|
|
|
}
|
|
|
|
|
2022-08-06 21:38:09 +02:00
|
|
|
void PatternDrawer::visit(pl::ptrn::PatternString& pattern) {
|
2022-07-16 13:08:08 +02:00
|
|
|
if (pattern.getSize() > 0)
|
2022-09-07 23:11:13 +02:00
|
|
|
createDefaultEntry(pattern);
|
2022-07-16 13:08:08 +02:00
|
|
|
}
|
2022-04-08 16:08:02 +02:00
|
|
|
|
2022-08-06 21:38:09 +02:00
|
|
|
void PatternDrawer::visit(pl::ptrn::PatternStruct& pattern) {
|
2022-04-08 16:08:02 +02:00
|
|
|
bool open = true;
|
|
|
|
|
|
|
|
if (!pattern.isInlined()) {
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
open = createTreeNode(pattern);
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
makeSelectable(pattern);
|
|
|
|
drawCommentTooltip(pattern);
|
2022-09-05 23:34:20 +02:00
|
|
|
if (pattern.isSealed())
|
|
|
|
drawColorColumn(pattern);
|
|
|
|
else
|
|
|
|
ImGui::TableNextColumn();
|
2022-04-08 16:08:02 +02:00
|
|
|
drawOffsetColumn(pattern);
|
|
|
|
drawSizeColumn(pattern);
|
|
|
|
drawTypenameColumn(pattern, "struct");
|
2022-05-27 20:42:07 +02:00
|
|
|
ImGui::TextFormatted("{}", pattern.getFormattedValue());
|
2022-04-08 16:08:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (open) {
|
2022-09-19 14:16:23 +02:00
|
|
|
pattern.forEachEntry(0, pattern.getMembers().size(), [&](u64, auto *member){
|
|
|
|
this->draw(*member);
|
2022-04-08 16:08:02 +02:00
|
|
|
});
|
|
|
|
|
2022-09-06 20:04:55 +02:00
|
|
|
if (!pattern.isInlined())
|
|
|
|
ImGui::TreePop();
|
2022-04-08 16:08:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-06 21:38:09 +02:00
|
|
|
void PatternDrawer::visit(pl::ptrn::PatternUnion& pattern) {
|
2022-04-08 16:08:02 +02:00
|
|
|
bool open = true;
|
|
|
|
|
|
|
|
if (!pattern.isInlined()) {
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
open = createTreeNode(pattern);
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
makeSelectable(pattern);
|
|
|
|
drawCommentTooltip(pattern);
|
2022-09-05 23:34:20 +02:00
|
|
|
if (pattern.isSealed())
|
|
|
|
drawColorColumn(pattern);
|
|
|
|
else
|
|
|
|
ImGui::TableNextColumn();
|
2022-04-08 16:08:02 +02:00
|
|
|
drawOffsetColumn(pattern);
|
|
|
|
drawSizeColumn(pattern);
|
|
|
|
drawTypenameColumn(pattern, "union");
|
2022-05-27 20:42:07 +02:00
|
|
|
ImGui::TextFormatted("{}", pattern.getFormattedValue());
|
2022-04-08 16:08:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (open) {
|
2022-09-19 14:16:23 +02:00
|
|
|
pattern.forEachEntry(0, pattern.getEntryCount(), [&](u64, auto *member) {
|
|
|
|
this->draw(*member);
|
2022-04-08 16:08:02 +02:00
|
|
|
});
|
|
|
|
|
2022-09-06 20:04:55 +02:00
|
|
|
if (!pattern.isInlined())
|
|
|
|
ImGui::TreePop();
|
2022-04-08 16:08:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-06 21:38:09 +02:00
|
|
|
void PatternDrawer::visit(pl::ptrn::PatternUnsigned& pattern) {
|
2022-09-07 23:11:13 +02:00
|
|
|
createDefaultEntry(pattern);
|
2022-04-08 16:08:02 +02:00
|
|
|
}
|
|
|
|
|
2022-08-06 21:38:09 +02:00
|
|
|
void PatternDrawer::visit(pl::ptrn::PatternWideCharacter& pattern) {
|
2022-09-07 23:11:13 +02:00
|
|
|
createDefaultEntry(pattern);
|
2022-04-08 16:08:02 +02:00
|
|
|
}
|
|
|
|
|
2022-08-06 21:38:09 +02:00
|
|
|
void PatternDrawer::visit(pl::ptrn::PatternWideString& pattern) {
|
2022-07-16 13:08:08 +02:00
|
|
|
if (pattern.getSize() > 0)
|
2022-09-07 23:11:13 +02:00
|
|
|
createDefaultEntry(pattern);
|
2022-04-08 16:08:02 +02:00
|
|
|
}
|
|
|
|
|
2022-08-06 21:38:09 +02:00
|
|
|
void PatternDrawer::draw(pl::ptrn::Pattern& pattern) {
|
2022-04-08 16:08:02 +02:00
|
|
|
if (pattern.isHidden())
|
|
|
|
return;
|
|
|
|
|
|
|
|
pattern.accept(*this);
|
|
|
|
}
|
|
|
|
|
2022-09-19 14:16:23 +02:00
|
|
|
void PatternDrawer::drawArray(pl::ptrn::Pattern& pattern, pl::ptrn::Iteratable &iteratable, bool isInlined) {
|
|
|
|
if (iteratable.getEntryCount() == 0)
|
|
|
|
return;
|
2022-04-08 16:08:02 +02:00
|
|
|
|
|
|
|
bool open = true;
|
|
|
|
if (!isInlined) {
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
open = createTreeNode(pattern);
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
makeSelectable(pattern);
|
|
|
|
drawCommentTooltip(pattern);
|
2022-09-05 23:34:20 +02:00
|
|
|
if (pattern.isSealed())
|
|
|
|
drawColorColumn(pattern);
|
|
|
|
else
|
|
|
|
ImGui::TableNextColumn();
|
2022-04-08 16:08:02 +02:00
|
|
|
drawOffsetColumn(pattern);
|
|
|
|
drawSizeColumn(pattern);
|
|
|
|
ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "{0}", pattern.getTypeName());
|
|
|
|
ImGui::SameLine(0, 0);
|
|
|
|
|
|
|
|
ImGui::TextUnformatted("[");
|
|
|
|
ImGui::SameLine(0, 0);
|
2022-09-19 14:16:23 +02:00
|
|
|
ImGui::TextFormattedColored(ImColor(0xFF00FF00), "{0}", iteratable.getEntryCount());
|
2022-04-08 16:08:02 +02:00
|
|
|
ImGui::SameLine(0, 0);
|
|
|
|
ImGui::TextUnformatted("]");
|
|
|
|
|
|
|
|
ImGui::TableNextColumn();
|
2022-05-27 20:42:07 +02:00
|
|
|
ImGui::TextFormatted("{}", pattern.getFormattedValue());
|
2022-04-08 16:08:02 +02:00
|
|
|
}
|
|
|
|
|
2022-09-19 14:16:23 +02:00
|
|
|
if (open) {
|
|
|
|
u64 chunkCount = 0;
|
|
|
|
for (u64 i = 0; i < iteratable.getEntryCount(); i += ChunkSize) {
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
2022-04-08 16:08:02 +02:00
|
|
|
|
2022-09-19 14:16:23 +02:00
|
|
|
chunkCount++;
|
|
|
|
|
|
|
|
auto &displayEnd = this->getDisplayEnd(pattern);
|
|
|
|
if (chunkCount > displayEnd) {
|
2022-11-08 21:43:22 +01:00
|
|
|
ImGui::Selectable(hex::format("... ({})", "hex.builtin.pattern_drawer.double_click"_lang).c_str(), false, ImGuiSelectableFlags_SpanAllColumns);
|
2022-09-19 14:16:23 +02:00
|
|
|
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left))
|
|
|
|
displayEnd += DisplayEndStep;
|
|
|
|
break;
|
|
|
|
} else {
|
2022-09-19 15:26:25 +02:00
|
|
|
auto endIndex = std::min<u64>(iteratable.getEntryCount(), i + ChunkSize);
|
2022-09-19 14:16:23 +02:00
|
|
|
|
|
|
|
auto startOffset = iteratable.getEntry(i)->getOffset();
|
|
|
|
auto endOffset = iteratable.getEntry(endIndex - 1)->getOffset();
|
|
|
|
auto endSize = iteratable.getEntry(endIndex - 1)->getSize();
|
|
|
|
|
|
|
|
size_t chunkSize = (endOffset - startOffset) + endSize;
|
|
|
|
|
2022-10-09 15:56:26 +02:00
|
|
|
auto chunkOpen = highlightWhenSelected(startOffset, ((endOffset + endSize) - startOffset) - 1, [&]{ return ImGui::TreeNodeEx(hex::format("[{} ... {}]", i, endIndex - 1).c_str(), ImGuiTreeNodeFlags_SpanFullWidth); });
|
2022-09-19 14:16:23 +02:00
|
|
|
ImGui::TableNextColumn();
|
|
|
|
drawColorColumn(pattern);
|
|
|
|
ImGui::TextFormatted("0x{0:08X} : 0x{1:08X}", startOffset, startOffset + chunkSize - (pattern.getSize() == 0 ? 0 : 1));
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TextFormatted("0x{0:04X}", chunkSize);
|
|
|
|
ImGui::TableNextColumn();
|
2022-09-19 14:21:36 +02:00
|
|
|
ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "{0}", pattern.getTypeName());
|
|
|
|
ImGui::SameLine(0, 0);
|
|
|
|
|
|
|
|
ImGui::TextUnformatted("[");
|
|
|
|
ImGui::SameLine(0, 0);
|
|
|
|
ImGui::TextFormattedColored(ImColor(0xFF00FF00), "{0}", endIndex - i);
|
|
|
|
ImGui::SameLine(0, 0);
|
|
|
|
ImGui::TextUnformatted("]");
|
|
|
|
|
2022-09-19 14:16:23 +02:00
|
|
|
ImGui::TableNextColumn();
|
2022-09-19 14:21:36 +02:00
|
|
|
ImGui::TextFormatted("[ ... ]");
|
2022-09-19 14:16:23 +02:00
|
|
|
|
|
|
|
if (chunkOpen) {
|
|
|
|
iteratable.forEachEntry(i, endIndex, [&](u64, auto *entry){
|
|
|
|
this->draw(*entry);
|
|
|
|
});
|
|
|
|
|
|
|
|
ImGui::TreePop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-08 16:08:02 +02:00
|
|
|
|
2022-09-19 14:16:23 +02:00
|
|
|
if (!isInlined)
|
2022-09-06 20:04:55 +02:00
|
|
|
ImGui::TreePop();
|
2022-04-08 16:08:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-06 21:38:09 +02:00
|
|
|
u64& PatternDrawer::getDisplayEnd(const pl::ptrn::Pattern& pattern) {
|
2022-09-07 23:11:13 +02:00
|
|
|
auto it = this->m_displayEnd.find(&pattern);
|
|
|
|
if (it != this->m_displayEnd.end()) {
|
2022-04-08 16:08:02 +02:00
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2022-09-07 23:11:13 +02:00
|
|
|
auto [value, success] = this->m_displayEnd.emplace(&pattern, DisplayEndDefault);
|
|
|
|
return value->second;
|
2022-04-08 16:08:02 +02:00
|
|
|
}
|
2022-11-08 21:43:22 +01:00
|
|
|
|
|
|
|
static bool sortPatterns(const ImGuiTableSortSpecs* sortSpecs, const pl::ptrn::Pattern * left, const pl::ptrn::Pattern * right) {
|
|
|
|
if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("name")) {
|
|
|
|
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
|
|
|
|
return left->getDisplayName() > right->getDisplayName();
|
|
|
|
else
|
|
|
|
return left->getDisplayName() < right->getDisplayName();
|
|
|
|
} else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("offset")) {
|
|
|
|
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
|
|
|
|
return left->getOffset() > right->getOffset();
|
|
|
|
else
|
|
|
|
return left->getOffset() < right->getOffset();
|
|
|
|
} else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("size")) {
|
|
|
|
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
|
|
|
|
return left->getSize() > right->getSize();
|
|
|
|
else
|
|
|
|
return left->getSize() < right->getSize();
|
|
|
|
} else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("value")) {
|
|
|
|
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
|
|
|
|
return left->getValue() > right->getValue();
|
|
|
|
else
|
|
|
|
return left->getValue() < right->getValue();
|
|
|
|
} else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("type")) {
|
|
|
|
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
|
|
|
|
return left->getTypeName() > right->getTypeName();
|
|
|
|
else
|
|
|
|
return left->getTypeName() < right->getTypeName();
|
|
|
|
} else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("color")) {
|
|
|
|
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
|
|
|
|
return left->getColor() > right->getColor();
|
|
|
|
else
|
|
|
|
return left->getColor() < right->getColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool beginPatternTable(const std::vector<std::shared_ptr<pl::ptrn::Pattern>> &patterns, std::vector<pl::ptrn::Pattern*> &sortedPatterns, float height) {
|
|
|
|
if (ImGui::BeginTable("##Patterntable", 6, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Sortable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollY, ImVec2(0, height))) {
|
|
|
|
ImGui::TableSetupScrollFreeze(0, 1);
|
|
|
|
ImGui::TableSetupColumn("hex.builtin.pattern_drawer.var_name"_lang, ImGuiTableColumnFlags_PreferSortAscending, 0, ImGui::GetID("name"));
|
|
|
|
ImGui::TableSetupColumn("hex.builtin.pattern_drawer.color"_lang, ImGuiTableColumnFlags_PreferSortAscending, 0, ImGui::GetID("color"));
|
|
|
|
ImGui::TableSetupColumn("hex.builtin.pattern_drawer.offset"_lang, ImGuiTableColumnFlags_PreferSortAscending | ImGuiTableColumnFlags_DefaultSort, 0, ImGui::GetID("offset"));
|
|
|
|
ImGui::TableSetupColumn("hex.builtin.pattern_drawer.size"_lang, ImGuiTableColumnFlags_PreferSortAscending, 0, ImGui::GetID("size"));
|
|
|
|
ImGui::TableSetupColumn("hex.builtin.pattern_drawer.type"_lang, ImGuiTableColumnFlags_PreferSortAscending, 0, ImGui::GetID("type"));
|
|
|
|
ImGui::TableSetupColumn("hex.builtin.pattern_drawer.value"_lang, ImGuiTableColumnFlags_PreferSortAscending, 0, ImGui::GetID("value"));
|
|
|
|
|
|
|
|
auto sortSpecs = ImGui::TableGetSortSpecs();
|
|
|
|
|
2022-11-10 11:18:14 +01:00
|
|
|
if (patterns.empty())
|
|
|
|
sortedPatterns.clear();
|
|
|
|
|
2022-11-08 21:43:22 +01:00
|
|
|
if (!patterns.empty() && (sortSpecs->SpecsDirty || sortedPatterns.empty())) {
|
|
|
|
sortedPatterns.clear();
|
|
|
|
std::transform(patterns.begin(), patterns.end(), std::back_inserter(sortedPatterns), [](const std::shared_ptr<pl::ptrn::Pattern> &pattern) {
|
|
|
|
return pattern.get();
|
|
|
|
});
|
|
|
|
|
|
|
|
std::sort(sortedPatterns.begin(), sortedPatterns.end(), [&sortSpecs](pl::ptrn::Pattern *left, pl::ptrn::Pattern *right) -> bool {
|
|
|
|
return 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);
|
|
|
|
});
|
|
|
|
|
|
|
|
sortSpecs->SpecsDirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PatternDrawer::draw(const std::vector<std::shared_ptr<pl::ptrn::Pattern>> &patterns, float height) {
|
|
|
|
if (beginPatternTable(patterns, this->m_sortedPatterns, height)) {
|
|
|
|
ImGui::TableHeadersRow();
|
|
|
|
|
|
|
|
for (auto &pattern : this->m_sortedPatterns) {
|
|
|
|
this->draw(*pattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndTable();
|
|
|
|
}
|
|
|
|
}
|
2022-10-02 17:30:26 +02:00
|
|
|
}
|