1
0
mirror of synced 2024-11-12 02:00:52 +01:00

feat: Added table pattern visualizer

This commit is contained in:
WerWolv 2024-03-17 13:19:37 +01:00
parent 35d29c8e30
commit 6295c1d0c3
3 changed files with 62 additions and 0 deletions

View File

@ -25,6 +25,7 @@ add_imhex_plugin(
source/content/pl_visualizers/sound.cpp source/content/pl_visualizers/sound.cpp
source/content/pl_visualizers/coordinates.cpp source/content/pl_visualizers/coordinates.cpp
source/content/pl_visualizers/timestamp.cpp source/content/pl_visualizers/timestamp.cpp
source/content/pl_visualizers/table.cpp
INCLUDES INCLUDES
include include
${MINIAUDIO_INCLUDE_DIRS} ${MINIAUDIO_INCLUDE_DIRS}

View File

@ -13,6 +13,7 @@ namespace hex::plugin::visualizers {
void drawSoundVisualizer(pl::ptrn::Pattern &, pl::ptrn::IIterable &, bool, std::span<const pl::core::Token::Literal> arguments); void drawSoundVisualizer(pl::ptrn::Pattern &, pl::ptrn::IIterable &, bool, std::span<const pl::core::Token::Literal> arguments);
void drawCoordinateVisualizer(pl::ptrn::Pattern &, pl::ptrn::IIterable &, bool, std::span<const pl::core::Token::Literal> arguments); void drawCoordinateVisualizer(pl::ptrn::Pattern &, pl::ptrn::IIterable &, bool, std::span<const pl::core::Token::Literal> arguments);
void drawTimestampVisualizer(pl::ptrn::Pattern &, pl::ptrn::IIterable &, bool, std::span<const pl::core::Token::Literal> arguments); void drawTimestampVisualizer(pl::ptrn::Pattern &, pl::ptrn::IIterable &, bool, std::span<const pl::core::Token::Literal> arguments);
void drawTableVisualizer(pl::ptrn::Pattern &, pl::ptrn::IIterable &, bool, std::span<const pl::core::Token::Literal> arguments);
void registerPatternLanguageVisualizers() { void registerPatternLanguageVisualizers() {
using ParamCount = pl::api::FunctionParameterCount; using ParamCount = pl::api::FunctionParameterCount;
@ -25,6 +26,7 @@ namespace hex::plugin::visualizers {
ContentRegistry::PatternLanguage::addVisualizer("sound", drawSoundVisualizer, ParamCount::exactly(3)); ContentRegistry::PatternLanguage::addVisualizer("sound", drawSoundVisualizer, ParamCount::exactly(3));
ContentRegistry::PatternLanguage::addVisualizer("coordinates", drawCoordinateVisualizer, ParamCount::exactly(2)); ContentRegistry::PatternLanguage::addVisualizer("coordinates", drawCoordinateVisualizer, ParamCount::exactly(2));
ContentRegistry::PatternLanguage::addVisualizer("timestamp", drawTimestampVisualizer, ParamCount::exactly(1)); ContentRegistry::PatternLanguage::addVisualizer("timestamp", drawTimestampVisualizer, ParamCount::exactly(1));
ContentRegistry::PatternLanguage::addVisualizer("table", drawTableVisualizer, ParamCount::exactly(3));
} }
} }

View File

@ -0,0 +1,59 @@
#include <hex/helpers/utils.hpp>
#include <content/visualizer_helpers.hpp>
#include <imgui.h>
#include <imgui_internal.h>
#include <hex/ui/imgui_imhex_extensions.h>
#include <pl/patterns/pattern_array_static.hpp>
#include <pl/patterns/pattern_array_dynamic.hpp>
#include <pl/patterns/pattern_bitfield.hpp>
namespace hex::plugin::visualizers {
void drawTableVisualizer(pl::ptrn::Pattern &, pl::ptrn::IIterable &, bool shouldReset, std::span<const pl::core::Token::Literal> arguments) {
static std::vector<std::string> tableContent;
static u128 width = 0, height = 0;
if (shouldReset) {
tableContent.clear();
width = height = 0;
auto pattern = arguments[0].toPattern();
if (dynamic_cast<pl::ptrn::PatternArrayStatic*>(pattern.get()) == nullptr &&
dynamic_cast<pl::ptrn::PatternArrayDynamic*>(pattern.get()) == nullptr &&
dynamic_cast<pl::ptrn::PatternBitfieldArray*>(pattern.get()) == nullptr)
{
throw std::logic_error("Table visualizer requires an array pattern as the first argument.");
}
width = arguments[1].toUnsigned();
height = arguments[2].toUnsigned();
auto iterable = dynamic_cast<pl::ptrn::IIterable*>(pattern.get());
iterable->forEachEntry(0, iterable->getEntryCount(), [&](u64, pl::ptrn::Pattern *entry) {
tableContent.push_back(entry->toString());
});
}
if (width >= IMGUI_TABLE_MAX_COLUMNS)
throw std::logic_error(hex::format("Table visualizer cannot have more than {} columns.", IMGUI_TABLE_MAX_COLUMNS));
if (ImGui::BeginTable("##visualizer_table", width, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg)) {
for (u128 i = 0; i < height; i += 1) {
ImGui::TableNextRow();
for (u128 j = 0; j < width; j += 1) {
ImGui::TableSetColumnIndex(j);
if (i * width + j < tableContent.size())
ImGui::TextUnformatted(tableContent[i * width + j].c_str());
else
ImGui::TextUnformatted("??");
}
}
ImGui::EndTable();
}
}
}