2022-11-08 21:43:22 +01:00
|
|
|
#include <ui/hex_editor.hpp>
|
2022-11-06 12:19:12 +01:00
|
|
|
|
|
|
|
#include <hex/api/content_registry.hpp>
|
|
|
|
#include <hex/api/localization.hpp>
|
|
|
|
|
|
|
|
#include <hex/helpers/encoding_file.hpp>
|
|
|
|
#include <hex/helpers/utils.hpp>
|
|
|
|
|
2022-11-08 21:43:22 +01:00
|
|
|
namespace hex::plugin::builtin::ui {
|
2022-11-06 12:19:12 +01:00
|
|
|
|
|
|
|
/* Data Visualizer */
|
|
|
|
|
|
|
|
class DataVisualizerAscii : public hex::ContentRegistry::HexEditor::DataVisualizer {
|
|
|
|
public:
|
2023-06-05 09:07:58 +02:00
|
|
|
DataVisualizerAscii() : DataVisualizer("ASCII", 1, 1) { }
|
2022-11-06 12:19:12 +01:00
|
|
|
|
|
|
|
void draw(u64 address, const u8 *data, size_t size, bool upperCase) override {
|
|
|
|
hex::unused(address, upperCase);
|
|
|
|
|
|
|
|
if (size == 1) {
|
|
|
|
const u8 c = data[0];
|
|
|
|
if (std::isprint(c))
|
|
|
|
ImGui::Text("%c", c);
|
|
|
|
else
|
|
|
|
ImGui::TextDisabled(".");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ImGui::TextDisabled(".");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool drawEditing(u64 address, u8 *data, size_t size, bool upperCase, bool startedEditing) override {
|
|
|
|
hex::unused(address, startedEditing, upperCase);
|
|
|
|
|
|
|
|
if (size == 1) {
|
|
|
|
struct UserData {
|
|
|
|
u8 *data;
|
|
|
|
i32 maxChars;
|
|
|
|
|
|
|
|
bool editingDone;
|
|
|
|
};
|
|
|
|
|
|
|
|
UserData userData = {
|
|
|
|
.data = data,
|
|
|
|
.maxChars = this->getMaxCharsPerCell(),
|
|
|
|
|
|
|
|
.editingDone = false
|
|
|
|
};
|
|
|
|
|
|
|
|
ImGui::PushID(reinterpret_cast<void*>(address));
|
|
|
|
char buffer[2] = { std::isprint(data[0]) ? char(data[0]) : '.', 0x00 };
|
|
|
|
ImGui::InputText("##editing_input", buffer, 2, TextInputFlags | ImGuiInputTextFlags_CallbackEdit, [](ImGuiInputTextCallbackData *data) -> int {
|
|
|
|
auto &userData = *reinterpret_cast<UserData*>(data->UserData);
|
|
|
|
|
|
|
|
if (data->BufTextLen >= userData.maxChars) {
|
|
|
|
userData.editingDone = true;
|
|
|
|
userData.data[0] = data->Buf[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}, &userData);
|
|
|
|
ImGui::PopID();
|
|
|
|
|
|
|
|
return userData.editingDone || ImGui::IsKeyPressed(ImGuiKey_Enter) || ImGui::IsKeyPressed(ImGuiKey_Escape);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Hex Editor */
|
|
|
|
|
2022-11-08 21:43:22 +01:00
|
|
|
HexEditor::HexEditor(prv::Provider *provider) : m_provider(provider) {
|
2023-06-21 17:48:51 +02:00
|
|
|
this->m_currDataVisualizer = ContentRegistry::HexEditor::getVisualizerByName("hex.builtin.visualizer.hexadecimal.8bit");
|
2023-09-02 17:52:05 +02:00
|
|
|
this->m_bytesPerRow = ContentRegistry::Settings::read("hex.builtin.setting.hex_editor", "hex.builtin.setting.hex_editor.bytes_per_row", this->m_bytesPerRow);
|
2023-06-05 09:45:03 +02:00
|
|
|
|
2022-11-06 12:19:12 +01:00
|
|
|
EventManager::subscribe<EventSettingsChanged>(this, [this] {
|
2023-03-21 15:33:43 +01:00
|
|
|
this->m_selectionColor = ContentRegistry::Settings::read("hex.builtin.setting.hex_editor", "hex.builtin.setting.hex_editor.highlight_color", 0x60C08080);
|
|
|
|
this->m_syncScrolling = ContentRegistry::Settings::read("hex.builtin.setting.hex_editor", "hex.builtin.setting.hex_editor.sync_scrolling", 0);
|
|
|
|
this->m_byteCellPadding = ContentRegistry::Settings::read("hex.builtin.setting.hex_editor", "hex.builtin.setting.hex_editor.byte_padding", 0);
|
|
|
|
this->m_characterCellPadding = ContentRegistry::Settings::read("hex.builtin.setting.hex_editor", "hex.builtin.setting.hex_editor.char_padding", 0);
|
2022-11-06 12:19:12 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
HexEditor::~HexEditor() {
|
|
|
|
EventManager::unsubscribe<EventSettingsChanged>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr static u16 getByteColumnSeparatorCount(u16 columnCount) {
|
|
|
|
return (columnCount - 1) / 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr static bool isColumnSeparatorColumn(u16 currColumn, u16 columnCount) {
|
|
|
|
return currColumn > 0 && (currColumn) < columnCount && ((currColumn) % 8) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<color_t> HexEditor::applySelectionColor(u64 byteAddress, std::optional<color_t> color) {
|
|
|
|
if (isSelectionValid()) {
|
|
|
|
auto selection = getSelection();
|
|
|
|
|
|
|
|
if (byteAddress >= selection.getStartAddress() && byteAddress <= selection.getEndAddress()) {
|
|
|
|
if (color.has_value())
|
|
|
|
color = (ImAlphaBlendColors(color.value(), this->m_selectionColor)) & 0x00FFFFFF;
|
|
|
|
else
|
|
|
|
color = this->m_selectionColor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (color.has_value())
|
|
|
|
color = (*color & 0x00FFFFFF) | (this->m_selectionColor & 0xFF000000);
|
|
|
|
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CustomEncodingData {
|
|
|
|
std::string displayValue;
|
|
|
|
size_t advance;
|
|
|
|
ImColor color;
|
|
|
|
};
|
|
|
|
|
2023-06-21 00:30:26 +02:00
|
|
|
static CustomEncodingData queryCustomEncodingData(prv::Provider *provider, const EncodingFile &encodingFile, u64 address) {
|
2022-11-06 12:19:12 +01:00
|
|
|
const auto longestSequence = encodingFile.getLongestSequence();
|
|
|
|
|
|
|
|
if (longestSequence == 0)
|
|
|
|
return { ".", 1, 0xFFFF8000 };
|
|
|
|
|
2023-06-21 00:30:26 +02:00
|
|
|
size_t size = std::min<size_t>(longestSequence, provider->getActualSize() - address);
|
2022-11-06 12:19:12 +01:00
|
|
|
|
|
|
|
std::vector<u8> buffer(size);
|
2023-01-22 14:17:55 +01:00
|
|
|
provider->read(address, buffer.data(), size);
|
2022-11-06 12:19:12 +01:00
|
|
|
|
|
|
|
const auto [decoded, advance] = encodingFile.getEncodingFor(buffer);
|
2023-07-28 10:31:44 +02:00
|
|
|
const ImColor color = [&]{
|
2022-11-06 12:19:12 +01:00
|
|
|
if (decoded.length() == 1 && std::isalnum(decoded[0]))
|
|
|
|
return ImGui::GetCustomColorU32(ImGuiCustomCol_ToolbarBlue);
|
|
|
|
else if (decoded.length() == 1 && advance == 1)
|
|
|
|
return ImGui::GetCustomColorU32(ImGuiCustomCol_ToolbarRed);
|
|
|
|
else if (decoded.length() > 1 && advance == 1)
|
|
|
|
return ImGui::GetCustomColorU32(ImGuiCustomCol_ToolbarYellow);
|
|
|
|
else if (advance > 1)
|
|
|
|
return ImGui::GetColorU32(ImGuiCol_Text);
|
|
|
|
else
|
|
|
|
return ImGui::GetCustomColorU32(ImGuiCustomCol_ToolbarBlue);
|
|
|
|
}();
|
|
|
|
|
|
|
|
return { std::string(decoded), advance, color };
|
|
|
|
}
|
|
|
|
|
|
|
|
static auto getCellPosition() {
|
|
|
|
return ImGui::GetCursorScreenPos() - ImGui::GetStyle().CellPadding;
|
|
|
|
}
|
|
|
|
|
2022-11-07 00:04:47 +01:00
|
|
|
void HexEditor::drawTooltip(u64 address, const u8 *data, size_t size) {
|
2022-11-06 12:19:12 +01:00
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, scaled(ImVec2(5, 5)));
|
|
|
|
|
2022-11-07 00:04:47 +01:00
|
|
|
this->m_tooltipCallback(address, data, size);
|
2022-11-06 12:19:12 +01:00
|
|
|
|
|
|
|
ImGui::PopStyleVar();
|
|
|
|
}
|
|
|
|
|
2022-11-08 21:43:22 +01:00
|
|
|
void HexEditor::drawCell(u64 address, u8 *data, size_t size, bool hovered, CellType cellType) {
|
2022-11-06 12:19:12 +01:00
|
|
|
static DataVisualizerAscii asciiVisualizer;
|
|
|
|
|
2023-05-15 08:17:16 +02:00
|
|
|
if (this->m_shouldUpdateEditingValue) {
|
|
|
|
this->m_shouldUpdateEditingValue = false;
|
|
|
|
this->m_editingBytes.resize(size);
|
|
|
|
std::memcpy(this->m_editingBytes.data(), data, size);
|
|
|
|
}
|
|
|
|
|
2022-11-06 12:19:12 +01:00
|
|
|
if (this->m_editingAddress != address || this->m_editingCellType != cellType) {
|
2023-07-16 19:52:48 +02:00
|
|
|
if (cellType == CellType::Hex) {
|
|
|
|
std::vector<u8> 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 {
|
2022-11-06 12:19:12 +01:00
|
|
|
asciiVisualizer.draw(address, data, size, this->m_upperCaseHex);
|
2023-07-16 19:52:48 +02:00
|
|
|
}
|
2022-11-06 12:19:12 +01:00
|
|
|
|
2022-11-08 21:43:22 +01:00
|
|
|
if (hovered && this->m_provider->isWritable()) {
|
2022-11-06 12:19:12 +01:00
|
|
|
// Enter editing mode when double-clicking a cell
|
|
|
|
if (ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
|
|
|
|
this->m_editingAddress = address;
|
|
|
|
this->m_shouldModifyValue = false;
|
|
|
|
this->m_enteredEditingMode = true;
|
|
|
|
|
|
|
|
this->m_editingBytes.resize(size);
|
|
|
|
std::memcpy(this->m_editingBytes.data(), data, size);
|
|
|
|
this->m_editingCellType = cellType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ImGui::SetKeyboardFocusHere();
|
|
|
|
ImGui::SetNextFrameWantCaptureKeyboard(true);
|
|
|
|
|
|
|
|
bool shouldExitEditingMode = true;
|
2023-07-16 19:52:48 +02:00
|
|
|
if (cellType == this->m_editingCellType && cellType == CellType::Hex) {
|
|
|
|
std::vector<u8> 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);
|
|
|
|
|
2023-08-02 12:51:33 +02:00
|
|
|
if (this->m_dataVisualizerEndianness != std::endian::native)
|
|
|
|
std::reverse(buffer.begin(), buffer.end());
|
|
|
|
|
2023-07-16 19:52:48 +02:00
|
|
|
this->m_editingBytes = buffer;
|
|
|
|
} else if (cellType == this->m_editingCellType && cellType == CellType::ASCII) {
|
2022-11-06 12:19:12 +01:00
|
|
|
shouldExitEditingMode = asciiVisualizer.drawEditing(*this->m_editingAddress, this->m_editingBytes.data(), this->m_editingBytes.size(), this->m_upperCaseHex, this->m_enteredEditingMode);
|
2023-07-16 19:52:48 +02:00
|
|
|
}
|
2022-11-06 12:19:12 +01:00
|
|
|
|
|
|
|
if (shouldExitEditingMode || this->m_shouldModifyValue) {
|
2022-11-08 21:43:22 +01:00
|
|
|
this->m_provider->write(*this->m_editingAddress, this->m_editingBytes.data(), this->m_editingBytes.size());
|
2022-11-06 12:19:12 +01:00
|
|
|
|
|
|
|
if (!this->m_selectionChanged && !ImGui::IsMouseDown(ImGuiMouseButton_Left) && !ImGui::IsMouseClicked(ImGuiMouseButton_Left)) {
|
|
|
|
auto nextEditingAddress = *this->m_editingAddress + this->m_currDataVisualizer->getBytesPerCell();
|
|
|
|
this->setSelection(nextEditingAddress, nextEditingAddress);
|
|
|
|
|
2023-01-10 13:48:50 +01:00
|
|
|
if (nextEditingAddress >= this->m_provider->getBaseAddress() + this->m_provider->getCurrentPageAddress() + this->m_provider->getSize())
|
2022-11-06 12:19:12 +01:00
|
|
|
this->m_editingAddress = std::nullopt;
|
|
|
|
else
|
|
|
|
this->m_editingAddress = nextEditingAddress;
|
|
|
|
} else {
|
|
|
|
this->m_editingAddress = std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->m_shouldModifyValue = false;
|
|
|
|
this->m_shouldUpdateEditingValue = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::IsMouseClicked(ImGuiMouseButton_Left) && !hovered && !this->m_enteredEditingMode) {
|
|
|
|
this->m_editingAddress = std::nullopt;
|
|
|
|
this->m_shouldModifyValue = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this->m_editingAddress.has_value())
|
|
|
|
this->m_editingCellType = CellType::None;
|
2023-02-18 22:20:02 +01:00
|
|
|
|
|
|
|
this->m_enteredEditingMode = false;
|
2022-11-06 12:19:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HexEditor::drawSelectionFrame(u32 x, u32 y, u64 byteAddress, u16 bytesPerCell, const ImVec2 &cellPos, const ImVec2 &cellSize) const {
|
|
|
|
if (!this->isSelectionValid()) return;
|
|
|
|
|
|
|
|
const auto selection = getSelection();
|
|
|
|
if (!Region { byteAddress, 1 }.isWithin(selection))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const color_t SelectionFrameColor = ImGui::GetColorU32(ImGuiCol_Text);
|
|
|
|
|
|
|
|
auto drawList = ImGui::GetWindowDrawList();
|
|
|
|
|
|
|
|
// Draw vertical line at the left of first byte and the start of the line
|
|
|
|
if (x == 0 || byteAddress == selection.getStartAddress())
|
2023-03-18 10:52:50 +01:00
|
|
|
drawList->AddLine(cellPos, cellPos + ImVec2(0, cellSize.y), ImColor(SelectionFrameColor), 1_scaled);
|
2022-11-06 12:19:12 +01:00
|
|
|
|
|
|
|
// Draw vertical line at the right of the last byte and the end of the line
|
|
|
|
if (x == u16((this->m_bytesPerRow / bytesPerCell) - 1) || (byteAddress + bytesPerCell) > selection.getEndAddress())
|
2023-03-18 10:52:50 +01:00
|
|
|
drawList->AddLine(cellPos + ImVec2(cellSize.x, -1), cellPos + cellSize, ImColor(SelectionFrameColor), 1_scaled);
|
2022-11-06 12:19:12 +01:00
|
|
|
|
|
|
|
// Draw horizontal line at the top of the bytes
|
|
|
|
if (y == 0 || (byteAddress - this->m_bytesPerRow) < selection.getStartAddress())
|
2023-03-18 10:52:50 +01:00
|
|
|
drawList->AddLine(cellPos, cellPos + ImVec2(cellSize.x + 1, 0), ImColor(SelectionFrameColor), 1_scaled);
|
2022-11-06 12:19:12 +01:00
|
|
|
|
|
|
|
// Draw horizontal line at the bottom of the bytes
|
|
|
|
if ((byteAddress + this->m_bytesPerRow) > selection.getEndAddress())
|
2023-03-18 10:52:50 +01:00
|
|
|
drawList->AddLine(cellPos + ImVec2(0, cellSize.y), cellPos + cellSize + ImVec2(1, 0), ImColor(SelectionFrameColor), 1_scaled);
|
2022-11-06 12:19:12 +01:00
|
|
|
}
|
|
|
|
|
2022-11-08 21:43:22 +01:00
|
|
|
void HexEditor::drawEditor(const ImVec2 &size) {
|
2022-11-06 12:19:12 +01:00
|
|
|
const float SeparatorColumWidth = 6_scaled;
|
|
|
|
const auto CharacterSize = ImGui::CalcTextSize("0");
|
|
|
|
|
|
|
|
const auto bytesPerCell = this->m_currDataVisualizer->getBytesPerCell();
|
|
|
|
const u16 columnCount = this->m_bytesPerRow / bytesPerCell;
|
|
|
|
const auto byteColumnCount = columnCount + getByteColumnSeparatorCount(columnCount);
|
|
|
|
|
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_CellPadding, ImVec2(0.5, 0));
|
|
|
|
if (ImGui::BeginTable("##hex", 2 + byteColumnCount + 2 + 2 , ImGuiTableFlags_ScrollY | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoKeepColumnsVisible, size)) {
|
|
|
|
View::discardNavigationRequests();
|
|
|
|
ImGui::TableSetupScrollFreeze(0, 2);
|
|
|
|
|
|
|
|
// Row address column
|
|
|
|
ImGui::TableSetupColumn("hex.builtin.common.address"_lang);
|
|
|
|
ImGui::TableSetupColumn("");
|
|
|
|
|
|
|
|
// Byte columns
|
|
|
|
for (u16 i = 0; i < columnCount; i++) {
|
|
|
|
if (isColumnSeparatorColumn(i, columnCount))
|
|
|
|
ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, SeparatorColumWidth);
|
|
|
|
|
2023-03-17 19:58:08 +01:00
|
|
|
ImGui::TableSetupColumn(hex::format(this->m_upperCaseHex ? "{:0{}X}" : "{:0{}x}", i * bytesPerCell, this->m_currDataVisualizer->getMaxCharsPerCell()).c_str(), ImGuiTableColumnFlags_WidthFixed, CharacterSize.x * this->m_currDataVisualizer->getMaxCharsPerCell() + (6 + this->m_byteCellPadding) * 1_scaled);
|
2022-11-06 12:19:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ASCII column
|
|
|
|
ImGui::TableSetupColumn("");
|
|
|
|
|
2023-07-28 10:31:44 +02:00
|
|
|
if (this->m_showAscii) {
|
|
|
|
ImGui::TableSetupColumn("hex.builtin.common.encoding.ascii"_lang, ImGuiTableColumnFlags_WidthFixed, (CharacterSize.x + this->m_characterCellPadding * 1_scaled) * this->m_bytesPerRow);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, 0);
|
|
|
|
|
2022-11-06 12:19:12 +01:00
|
|
|
ImGui::TableSetupColumn("");
|
2023-07-28 10:31:44 +02:00
|
|
|
// Custom encoding column
|
2023-07-11 00:04:26 +02:00
|
|
|
{
|
2023-07-28 10:31:44 +02:00
|
|
|
if (this->m_currCustomEncoding.has_value() && this->m_showCustomEncoding) {
|
2023-07-11 00:04:26 +02:00
|
|
|
ImGui::TableSetupColumn(this->m_currCustomEncoding->getName().c_str(), ImGuiTableColumnFlags_WidthStretch);
|
|
|
|
} else {
|
2023-07-28 10:31:44 +02:00
|
|
|
ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, 0);
|
2023-07-11 00:04:26 +02:00
|
|
|
}
|
|
|
|
}
|
2022-11-06 12:19:12 +01:00
|
|
|
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
for (i32 i = 0; i < ImGui::TableGetColumnCount(); i++) {
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TextUnformatted(ImGui::TableGetColumnName(i));
|
2023-06-04 00:09:45 +02:00
|
|
|
ImGui::Dummy(ImVec2(0, CharacterSize.y / 2));
|
2022-11-06 12:19:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
2022-11-08 21:43:22 +01:00
|
|
|
if (this->m_provider != nullptr && this->m_provider->isReadable()) {
|
2023-03-21 16:11:40 +01:00
|
|
|
const auto isCurrRegionValid = [this](u64 address) {
|
2023-01-01 12:26:27 +01:00
|
|
|
auto &[currRegion, currRegionValid] = this->m_currValidRegion;
|
2022-11-06 12:19:12 +01:00
|
|
|
if (!Region{ address, 1 }.isWithin(currRegion)) {
|
2023-01-01 12:26:27 +01:00
|
|
|
this->m_currValidRegion = this->m_provider->getRegionValidity(address);
|
2022-11-06 12:19:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return currRegionValid;
|
|
|
|
};
|
|
|
|
|
|
|
|
ImGuiListClipper clipper;
|
|
|
|
|
2023-07-18 14:22:39 +02:00
|
|
|
u64 numRows = std::ceil(this->m_provider->getSize() / (long double)(this->m_bytesPerRow));
|
|
|
|
clipper.Begin(numRows + size.y / CharacterSize.y - 3, CharacterSize.y);
|
2022-11-06 12:19:12 +01:00
|
|
|
while (clipper.Step()) {
|
|
|
|
this->m_visibleRowCount = clipper.DisplayEnd - clipper.DisplayStart;
|
|
|
|
|
|
|
|
// Loop over rows
|
2023-07-18 14:22:39 +02:00
|
|
|
for (u64 y = u64(clipper.DisplayStart); y < std::min(numRows, u64(clipper.DisplayEnd)); y++) {
|
2022-11-06 12:19:12 +01:00
|
|
|
// Draw address column
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
2022-11-08 21:43:22 +01:00
|
|
|
ImGui::TextFormatted(this->m_upperCaseHex ? "{:08X}: " : "{:08x}: ", y * this->m_bytesPerRow + this->m_provider->getBaseAddress() + this->m_provider->getCurrentPageAddress());
|
2022-11-06 12:19:12 +01:00
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
2022-11-08 21:43:22 +01:00
|
|
|
const u8 validBytes = std::min<u64>(this->m_bytesPerRow, this->m_provider->getSize() - y * this->m_bytesPerRow);
|
2022-11-06 12:19:12 +01:00
|
|
|
|
|
|
|
std::vector<u8> bytes(this->m_bytesPerRow, 0x00);
|
2022-11-08 21:43:22 +01:00
|
|
|
this->m_provider->read(y * this->m_bytesPerRow + this->m_provider->getBaseAddress() + this->m_provider->getCurrentPageAddress(), bytes.data(), validBytes);
|
2022-11-06 12:19:12 +01:00
|
|
|
|
|
|
|
std::vector<std::tuple<std::optional<color_t>, std::optional<color_t>>> cellColors;
|
|
|
|
{
|
|
|
|
for (u64 x = 0; x < std::ceil(float(validBytes) / bytesPerCell); x++) {
|
2022-11-08 21:43:22 +01:00
|
|
|
const u64 byteAddress = y * this->m_bytesPerRow + x * bytesPerCell + this->m_provider->getBaseAddress() + this->m_provider->getCurrentPageAddress();
|
2022-11-06 12:19:12 +01:00
|
|
|
|
|
|
|
const auto cellBytes = std::min<u64>(validBytes, bytesPerCell);
|
|
|
|
|
|
|
|
// Query cell colors
|
|
|
|
if (x < std::ceil(float(validBytes) / bytesPerCell)) {
|
2023-06-05 09:07:58 +02:00
|
|
|
auto foregroundColor = this->m_foregroundColorCallback(byteAddress, &bytes[x * cellBytes], cellBytes);
|
|
|
|
auto backgroundColor = this->m_backgroundColorCallback(byteAddress, &bytes[x * cellBytes], cellBytes);
|
|
|
|
|
|
|
|
if (this->m_grayOutZero && !foregroundColor.has_value()) {
|
|
|
|
bool allZero = true;
|
2023-07-20 20:58:28 +02:00
|
|
|
for (u64 i = 0; i < cellBytes && (x * cellBytes + i) < bytes.size(); i++) {
|
2023-06-05 09:07:58 +02:00
|
|
|
if (bytes[x * cellBytes + i] != 0x00) {
|
|
|
|
allZero = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (allZero)
|
|
|
|
foregroundColor = ImGui::GetColorU32(ImGuiCol_TextDisabled);
|
|
|
|
}
|
2022-11-06 12:19:12 +01:00
|
|
|
|
|
|
|
cellColors.emplace_back(
|
|
|
|
foregroundColor,
|
|
|
|
backgroundColor
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
cellColors.emplace_back(
|
|
|
|
std::nullopt,
|
|
|
|
std::nullopt
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw byte columns
|
2023-03-18 10:52:50 +01:00
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_CellPadding, scaled(ImVec2(2.75F, 0.0F)));
|
2022-11-06 12:19:12 +01:00
|
|
|
|
|
|
|
for (u64 x = 0; x < columnCount; x++) {
|
2022-11-08 21:43:22 +01:00
|
|
|
const u64 byteAddress = y * this->m_bytesPerRow + x * bytesPerCell + this->m_provider->getBaseAddress() + this->m_provider->getCurrentPageAddress();
|
2022-11-06 12:19:12 +01:00
|
|
|
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
if (isColumnSeparatorColumn(x, columnCount))
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
|
|
|
if (x < std::ceil(float(validBytes) / bytesPerCell)) {
|
|
|
|
auto cellStartPos = getCellPosition();
|
2023-03-18 10:52:50 +01:00
|
|
|
auto cellSize = (CharacterSize * ImVec2(this->m_currDataVisualizer->getMaxCharsPerCell(), 1)) + (ImVec2(2, 2) * ImGui::GetStyle().CellPadding) + scaled(ImVec2(1 + this->m_byteCellPadding, 0));
|
2022-11-06 12:19:12 +01:00
|
|
|
auto maxCharsPerCell = this->m_currDataVisualizer->getMaxCharsPerCell();
|
|
|
|
|
|
|
|
auto [foregroundColor, backgroundColor] = cellColors[x];
|
|
|
|
|
|
|
|
if (isColumnSeparatorColumn(x + 1, columnCount) && cellColors.size() > x + 1) {
|
|
|
|
auto separatorAddress = x + y * columnCount;
|
|
|
|
auto [nextForegroundColor, nextBackgroundColor] = cellColors[x + 1];
|
|
|
|
if ((isSelectionValid() && getSelection().overlaps({ separatorAddress, 1 }) && getSelection().getEndAddress() != separatorAddress) || backgroundColor == nextBackgroundColor)
|
|
|
|
cellSize.x += SeparatorColumWidth + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y == u64(clipper.DisplayStart))
|
2023-06-04 22:52:36 +02:00
|
|
|
cellSize.y -= (ImGui::GetStyle().CellPadding.y);
|
2022-11-06 12:19:12 +01:00
|
|
|
|
|
|
|
backgroundColor = applySelectionColor(byteAddress, backgroundColor);
|
|
|
|
|
|
|
|
// Draw highlights and selection
|
|
|
|
if (backgroundColor.has_value()) {
|
|
|
|
auto drawList = ImGui::GetWindowDrawList();
|
|
|
|
|
|
|
|
// Draw background color
|
|
|
|
drawList->AddRectFilled(cellStartPos, cellStartPos + cellSize, backgroundColor.value());
|
|
|
|
|
|
|
|
// Draw frame around mouse selection
|
|
|
|
this->drawSelectionFrame(x, y, byteAddress, bytesPerCell, cellStartPos, cellSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool cellHovered = ImGui::IsMouseHoveringRect(cellStartPos, cellStartPos + cellSize, false);
|
|
|
|
|
|
|
|
this->handleSelection(byteAddress, bytesPerCell, &bytes[x * bytesPerCell], cellHovered);
|
|
|
|
|
|
|
|
// Get byte foreground color
|
|
|
|
if (foregroundColor.has_value())
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_Text, *foregroundColor);
|
|
|
|
|
|
|
|
// Draw cell content
|
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
|
|
|
|
ImGui::PushItemWidth((CharacterSize * maxCharsPerCell).x);
|
|
|
|
if (isCurrRegionValid(byteAddress))
|
2022-11-08 21:43:22 +01:00
|
|
|
this->drawCell(byteAddress, &bytes[x * bytesPerCell], bytesPerCell, cellHovered, CellType::Hex);
|
2022-11-06 12:19:12 +01:00
|
|
|
else
|
|
|
|
ImGui::TextFormatted("{}", std::string(maxCharsPerCell, '?'));
|
|
|
|
ImGui::PopItemWidth();
|
|
|
|
ImGui::PopStyleVar();
|
|
|
|
|
|
|
|
if (foregroundColor.has_value())
|
|
|
|
ImGui::PopStyleColor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ImGui::PopStyleVar();
|
|
|
|
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
|
|
|
// Draw ASCII column
|
|
|
|
if (this->m_showAscii) {
|
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_CellPadding, ImVec2(0, 0));
|
|
|
|
if (ImGui::BeginTable("##ascii_column", this->m_bytesPerRow)) {
|
|
|
|
for (u64 x = 0; x < this->m_bytesPerRow; x++)
|
2023-03-17 19:58:08 +01:00
|
|
|
ImGui::TableSetupColumn(hex::format("##ascii_cell{}", x).c_str(), ImGuiTableColumnFlags_WidthFixed, CharacterSize.x + this->m_characterCellPadding * 1_scaled);
|
2022-11-06 12:19:12 +01:00
|
|
|
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
|
|
|
|
for (u64 x = 0; x < this->m_bytesPerRow; x++) {
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
2022-11-08 21:43:22 +01:00
|
|
|
const u64 byteAddress = y * this->m_bytesPerRow + x + this->m_provider->getBaseAddress() + this->m_provider->getCurrentPageAddress();
|
2022-11-06 12:19:12 +01:00
|
|
|
|
|
|
|
const auto cellStartPos = getCellPosition();
|
2023-03-17 19:58:08 +01:00
|
|
|
const auto cellSize = CharacterSize + scaled(ImVec2(this->m_characterCellPadding, 0));
|
2022-11-06 12:19:12 +01:00
|
|
|
|
|
|
|
const bool cellHovered = ImGui::IsMouseHoveringRect(cellStartPos, cellStartPos + cellSize, true);
|
|
|
|
|
|
|
|
if (x < validBytes) {
|
|
|
|
this->handleSelection(byteAddress, bytesPerCell, &bytes[x], cellHovered);
|
|
|
|
|
|
|
|
auto [foregroundColor, backgroundColor] = cellColors[x / bytesPerCell];
|
|
|
|
|
|
|
|
backgroundColor = applySelectionColor(byteAddress, backgroundColor);
|
|
|
|
|
|
|
|
// Draw highlights and selection
|
|
|
|
if (backgroundColor.has_value()) {
|
|
|
|
auto drawList = ImGui::GetWindowDrawList();
|
|
|
|
|
|
|
|
// Draw background color
|
|
|
|
drawList->AddRectFilled(cellStartPos, cellStartPos + cellSize, backgroundColor.value());
|
|
|
|
|
|
|
|
this->drawSelectionFrame(x, y, byteAddress, 1, cellStartPos, cellSize);
|
|
|
|
}
|
|
|
|
|
2023-03-17 19:58:08 +01:00
|
|
|
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + (this->m_characterCellPadding * 1_scaled) / 2);
|
2022-11-06 12:19:12 +01:00
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
|
|
|
|
ImGui::PushItemWidth(CharacterSize.x);
|
|
|
|
if (!isCurrRegionValid(byteAddress))
|
2023-02-15 17:01:36 +01:00
|
|
|
ImGui::TextFormattedDisabled("{}", this->m_unknownDataCharacter);
|
2022-11-06 12:19:12 +01:00
|
|
|
else
|
2022-11-08 21:43:22 +01:00
|
|
|
this->drawCell(byteAddress, &bytes[x], 1, cellHovered, CellType::ASCII);
|
2022-11-06 12:19:12 +01:00
|
|
|
ImGui::PopItemWidth();
|
|
|
|
ImGui::PopStyleVar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndTable();
|
|
|
|
}
|
|
|
|
ImGui::PopStyleVar();
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
|
|
|
// Draw Custom encoding column
|
2023-03-31 13:49:33 +02:00
|
|
|
if (this->m_showCustomEncoding && this->m_currCustomEncoding.has_value()) {
|
2022-11-06 12:19:12 +01:00
|
|
|
std::vector<std::pair<u64, CustomEncodingData>> encodingData;
|
|
|
|
|
2023-06-21 00:30:26 +02:00
|
|
|
if (this->m_encodingLineStartAddresses.empty()) {
|
|
|
|
this->m_encodingLineStartAddresses.push_back(0);
|
|
|
|
}
|
|
|
|
|
2023-06-21 23:58:21 +02:00
|
|
|
if (y < this->m_encodingLineStartAddresses.size()) {
|
|
|
|
if (this->m_encodingLineStartAddresses[y] >= this->m_bytesPerRow) {
|
|
|
|
encodingData.emplace_back(y * this->m_bytesPerRow + this->m_provider->getBaseAddress() + this->m_provider->getCurrentPageAddress(), CustomEncodingData(".", 1, ImGui::GetCustomColorU32(ImGuiCustomCol_ToolbarRed)));
|
|
|
|
this->m_encodingLineStartAddresses.push_back(0);
|
|
|
|
} else {
|
|
|
|
u32 offset = this->m_encodingLineStartAddresses[y];
|
|
|
|
do {
|
|
|
|
const u64 address = y * this->m_bytesPerRow + offset + this->m_provider->getBaseAddress() + this->m_provider->getCurrentPageAddress();
|
2022-11-06 12:19:12 +01:00
|
|
|
|
2023-06-21 23:58:21 +02:00
|
|
|
auto result = queryCustomEncodingData(this->m_provider, *this->m_currCustomEncoding, address);
|
2023-06-21 00:30:26 +02:00
|
|
|
|
2023-06-21 23:58:21 +02:00
|
|
|
offset += result.advance;
|
|
|
|
encodingData.emplace_back(address, result);
|
|
|
|
} while (offset < this->m_bytesPerRow);
|
2023-06-21 00:30:26 +02:00
|
|
|
|
2023-06-21 23:58:21 +02:00
|
|
|
this->m_encodingLineStartAddresses.push_back(offset - this->m_bytesPerRow);
|
|
|
|
}
|
2022-11-06 12:19:12 +01:00
|
|
|
|
2023-06-21 23:58:21 +02:00
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_CellPadding, ImVec2(0, 0));
|
|
|
|
ImGui::PushID(y);
|
|
|
|
if (ImGui::BeginTable("##encoding_cell", encodingData.size(), ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoKeepColumnsVisible)) {
|
|
|
|
ImGui::TableNextRow();
|
2022-11-06 12:19:12 +01:00
|
|
|
|
2023-06-21 23:58:21 +02:00
|
|
|
for (const auto &[address, data] : encodingData) {
|
|
|
|
ImGui::TableNextColumn();
|
2022-11-06 12:19:12 +01:00
|
|
|
|
2023-06-21 23:58:21 +02:00
|
|
|
const auto cellStartPos = getCellPosition();
|
|
|
|
const auto cellSize = ImGui::CalcTextSize(data.displayValue.c_str()) * ImVec2(1, 0) + ImVec2(this->m_characterCellPadding * 1_scaled, CharacterSize.y);
|
|
|
|
const bool cellHovered = ImGui::IsMouseHoveringRect(cellStartPos, cellStartPos + cellSize, true);
|
2022-11-06 12:19:12 +01:00
|
|
|
|
2023-06-21 23:58:21 +02:00
|
|
|
const auto x = address % this->m_bytesPerRow;
|
|
|
|
if (x < validBytes && isCurrRegionValid(address)) {
|
|
|
|
auto [foregroundColor, backgroundColor] = cellColors[x / bytesPerCell];
|
2022-11-06 12:19:12 +01:00
|
|
|
|
2023-06-21 23:58:21 +02:00
|
|
|
backgroundColor = applySelectionColor(address, backgroundColor);
|
2022-11-06 12:19:12 +01:00
|
|
|
|
2023-06-21 23:58:21 +02:00
|
|
|
// Draw highlights and selection
|
|
|
|
if (backgroundColor.has_value()) {
|
|
|
|
auto drawList = ImGui::GetWindowDrawList();
|
2022-11-06 12:19:12 +01:00
|
|
|
|
2023-06-21 23:58:21 +02:00
|
|
|
// Draw background color
|
|
|
|
drawList->AddRectFilled(cellStartPos, cellStartPos + cellSize, backgroundColor.value());
|
2022-11-06 12:19:12 +01:00
|
|
|
|
2023-06-21 23:58:21 +02:00
|
|
|
this->drawSelectionFrame(x, y, address, 1, cellStartPos, cellSize);
|
|
|
|
}
|
2022-11-06 12:19:12 +01:00
|
|
|
|
2023-06-21 23:58:21 +02:00
|
|
|
auto startPos = ImGui::GetCursorPos();
|
|
|
|
ImGui::TextFormattedColored(data.color, "{}", data.displayValue);
|
|
|
|
ImGui::SetCursorPosX(startPos.x + cellSize.x);
|
|
|
|
ImGui::SameLine(0, 0);
|
|
|
|
ImGui::Dummy({ 0, 0 });
|
2022-11-06 12:19:12 +01:00
|
|
|
|
2023-06-21 23:58:21 +02:00
|
|
|
this->handleSelection(address, data.advance, &bytes[address % this->m_bytesPerRow], cellHovered);
|
|
|
|
}
|
2022-11-06 12:19:12 +01:00
|
|
|
}
|
|
|
|
|
2023-06-21 23:58:21 +02:00
|
|
|
ImGui::EndTable();
|
|
|
|
}
|
|
|
|
ImGui::PopStyleVar();
|
|
|
|
ImGui::PopID();
|
2022-11-06 12:19:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scroll to the cursor if it's either at the top or bottom edge of the screen
|
|
|
|
if (this->m_shouldScrollToSelection && isSelectionValid()) {
|
|
|
|
// Make sure simply clicking on a byte at the edge of the screen won't cause scrolling
|
2022-12-24 15:29:42 +01:00
|
|
|
if ((ImGui::IsMouseDragging(ImGuiMouseButton_Left) && *this->m_selectionStart != *this->m_selectionEnd)) {
|
2022-11-06 12:19:12 +01:00
|
|
|
auto fractionPerLine = 1.0 / (this->m_visibleRowCount + 1);
|
|
|
|
|
|
|
|
if (y == (u64(clipper.DisplayStart) + 3)) {
|
2022-11-08 21:43:22 +01:00
|
|
|
if (i128(this->m_selectionEnd.value() - this->m_provider->getBaseAddress() - this->m_provider->getCurrentPageAddress()) <= (i64(clipper.DisplayStart + 3) * this->m_bytesPerRow)) {
|
2022-11-06 12:19:12 +01:00
|
|
|
this->m_shouldScrollToSelection = false;
|
|
|
|
ImGui::SetScrollHereY(fractionPerLine * 5);
|
|
|
|
}
|
|
|
|
} else if (y == (u64(clipper.DisplayEnd) - 1)) {
|
2022-11-08 21:43:22 +01:00
|
|
|
if (i128(this->m_selectionEnd.value() - this->m_provider->getBaseAddress() - this->m_provider->getCurrentPageAddress()) >= (i64(clipper.DisplayEnd - 2) * this->m_bytesPerRow)) {
|
2022-11-06 12:19:12 +01:00
|
|
|
this->m_shouldScrollToSelection = false;
|
|
|
|
ImGui::SetScrollHereY(fractionPerLine * (this->m_visibleRowCount));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the cursor is off-screen, directly jump to the byte
|
|
|
|
if (this->m_shouldJumpWhenOffScreen) {
|
|
|
|
this->m_shouldJumpWhenOffScreen = false;
|
|
|
|
|
2022-11-08 21:43:22 +01:00
|
|
|
const auto pageAddress = this->m_provider->getCurrentPageAddress() + this->m_provider->getBaseAddress();
|
2022-11-06 12:19:12 +01:00
|
|
|
auto newSelection = getSelection();
|
|
|
|
newSelection.address -= pageAddress;
|
|
|
|
|
|
|
|
if ((newSelection.getStartAddress()) < u64(clipper.DisplayStart * this->m_bytesPerRow))
|
|
|
|
this->jumpToSelection(false);
|
|
|
|
if ((newSelection.getEndAddress()) > u64(clipper.DisplayEnd * this->m_bytesPerRow))
|
|
|
|
this->jumpToSelection(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle jumping to selection
|
|
|
|
if (this->m_shouldJumpToSelection) {
|
|
|
|
this->m_shouldJumpToSelection = false;
|
|
|
|
|
|
|
|
auto newSelection = getSelection();
|
2022-11-08 21:43:22 +01:00
|
|
|
this->m_provider->setCurrentPage(this->m_provider->getPageOfAddress(newSelection.address).value_or(0));
|
2022-11-06 12:19:12 +01:00
|
|
|
|
2022-11-08 21:43:22 +01:00
|
|
|
const auto pageAddress = this->m_provider->getCurrentPageAddress() + this->m_provider->getBaseAddress();
|
2022-11-06 12:19:12 +01:00
|
|
|
auto scrollPos = (static_cast<long double>(newSelection.getStartAddress() - pageAddress) / this->m_bytesPerRow) * CharacterSize.y;
|
|
|
|
bool scrollUpwards = scrollPos < ImGui::GetScrollY();
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this->m_syncScrolling) {
|
|
|
|
if (this->m_shouldUpdateScrollPosition) {
|
|
|
|
this->m_shouldUpdateScrollPosition = false;
|
2022-11-07 00:04:47 +01:00
|
|
|
ImGui::SetScrollY(this->m_scrollPosition);
|
2022-11-06 12:19:12 +01:00
|
|
|
} else {
|
2022-11-07 00:04:47 +01:00
|
|
|
this->m_scrollPosition = ImGui::GetScrollY();
|
2022-11-06 12:19:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndTable();
|
|
|
|
}
|
|
|
|
ImGui::PopStyleVar();
|
2023-03-22 13:11:09 +01:00
|
|
|
|
|
|
|
this->m_shouldScrollToSelection = false;
|
2022-11-06 12:19:12 +01:00
|
|
|
}
|
|
|
|
|
2022-11-08 21:43:22 +01:00
|
|
|
void HexEditor::drawFooter(const ImVec2 &size) {
|
|
|
|
if (this->m_provider != nullptr && this->m_provider->isReadable()) {
|
2023-06-26 11:13:12 +02:00
|
|
|
const auto pageCount = std::max<u32>(1, this->m_provider->getPageCount());
|
2022-11-06 12:19:12 +01:00
|
|
|
constexpr static u32 MinPage = 1;
|
|
|
|
|
2022-11-08 21:43:22 +01:00
|
|
|
const auto windowEndPos = ImGui::GetWindowPos() + size - ImGui::GetStyle().WindowPadding;
|
2022-11-06 12:19:12 +01:00
|
|
|
ImGui::GetWindowDrawList()->AddLine(windowEndPos - ImVec2(0, size.y - 1_scaled), windowEndPos - size + ImVec2(0, 1_scaled), ImGui::GetColorU32(ImGuiCol_Separator), 2.0_scaled);
|
|
|
|
|
|
|
|
if (ImGui::BeginChild("##footer", size, false, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse)) {
|
|
|
|
if (ImGui::BeginTable("##footer_table", 2)) {
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
|
|
|
|
// Page slider
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
{
|
2022-11-08 21:43:22 +01:00
|
|
|
u32 page = this->m_provider->getCurrentPage() + 1;
|
2022-11-06 12:19:12 +01:00
|
|
|
|
2022-11-09 19:03:57 +01:00
|
|
|
ImGui::TextFormatted("{}: ", "hex.builtin.hex_editor.page"_lang);
|
2022-11-06 12:19:12 +01:00
|
|
|
ImGui::SameLine();
|
|
|
|
|
|
|
|
ImGui::BeginDisabled(pageCount <= 1);
|
|
|
|
{
|
2023-01-28 14:15:15 +01:00
|
|
|
if (ImGui::SliderScalar("##page_selection", ImGuiDataType_U32, &page, &MinPage, &pageCount, hex::format("0x%02llX / 0x{:02X}", pageCount).c_str()))
|
2022-11-08 21:43:22 +01:00
|
|
|
this->m_provider->setCurrentPage(page - 1);
|
2022-11-06 12:19:12 +01:00
|
|
|
}
|
|
|
|
ImGui::EndDisabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Page Address
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
{
|
2023-05-23 11:34:30 +02:00
|
|
|
auto pageAddress = this->m_provider->getCurrentPageAddress();
|
2023-08-13 17:08:17 +02:00
|
|
|
ImGui::TextFormatted("{}:", "hex.builtin.hex_editor.region"_lang);
|
|
|
|
ImGui::SameLine();
|
|
|
|
ImGui::TextFormattedSelectable("0x{0:08X} - 0x{1:08X} ({0} - {1})",
|
|
|
|
pageAddress,
|
|
|
|
pageAddress + this->m_provider->getSize() - 1
|
|
|
|
);
|
2022-11-06 12:19:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
|
|
|
|
// Selection
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
{
|
|
|
|
auto selection = getSelection();
|
|
|
|
std::string value;
|
|
|
|
if (isSelectionValid()) {
|
|
|
|
value = hex::format("0x{0:08X} - 0x{1:08X} (0x{2:X} | {3})",
|
|
|
|
selection.getStartAddress(),
|
|
|
|
selection.getEndAddress(),
|
|
|
|
selection.getSize(),
|
2023-08-13 19:08:09 +02:00
|
|
|
this->m_showHumanReadableUnits
|
|
|
|
? hex::toByteString(selection.getSize())
|
|
|
|
: hex::format("{}", selection.getSize())
|
2022-11-06 12:19:12 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
2022-11-09 19:03:57 +01:00
|
|
|
value = std::string("hex.builtin.hex_editor.selection.none"_lang);
|
2022-11-06 12:19:12 +01:00
|
|
|
|
2023-08-13 17:08:17 +02:00
|
|
|
ImGui::TextFormatted("{}:", "hex.builtin.hex_editor.selection"_lang);
|
|
|
|
ImGui::SameLine();
|
|
|
|
ImGui::TextFormattedSelectable(value);
|
2022-11-06 12:19:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Loaded data size
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
{
|
2023-08-13 17:08:17 +02:00
|
|
|
ImGui::TextFormatted("{}:", "hex.builtin.hex_editor.data_size"_lang);
|
|
|
|
ImGui::SameLine();
|
|
|
|
ImGui::TextFormattedSelectable("0x{0:08X} (0x{1:X} | {2})",
|
|
|
|
this->m_provider->getActualSize(),
|
|
|
|
this->m_provider->getActualSize(),
|
2023-08-13 19:08:09 +02:00
|
|
|
this->m_showHumanReadableUnits
|
|
|
|
? hex::toByteString(this->m_provider->getActualSize())
|
|
|
|
: hex::format("{}", this->m_provider->getActualSize())
|
2022-11-06 12:19:12 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-05 09:07:58 +02:00
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + 2_scaled);
|
|
|
|
|
|
|
|
// Upper/lower case hex
|
2023-07-23 09:14:00 +02:00
|
|
|
ImGui::DimmedIconToggle(ICON_VS_CASE_SENSITIVE, &this->m_upperCaseHex);
|
|
|
|
ImGui::InfoTooltip("hex.builtin.hex_editor.uppercase_hex"_lang);
|
2023-06-05 09:07:58 +02:00
|
|
|
|
|
|
|
ImGui::SameLine();
|
|
|
|
|
|
|
|
// Grayed out zeros
|
2023-07-23 09:14:00 +02:00
|
|
|
ImGui::DimmedIconToggle(ICON_VS_LIGHTBULB, &this->m_grayOutZero);
|
|
|
|
ImGui::InfoTooltip("hex.builtin.hex_editor.gray_out_zero"_lang);
|
2023-06-05 09:07:58 +02:00
|
|
|
|
|
|
|
ImGui::SameLine();
|
|
|
|
|
|
|
|
// ASCII view
|
2023-07-23 09:14:00 +02:00
|
|
|
ImGui::DimmedIconToggle(ICON_VS_SYMBOL_KEY, &this->m_showAscii);
|
|
|
|
ImGui::InfoTooltip("hex.builtin.hex_editor.ascii_view"_lang);
|
2023-06-05 09:07:58 +02:00
|
|
|
|
|
|
|
ImGui::SameLine(0, 1_scaled);
|
|
|
|
|
|
|
|
// Custom encoding view
|
2023-07-23 09:14:00 +02:00
|
|
|
ImGui::BeginDisabled(!this->m_currCustomEncoding.has_value());
|
|
|
|
ImGui::DimmedIconToggle(ICON_VS_WHITESPACE, &this->m_showCustomEncoding);
|
|
|
|
ImGui::EndDisabled();
|
2023-06-05 09:07:58 +02:00
|
|
|
|
2023-07-23 09:14:00 +02:00
|
|
|
ImGui::InfoTooltip("hex.builtin.hex_editor.custom_encoding_view"_lang);
|
2023-06-05 09:07:58 +02:00
|
|
|
|
2023-08-13 19:08:09 +02:00
|
|
|
ImGui::SameLine();
|
|
|
|
|
|
|
|
// Human-readable units
|
|
|
|
ImGui::DimmedIconToggle(ICON_VS_SYMBOL_NUMERIC, &this->m_showHumanReadableUnits);
|
|
|
|
ImGui::InfoTooltip("hex.builtin.hex_editor.human_readable_units_footer"_lang);
|
|
|
|
|
2023-06-05 09:07:58 +02:00
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
|
|
|
// Visualizer
|
|
|
|
{
|
|
|
|
auto &visualizers = ContentRegistry::HexEditor::impl::getVisualizers();
|
|
|
|
|
|
|
|
ImGui::TextFormatted("{}: ", "hex.builtin.hex_editor.visualizer"_lang);
|
2023-07-16 19:52:48 +02:00
|
|
|
|
2023-06-05 09:07:58 +02:00
|
|
|
ImGui::SameLine(0, 0);
|
2023-07-16 19:52:48 +02:00
|
|
|
|
2023-07-18 14:23:02 +02:00
|
|
|
{
|
|
|
|
bool hasEndianess = this->m_currDataVisualizer->getBytesPerCell() > 1;
|
2023-07-20 20:58:28 +02:00
|
|
|
|
2023-07-18 14:23:02 +02:00
|
|
|
if (!hasEndianess)
|
|
|
|
this->m_dataVisualizerEndianness = std::endian::native;
|
|
|
|
|
|
|
|
ImGui::BeginDisabled(!hasEndianess);
|
|
|
|
{
|
|
|
|
int sliderPos = this->m_dataVisualizerEndianness == std::endian::little ? 0 : 1;
|
|
|
|
ImGui::PushItemWidth(60_scaled);
|
|
|
|
ImGui::SliderInt("##visualizer_endianness", &sliderPos, 0, 1, sliderPos == 0 ? "hex.builtin.common.little"_lang : "hex.builtin.common.big"_lang);
|
|
|
|
ImGui::PopItemWidth();
|
|
|
|
this->m_dataVisualizerEndianness = sliderPos == 0 ? std::endian::little : std::endian::big;
|
|
|
|
}
|
|
|
|
ImGui::EndDisabled();
|
|
|
|
}
|
2023-07-16 19:52:48 +02:00
|
|
|
|
|
|
|
ImGui::SameLine(0, 2_scaled);
|
2023-07-20 20:58:28 +02:00
|
|
|
ImGui::PushItemWidth((ImGui::GetContentRegionAvail().x / 3) * 2);
|
2023-06-05 09:07:58 +02:00
|
|
|
if (ImGui::BeginCombo("##visualizer", LangEntry(this->m_currDataVisualizer->getUnlocalizedName()))) {
|
|
|
|
|
2023-06-21 17:48:51 +02:00
|
|
|
for (const auto &visualizer : visualizers) {
|
|
|
|
if (ImGui::Selectable(LangEntry(visualizer->getUnlocalizedName()))) {
|
2023-06-05 09:07:58 +02:00
|
|
|
this->m_currDataVisualizer = visualizer;
|
2023-07-20 20:58:28 +02:00
|
|
|
this->m_encodingLineStartAddresses.clear();
|
|
|
|
|
|
|
|
if (this->m_bytesPerRow < visualizer->getBytesPerCell())
|
|
|
|
this->m_bytesPerRow = visualizer->getBytesPerCell();
|
2023-06-05 09:07:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndCombo();
|
|
|
|
}
|
|
|
|
ImGui::PopItemWidth();
|
2023-07-20 20:58:28 +02:00
|
|
|
|
|
|
|
ImGui::SameLine(0, 2_scaled);
|
|
|
|
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
|
|
|
|
int bytesPerRow = this->m_bytesPerRow / this->getBytesPerCell();
|
|
|
|
if (ImGui::SliderInt("##row_size", &bytesPerRow, 1, 32 / this->getBytesPerCell(), hex::format("{}", bytesPerRow * this->getBytesPerCell()).c_str())) {
|
|
|
|
this->m_bytesPerRow = bytesPerRow * this->getBytesPerCell();
|
|
|
|
this->m_encodingLineStartAddresses.clear();
|
2023-09-02 17:52:05 +02:00
|
|
|
|
|
|
|
ContentRegistry::Settings::write("hex.builtin.setting.hex_editor", "hex.builtin.setting.hex_editor.bytes_per_row", this->m_bytesPerRow);
|
2023-07-20 20:58:28 +02:00
|
|
|
}
|
|
|
|
ImGui::PopItemWidth();
|
2023-06-05 09:07:58 +02:00
|
|
|
}
|
|
|
|
|
2022-11-06 12:19:12 +01:00
|
|
|
ImGui::EndTable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ImGui::EndChild();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HexEditor::handleSelection(u64 address, u32 bytesPerCell, const u8 *data, bool cellHovered) {
|
|
|
|
if (ImGui::IsWindowHovered() && cellHovered) {
|
|
|
|
drawTooltip(address, data, bytesPerCell);
|
|
|
|
|
|
|
|
auto endAddress = address + bytesPerCell - 1;
|
2022-11-07 00:04:47 +01:00
|
|
|
auto &selectionStart = this->m_selectionStart;
|
2022-11-06 12:19:12 +01:00
|
|
|
|
|
|
|
if (ImGui::IsMouseDragging(ImGuiMouseButton_Left)) {
|
|
|
|
this->setSelection(selectionStart.value_or(address), endAddress);
|
|
|
|
this->scrollToSelection();
|
|
|
|
}
|
2022-12-18 13:27:28 +01:00
|
|
|
else if (ImGui::IsMouseClicked(ImGuiMouseButton_Left)) {
|
2022-11-06 12:19:12 +01:00
|
|
|
if (ImGui::GetIO().KeyShift)
|
|
|
|
this->setSelection(selectionStart.value_or(address), endAddress);
|
|
|
|
else
|
|
|
|
this->setSelection(address, endAddress);
|
|
|
|
|
|
|
|
this->scrollToSelection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 21:43:22 +01:00
|
|
|
void HexEditor::draw(float height) {
|
2022-11-07 00:04:47 +01:00
|
|
|
const auto width = ImGui::GetContentRegionAvail().x;
|
|
|
|
|
2023-07-20 20:58:28 +02:00
|
|
|
auto FooterSize = ImVec2(width, ImGui::GetTextLineHeightWithSpacing() * 3.6F);
|
|
|
|
auto TableSize = ImVec2(width, height - FooterSize.y);
|
|
|
|
|
|
|
|
if (TableSize.y <= 0)
|
|
|
|
TableSize.y = height;
|
2022-11-06 12:19:12 +01:00
|
|
|
|
2022-11-08 21:43:22 +01:00
|
|
|
this->drawEditor(TableSize);
|
2023-07-20 20:58:28 +02:00
|
|
|
|
|
|
|
if (TableSize.y > 0)
|
|
|
|
this->drawFooter(FooterSize);
|
2022-11-06 12:19:12 +01:00
|
|
|
|
|
|
|
this->m_selectionChanged = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|