1
0
mirror of synced 2024-12-03 03:37:19 +01:00
ImHex/source/views/view_pattern_data.cpp

119 lines
5.9 KiB
C++
Raw Normal View History

2020-11-10 21:31:04 +01:00
#include "views/view_pattern_data.hpp"
#include "providers/provider.hpp"
2020-11-10 21:31:04 +01:00
namespace hex {
ViewPatternData::ViewPatternData(prv::Provider* &dataProvider, std::vector<hex::PatternData*> &patternData)
: View(), m_dataProvider(dataProvider), m_patternData(patternData) {
2020-11-10 21:31:04 +01:00
2020-11-15 00:46:18 +01:00
this->subscribeEvent(Events::PatternChanged, [this](auto data) {
this->m_sortedPatternData.clear();
});
2020-11-10 21:31:04 +01:00
}
ViewPatternData::~ViewPatternData() {
2020-11-15 00:46:18 +01:00
this->unsubscribeEvent(Events::PatternChanged);
2020-11-10 21:31:04 +01:00
}
void ViewPatternData::createView() {
if (!this->m_windowOpen)
return;
if (ImGui::Begin("Pattern Data", &this->m_windowOpen)) {
ImGui::BeginChild("##scrolling", ImVec2(0, 0), false, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoNav);
2020-11-12 23:57:43 +01:00
if (this->m_dataProvider != nullptr && this->m_dataProvider->isReadable()) {
2020-11-10 21:31:04 +01:00
2020-11-15 00:46:18 +01:00
if (ImGui::BeginTable("##patterndatatable", 5, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Sortable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_RowBg)) {
ImGui::TableSetupColumn("Name", 0, -1, ImGui::GetID("name"));
ImGui::TableSetupColumn("Position", 0, -1, ImGui::GetID("position"));
ImGui::TableSetupColumn("Size", 0, -1, ImGui::GetID("size"));
ImGui::TableSetupColumn("Type", 0, -1, ImGui::GetID("type"));
ImGui::TableSetupColumn("Value", 0, -1, ImGui::GetID("value"));
auto sortSpecs = ImGui::TableGetSortSpecs();
if (sortSpecs->SpecsDirty || this->m_sortedPatternData.empty()) {
this->m_sortedPatternData = this->m_patternData;
std::sort(this->m_sortedPatternData.begin(), this->m_sortedPatternData.end(), [this, &sortSpecs](PatternData* left, PatternData* right) -> bool {
if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("name")) {
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
return left->getName() > right->getName();
else
return left->getName() < right->getName();
}
else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("position")) {
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")) {
size_t biggerSize = std::max(left->getSize(), right->getSize());
std::vector<u8> leftBuffer(biggerSize, 0x00), rightBuffer(biggerSize, 0x00);
this->m_dataProvider->read(left->getOffset(), leftBuffer.data(), left->getSize());
this->m_dataProvider->read(right->getOffset(), rightBuffer.data(), right->getSize());
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
return leftBuffer > rightBuffer;
else
return leftBuffer < rightBuffer;
}
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();
}
return false;
});
sortSpecs->SpecsDirty = false;
}
ImGui::TableHeadersRow();
u32 rowCount = 0;
for (auto& patternData : this->m_sortedPatternData) {
ImGui::TableNextRow(ImGuiTableRowFlags_Headers);
ImGui::TableNextColumn();
ImGui::Text("%s", patternData->getName().c_str());
ImGui::TableNextColumn();
ImGui::Text("0x%08lx : 0x%08lx", patternData->getOffset(), patternData->getOffset() + patternData->getSize());
ImGui::TableNextColumn();
ImGui::Text("0x%08lx", patternData->getSize());
ImGui::TableNextColumn();
ImGui::Text("%s", patternData->getTypeName().c_str());
ImGui::TableNextColumn();
ImGui::Text("%s", patternData->format(this->m_dataProvider).c_str());
ImGui::TableSetBgColor(ImGuiTableBgTarget_RowBg0, ((rowCount % 2) == 0) ? 0xFF101010 : 0xFF303030);
rowCount++;
}
ImGui::EndTable();
}
2020-11-15 00:46:18 +01:00
2020-11-10 21:31:04 +01:00
}
ImGui::EndChild();
}
ImGui::End();
2020-11-10 21:31:04 +01:00
}
void ViewPatternData::createMenu() {
2020-11-11 09:22:55 +01:00
if (ImGui::BeginMenu("View")) {
2020-11-10 21:31:04 +01:00
ImGui::MenuItem("Data View", "", &this->m_windowOpen);
ImGui::EndMenu();
}
}
}