Skip to content

Commit

Permalink
feat: Highlight patterns in pattern data view that are fully selected
Browse files Browse the repository at this point in the history
Fixes #1741
  • Loading branch information
WerWolv committed Jun 7, 2024
1 parent c0dde57 commit ea09bfe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion plugins/builtin/romfs/themes/dark.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"advanced-encoding-multi": "#F1C40FFF",
"advanced-encoding-unknown": "#E74C3CFF",
"patches": "#E74C3CFF",
"pattern-selected": "#06539BFF"
"pattern-selected": "#3683CBFF"
},
"imnodes": {
"box-selector": "#3D85E01E",
Expand Down
24 changes: 18 additions & 6 deletions plugins/ui/source/ui/pattern_drawer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,46 @@ namespace hex::ui {

using namespace ::std::literals::string_literals;

bool isPatternSelected(u64 address, u64 size) {
bool isPatternOverlapSelected(u64 address, u64 size) {
auto currSelection = ImHexApi::HexEditor::getSelection();
if (!currSelection.has_value())
return false;

return Region{ address, size }.overlaps(*currSelection);
}

bool isPatternFullySelected(u64 address, u64 size) {
auto currSelection = ImHexApi::HexEditor::getSelection();
if (!currSelection.has_value())
return false;

return currSelection->address == address && currSelection->size == size;
}

template<typename T>
auto highlightWhenSelected(u64 address, u64 size, const T &callback) {
constexpr static bool HasReturn = !requires(T t) { { t() } -> std::same_as<void>; };

auto selected = isPatternSelected(address, size);
const auto overlapSelected = isPatternOverlapSelected(address, size);
const auto fullySelected = isPatternFullySelected(address, size);

if (selected)
ImGui::PushStyleColor(ImGuiCol_Text, ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_PatternSelected));
const u32 selectionColor = ImColor(ImGuiExt::GetCustomColorVec4(ImGuiCustomCol_PatternSelected));
if (overlapSelected)
ImGui::PushStyleColor(ImGuiCol_Text, selectionColor);
if (fullySelected)
ImGui::TableSetBgColor(ImGuiTableBgTarget_RowBg0, (selectionColor & 0x00'FF'FF'FF) | 0x30'00'00'00);

if constexpr (HasReturn) {
auto result = callback();

if (selected)
if (overlapSelected)
ImGui::PopStyleColor();

return result;
} else {
callback();

if (selected)
if (overlapSelected)
ImGui::PopStyleColor();
}
}
Expand Down

0 comments on commit ea09bfe

Please sign in to comment.