fix: Various issue with the hex editor editing mode

This commit is contained in:
WerWolv 2022-10-12 10:56:03 +02:00
parent 299933c4f7
commit 015266181e
3 changed files with 47 additions and 47 deletions

View File

@ -586,7 +586,7 @@ namespace hex {
}, &userData); }, &userData);
ImGui::PopID(); 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) { void impl::addDataVisualizer(const std::string &unlocalizedName, DataVisualizer *visualizer) {

View File

@ -28,7 +28,7 @@ namespace hex::plugin::builtin {
void drawCell(u64 address, u8 *data, size_t size, bool hovered, CellType cellType); void drawCell(u64 address, u8 *data, size_t size, bool hovered, CellType cellType);
void drawPopup(); 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: public:
void setSelection(const Region &region) { this->setSelection(region.getStartAddress(), region.getEndAddress()); } void setSelection(const Region &region) { 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; auto &data = ProviderExtraData::getCurrent().editor;
if (!isSelectionValid()) if (!isSelectionValid())
@ -64,7 +64,7 @@ namespace hex::plugin::builtin {
return { start, size }; return { start, size };
} }
[[nodiscard]] bool isSelectionValid() const { [[nodiscard]] static bool isSelectionValid() {
auto &data = ProviderExtraData::getCurrent().editor; auto &data = ProviderExtraData::getCurrent().editor;
return data.selectionStart.has_value() && data.selectionEnd.has_value(); return data.selectionStart.has_value() && data.selectionEnd.has_value();

View File

@ -27,7 +27,7 @@ namespace hex::plugin::builtin {
ImGui::EndTabItem(); ImGui::EndTabItem();
} }
ImGui::BeginDisabled(!editor->isSelectionValid()); ImGui::BeginDisabled(!ViewHexEditor::isSelectionValid());
if (ImGui::BeginTabItem("hex.builtin.view.hex_editor.goto.offset.relative"_lang)) { if (ImGui::BeginTabItem("hex.builtin.view.hex_editor.goto.offset.relative"_lang)) {
this->m_mode = Mode::Relative; this->m_mode = Mode::Relative;
ImGui::EndTabItem(); ImGui::EndTabItem();
@ -59,7 +59,7 @@ namespace hex::plugin::builtin {
} }
break; break;
case Mode::Relative: { case Mode::Relative: {
const auto selection = editor->getSelection(); const auto selection = ViewHexEditor::getSelection();
newAddress = selection.getStartAddress() + inputResult; newAddress = selection.getStartAddress() + inputResult;
} }
break; break;
@ -208,7 +208,7 @@ namespace hex::plugin::builtin {
auto region = this->findSequence(searchSequence, this->m_backwards); auto region = this->findSequence(searchSequence, this->m_backwards);
if (region.has_value()) { if (region.has_value()) {
if (editor->getSelection() == region) { if (ViewHexEditor::getSelection() == region) {
if (this->m_nextSearchPosition.has_value()) if (this->m_nextSearchPosition.has_value())
this->m_searchPosition = this->m_nextSearchPosition.value(); this->m_searchPosition = this->m_nextSearchPosition.value();
this->m_nextSearchPosition.reset(); this->m_nextSearchPosition.reset();
@ -499,16 +499,16 @@ namespace hex::plugin::builtin {
ImGui::InputText("##editing_input", buffer, 2, TextInputFlags | ImGuiInputTextFlags_CallbackEdit, [](ImGuiInputTextCallbackData *data) -> int { ImGui::InputText("##editing_input", buffer, 2, TextInputFlags | ImGuiInputTextFlags_CallbackEdit, [](ImGuiInputTextCallbackData *data) -> int {
auto &userData = *reinterpret_cast<UserData*>(data->UserData); auto &userData = *reinterpret_cast<UserData*>(data->UserData);
if (data->BufTextLen >= userData.maxChars) if (data->BufTextLen >= userData.maxChars) {
userData.editingDone = true; userData.editingDone = true;
userData.data[0] = data->Buf[0];
}
return 0; return 0;
}, &userData); }, &userData);
ImGui::PopID(); ImGui::PopID();
data[0] = buffer[0]; return userData.editingDone || ImGui::IsKeyPressed(ImGuiKey_Enter) || ImGui::IsKeyPressed(ImGuiKey_Escape);
return userData.editingDone || ImGui::IsKeyPressed(ImGuiKey_Enter) || ImGui::IsKeyPressed(ImGuiKey_Enter);
} }
else else
return false; return false;
@ -587,8 +587,8 @@ namespace hex::plugin::builtin {
} }
std::optional<color_t> ViewHexEditor::applySelectionColor(u64 byteAddress, std::optional<color_t> color) { std::optional<color_t> ViewHexEditor::applySelectionColor(u64 byteAddress, std::optional<color_t> color) {
if (this->isSelectionValid()) { if (isSelectionValid()) {
auto selection = this->getSelection(); auto selection = getSelection();
if (byteAddress >= selection.getStartAddress() && byteAddress <= selection.getEndAddress()) { if (byteAddress >= selection.getStartAddress() && byteAddress <= selection.getEndAddress()) {
if (color.has_value()) 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) { void ViewHexEditor::drawSelectionFrame(u32 x, u32 y, u64 byteAddress, u16 bytesPerCell, const ImVec2 &cellPos, const ImVec2 &cellSize) const {
if (!this->isSelectionValid()) return; if (!isSelectionValid()) return;
const auto selection = this->getSelection(); const auto selection = getSelection();
if (!Region { byteAddress, 1 }.isWithin(selection)) if (!Region { byteAddress, 1 }.isWithin(selection))
return; return;
@ -941,7 +941,7 @@ namespace hex::plugin::builtin {
if (isColumnSeparatorColumn(x + 1, columnCount)) { if (isColumnSeparatorColumn(x + 1, columnCount)) {
auto separatorAddress = x + y * columnCount; auto separatorAddress = x + y * columnCount;
auto [nextForegroundColor, nextBackgroundColor] = cellColors[x + 1]; 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; 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 // 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 // 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)) { if ((ImGui::IsMouseDown(ImGuiMouseButton_Left) && providerData.selectionStart != providerData.selectionEnd)) {
auto fractionPerLine = 1.0 / (this->m_visibleRowCount + 1); auto fractionPerLine = 1.0 / (this->m_visibleRowCount + 1);
@ -1126,7 +1126,7 @@ namespace hex::plugin::builtin {
this->m_shouldJumpWhenOffScreen = false; this->m_shouldJumpWhenOffScreen = false;
const auto pageAddress = provider->getCurrentPageAddress() + provider->getBaseAddress(); const auto pageAddress = provider->getCurrentPageAddress() + provider->getBaseAddress();
auto newSelection = this->getSelection(); auto newSelection = getSelection();
newSelection.address -= pageAddress; newSelection.address -= pageAddress;
if ((newSelection.getStartAddress()) < u64(clipper.DisplayStart * this->m_bytesPerRow)) if ((newSelection.getStartAddress()) < u64(clipper.DisplayStart * this->m_bytesPerRow))
@ -1143,7 +1143,7 @@ namespace hex::plugin::builtin {
if (this->m_shouldJumpToSelection) { if (this->m_shouldJumpToSelection) {
this->m_shouldJumpToSelection = false; this->m_shouldJumpToSelection = false;
auto newSelection = this->getSelection(); auto newSelection = getSelection();
provider->setCurrentPage(provider->getPageOfAddress(newSelection.address).value_or(0)); provider->setCurrentPage(provider->getPageOfAddress(newSelection.address).value_or(0));
const auto pageAddress = provider->getCurrentPageAddress() + provider->getBaseAddress(); const auto pageAddress = provider->getCurrentPageAddress() + provider->getBaseAddress();
@ -1220,9 +1220,9 @@ namespace hex::plugin::builtin {
// Selection // Selection
ImGui::TableNextColumn(); ImGui::TableNextColumn();
{ {
auto selection = this->getSelection(); auto selection = getSelection();
std::string value; std::string value;
if (this->isSelectionValid()) { if (isSelectionValid()) {
value = hex::format("0x{0:08X} - 0x{1:08X} (0x{2:X} | {3})", value = hex::format("0x{0:08X} - 0x{1:08X} (0x{2:X} | {3})",
selection.getStartAddress(), selection.getStartAddress(),
selection.getEndAddress(), selection.getEndAddress(),
@ -1380,17 +1380,17 @@ namespace hex::plugin::builtin {
}); });
// Remove selection // Remove selection
ShortcutManager::addShortcut(this, Keys::Escape, [this] { ShortcutManager::addShortcut(this, Keys::Escape, [] {
auto &data = ProviderExtraData::getCurrent().editor; auto &data = ProviderExtraData::getCurrent().editor;
data.selectionStart.reset(); data.selectionStart.reset();
data.selectionEnd.reset(); data.selectionEnd.reset();
EventManager::post<EventRegionSelected>(this->getSelection()); EventManager::post<EventRegionSelected>(getSelection());
}); });
// Move cursor around // Move cursor around
ShortcutManager::addShortcut(this, Keys::Up, [this] { ShortcutManager::addShortcut(this, Keys::Up, [this] {
auto selection = this->getSelection(); auto selection = getSelection();
if (selection.getEndAddress() >= this->m_bytesPerRow) { if (selection.getEndAddress() >= this->m_bytesPerRow) {
auto pos = 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] { ShortcutManager::addShortcut(this, Keys::Down, [this] {
auto selection = this->getSelection(); auto selection = getSelection();
auto pos = selection.getEndAddress() + this->m_bytesPerRow; auto pos = selection.getEndAddress() + this->m_bytesPerRow;
this->setSelection(pos, pos); this->setSelection(pos, pos);
@ -1408,7 +1408,7 @@ namespace hex::plugin::builtin {
this->jumpIfOffScreen(); this->jumpIfOffScreen();
}); });
ShortcutManager::addShortcut(this, Keys::Left, [this] { ShortcutManager::addShortcut(this, Keys::Left, [this] {
auto selection = this->getSelection(); auto selection = getSelection();
if (selection.getEndAddress() > 0) { if (selection.getEndAddress() > 0) {
auto pos = selection.getEndAddress() - 1; auto pos = selection.getEndAddress() - 1;
@ -1418,7 +1418,7 @@ namespace hex::plugin::builtin {
} }
}); });
ShortcutManager::addShortcut(this, Keys::Right, [this] { ShortcutManager::addShortcut(this, Keys::Right, [this] {
auto selection = this->getSelection(); auto selection = getSelection();
auto pos = selection.getEndAddress() + 1; auto pos = selection.getEndAddress() + 1;
this->setSelection(pos, pos); this->setSelection(pos, pos);
@ -1427,7 +1427,7 @@ namespace hex::plugin::builtin {
}); });
ShortcutManager::addShortcut(this, Keys::PageUp, [this] { ShortcutManager::addShortcut(this, Keys::PageUp, [this] {
auto selection = this->getSelection(); auto selection = getSelection();
u64 visibleByteCount = this->m_bytesPerRow * this->m_visibleRowCount; u64 visibleByteCount = this->m_bytesPerRow * this->m_visibleRowCount;
if (selection.getEndAddress() >= visibleByteCount) { if (selection.getEndAddress() >= visibleByteCount) {
@ -1438,7 +1438,7 @@ namespace hex::plugin::builtin {
} }
}); });
ShortcutManager::addShortcut(this, Keys::PageDown, [this] { ShortcutManager::addShortcut(this, Keys::PageDown, [this] {
auto selection = this->getSelection(); auto selection = getSelection();
auto pos = selection.getEndAddress() + (this->m_bytesPerRow * this->m_visibleRowCount); auto pos = selection.getEndAddress() + (this->m_bytesPerRow * this->m_visibleRowCount);
this->setSelection(pos, pos); this->setSelection(pos, pos);
@ -1448,14 +1448,14 @@ namespace hex::plugin::builtin {
// Move selection around // Move selection around
ShortcutManager::addShortcut(this, SHIFT + Keys::Up, [this] { ShortcutManager::addShortcut(this, SHIFT + Keys::Up, [this] {
auto selection = this->getSelection(); auto selection = getSelection();
this->setSelection(std::max<u64>(selection.getStartAddress(), this->m_bytesPerRow) - this->m_bytesPerRow, selection.getEndAddress()); this->setSelection(std::max<u64>(selection.getStartAddress(), this->m_bytesPerRow) - this->m_bytesPerRow, selection.getEndAddress());
this->scrollToSelection(); this->scrollToSelection();
this->jumpIfOffScreen(); this->jumpIfOffScreen();
}); });
ShortcutManager::addShortcut(this, SHIFT + Keys::Down, [this] { ShortcutManager::addShortcut(this, SHIFT + Keys::Down, [this] {
auto selection = this->getSelection(); auto selection = getSelection();
this->setSelection(selection.getStartAddress() + this->m_bytesPerRow, selection.getEndAddress()); this->setSelection(selection.getStartAddress() + this->m_bytesPerRow, selection.getEndAddress());
this->scrollToSelection(); this->scrollToSelection();
@ -1463,21 +1463,21 @@ namespace hex::plugin::builtin {
}); });
ShortcutManager::addShortcut(this, SHIFT + Keys::Left, [this] { ShortcutManager::addShortcut(this, SHIFT + Keys::Left, [this] {
auto selection = this->getSelection(); auto selection = getSelection();
this->setSelection(std::max<u64>(selection.getStartAddress(), 1) - 1, selection.getEndAddress()); this->setSelection(std::max<u64>(selection.getStartAddress(), 1) - 1, selection.getEndAddress());
this->scrollToSelection(); this->scrollToSelection();
this->jumpIfOffScreen(); this->jumpIfOffScreen();
}); });
ShortcutManager::addShortcut(this, SHIFT + Keys::Right, [this] { ShortcutManager::addShortcut(this, SHIFT + Keys::Right, [this] {
auto selection = this->getSelection(); auto selection = getSelection();
this->setSelection(selection.getStartAddress() + 1, selection.getEndAddress()); this->setSelection(selection.getStartAddress() + 1, selection.getEndAddress());
this->scrollToSelection(); this->scrollToSelection();
this->jumpIfOffScreen(); this->jumpIfOffScreen();
}); });
ShortcutManager::addShortcut(this, Keys::PageUp, [this] { ShortcutManager::addShortcut(this, Keys::PageUp, [this] {
auto selection = this->getSelection(); auto selection = getSelection();
u64 visibleByteCount = this->m_bytesPerRow * this->m_visibleRowCount; u64 visibleByteCount = this->m_bytesPerRow * this->m_visibleRowCount;
if (selection.getEndAddress() >= visibleByteCount) { if (selection.getEndAddress() >= visibleByteCount) {
@ -1488,7 +1488,7 @@ namespace hex::plugin::builtin {
} }
}); });
ShortcutManager::addShortcut(this, Keys::PageDown, [this] { ShortcutManager::addShortcut(this, Keys::PageDown, [this] {
auto selection = this->getSelection(); auto selection = getSelection();
auto pos = selection.getEndAddress() + (this->m_bytesPerRow * this->m_visibleRowCount); auto pos = selection.getEndAddress() + (this->m_bytesPerRow * this->m_visibleRowCount);
this->setSelection(pos, selection.getEndAddress()); this->setSelection(pos, selection.getEndAddress());
@ -1508,18 +1508,18 @@ namespace hex::plugin::builtin {
}); });
// Copy // Copy
ShortcutManager::addShortcut(this, CTRL + Keys::C, [this] { ShortcutManager::addShortcut(this, CTRL + Keys::C, [] {
const auto selection = this->getSelection(); const auto selection = getSelection();
copyBytes(selection); copyBytes(selection);
}); });
ShortcutManager::addShortcut(this, CTRL + SHIFT + Keys::C, [this] { ShortcutManager::addShortcut(this, CTRL + SHIFT + Keys::C, [] {
const auto selection = this->getSelection(); const auto selection = getSelection();
copyString(selection); copyString(selection);
}); });
// Paste // Paste
ShortcutManager::addShortcut(this, CTRL + Keys::V, [this] { ShortcutManager::addShortcut(this, CTRL + Keys::V, [] {
const auto selection = this->getSelection(); const auto selection = getSelection();
pasteBytes(selection); pasteBytes(selection);
}); });
@ -1562,16 +1562,16 @@ namespace hex::plugin::builtin {
} }
}); });
EventManager::subscribe<QuerySelection>(this, [this](auto &region) { EventManager::subscribe<QuerySelection>(this, [](auto &region) {
if (this->isSelectionValid()) if (isSelectionValid())
region = this->getSelection(); region = getSelection();
}); });
EventManager::subscribe<EventProviderChanged>(this, [this](auto, auto) { EventManager::subscribe<EventProviderChanged>(this, [this](auto, auto) {
this->m_shouldUpdateScrollPosition = true; this->m_shouldUpdateScrollPosition = true;
if (this->isSelectionValid()) if (isSelectionValid())
EventManager::post<EventRegionSelected>(this->getSelection()); EventManager::post<EventRegionSelected>(getSelection());
}); });
EventManager::subscribe<EventSettingsChanged>(this, [this] { EventManager::subscribe<EventSettingsChanged>(this, [this] {