From 3a9c3f939e6f0f4c4d2ca0e58749ff7a67758e82 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 9 Oct 2022 14:47:38 +0200 Subject: [PATCH] sys: Properly center hex view around selection when jumping --- .../include/content/views/view_hex_editor.hpp | 6 +++++- .../source/content/views/view_hex_editor.cpp | 13 +++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/plugins/builtin/include/content/views/view_hex_editor.hpp b/plugins/builtin/include/content/views/view_hex_editor.hpp index bb13d0bbb..588de8442 100644 --- a/plugins/builtin/include/content/views/view_hex_editor.hpp +++ b/plugins/builtin/include/content/views/view_hex_editor.hpp @@ -68,8 +68,11 @@ namespace hex::plugin::builtin { return data.selectionStart.has_value() && data.selectionEnd.has_value(); } - void jumpToSelection() { + void jumpToSelection(bool center = true) { this->m_shouldJumpToSelection = true; + + if (center) + this->m_centerOnJump = true; } void scrollToSelection() { @@ -118,6 +121,7 @@ namespace hex::plugin::builtin { ContentRegistry::HexEditor::DataVisualizer *m_currDataVisualizer; bool m_shouldJumpToSelection = false; + bool m_centerOnJump = false; bool m_shouldScrollToSelection = false; bool m_shouldJumpWhenOffScreen = false; bool m_shouldUpdateScrollPosition = false; diff --git a/plugins/builtin/source/content/views/view_hex_editor.cpp b/plugins/builtin/source/content/views/view_hex_editor.cpp index 4b325c72f..192133bcd 100644 --- a/plugins/builtin/source/content/views/view_hex_editor.cpp +++ b/plugins/builtin/source/content/views/view_hex_editor.cpp @@ -1043,9 +1043,9 @@ namespace hex::plugin::builtin { newSelection.address -= pageAddress; if ((newSelection.getStartAddress()) < u64(clipper.DisplayStart * this->m_bytesPerRow)) - this->jumpToSelection(); + this->jumpToSelection(false); if ((newSelection.getEndAddress()) > u64(clipper.DisplayEnd * this->m_bytesPerRow)) - this->jumpToSelection(); + this->jumpToSelection(false); } } @@ -1062,8 +1062,13 @@ namespace hex::plugin::builtin { const auto pageAddress = provider->getCurrentPageAddress() + provider->getBaseAddress(); auto scrollPos = (static_cast(newSelection.getStartAddress() - pageAddress) / this->m_bytesPerRow) * CharacterSize.y; bool scrollUpwards = scrollPos < ImGui::GetScrollY(); - const auto scrollFraction = scrollUpwards ? 0.0F : (1.0F - ((1.0F / this->m_visibleRowCount) * 2)); - + auto scrollFraction = scrollUpwards ? 0.0F : (1.0F - ((1.0F / this->m_visibleRowCount) * 2)); + + if (this->m_centerOnJump) { + scrollFraction = 0.5F; + this->m_centerOnJump = false; + } + ImGui::SetScrollFromPosY(ImGui::GetCursorStartPos().y + scrollPos, scrollFraction); }