sys: Finish implementing constants view and its store

This commit is contained in:
WerWolv
2021-09-09 12:58:44 +02:00
parent a1d9794c0e
commit ccac2e497d
8 changed files with 111 additions and 33 deletions

View File

@@ -2,6 +2,7 @@
#include <hex/helpers/paths.hpp>
#include <hex/helpers/logger.hpp>
#include <hex/helpers/utils.hpp>
#include <fstream>
#include <filesystem>
@@ -11,7 +12,9 @@ namespace hex {
ViewConstants::ViewConstants() : View("hex.view.constants.name") {
this->reloadConstants();
this->m_search.resize(0xFFF, 0x00);
this->m_filter.reserve(0xFFFF);
std::memset(this->m_filter.data(), 0x00, this->m_filter.capacity());
}
ViewConstants::~ViewConstants() {
@@ -20,6 +23,7 @@ namespace hex {
void ViewConstants::reloadConstants() {
this->m_constants.clear();
this->m_filterIndices.clear();
for (auto &path : hex::getPath(ImHexPath::Constants)) {
if (!std::filesystem::exists(path)) continue;
@@ -49,14 +53,13 @@ namespace hex {
else
throw std::runtime_error("Invalid type");
this->m_filterIndices.push_back(this->m_constants.size());
this->m_constants.push_back(constant);
}
} catch (...) {
log::info("Error");
log::error("Failed to parse constants file {}", file.path().string());
continue;
}
}
}
}
@@ -64,39 +67,82 @@ namespace hex {
void ViewConstants::drawContent() {
if (ImGui::Begin(View::toWindowName("hex.view.constants.name").c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) {
if (ImGui::InputText("##search", this->m_search.data(), this->m_search.size())) {
this->m_filteredConstants.clear();
for (auto &constant : this->m_constants) {
if (constant.value.starts_with(this->m_search.c_str()) || constant.name.starts_with(this->m_search.c_str()) || constant.description.starts_with(this->m_search.c_str()))
this->m_filteredConstants.push_back(&constant);
ImGui::InputText("##search", this->m_filter.data(), this->m_filter.capacity(), ImGuiInputTextFlags_CallbackEdit, [](ImGuiInputTextCallbackData *data) {
auto &view = *static_cast<ViewConstants*>(data->UserData);
view.m_filter.resize(data->BufTextLen);
view.m_filterIndices.clear();
for (u64 i = 0; i < view.m_constants.size(); i++) {
auto &constant = view.m_constants[i];
if (hex::containsIgnoreCase(constant.name, data->Buf) ||
hex::containsIgnoreCase(constant.category, data->Buf) ||
hex::containsIgnoreCase(constant.description, data->Buf) ||
hex::containsIgnoreCase(constant.value, data->Buf))
view.m_filterIndices.push_back(i);
}
}
return 0;
}, this);
if (ImGui::BeginTable("##strings", 4,
ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Sortable |
ImGuiTableFlags_Reorderable | ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollY)) {
ImGui::TableSetupScrollFreeze(0, 1);
ImGui::TableSetupColumn("hex.view.constants.category"_lang, 0, -1, ImGui::GetID("category"));
ImGui::TableSetupColumn("hex.view.constants.name"_lang, 0, -1, ImGui::GetID("name"));
ImGui::TableSetupColumn("hex.view.constants.desc"_lang, 0, -1, ImGui::GetID("desc"));
ImGui::TableSetupColumn("hex.view.constants.value"_lang, 0, -1, ImGui::GetID("value"));
ImGui::TableSetupColumn("hex.view.constants.row.category"_lang, 0, -1, ImGui::GetID("category"));
ImGui::TableSetupColumn("hex.view.constants.row.name"_lang, 0, -1, ImGui::GetID("name"));
ImGui::TableSetupColumn("hex.view.constants.row.desc"_lang, 0, -1, ImGui::GetID("desc"));
ImGui::TableSetupColumn("hex.view.constants.row.value"_lang, 0, -1, ImGui::GetID("value"));
auto sortSpecs = ImGui::TableGetSortSpecs();
if (sortSpecs->SpecsDirty) {
std::sort(this->m_constants.begin(), this->m_constants.end(),
[&sortSpecs](Constant &left, Constant &right) -> bool {
if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("category")) {
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
return left.category > right.category;
else
return left.category < right.category;
} else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("name")) {
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
return left.name > right.name;
else
return left.name < right.name;
} else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("desc")) {
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
return left.description > right.description;
else
return left.description < right.description;
} else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("value")) {
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
return left.value > right.value;
else
return left.value < right.value;
}
return false;
});
sortSpecs->SpecsDirty = false;
}
ImGui::TableHeadersRow();
ImGuiListClipper clipper;
clipper.Begin(this->m_filteredConstants.size());
clipper.Begin(this->m_filterIndices.size());
while (clipper.Step()) {
for (u64 i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) {
auto &constant = this->m_constants[this->m_filterIndices[i]];
ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::Text("%s", this->m_filteredConstants[i]->category.c_str());
ImGui::Text("%s", constant.category.c_str());
ImGui::TableNextColumn();
ImGui::Text("%s", this->m_filteredConstants[i]->name.c_str());
ImGui::Text("%s", constant.name.c_str());
ImGui::TableNextColumn();
ImGui::Text("%s", this->m_filteredConstants[i]->description.c_str());
ImGui::Text("%s", constant.description.c_str());
ImGui::TableNextColumn();
ImGui::Text("%s", this->m_filteredConstants[i]->value.c_str());
ImGui::Text("%s", constant.value.c_str());
}
}
clipper.End();