From 015266181eead859d7885b411f7adf4280f00286 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 12 Oct 2022 10:56:03 +0200 Subject: [PATCH] fix: Various issue with the hex editor editing mode --- lib/libimhex/source/api/content_registry.cpp | 2 +- .../include/content/views/view_hex_editor.hpp | 6 +- .../source/content/views/view_hex_editor.cpp | 86 +++++++++---------- 3 files changed, 47 insertions(+), 47 deletions(-) diff --git a/lib/libimhex/source/api/content_registry.cpp b/lib/libimhex/source/api/content_registry.cpp index 285566828..59174fbe5 100644 --- a/lib/libimhex/source/api/content_registry.cpp +++ b/lib/libimhex/source/api/content_registry.cpp @@ -586,7 +586,7 @@ namespace hex { }, &userData); ImGui::PopID(); - return userData.editingDone || ImGui::IsKeyPressed(ImGuiKey_Enter) || ImGui::IsKeyPressed(ImGuiKey_Enter); + return userData.editingDone || ImGui::IsKeyPressed(ImGuiKey_Enter) || ImGui::IsKeyPressed(ImGuiKey_Escape); } void impl::addDataVisualizer(const std::string &unlocalizedName, DataVisualizer *visualizer) { diff --git a/plugins/builtin/include/content/views/view_hex_editor.hpp b/plugins/builtin/include/content/views/view_hex_editor.hpp index c4c20315d..1ff33173e 100644 --- a/plugins/builtin/include/content/views/view_hex_editor.hpp +++ b/plugins/builtin/include/content/views/view_hex_editor.hpp @@ -28,7 +28,7 @@ namespace hex::plugin::builtin { void drawCell(u64 address, u8 *data, size_t size, bool hovered, CellType cellType); void drawPopup(); - void drawSelectionFrame(u32 x, u32 y, u64 byteAddress, u16 bytesPerCell, const ImVec2 &cellPos, const ImVec2 &cellSize); + void drawSelectionFrame(u32 x, u32 y, u64 byteAddress, u16 bytesPerCell, const ImVec2 &cellPos, const ImVec2 &cellSize) const; public: void setSelection(const Region ®ion) { this->setSelection(region.getStartAddress(), region.getEndAddress()); } @@ -51,7 +51,7 @@ namespace hex::plugin::builtin { } } - [[nodiscard]] Region getSelection() const { + [[nodiscard]] static Region getSelection() { auto &data = ProviderExtraData::getCurrent().editor; if (!isSelectionValid()) @@ -64,7 +64,7 @@ namespace hex::plugin::builtin { return { start, size }; } - [[nodiscard]] bool isSelectionValid() const { + [[nodiscard]] static bool isSelectionValid() { auto &data = ProviderExtraData::getCurrent().editor; return data.selectionStart.has_value() && data.selectionEnd.has_value(); diff --git a/plugins/builtin/source/content/views/view_hex_editor.cpp b/plugins/builtin/source/content/views/view_hex_editor.cpp index b4ffbc7d6..37b739d4b 100644 --- a/plugins/builtin/source/content/views/view_hex_editor.cpp +++ b/plugins/builtin/source/content/views/view_hex_editor.cpp @@ -27,7 +27,7 @@ namespace hex::plugin::builtin { ImGui::EndTabItem(); } - ImGui::BeginDisabled(!editor->isSelectionValid()); + ImGui::BeginDisabled(!ViewHexEditor::isSelectionValid()); if (ImGui::BeginTabItem("hex.builtin.view.hex_editor.goto.offset.relative"_lang)) { this->m_mode = Mode::Relative; ImGui::EndTabItem(); @@ -59,7 +59,7 @@ namespace hex::plugin::builtin { } break; case Mode::Relative: { - const auto selection = editor->getSelection(); + const auto selection = ViewHexEditor::getSelection(); newAddress = selection.getStartAddress() + inputResult; } break; @@ -208,7 +208,7 @@ namespace hex::plugin::builtin { auto region = this->findSequence(searchSequence, this->m_backwards); if (region.has_value()) { - if (editor->getSelection() == region) { + if (ViewHexEditor::getSelection() == region) { if (this->m_nextSearchPosition.has_value()) this->m_searchPosition = this->m_nextSearchPosition.value(); this->m_nextSearchPosition.reset(); @@ -499,16 +499,16 @@ namespace hex::plugin::builtin { ImGui::InputText("##editing_input", buffer, 2, TextInputFlags | ImGuiInputTextFlags_CallbackEdit, [](ImGuiInputTextCallbackData *data) -> int { auto &userData = *reinterpret_cast(data->UserData); - if (data->BufTextLen >= userData.maxChars) + if (data->BufTextLen >= userData.maxChars) { userData.editingDone = true; + userData.data[0] = data->Buf[0]; + } return 0; }, &userData); ImGui::PopID(); - data[0] = buffer[0]; - - return userData.editingDone || ImGui::IsKeyPressed(ImGuiKey_Enter) || ImGui::IsKeyPressed(ImGuiKey_Enter); + return userData.editingDone || ImGui::IsKeyPressed(ImGuiKey_Enter) || ImGui::IsKeyPressed(ImGuiKey_Escape); } else return false; @@ -587,8 +587,8 @@ namespace hex::plugin::builtin { } std::optional ViewHexEditor::applySelectionColor(u64 byteAddress, std::optional color) { - if (this->isSelectionValid()) { - auto selection = this->getSelection(); + if (isSelectionValid()) { + auto selection = getSelection(); if (byteAddress >= selection.getStartAddress() && byteAddress <= selection.getEndAddress()) { if (color.has_value()) @@ -749,10 +749,10 @@ namespace hex::plugin::builtin { } } - void ViewHexEditor::drawSelectionFrame(u32 x, u32 y, u64 byteAddress, u16 bytesPerCell, const ImVec2 &cellPos, const ImVec2 &cellSize) { - if (!this->isSelectionValid()) return; + void ViewHexEditor::drawSelectionFrame(u32 x, u32 y, u64 byteAddress, u16 bytesPerCell, const ImVec2 &cellPos, const ImVec2 &cellSize) const { + if (!isSelectionValid()) return; - const auto selection = this->getSelection(); + const auto selection = getSelection(); if (!Region { byteAddress, 1 }.isWithin(selection)) return; @@ -941,7 +941,7 @@ namespace hex::plugin::builtin { if (isColumnSeparatorColumn(x + 1, columnCount)) { auto separatorAddress = x + y * columnCount; auto [nextForegroundColor, nextBackgroundColor] = cellColors[x + 1]; - if ((this->isSelectionValid() && this->getSelection().overlaps({ separatorAddress, 1 }) && this->getSelection().getEndAddress() != separatorAddress) || backgroundColor == nextBackgroundColor) + if ((isSelectionValid() && getSelection().overlaps({ separatorAddress, 1 }) && getSelection().getEndAddress() != separatorAddress) || backgroundColor == nextBackgroundColor) cellSize.x += SeparatorColumWidth + 1; } @@ -1102,7 +1102,7 @@ namespace hex::plugin::builtin { } // Scroll to the cursor if it's either at the top or bottom edge of the screen - if (this->m_shouldScrollToSelection && this->isSelectionValid()) { + if (this->m_shouldScrollToSelection && isSelectionValid()) { // Make sure simply clicking on a byte at the edge of the screen won't cause scrolling if ((ImGui::IsMouseDown(ImGuiMouseButton_Left) && providerData.selectionStart != providerData.selectionEnd)) { auto fractionPerLine = 1.0 / (this->m_visibleRowCount + 1); @@ -1126,7 +1126,7 @@ namespace hex::plugin::builtin { this->m_shouldJumpWhenOffScreen = false; const auto pageAddress = provider->getCurrentPageAddress() + provider->getBaseAddress(); - auto newSelection = this->getSelection(); + auto newSelection = getSelection(); newSelection.address -= pageAddress; if ((newSelection.getStartAddress()) < u64(clipper.DisplayStart * this->m_bytesPerRow)) @@ -1143,7 +1143,7 @@ namespace hex::plugin::builtin { if (this->m_shouldJumpToSelection) { this->m_shouldJumpToSelection = false; - auto newSelection = this->getSelection(); + auto newSelection = getSelection(); provider->setCurrentPage(provider->getPageOfAddress(newSelection.address).value_or(0)); const auto pageAddress = provider->getCurrentPageAddress() + provider->getBaseAddress(); @@ -1220,9 +1220,9 @@ namespace hex::plugin::builtin { // Selection ImGui::TableNextColumn(); { - auto selection = this->getSelection(); + auto selection = getSelection(); std::string value; - if (this->isSelectionValid()) { + if (isSelectionValid()) { value = hex::format("0x{0:08X} - 0x{1:08X} (0x{2:X} | {3})", selection.getStartAddress(), selection.getEndAddress(), @@ -1380,17 +1380,17 @@ namespace hex::plugin::builtin { }); // Remove selection - ShortcutManager::addShortcut(this, Keys::Escape, [this] { + ShortcutManager::addShortcut(this, Keys::Escape, [] { auto &data = ProviderExtraData::getCurrent().editor; data.selectionStart.reset(); data.selectionEnd.reset(); - EventManager::post(this->getSelection()); + EventManager::post(getSelection()); }); // Move cursor around ShortcutManager::addShortcut(this, Keys::Up, [this] { - auto selection = this->getSelection(); + auto selection = getSelection(); if (selection.getEndAddress() >= this->m_bytesPerRow) { auto pos = selection.getEndAddress() - this->m_bytesPerRow; @@ -1400,7 +1400,7 @@ namespace hex::plugin::builtin { } }); ShortcutManager::addShortcut(this, Keys::Down, [this] { - auto selection = this->getSelection(); + auto selection = getSelection(); auto pos = selection.getEndAddress() + this->m_bytesPerRow; this->setSelection(pos, pos); @@ -1408,7 +1408,7 @@ namespace hex::plugin::builtin { this->jumpIfOffScreen(); }); ShortcutManager::addShortcut(this, Keys::Left, [this] { - auto selection = this->getSelection(); + auto selection = getSelection(); if (selection.getEndAddress() > 0) { auto pos = selection.getEndAddress() - 1; @@ -1418,7 +1418,7 @@ namespace hex::plugin::builtin { } }); ShortcutManager::addShortcut(this, Keys::Right, [this] { - auto selection = this->getSelection(); + auto selection = getSelection(); auto pos = selection.getEndAddress() + 1; this->setSelection(pos, pos); @@ -1427,7 +1427,7 @@ namespace hex::plugin::builtin { }); ShortcutManager::addShortcut(this, Keys::PageUp, [this] { - auto selection = this->getSelection(); + auto selection = getSelection(); u64 visibleByteCount = this->m_bytesPerRow * this->m_visibleRowCount; if (selection.getEndAddress() >= visibleByteCount) { @@ -1438,7 +1438,7 @@ namespace hex::plugin::builtin { } }); ShortcutManager::addShortcut(this, Keys::PageDown, [this] { - auto selection = this->getSelection(); + auto selection = getSelection(); auto pos = selection.getEndAddress() + (this->m_bytesPerRow * this->m_visibleRowCount); this->setSelection(pos, pos); @@ -1448,14 +1448,14 @@ namespace hex::plugin::builtin { // Move selection around ShortcutManager::addShortcut(this, SHIFT + Keys::Up, [this] { - auto selection = this->getSelection(); + auto selection = getSelection(); this->setSelection(std::max(selection.getStartAddress(), this->m_bytesPerRow) - this->m_bytesPerRow, selection.getEndAddress()); this->scrollToSelection(); this->jumpIfOffScreen(); }); ShortcutManager::addShortcut(this, SHIFT + Keys::Down, [this] { - auto selection = this->getSelection(); + auto selection = getSelection(); this->setSelection(selection.getStartAddress() + this->m_bytesPerRow, selection.getEndAddress()); this->scrollToSelection(); @@ -1463,21 +1463,21 @@ namespace hex::plugin::builtin { }); ShortcutManager::addShortcut(this, SHIFT + Keys::Left, [this] { - auto selection = this->getSelection(); + auto selection = getSelection(); this->setSelection(std::max(selection.getStartAddress(), 1) - 1, selection.getEndAddress()); this->scrollToSelection(); this->jumpIfOffScreen(); }); ShortcutManager::addShortcut(this, SHIFT + Keys::Right, [this] { - auto selection = this->getSelection(); + auto selection = getSelection(); this->setSelection(selection.getStartAddress() + 1, selection.getEndAddress()); this->scrollToSelection(); this->jumpIfOffScreen(); }); ShortcutManager::addShortcut(this, Keys::PageUp, [this] { - auto selection = this->getSelection(); + auto selection = getSelection(); u64 visibleByteCount = this->m_bytesPerRow * this->m_visibleRowCount; if (selection.getEndAddress() >= visibleByteCount) { @@ -1488,7 +1488,7 @@ namespace hex::plugin::builtin { } }); ShortcutManager::addShortcut(this, Keys::PageDown, [this] { - auto selection = this->getSelection(); + auto selection = getSelection(); auto pos = selection.getEndAddress() + (this->m_bytesPerRow * this->m_visibleRowCount); this->setSelection(pos, selection.getEndAddress()); @@ -1508,18 +1508,18 @@ namespace hex::plugin::builtin { }); // Copy - ShortcutManager::addShortcut(this, CTRL + Keys::C, [this] { - const auto selection = this->getSelection(); + ShortcutManager::addShortcut(this, CTRL + Keys::C, [] { + const auto selection = getSelection(); copyBytes(selection); }); - ShortcutManager::addShortcut(this, CTRL + SHIFT + Keys::C, [this] { - const auto selection = this->getSelection(); + ShortcutManager::addShortcut(this, CTRL + SHIFT + Keys::C, [] { + const auto selection = getSelection(); copyString(selection); }); // Paste - ShortcutManager::addShortcut(this, CTRL + Keys::V, [this] { - const auto selection = this->getSelection(); + ShortcutManager::addShortcut(this, CTRL + Keys::V, [] { + const auto selection = getSelection(); pasteBytes(selection); }); @@ -1562,16 +1562,16 @@ namespace hex::plugin::builtin { } }); - EventManager::subscribe(this, [this](auto ®ion) { - if (this->isSelectionValid()) - region = this->getSelection(); + EventManager::subscribe(this, [](auto ®ion) { + if (isSelectionValid()) + region = getSelection(); }); EventManager::subscribe(this, [this](auto, auto) { this->m_shouldUpdateScrollPosition = true; - if (this->isSelectionValid()) - EventManager::post(this->getSelection()); + if (isSelectionValid()) + EventManager::post(getSelection()); }); EventManager::subscribe(this, [this] {