1
0
mirror of synced 2024-11-28 17:40:51 +01:00

ui: Improve visualizer button layout and error messages

This commit is contained in:
WerWolv 2023-01-25 10:16:31 +01:00
parent 434b7649c3
commit f9bb4d828a
3 changed files with 42 additions and 23 deletions

View File

@ -50,7 +50,9 @@ namespace hex::plugin::builtin::ui {
void drawArray(pl::ptrn::Pattern& pattern, pl::ptrn::Iteratable &iteratable, bool isInlined);
u64& getDisplayEnd(const pl::ptrn::Pattern& pattern);
void makeSelectable(const pl::ptrn::Pattern &pattern);
void drawVisualizerButton(pl::ptrn::Pattern& pattern, pl::ptrn::Iteratable &iteratable);
void drawVisualizer(const std::vector<pl::core::Token::Literal> &arguments, pl::ptrn::Pattern &pattern, pl::ptrn::Iteratable &iteratable, bool reset);
void createLeafNode(const pl::ptrn::Pattern& pattern);
bool createTreeNode(const pl::ptrn::Pattern& pattern);
@ -67,5 +69,6 @@ namespace hex::plugin::builtin::ui {
pl::ptrn::Pattern *m_currVisualizedPattern = nullptr;
std::set<pl::ptrn::Pattern*> m_visualizedPatterns;
std::string m_lastVisualizerError;
};
}

View File

@ -312,7 +312,8 @@
"hex.builtin.pattern_drawer.type": "Type",
"hex.builtin.pattern_drawer.value": "Value",
"hex.builtin.pattern_drawer.var_name": "Name",
"hex.builtin.pattern_drawer.unknown_visualizer": "Unknown visualizer",
"hex.builtin.pattern_drawer.visualizer.unknown": "Unknown visualizer",
"hex.builtin.pattern_drawer.visualizer.invalid_parameter_count": "Invalid parameter count",
"hex.builtin.popup.close_provider.desc": "You have unsaved changes made to this Provider.\nAre you sure you want to close it?",
"hex.builtin.popup.close_provider.title": "Close Provider?",
"hex.builtin.popup.error.create": "Failed to create new file!",

View File

@ -28,6 +28,7 @@
#include <imgui.h>
#include <implot.h>
#include <hex/ui/imgui_imhex_extensions.h>
#include <fonts/codicons_font.h>
namespace hex::plugin::builtin::ui {
@ -113,40 +114,46 @@ namespace hex::plugin::builtin::ui {
}
}
void drawVisualizer(const std::vector<pl::core::Token::Literal> &arguments, pl::ptrn::Pattern &pattern, pl::ptrn::Iteratable &iteratable, bool reset) {
auto visualizerName = arguments.front().toString(true);
}
const auto &visualizers = ContentRegistry::PatternLanguage::impl::getVisualizers();
void PatternDrawer::drawVisualizer(const std::vector<pl::core::Token::Literal> &arguments, pl::ptrn::Pattern &pattern, pl::ptrn::Iteratable &iteratable, bool reset) {
auto visualizerName = arguments.front().toString(true);
if (auto entry = visualizers.find(visualizerName); entry != visualizers.end()) {
const auto &[name, visualizer] = *entry;
if (visualizer.parameterCount != arguments.size() - 1) {
ImGui::TextUnformatted("hex.builtin.pattern_drawer.visualizer.invalid_parameter_count"_lang);
} else {
try {
visualizer.callback(pattern, iteratable, reset, arguments);
} catch (std::exception &e) {
ImGui::TextUnformatted(e.what());
}
const auto &visualizers = ContentRegistry::PatternLanguage::impl::getVisualizers();
}
if (auto entry = visualizers.find(visualizerName); entry != visualizers.end()) {
const auto &[name, visualizer] = *entry;
if (visualizer.parameterCount != arguments.size() - 1) {
ImGui::TextUnformatted("hex.builtin.pattern_drawer.visualizer.invalid_parameter_count"_lang);
} else {
ImGui::TextUnformatted("hex.builtin.pattern_drawer.visualizer.unknown"_lang);
try {
visualizer.callback(pattern, iteratable, reset, arguments);
} catch (std::exception &e) {
this->m_lastVisualizerError = e.what();
}
}
} else {
ImGui::TextUnformatted("hex.builtin.pattern_drawer.visualizer.unknown"_lang);
}
if (!this->m_lastVisualizerError.empty())
ImGui::TextUnformatted(this->m_lastVisualizerError.c_str());
}
void PatternDrawer::drawVisualizerButton(pl::ptrn::Pattern& pattern, pl::ptrn::Iteratable &iteratable) {
if (const auto &arguments = pattern.getAttributeArguments("hex::visualize"); !arguments.empty()) {
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
if (ImGui::Button(pattern.getFormattedValue().c_str(), ImVec2(-1, ImGui::GetTextLineHeight()))) {
if (ImGui::IconButton(ICON_VS_EYE, ImGui::GetStyleColorVec4(ImGuiCol_Text), ImVec2(ImGui::GetTextLineHeightWithSpacing(), ImGui::GetTextLineHeight()))) {
this->m_currVisualizedPattern = &pattern;
this->m_lastVisualizerError.clear();
ImGui::OpenPopup("Visualizer");
}
ImGui::PopStyleVar();
ImGui::SameLine();
if (ImGui::BeginPopup("Visualizer")) {
if (this->m_currVisualizedPattern == &pattern) {
drawVisualizer(arguments, pattern, iteratable, !this->m_visualizedPatterns.contains(&pattern));
@ -155,11 +162,10 @@ namespace hex::plugin::builtin::ui {
ImGui::EndPopup();
}
} else {
ImGui::TextFormatted("{}", pattern.getFormattedValue());
}
}
void PatternDrawer::createLeafNode(const pl::ptrn::Pattern& pattern) {
ImGui::TreeNodeEx(pattern.getDisplayName().c_str(), ImGuiTreeNodeFlags_Leaf |
ImGuiTreeNodeFlags_NoTreePushOnOpen |
@ -272,6 +278,8 @@ namespace hex::plugin::builtin::ui {
ImGui::TableNextRow();
ImGui::TableNextColumn();
open = createTreeNode(pattern);
ImGui::SameLine();
drawVisualizerButton(pattern, static_cast<pl::ptrn::Iteratable&>(pattern));
ImGui::TableNextColumn();
makeSelectable(pattern);
drawCommentTooltip(pattern);
@ -488,6 +496,8 @@ namespace hex::plugin::builtin::ui {
ImGui::TableNextRow();
ImGui::TableNextColumn();
open = createTreeNode(pattern);
ImGui::SameLine();
drawVisualizerButton(pattern, static_cast<pl::ptrn::Iteratable&>(pattern));
ImGui::TableNextColumn();
makeSelectable(pattern);
drawCommentTooltip(pattern);
@ -514,7 +524,7 @@ namespace hex::plugin::builtin::ui {
ImGui::PopStyleVar();
}
} else {
drawVisualizerButton(pattern, static_cast<pl::ptrn::Iteratable&>(pattern));
ImGui::TextFormatted("{}", pattern.getFormattedValue());
}
}
@ -539,6 +549,8 @@ namespace hex::plugin::builtin::ui {
ImGui::TableNextRow();
ImGui::TableNextColumn();
open = createTreeNode(pattern);
ImGui::SameLine();
drawVisualizerButton(pattern, static_cast<pl::ptrn::Iteratable&>(pattern));
ImGui::TableNextColumn();
makeSelectable(pattern);
drawCommentTooltip(pattern);
@ -565,7 +577,7 @@ namespace hex::plugin::builtin::ui {
ImGui::PopStyleVar();
}
} else {
drawVisualizerButton(pattern, static_cast<pl::ptrn::Iteratable&>(pattern));
ImGui::TextFormatted("{}", pattern.getFormattedValue());
}
}
@ -633,6 +645,8 @@ namespace hex::plugin::builtin::ui {
ImGui::TableNextRow();
ImGui::TableNextColumn();
open = createTreeNode(pattern);
ImGui::SameLine();
drawVisualizerButton(pattern, iteratable);
ImGui::TableNextColumn();
makeSelectable(pattern);
drawCommentTooltip(pattern);
@ -654,7 +668,7 @@ namespace hex::plugin::builtin::ui {
ImGui::TableNextColumn();
drawVisualizerButton(pattern, iteratable);
ImGui::TextFormatted("{}", pattern.getFormattedValue());
}
if (open) {
@ -840,5 +854,6 @@ namespace hex::plugin::builtin::ui {
this->m_visualizedPatterns.clear();
this->m_currVisualizedPattern = nullptr;
this->m_sortedPatterns.clear();
this->m_lastVisualizerError.clear();
}
}