From 7631778edbd21ae7381c143b7f78eb305ec60fb2 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 16 Jul 2023 19:52:48 +0200 Subject: [PATCH] feat: Added support for big endian in data visualizers --- plugins/builtin/include/ui/hex_editor.hpp | 1 + plugins/builtin/source/ui/hex_editor.cpp | 38 ++++++++++++++++++----- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/plugins/builtin/include/ui/hex_editor.hpp b/plugins/builtin/include/ui/hex_editor.hpp index b6e467fe5..07131d078 100644 --- a/plugins/builtin/include/ui/hex_editor.hpp +++ b/plugins/builtin/include/ui/hex_editor.hpp @@ -206,6 +206,7 @@ namespace hex::plugin::builtin::ui { float m_scrollPosition = 0; u16 m_bytesPerRow = 16; + std::endian m_dataVisualizerEndianness = std::endian::little; std::shared_ptr m_currDataVisualizer; char m_unknownDataCharacter = '?'; diff --git a/plugins/builtin/source/ui/hex_editor.cpp b/plugins/builtin/source/ui/hex_editor.cpp index 18357f32c..8495ffc9e 100644 --- a/plugins/builtin/source/ui/hex_editor.cpp +++ b/plugins/builtin/source/ui/hex_editor.cpp @@ -171,10 +171,17 @@ namespace hex::plugin::builtin::ui { } if (this->m_editingAddress != address || this->m_editingCellType != cellType) { - if (cellType == CellType::Hex) - this->m_currDataVisualizer->draw(address, data, size, this->m_upperCaseHex); - else + if (cellType == CellType::Hex) { + std::vector buffer(size); + std::memcpy(buffer.data(), data, size); + + if (this->m_dataVisualizerEndianness != std::endian::native) + std::reverse(buffer.begin(), buffer.end()); + + this->m_currDataVisualizer->draw(address, buffer.data(), buffer.size(), this->m_upperCaseHex); + } else { asciiVisualizer.draw(address, data, size, this->m_upperCaseHex); + } if (hovered && this->m_provider->isWritable()) { // Enter editing mode when double-clicking a cell @@ -194,10 +201,18 @@ namespace hex::plugin::builtin::ui { ImGui::SetNextFrameWantCaptureKeyboard(true); bool shouldExitEditingMode = true; - if (cellType == this->m_editingCellType && cellType == CellType::Hex) - shouldExitEditingMode = this->m_currDataVisualizer->drawEditing(*this->m_editingAddress, this->m_editingBytes.data(), this->m_editingBytes.size(), this->m_upperCaseHex, this->m_enteredEditingMode); - else if (cellType == this->m_editingCellType && cellType == CellType::ASCII) + if (cellType == this->m_editingCellType && cellType == CellType::Hex) { + std::vector buffer = this->m_editingBytes; + + if (this->m_dataVisualizerEndianness != std::endian::native) + std::reverse(buffer.begin(), buffer.end()); + + shouldExitEditingMode = this->m_currDataVisualizer->drawEditing(*this->m_editingAddress, buffer.data(), buffer.size(), this->m_upperCaseHex, this->m_enteredEditingMode); + + this->m_editingBytes = buffer; + } else if (cellType == this->m_editingCellType && cellType == CellType::ASCII) { shouldExitEditingMode = asciiVisualizer.drawEditing(*this->m_editingAddress, this->m_editingBytes.data(), this->m_editingBytes.size(), this->m_upperCaseHex, this->m_enteredEditingMode); + } if (shouldExitEditingMode || this->m_shouldModifyValue) { this->m_provider->write(*this->m_editingAddress, this->m_editingBytes.data(), this->m_editingBytes.size()); @@ -805,8 +820,17 @@ namespace hex::plugin::builtin::ui { auto &visualizers = ContentRegistry::HexEditor::impl::getVisualizers(); ImGui::TextFormatted("{}: ", "hex.builtin.hex_editor.visualizer"_lang); + ImGui::SameLine(0, 0); - ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x); + + int sliderPos = this->m_dataVisualizerEndianness == std::endian::little ? 0 : 1; + ImGui::PushItemWidth(60_scaled); + ImGui::SliderInt("##visualizer_endianness", &sliderPos, 0, 1, sliderPos == 0 ? "Little" : "Big"); + ImGui::PopItemWidth(); + this->m_dataVisualizerEndianness = sliderPos == 0 ? std::endian::little : std::endian::big; + + ImGui::SameLine(0, 2_scaled); + ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x - 60_scaled); if (ImGui::BeginCombo("##visualizer", LangEntry(this->m_currDataVisualizer->getUnlocalizedName()))) { for (const auto &visualizer : visualizers) {