Skip to content

Commit

Permalink
feat: Highlight patterns in hex editor when hovering over pattern dat…
Browse files Browse the repository at this point in the history
…a row

Fixes #1742
  • Loading branch information
WerWolv committed Jun 7, 2024
1 parent 6fd3fa7 commit c0dde57
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace hex::plugin::builtin {
ui::PatternDrawer::TreeStyle m_treeStyle = ui::PatternDrawer::TreeStyle::Default;

PerProvider<std::unique_ptr<ui::PatternDrawer>> m_patternDrawer;
Region m_hoveredPatternRegion = Region::Invalid();
};

}
11 changes: 11 additions & 0 deletions plugins/builtin/source/content/views/view_pattern_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ namespace hex::plugin::builtin {
(*m_patternDrawer)->jumpToPattern(pattern);
});

ImHexApi::HexEditor::addHoverHighlightProvider([this](const prv::Provider *, u64 address, const u8 *, size_t size) {
return m_hoveredPatternRegion.overlaps(Region { address, size });
});

m_patternDrawer.setOnCreateCallback([this](const prv::Provider *provider, auto &drawer) {
drawer = std::make_unique<ui::PatternDrawer>();

Expand All @@ -45,6 +49,13 @@ namespace hex::plugin::builtin {
RequestPatternEditorSelectionChange::post(pattern->getLine(), 0);
});

drawer->setHoverCallback([this](const pl::ptrn::Pattern *pattern) {
if (pattern == nullptr)
m_hoveredPatternRegion = Region::Invalid();
else
m_hoveredPatternRegion = { pattern->getOffset(), pattern->getSize() };
});

drawer->setTreeStyle(m_treeStyle);
drawer->enableRowColoring(m_rowColoring);
drawer->enablePatternEditing(provider->isWritable());
Expand Down
2 changes: 2 additions & 0 deletions plugins/ui/include/ui/pattern_drawer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace hex::ui {

void setTreeStyle(TreeStyle style) { m_treeStyle = style; }
void setSelectionCallback(std::function<void(const pl::ptrn::Pattern *)> callback) { m_selectionCallback = std::move(callback); }
void setHoverCallback(std::function<void(const pl::ptrn::Pattern *)> callback) { m_hoverCallback = std::move(callback); }
void enableRowColoring(bool enabled) { m_rowColoring = enabled; }
void enablePatternEditing(bool enabled) { m_editingEnabled = enabled; }
void reset();
Expand Down Expand Up @@ -126,6 +127,7 @@ namespace hex::ui {
TaskHolder m_favoritesUpdateTask;

std::function<void(const pl::ptrn::Pattern *)> m_selectionCallback = [](const pl::ptrn::Pattern *) { };
std::function<void(const pl::ptrn::Pattern *)> m_hoverCallback = [](const pl::ptrn::Pattern *) { };

pl::gen::fmt::FormatterArray m_formatters;
};
Expand Down
14 changes: 10 additions & 4 deletions plugins/ui/source/ui/pattern_drawer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,14 @@ namespace hex::ui {
}
}

if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left) && m_editingEnabled) {
m_editingPattern = &pattern;
m_editingPatternOffset = pattern.getOffset();
AchievementManager::unlockAchievement("hex.builtin.achievement.patterns", "hex.builtin.achievement.patterns.modify_data.name");
if (ImGui::IsItemHovered()) {
m_hoverCallback(&pattern);

if (ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left) && m_editingEnabled) {
m_editingPattern = &pattern;
m_editingPatternOffset = pattern.getOffset();
AchievementManager::unlockAchievement("hex.builtin.achievement.patterns", "hex.builtin.achievement.patterns.modify_data.name");
}
}

ImGui::SameLine(0, 0);
Expand Down Expand Up @@ -1114,6 +1118,8 @@ namespace hex::ui {
void PatternDrawer::draw(const std::vector<std::shared_ptr<pl::ptrn::Pattern>> &patterns, const pl::PatternLanguage *runtime, float height) {
std::scoped_lock lock(s_resetDrawMutex);

m_hoverCallback(nullptr);

const auto treeStyleButton = [this](auto icon, TreeStyle style, const char *tooltip) {
bool pushed = false;

Expand Down

0 comments on commit c0dde57

Please sign in to comment.