1
0
mirror of synced 2024-12-04 20:17:57 +01:00
ImHex/plugins/builtin/include/ui/hex_editor.hpp

279 lines
9.7 KiB
C++
Raw Normal View History

#pragma once
#include <hex.hpp>
#include <hex/api/imhex_api.hpp>
#include <hex/api/content_registry.hpp>
#include <hex/providers/provider.hpp>
#include <hex/helpers/encoding_file.hpp>
#include <imgui.h>
#include <hex/ui/view.hpp>
namespace hex::plugin::builtin::ui {
class HexEditor {
public:
explicit HexEditor(prv::Provider *provider = nullptr);
~HexEditor();
void draw(float height = ImGui::GetContentRegionAvail().y);
void setProvider(prv::Provider *provider) {
2023-12-19 13:10:25 +01:00
m_provider = provider;
m_currValidRegion = { Region::Invalid(), false };
}
2023-12-19 13:10:25 +01:00
void setUnknownDataCharacter(char character) { m_unknownDataCharacter = character; }
private:
enum class CellType { None, Hex, ASCII };
2023-11-10 20:47:08 +01:00
void drawCell(u64 address, const u8 *data, size_t size, bool hovered, CellType cellType);
void drawSelectionFrame(u32 x, u32 y, Region selection, u64 byteAddress, u16 bytesPerCell, const ImVec2 &cellPos, const ImVec2 &cellSize, const ImColor &backgroundColor) const;
void drawEditor(const ImVec2 &size);
void drawFooter(const ImVec2 &size);
2023-11-10 20:47:08 +01:00
void drawTooltip(u64 address, const u8 *data, size_t size) const;
void handleSelection(u64 address, u32 bytesPerCell, const u8 *data, bool cellHovered);
std::optional<color_t> applySelectionColor(u64 byteAddress, std::optional<color_t> color);
public:
void setSelectionUnchecked(std::optional<u64> start, std::optional<u64> end) {
2023-12-19 13:10:25 +01:00
m_selectionStart = start;
m_selectionEnd = end;
m_cursorPosition = end;
}
void setSelection(const Region &region) { this->setSelection(region.getStartAddress(), region.getEndAddress()); }
void setSelection(u128 start, u128 end) {
if (!ImHexApi::Provider::isValid())
return;
2023-12-19 13:10:25 +01:00
if (start > m_provider->getBaseAddress() + m_provider->getActualSize())
return;
2023-12-19 13:10:25 +01:00
if (start < m_provider->getBaseAddress())
return;
2023-12-19 13:10:25 +01:00
if (m_provider->getActualSize() == 0)
return;
2023-12-19 13:10:25 +01:00
const size_t maxAddress = m_provider->getActualSize() + m_provider->getBaseAddress() - 1;
constexpr static auto alignDown = [](u128 value, u128 alignment) {
return value & ~(alignment - 1);
};
2023-12-19 13:10:25 +01:00
m_selectionChanged = m_selectionStart != start || m_selectionEnd != end;
2023-12-19 13:10:25 +01:00
if (!m_selectionStart.has_value()) m_selectionStart = start;
if (!m_selectionEnd.has_value()) m_selectionEnd = end;
2023-12-19 13:10:25 +01:00
if (auto bytesPerCell = m_currDataVisualizer->getBytesPerCell(); bytesPerCell > 1) {
if (end > start) {
start = alignDown(start, bytesPerCell);
end = alignDown(end, bytesPerCell) + (bytesPerCell - 1);
} else {
start = alignDown(start, bytesPerCell) + (bytesPerCell - 1);
end = alignDown(end, bytesPerCell);
}
}
2023-12-19 13:10:25 +01:00
m_selectionStart = std::clamp<u128>(start, 0, maxAddress);
m_selectionEnd = std::clamp<u128>(end, 0, maxAddress);
m_cursorPosition = m_selectionEnd;
2023-12-19 13:10:25 +01:00
if (m_selectionChanged) {
auto selection = this->getSelection();
2023-12-19 13:10:25 +01:00
EventRegionSelected::post(ImHexApi::HexEditor::ProviderRegion{ { selection.address, selection.size }, m_provider });
m_shouldModifyValue = true;
}
}
[[nodiscard]] Region getSelection() const {
if (!isSelectionValid())
return Region::Invalid();
2023-12-19 13:10:25 +01:00
const auto start = std::min(m_selectionStart.value(), m_selectionEnd.value());
const auto end = std::max(m_selectionStart.value(), m_selectionEnd.value());
const size_t size = end - start + 1;
return { start, size };
}
[[nodiscard]] std::optional<u64> getCursorPosition() const {
2023-12-19 13:10:25 +01:00
return m_cursorPosition;
}
void setCursorPosition(u64 cursorPosition) {
2023-12-19 13:10:25 +01:00
m_cursorPosition = cursorPosition;
}
[[nodiscard]] bool isSelectionValid() const {
2023-12-19 13:10:25 +01:00
return m_selectionStart.has_value() && m_selectionEnd.has_value();
}
void jumpToSelection(bool center = true) {
2023-12-19 13:10:25 +01:00
m_shouldJumpToSelection = true;
if (center)
2023-12-19 13:10:25 +01:00
m_centerOnJump = true;
}
void scrollToSelection() {
2023-12-19 13:10:25 +01:00
m_shouldScrollToSelection = true;
}
void jumpIfOffScreen() {
2023-12-19 13:10:25 +01:00
m_shouldJumpWhenOffScreen = true;
}
[[nodiscard]] u16 getBytesPerRow() const {
2023-12-19 13:10:25 +01:00
return m_bytesPerRow;
}
[[nodiscard]] u16 getBytesPerCell() const {
2023-12-19 13:10:25 +01:00
return m_currDataVisualizer->getBytesPerCell();
}
void setBytesPerRow(u16 bytesPerRow) {
2023-12-19 13:10:25 +01:00
m_bytesPerRow = bytesPerRow;
}
[[nodiscard]] u16 getVisibleRowCount() const {
2023-12-19 13:10:25 +01:00
return m_visibleRowCount;
}
void setSelectionColor(color_t color) {
2023-12-19 13:10:25 +01:00
m_selectionColor = color;
}
void enableUpperCaseHex(bool upperCaseHex) {
2023-12-19 13:10:25 +01:00
m_upperCaseHex = upperCaseHex;
}
void enableGrayOutZeros(bool grayOutZeros) {
2023-12-19 13:10:25 +01:00
m_grayOutZero = grayOutZeros;
}
void enableShowAscii(bool showAscii) {
2023-12-19 13:10:25 +01:00
m_showAscii = showAscii;
}
void enableShowHumanReadableUnits(bool showHumanReadableUnits) {
2023-12-19 13:10:25 +01:00
m_showHumanReadableUnits = showHumanReadableUnits;
}
void enableSyncScrolling(bool syncScrolling) {
2023-12-19 13:10:25 +01:00
m_syncScrolling = syncScrolling;
}
void setByteCellPadding(u32 byteCellPadding) {
2023-12-19 13:10:25 +01:00
m_byteCellPadding = byteCellPadding;
}
void setCharacterCellPadding(u32 characterCellPadding) {
2023-12-19 13:10:25 +01:00
m_characterCellPadding = characterCellPadding;
}
[[nodiscard]] const std::optional<EncodingFile>& getCustomEncoding() const {
2023-12-19 13:10:25 +01:00
return m_currCustomEncoding;
}
void setCustomEncoding(const EncodingFile &encoding) {
2023-12-19 13:10:25 +01:00
m_currCustomEncoding = encoding;
m_encodingLineStartAddresses.clear();
}
void setCustomEncoding(EncodingFile &&encoding) {
2023-12-19 13:10:25 +01:00
m_currCustomEncoding = std::move(encoding);
m_encodingLineStartAddresses.clear();
}
void forceUpdateScrollPosition() {
2023-12-19 13:10:25 +01:00
m_shouldUpdateScrollPosition = true;
}
void setForegroundHighlightCallback(const std::function<std::optional<color_t>(u64, const u8 *, size_t)> &callback) {
2023-12-19 13:10:25 +01:00
m_foregroundColorCallback = callback;
}
void setBackgroundHighlightCallback(const std::function<std::optional<color_t>(u64, const u8 *, size_t)> &callback) {
2023-12-19 13:10:25 +01:00
m_backgroundColorCallback = callback;
}
void setTooltipCallback(const std::function<void(u64, const u8 *, size_t)> &callback) {
2023-12-19 13:10:25 +01:00
m_tooltipCallback = callback;
}
[[nodiscard]] float getScrollPosition() const {
2023-12-19 13:10:25 +01:00
return m_scrollPosition;
}
void setScrollPosition(float scrollPosition) {
2023-12-19 13:10:25 +01:00
m_scrollPosition = scrollPosition;
}
void setEditingAddress(u64 address) {
2023-12-19 13:10:25 +01:00
m_editingAddress = address;
m_shouldModifyValue = false;
m_enteredEditingMode = true;
2023-12-19 13:10:25 +01:00
m_editingBytes.resize(m_currDataVisualizer->getBytesPerCell());
m_provider->read(address + m_provider->getBaseAddress(), m_editingBytes.data(), m_editingBytes.size());
m_editingCellType = CellType::Hex;
}
void clearEditingAddress() {
2023-12-19 13:10:25 +01:00
m_editingAddress = std::nullopt;
}
private:
prv::Provider *m_provider;
std::optional<u64> m_selectionStart;
std::optional<u64> m_selectionEnd;
std::optional<u64> m_cursorPosition;
float m_scrollPosition = 0;
u16 m_bytesPerRow = 16;
std::endian m_dataVisualizerEndianness = std::endian::little;
std::shared_ptr<ContentRegistry::HexEditor::DataVisualizer> m_currDataVisualizer;
char m_unknownDataCharacter = '?';
bool m_shouldJumpToSelection = false;
bool m_centerOnJump = false;
bool m_shouldScrollToSelection = false;
bool m_shouldJumpWhenOffScreen = false;
bool m_shouldUpdateScrollPosition = false;
bool m_selectionChanged = false;
u16 m_visibleRowCount = 0;
CellType m_editingCellType = CellType::None;
std::optional<u64> m_editingAddress;
bool m_shouldModifyValue = false;
bool m_enteredEditingMode = false;
bool m_shouldUpdateEditingValue = false;
std::vector<u8> m_editingBytes;
color_t m_selectionColor = 0x00;
bool m_upperCaseHex = true;
bool m_grayOutZero = true;
bool m_showAscii = true;
bool m_showCustomEncoding = true;
bool m_showHumanReadableUnits = true;
bool m_syncScrolling = false;
u32 m_byteCellPadding = 0, m_characterCellPadding = 0;
bool m_footerCollapsed = true;
std::optional<EncodingFile> m_currCustomEncoding;
std::vector<u64> m_encodingLineStartAddresses;
std::pair<Region, bool> m_currValidRegion = { Region::Invalid(), false };
2023-11-10 20:47:08 +01:00
static std::optional<color_t> defaultColorCallback(u64, const u8 *, size_t) { return std::nullopt; }
static void defaultTooltipCallback(u64, const u8 *, size_t) { }
std::function<std::optional<color_t>(u64, const u8 *, size_t)> m_foregroundColorCallback = defaultColorCallback, m_backgroundColorCallback = defaultColorCallback;
std::function<void(u64, const u8 *, size_t)> m_tooltipCallback = defaultTooltipCallback;
};
}