1
0
mirror of synced 2024-11-28 09:30:51 +01:00

patterns: Added [[hex::spec_name]]

This commit is contained in:
WerWolv 2023-07-23 09:14:00 +02:00
parent 591435761b
commit fdd2e1fcde
6 changed files with 65 additions and 84 deletions

View File

@ -191,5 +191,6 @@ namespace ImGui {
bool DimmedButton(const char* label);
bool DimmedIconButton(const char *symbol, ImVec4 color, ImVec2 size_arg = ImVec2(0, 0));
bool DimmedIconToggle(const char *icon, bool *v);
}

View File

@ -788,4 +788,24 @@ namespace ImGui {
return res;
}
bool DimmedIconToggle(const char *icon, bool *v) {
bool pushed = false;
bool toggled = false;
if (*v) {
ImGui::PushStyleColor(ImGuiCol_Border, ImGui::GetStyleColorVec4(ImGuiCol_ButtonActive));
pushed = true;
}
if (ImGui::DimmedIconButton(icon, ImGui::GetStyleColorVec4(ImGuiCol_Text))) {
*v = !*v;
toggled = true;
}
if (pushed)
ImGui::PopStyleColor();
return toggled;
}
}

View File

@ -10,6 +10,8 @@
#include <hex/providers/provider.hpp>
struct ImGuiTableSortSpecs;
namespace hex::plugin::builtin::ui {
class PatternDrawer : public pl::PatternVisitor {
@ -65,14 +67,17 @@ namespace hex::plugin::builtin::ui {
void drawVisualizer(const std::map<std::string, ContentRegistry::PatternLanguage::impl::Visualizer> &visualizers, const std::vector<pl::core::Token::Literal> &arguments, pl::ptrn::Pattern &pattern, pl::ptrn::IIterable &iterable, bool reset);
void drawFavoriteColumn(const pl::ptrn::Pattern& pattern);
bool beginPatternTable(const std::vector<std::shared_ptr<pl::ptrn::Pattern>> &patterns, std::vector<pl::ptrn::Pattern*> &sortedPatterns, float height);
bool createTreeNode(const pl::ptrn::Pattern& pattern, bool leaf = false);
void createDefaultEntry(pl::ptrn::Pattern &pattern);
void closeTreeNode(bool inlined);
bool sortPatterns(const ImGuiTableSortSpecs* sortSpecs, const pl::ptrn::Pattern * left, const pl::ptrn::Pattern * right) const;
bool isEditingPattern(const pl::ptrn::Pattern& pattern) const;
void resetEditing();
bool matchesFilter(const std::vector<std::string> &filterPath, const std::vector<std::string> &patternPath, bool fullMatch);
void traversePatternTree(pl::ptrn::Pattern &pattern, std::vector<std::string> &patternPath, const std::function<void(pl::ptrn::Pattern&)> &callback);
std::string getDisplayName(const pl::ptrn::Pattern& pattern) const;
private:
std::map<const pl::ptrn::Pattern*, u64> m_displayEnd;
@ -93,6 +98,8 @@ namespace hex::plugin::builtin::ui {
std::map<std::vector<std::string>, std::unique_ptr<pl::ptrn::Pattern>> m_favorites;
bool m_showFavoriteStars = false;
bool m_favoritesUpdated = false;
bool m_showSpecName = false;
TaskHolder m_favoritesUpdateTask;
std::function<void(Region)> m_selectionCallback = [](Region) { };

View File

@ -374,6 +374,7 @@
"hex.builtin.pattern_drawer.favorites": "Favorites",
"hex.builtin.pattern_drawer.local": "Local",
"hex.builtin.pattern_drawer.size": "Size",
"hex.builtin.pattern_drawer.spec_name": "Display specification names",
"hex.builtin.pattern_drawer.start": "Start",
"hex.builtin.pattern_drawer.tree_style.tree": "Tree",
"hex.builtin.pattern_drawer.tree_style.auto_expanded": "Auto Expanded Tree",

View File

@ -725,89 +725,29 @@ namespace hex::plugin::builtin::ui {
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + 2_scaled);
// Upper/lower case hex
{
bool pushed = false;
if (this->m_upperCaseHex) {
ImGui::PushStyleColor(ImGuiCol_Border, ImGui::GetStyleColorVec4(ImGuiCol_ButtonActive));
pushed = true;
}
if (ImGui::DimmedIconButton(ICON_VS_CASE_SENSITIVE, ImGui::GetStyleColorVec4(ImGuiCol_Text))) {
this->m_upperCaseHex = !this->m_upperCaseHex;
}
if (pushed)
ImGui::PopStyleColor();
ImGui::DimmedIconToggle(ICON_VS_CASE_SENSITIVE, &this->m_upperCaseHex);
ImGui::InfoTooltip("hex.builtin.hex_editor.uppercase_hex"_lang);
}
ImGui::SameLine();
// Grayed out zeros
{
bool pushed = false;
if (this->m_grayOutZero) {
ImGui::PushStyleColor(ImGuiCol_Border, ImGui::GetStyleColorVec4(ImGuiCol_ButtonActive));
pushed = true;
}
if (ImGui::DimmedIconButton(ICON_VS_LIGHTBULB, ImGui::GetStyleColorVec4(ImGuiCol_Text))) {
this->m_grayOutZero = !this->m_grayOutZero;
EventManager::post<EventHighlightingChanged>();
}
if (pushed)
ImGui::PopStyleColor();
ImGui::DimmedIconToggle(ICON_VS_LIGHTBULB, &this->m_grayOutZero);
ImGui::InfoTooltip("hex.builtin.hex_editor.gray_out_zero"_lang);
}
ImGui::SameLine();
// ASCII view
{
bool pushed = false;
if (this->m_showAscii) {
ImGui::PushStyleColor(ImGuiCol_Border, ImGui::GetStyleColorVec4(ImGuiCol_ButtonActive));
pushed = true;
}
if (ImGui::DimmedIconButton(ICON_VS_SYMBOL_KEY, ImGui::GetStyleColorVec4(ImGuiCol_Text))) {
this->m_showAscii = !this->m_showAscii;
}
if (pushed)
ImGui::PopStyleColor();
ImGui::DimmedIconToggle(ICON_VS_SYMBOL_KEY, &this->m_showAscii);
ImGui::InfoTooltip("hex.builtin.hex_editor.ascii_view"_lang);
}
ImGui::SameLine(0, 1_scaled);
// Custom encoding view
{
bool pushed = false;
if (this->m_showCustomEncoding) {
ImGui::PushStyleColor(ImGuiCol_Border, ImGui::GetStyleColorVec4(ImGuiCol_ButtonActive));
pushed = true;
}
ImGui::BeginDisabled(!this->m_currCustomEncoding.has_value());
if (ImGui::DimmedIconButton(ICON_VS_WHITESPACE, ImGui::GetStyleColorVec4(ImGuiCol_Text))) {
this->m_showCustomEncoding = !this->m_showCustomEncoding;
}
ImGui::DimmedIconToggle(ICON_VS_WHITESPACE, &this->m_showCustomEncoding);
ImGui::EndDisabled();
if (pushed)
ImGui::PopStyleColor();
ImGui::InfoTooltip("hex.builtin.hex_editor.custom_encoding_view"_lang);
}
ImGui::TableNextColumn();

View File

@ -285,12 +285,19 @@ namespace hex::plugin::builtin::ui {
}
}
std::string PatternDrawer::getDisplayName(const pl::ptrn::Pattern& pattern) const {
if (this->m_showSpecName && pattern.hasAttribute("hex::spec_name"))
return pattern.getAttributeArguments("hex::spec_name")[0].toString(true);
else
return pattern.getDisplayName();
}
bool PatternDrawer::createTreeNode(const pl::ptrn::Pattern& pattern, bool leaf) {
drawFavoriteColumn(pattern);
if (pattern.isSealed() || leaf) {
ImGui::Indent();
highlightWhenSelected(pattern, [&]{ ImGui::TextUnformatted(pattern.getDisplayName().c_str()); });
highlightWhenSelected(pattern, [&]{ ImGui::TextUnformatted(this->getDisplayName(pattern).c_str()); });
ImGui::Unindent();
return false;
}
@ -300,11 +307,11 @@ namespace hex::plugin::builtin::ui {
using enum TreeStyle;
default:
case Default:
return ImGui::TreeNodeEx(pattern.getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth);
return ImGui::TreeNodeEx(this->getDisplayName(pattern).c_str(), ImGuiTreeNodeFlags_SpanFullWidth);
case AutoExpanded:
return ImGui::TreeNodeEx(pattern.getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_DefaultOpen);
return ImGui::TreeNodeEx(this->getDisplayName(pattern).c_str(), ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_DefaultOpen);
case Flattened:
return ImGui::TreeNodeEx(pattern.getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen);
return ImGui::TreeNodeEx(this->getDisplayName(pattern).c_str(), ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen);
}
});
}
@ -847,7 +854,7 @@ namespace hex::plugin::builtin::ui {
ImGui::TableNextColumn();
chunkOpen = highlightWhenSelected(startOffset, ((endOffset + endSize) - startOffset) - 1, [&]{
return ImGui::TreeNodeEx(hex::format("{0}[{1} ... {2}]", this->m_treeStyle == TreeStyle::Flattened ? pattern.getDisplayName().c_str() : "", i, endIndex - 1).c_str(), ImGuiTreeNodeFlags_SpanFullWidth);
return ImGui::TreeNodeEx(hex::format("{0}[{1} ... {2}]", this->m_treeStyle == TreeStyle::Flattened ? this->getDisplayName(pattern).c_str() : "", i, endIndex - 1).c_str(), ImGuiTreeNodeFlags_SpanFullWidth);
});
ImGui::TableNextColumn();
@ -900,12 +907,12 @@ namespace hex::plugin::builtin::ui {
return value->second;
}
static bool sortPatterns(const ImGuiTableSortSpecs* sortSpecs, const pl::ptrn::Pattern * left, const pl::ptrn::Pattern * right) {
bool PatternDrawer::sortPatterns(const ImGuiTableSortSpecs* sortSpecs, const pl::ptrn::Pattern * left, const pl::ptrn::Pattern * right) const {
if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("name")) {
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
return left->getDisplayName() < right->getDisplayName();
return this->getDisplayName(*left) < this->getDisplayName(*right);
else
return left->getDisplayName() > right->getDisplayName();
return this->getDisplayName(*left) > this->getDisplayName(*right);
} else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("start")) {
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
return left->getOffsetForSorting() < right->getOffsetForSorting();
@ -941,7 +948,7 @@ namespace hex::plugin::builtin::ui {
return false;
}
static bool beginPatternTable(const std::vector<std::shared_ptr<pl::ptrn::Pattern>> &patterns, std::vector<pl::ptrn::Pattern*> &sortedPatterns, float height) {
bool PatternDrawer::beginPatternTable(const std::vector<std::shared_ptr<pl::ptrn::Pattern>> &patterns, std::vector<pl::ptrn::Pattern*> &sortedPatterns, float height) {
if (ImGui::BeginTable("##Patterntable", 8, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Sortable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollY, ImVec2(0, height))) {
ImGui::TableSetupScrollFreeze(0, 1);
ImGui::TableSetupColumn("##favorite", ImGuiTableColumnFlags_NoSort | ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_NoResize | ImGuiTableColumnFlags_NoReorder | ImGuiTableColumnFlags_NoHide | ImGuiTableColumnFlags_IndentDisable, ImGui::GetTextLineHeight(), ImGui::GetID("favorite"));
@ -964,13 +971,13 @@ namespace hex::plugin::builtin::ui {
return pattern.get();
});
std::sort(sortedPatterns.begin(), sortedPatterns.end(), [&sortSpecs](pl::ptrn::Pattern *left, pl::ptrn::Pattern *right) -> bool {
return sortPatterns(sortSpecs, left, right);
std::sort(sortedPatterns.begin(), sortedPatterns.end(), [this, &sortSpecs](pl::ptrn::Pattern *left, pl::ptrn::Pattern *right) -> bool {
return this->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);
pattern->sort([this, &sortSpecs](const pl::ptrn::Pattern *left, const pl::ptrn::Pattern *right){
return this->sortPatterns(sortSpecs, left, right);
});
sortSpecs->SpecsDirty = false;
@ -1016,7 +1023,7 @@ namespace hex::plugin::builtin::ui {
this->resetEditing();
}
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x - ImGui::GetTextLineHeightWithSpacing() * 7.5);
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x - ImGui::GetTextLineHeightWithSpacing() * 9.5);
if (ImGui::InputTextIcon("##Search", ICON_VS_FILTER, this->m_filterText)) {
this->m_filter = parseRValueFilter(this->m_filterText);
}
@ -1024,6 +1031,11 @@ namespace hex::plugin::builtin::ui {
ImGui::SameLine();
ImGui::DimmedIconToggle(ICON_VS_BOOK, &this->m_showSpecName);
ImGui::InfoTooltip("hex.builtin.pattern_drawer.spec_name"_lang);
ImGui::SameLine();
treeStyleButton(ICON_VS_SYMBOL_KEYWORD, TreeStyle::Default, "hex.builtin.pattern_drawer.tree_style.tree"_lang);
ImGui::SameLine(0, 0);
treeStyleButton(ICON_VS_LIST_TREE, TreeStyle::AutoExpanded, "hex.builtin.pattern_drawer.tree_style.auto_expanded"_lang);