update imgui_memory_editor to the upstream version

This commit is contained in:
silverweed 2024-08-21 11:43:48 +02:00
parent fc6f32ceac
commit c170ff8ad2
3 changed files with 112 additions and 119 deletions

View file

@ -118,14 +118,6 @@ u32 mem_edit_bg_color_fn(const u8 *data, u64 off, void *user_data)
return imcol(app->viewer.col_section[section.id], brighten); return imcol(app->viewer.col_section[section.id], brighten);
} }
internal
void mem_edit_interact_fn(const u8 *, u64 off, void *user_data)
{
App_State *app = reinterpret_cast<App_State *>(user_data);
off += app->base_display_addr;
app->viewer.hovered_off = off + 1;
}
internal internal
MemoryEditor make_memory_editor(App_State &app, i32 n_cols) MemoryEditor make_memory_editor(App_State &app, i32 n_cols)
{ {
@ -134,11 +126,9 @@ MemoryEditor make_memory_editor(App_State &app, i32 n_cols)
mem_edit.OptShowDataPreview = true; mem_edit.OptShowDataPreview = true;
// Do nothing on write. // Do nothing on write.
// Note that we don't use ReadOnly = true because that disables selecting bytes // Note that we don't use ReadOnly = true because that disables selecting bytes
mem_edit.WriteFn = [] (ImU8*, size_t, ImU8) {}; mem_edit.WriteFn = [] (ImU8*, size_t, ImU8, void*) {};
mem_edit.BgColorFn = mem_edit_bg_color_fn; mem_edit.BgColorFn = mem_edit_bg_color_fn;
mem_edit.BgColorFnUserData = &app; mem_edit.UserData = &app;
mem_edit.InteractFn = mem_edit_interact_fn;
mem_edit.InteractFnUserData = &app;
return mem_edit; return mem_edit;
} }
@ -286,7 +276,9 @@ void update_and_render(Arena *arena, App_State &app, f32 delta_time_ms)
ImGui::TableNextColumn(); ImGui::TableNextColumn();
app.viewer.hovered_off = 0; // Absolute byte offset into inspected_file.mem that has been hovered last.
// 0 means "invalid", otherwise the actual offset is `hovered_off - 1`.
u64 hovered_off = app.viewer.mem_edit.MouseHovered * (app.viewer.mem_edit.MouseHoveredAddr + 1);
assert(app.base_display_addr < app.inspected_file.size); assert(app.base_display_addr < app.inspected_file.size);
void *content = app.inspected_file.mem + app.base_display_addr; void *content = app.inspected_file.mem + app.base_display_addr;
@ -391,10 +383,10 @@ void update_and_render(Arena *arena, App_State &app, f32 delta_time_ms)
// --------------------------------- // ---------------------------------
ImGui::Separator(); ImGui::Separator();
if (app.viewer.hovered_off) if (hovered_off)
{ {
Section hovered_section = find_section(app, app.viewer.hovered_off - 1); Section hovered_section = find_section(app, hovered_off - 1);
Sec_Hover_Info hover_info = get_section_hover_info(scratch.arena, hovered_section, app.viewer.hovered_off - 1, app.inspected_file.mem); Sec_Hover_Info hover_info = get_section_hover_info(scratch.arena, hovered_section, hovered_off - 1, app.inspected_file.mem);
imgui_render_string_tree(scratch.arena, hover_info.desc); imgui_render_string_tree(scratch.arena, hover_info.desc);
app.viewer.hovered_range = hover_info.rng; app.viewer.hovered_range = hover_info.rng;
} else { } else {

View file

@ -21,9 +21,6 @@ struct Viewer {
u64 latest_checksum_gone_to; u64 latest_checksum_gone_to;
u64 latest_page_list_gone_to; u64 latest_page_list_gone_to;
// Absolute byte offset into inspected_file.mem that has been hovered last.
// 0 means "invalid", otherwise the actual offset is `hovered_off - 1`.
u64 hovered_off;
Byte_Range hovered_range; Byte_Range hovered_range;
}; };

View file

@ -1,6 +1,7 @@
// Mini memory editor for Dear ImGui (to embed in your game/tools) // Mini memory editor for Dear ImGui (to embed in your game/tools)
// Get latest version at http://www.github.com/ocornut/imgui_club // Get latest version at http://www.github.com/ocornut/imgui_club
// // Licensed under The MIT License (MIT)
// Right-click anywhere to access the Options menu! // Right-click anywhere to access the Options menu!
// You can adjust the keyboard repeat delay/rate in ImGuiIO. // You can adjust the keyboard repeat delay/rate in ImGuiIO.
// The code assume a mono-space font for simplicity! // The code assume a mono-space font for simplicity!
@ -42,8 +43,13 @@
// - v0.51 (2024/02/22): fix for layout change in 1.89 when using IMGUI_DISABLE_OBSOLETE_FUNCTIONS. (#34) // - v0.51 (2024/02/22): fix for layout change in 1.89 when using IMGUI_DISABLE_OBSOLETE_FUNCTIONS. (#34)
// - v0.52 (2024/03/08): removed unnecessary GetKeyIndex() calls, they are a no-op since 1.87. // - v0.52 (2024/03/08): removed unnecessary GetKeyIndex() calls, they are a no-op since 1.87.
// - v0.53 (2024/05/27): fixed right-click popup from not appearing when using DrawContents(). warning fixes. (#35) // - v0.53 (2024/05/27): fixed right-click popup from not appearing when using DrawContents(). warning fixes. (#35)
// - v0.54 (2024/07/29): allow ReadOnly mode to still select and preview data. (#46) [@DeltaGW2]
// - v0.55 (2024/08/19): added BgColorFn to allow setting background colors independently from highlighted selection. (#27) [@StrikerX3]
// added MouseHoveredAddr public readable field. (#47, #27) [@StrikerX3]
// fixed a data preview crash with 1.91.0 WIP. fixed contiguous highlight color when using data preview.
// *BREAKING* added UserData field passed to all optional function handlers: ReadFn, WriteFn, HighlightFn, BgColorFn. (#50) [@silverweed]
// //
// Todo/Bugs: // TODO:
// - This is generally old/crappy code, it should work but isn't very good.. to be rewritten some day. // - This is generally old/crappy code, it should work but isn't very good.. to be rewritten some day.
// - PageUp/PageDown are not supported because we use _NoNav. This is a good test scenario for working out idioms of how to mix natural nav and our own... // - PageUp/PageDown are not supported because we use _NoNav. This is a good test scenario for working out idioms of how to mix natural nav and our own...
// - Arrows are being sent to the InputText() about to disappear which for LeftArrow makes the text cursor appear at position 1 for one frame. // - Arrows are being sent to the InputText() about to disappear which for LeftArrow makes the text cursor appear at position 1 for one frame.
@ -91,13 +97,17 @@ struct MemoryEditor
int OptAddrDigitsCount; // = 0 // number of addr digits to display (default calculated based on maximum displayed addr). int OptAddrDigitsCount; // = 0 // number of addr digits to display (default calculated based on maximum displayed addr).
float OptFooterExtraHeight; // = 0 // space to reserve at the bottom of the widget to add custom widgets float OptFooterExtraHeight; // = 0 // space to reserve at the bottom of the widget to add custom widgets
ImU32 HighlightColor; // // background color of highlighted bytes. ImU32 HighlightColor; // // background color of highlighted bytes.
ImU8 (*ReadFn)(const ImU8* data, size_t off); // = 0 // optional handler to read bytes.
void (*WriteFn)(ImU8* data, size_t off, ImU8 d); // = 0 // optional handler to write bytes. // Function handlers
bool (*HighlightFn)(const ImU8* data, size_t off);//= 0 // optional handler to return Highlight property (to support non-contiguous highlighting). ImU8 (*ReadFn)(const ImU8* data, size_t off, void* user_data); // = 0 // optional handler to read bytes.
ImU32 (*BgColorFn)(const ImU8* data, size_t off, void *UserData); // = 0 // optional handler to return custom background color of individual bytes. void (*WriteFn)(ImU8* data, size_t off, ImU8 d, void* user_data); // = 0 // optional handler to write bytes.
void *BgColorFnUserData; bool (*HighlightFn)(const ImU8* data, size_t off, void* user_data);// = 0 // optional handler to return Highlight property (to support non-contiguous highlighting).
void (*InteractFn)(const ImU8* data, size_t off, void *UserData);// = 0 // optional handler to interact with individual bytes using ImGui::IsItemHovered, ImGui::IsItemClicked, etc. ImU32 (*BgColorFn)(const ImU8* data, size_t off, void* user_data); // = 0 // optional handler to return custom background color of individual bytes.
void *InteractFnUserData; void* UserData; // = NULL // user data forwarded to the function handlers
// Public read-only data
bool MouseHovered; // set when mouse is hovering a value.
size_t MouseHoveredAddr; // the address currently being hovered if MouseHovered is set.
// [Internal State] // [Internal State]
bool ContentsWidthChanged; bool ContentsWidthChanged;
@ -127,13 +137,11 @@ struct MemoryEditor
OptAddrDigitsCount = 0; OptAddrDigitsCount = 0;
OptFooterExtraHeight = 0.0f; OptFooterExtraHeight = 0.0f;
HighlightColor = IM_COL32(255, 255, 255, 50); HighlightColor = IM_COL32(255, 255, 255, 50);
ReadFn = NULL; ReadFn = nullptr;
WriteFn = NULL; WriteFn = nullptr;
HighlightFn = NULL; HighlightFn = nullptr;
BgColorFn = NULL; BgColorFn = nullptr;
BgColorFnUserData = NULL; UserData = nullptr;
InteractFn = NULL;
InteractFnUserData = NULL;
// State/Internals // State/Internals
ContentsWidthChanged = false; ContentsWidthChanged = false;
@ -142,6 +150,8 @@ struct MemoryEditor
memset(DataInputBuf, 0, sizeof(DataInputBuf)); memset(DataInputBuf, 0, sizeof(DataInputBuf));
memset(AddrInputBuf, 0, sizeof(AddrInputBuf)); memset(AddrInputBuf, 0, sizeof(AddrInputBuf));
GotoAddr = (size_t)-1; GotoAddr = (size_t)-1;
MouseHovered = false;
MouseHoveredAddr = 0;
HighlightMin = HighlightMax = (size_t)-1; HighlightMin = HighlightMax = (size_t)-1;
PreviewEndianness = 0; PreviewEndianness = 0;
PreviewDataType = ImGuiDataType_S32; PreviewDataType = ImGuiDataType_S32;
@ -243,13 +253,13 @@ struct MemoryEditor
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0)); ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0));
// We are not really using the clipper API correctly here, because we rely on visible_start_addr/visible_end_addr for our scrolling function. // We are not really using the clipper API correctly here, because we rely on visible_start_addr/visible_end_addr for our scrolling function.
const size_t line_total_count = (size_t)((mem_size + Cols - 1) / Cols); const int line_total_count = (int)((mem_size + Cols - 1) / Cols);
ImGuiListClipper clipper; ImGuiListClipper clipper;
clipper.Begin(line_total_count, s.LineHeight); clipper.Begin(line_total_count, s.LineHeight);
bool data_next = false; bool data_next = false;
if (ReadOnly || DataEditingAddr >= mem_size) if (DataEditingAddr >= mem_size)
DataEditingAddr = (size_t)-1; DataEditingAddr = (size_t)-1;
if (DataPreviewAddr >= mem_size) if (DataPreviewAddr >= mem_size)
DataPreviewAddr = (size_t)-1; DataPreviewAddr = (size_t)-1;
@ -279,16 +289,13 @@ struct MemoryEditor
const char* format_byte = OptUpperCaseHex ? "%02X" : "%02x"; const char* format_byte = OptUpperCaseHex ? "%02X" : "%02x";
const char* format_byte_space = OptUpperCaseHex ? "%02X " : "%02x "; const char* format_byte_space = OptUpperCaseHex ? "%02X " : "%02x ";
// Disallow interacting with multiple bytes simultaneously. MouseHovered = false;
// This is needed because consecutive hex cells overlap each other by 1 pixel. MouseHoveredAddr = 0;
bool interact_invoked = false;
size_t addr;
while (clipper.Step()) while (clipper.Step())
{
for (int line_i = clipper.DisplayStart; line_i < clipper.DisplayEnd; line_i++) // display only visible lines for (int line_i = clipper.DisplayStart; line_i < clipper.DisplayEnd; line_i++) // display only visible lines
{ {
addr = (size_t)(line_i) * Cols; size_t addr = (size_t)line_i * Cols;
ImGui::Text(format_address, s.AddrDigitsCount, base_display_addr + addr); ImGui::Text(format_address, s.AddrDigitsCount, base_display_addr + addr);
// Draw Hexadecimal // Draw Hexadecimal
@ -299,35 +306,34 @@ struct MemoryEditor
byte_pos_x += (float)(n / OptMidColsCount) * s.SpacingBetweenMidCols; byte_pos_x += (float)(n / OptMidColsCount) * s.SpacingBetweenMidCols;
ImGui::SameLine(byte_pos_x); ImGui::SameLine(byte_pos_x);
// Draw highlight // Draw highlight or custom background color
bool is_highlight_from_user_range = (addr >= HighlightMin && addr < HighlightMax); const bool is_highlight_from_user_range = (addr >= HighlightMin && addr < HighlightMax);
bool is_highlight_from_user_func = (HighlightFn && HighlightFn(mem_data, addr)); const bool is_highlight_from_user_func = (HighlightFn && HighlightFn(mem_data, addr, UserData));
bool is_highlight_from_preview = (addr >= DataPreviewAddr && addr < DataPreviewAddr + preview_data_type_size); const bool is_highlight_from_preview = (addr >= DataPreviewAddr && addr < DataPreviewAddr + preview_data_type_size);
ImU32 bg_color = 0;
bool is_next_byte_highlighted = false;
if (is_highlight_from_user_range || is_highlight_from_user_func || is_highlight_from_preview) if (is_highlight_from_user_range || is_highlight_from_user_func || is_highlight_from_preview)
{ {
ImVec2 pos = ImGui::GetCursorScreenPos(); is_next_byte_highlighted = (addr + 1 < mem_size) && ((HighlightMax != (size_t)-1 && addr + 1 < HighlightMax) || (HighlightFn && HighlightFn(mem_data, addr + 1, UserData)) || (addr + 1 < DataPreviewAddr + preview_data_type_size));
float highlight_width = s.GlyphWidth * 2; bg_color = HighlightColor;
bool is_next_byte_highlighted = (addr + 1 < mem_size) && ((HighlightMax != (size_t)-1 && addr + 1 < HighlightMax) || (HighlightFn && HighlightFn(mem_data, addr + 1)));
if (is_next_byte_highlighted || (n + 1 == Cols))
{
highlight_width = s.HexCellWidth;
if (OptMidColsCount > 0 && n > 0 && (n + 1) < Cols && ((n + 1) % OptMidColsCount) == 0)
highlight_width += s.SpacingBetweenMidCols;
}
draw_list->AddRectFilled(pos, ImVec2(pos.x + highlight_width, pos.y + s.LineHeight), HighlightColor);
} }
else if (BgColorFn) else if (BgColorFn != nullptr)
{ {
ImVec2 pos = ImGui::GetCursorScreenPos(); is_next_byte_highlighted = (addr + 1 < mem_size) && ((BgColorFn(mem_data, addr + 1, UserData) & IM_COL32_A_MASK) != 0);
float highlight_width = s.GlyphWidth * 2; bg_color = BgColorFn(mem_data, addr, UserData);
bool is_next_byte_highlighted = (addr + 1 < mem_size) && ((BgColorFn(mem_data, addr + 1, BgColorFnUserData) & IM_COL32_A_MASK) != 0); }
if (bg_color != 0)
{
float bg_width = s.GlyphWidth * 2;
if (is_next_byte_highlighted || (n + 1 == Cols)) if (is_next_byte_highlighted || (n + 1 == Cols))
{ {
highlight_width = s.HexCellWidth; bg_width = s.HexCellWidth;
if (OptMidColsCount > 0 && n > 0 && (n + 1) < Cols && ((n + 1) % OptMidColsCount) == 0) if (OptMidColsCount > 0 && n > 0 && (n + 1) < Cols && ((n + 1) % OptMidColsCount) == 0)
highlight_width += s.SpacingBetweenMidCols; bg_width += s.SpacingBetweenMidCols;
} }
draw_list->AddRectFilled(pos, ImVec2(pos.x + highlight_width, pos.y + s.LineHeight), BgColorFn(mem_data, addr, BgColorFnUserData)); ImVec2 pos = ImGui::GetCursorScreenPos();
draw_list->AddRectFilled(pos, ImVec2(pos.x + bg_width, pos.y + s.LineHeight), bg_color);
} }
if (DataEditingAddr == addr) if (DataEditingAddr == addr)
@ -339,14 +345,14 @@ struct MemoryEditor
{ {
ImGui::SetKeyboardFocusHere(0); ImGui::SetKeyboardFocusHere(0);
ImSnprintf(AddrInputBuf, 32, format_data, s.AddrDigitsCount, base_display_addr + addr); ImSnprintf(AddrInputBuf, 32, format_data, s.AddrDigitsCount, base_display_addr + addr);
ImSnprintf(DataInputBuf, 32, format_byte, ReadFn ? ReadFn(mem_data, addr) : mem_data[addr]); ImSnprintf(DataInputBuf, 32, format_byte, ReadFn ? ReadFn(mem_data, addr, UserData) : mem_data[addr]);
} }
struct UserData struct InputTextUserData
{ {
// FIXME: We should have a way to retrieve the text edit cursor position more easily in the API, this is rather tedious. This is such a ugly mess we may be better off not using InputText() at all here. // FIXME: We should have a way to retrieve the text edit cursor position more easily in the API, this is rather tedious. This is such a ugly mess we may be better off not using InputText() at all here.
static int Callback(ImGuiInputTextCallbackData* data) static int Callback(ImGuiInputTextCallbackData* data)
{ {
UserData* user_data = (UserData*)data->UserData; InputTextUserData* user_data = (InputTextUserData*)data->UserData;
if (!data->HasSelection()) if (!data->HasSelection())
user_data->CursorPos = data->CursorPos; user_data->CursorPos = data->CursorPos;
if (data->SelectionStart == 0 && data->SelectionEnd == data->BufTextLen) if (data->SelectionStart == 0 && data->SelectionEnd == data->BufTextLen)
@ -364,26 +370,28 @@ struct MemoryEditor
char CurrentBufOverwrite[3]; // Input char CurrentBufOverwrite[3]; // Input
int CursorPos; // Output int CursorPos; // Output
}; };
UserData user_data; InputTextUserData input_text_user_data;
user_data.CursorPos = -1; input_text_user_data.CursorPos = -1;
ImSnprintf(user_data.CurrentBufOverwrite, 3, format_byte, ReadFn ? ReadFn(mem_data, addr) : mem_data[addr]); ImSnprintf(input_text_user_data.CurrentBufOverwrite, 3, format_byte, ReadFn ? ReadFn(mem_data, addr, UserData) : mem_data[addr]);
ImGuiInputTextFlags flags = ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_NoHorizontalScroll | ImGuiInputTextFlags_CallbackAlways; ImGuiInputTextFlags flags = ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_NoHorizontalScroll | ImGuiInputTextFlags_CallbackAlways;
if (ReadOnly)
flags |= ImGuiInputTextFlags_ReadOnly;
flags |= ImGuiInputTextFlags_AlwaysOverwrite; // was ImGuiInputTextFlags_AlwaysInsertMode flags |= ImGuiInputTextFlags_AlwaysOverwrite; // was ImGuiInputTextFlags_AlwaysInsertMode
ImGui::SetNextItemWidth(s.GlyphWidth * 2); ImGui::SetNextItemWidth(s.GlyphWidth * 2);
if (ImGui::InputText("##data", DataInputBuf, IM_ARRAYSIZE(DataInputBuf), flags, UserData::Callback, &user_data)) if (ImGui::InputText("##data", DataInputBuf, IM_ARRAYSIZE(DataInputBuf), flags, InputTextUserData::Callback, &input_text_user_data))
data_write = data_next = true; data_write = data_next = true;
else if (!DataEditingTakeFocus && !ImGui::IsItemActive()) else if (!DataEditingTakeFocus && !ImGui::IsItemActive())
DataEditingAddr = data_editing_addr_next = (size_t)-1; DataEditingAddr = data_editing_addr_next = (size_t)-1;
DataEditingTakeFocus = false; DataEditingTakeFocus = false;
if (user_data.CursorPos >= 2) if (input_text_user_data.CursorPos >= 2)
data_write = data_next = true; data_write = data_next = true;
if (data_editing_addr_next != (size_t)-1) if (data_editing_addr_next != (size_t)-1)
data_write = data_next = false; data_write = data_next = false;
unsigned int data_input_value = 0; unsigned int data_input_value = 0;
if (data_write && sscanf(DataInputBuf, "%X", &data_input_value) == 1) if (!ReadOnly && data_write && sscanf(DataInputBuf, "%X", &data_input_value) == 1)
{ {
if (WriteFn) if (WriteFn)
WriteFn(mem_data, addr, (ImU8)data_input_value); WriteFn(mem_data, addr, (ImU8)data_input_value, UserData);
else else
mem_data[addr] = (ImU8)data_input_value; mem_data[addr] = (ImU8)data_input_value;
} }
@ -392,7 +400,7 @@ struct MemoryEditor
else else
{ {
// NB: The trailing space is not visible but ensure there's no gap that the mouse cannot click on. // NB: The trailing space is not visible but ensure there's no gap that the mouse cannot click on.
ImU8 b = ReadFn ? ReadFn(mem_data, addr) : mem_data[addr]; ImU8 b = ReadFn ? ReadFn(mem_data, addr, UserData) : mem_data[addr];
if (OptShowHexII) if (OptShowHexII)
{ {
@ -412,21 +420,14 @@ struct MemoryEditor
else else
ImGui::Text(format_byte_space, b); ImGui::Text(format_byte_space, b);
} }
if (!ReadOnly && ImGui::IsItemHovered() && ImGui::IsMouseClicked(0)) if (ImGui::IsItemHovered())
{ {
DataEditingTakeFocus = true; MouseHovered = true;
data_editing_addr_next = addr; MouseHoveredAddr = addr;
} if (ImGui::IsMouseClicked(0))
else if (InteractFn && ImGui::IsItemHovered())
{
if (!interact_invoked)
{ {
// Revert padding/spacing to let users draw popups/windows without interference DataEditingTakeFocus = true;
ImGui::PopStyleVar(2); data_editing_addr_next = addr;
InteractFn(mem_data, addr, InteractFnUserData);
interact_invoked = true;
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0));
} }
} }
} }
@ -438,28 +439,20 @@ struct MemoryEditor
ImGui::SameLine(s.PosAsciiStart); ImGui::SameLine(s.PosAsciiStart);
ImVec2 pos = ImGui::GetCursorScreenPos(); ImVec2 pos = ImGui::GetCursorScreenPos();
addr = (size_t)line_i * Cols; addr = (size_t)line_i * Cols;
float mposx = ImGui::GetIO().MousePos.x;
float posdiff = mposx - pos.x; const float mouse_off_x = ImGui::GetIO().MousePos.x - pos.x;
if (posdiff < 0) const size_t mouse_addr = (mouse_off_x >= 0.0f && mouse_off_x < s.PosAsciiEnd - s.PosAsciiStart) ? addr + (size_t)(mouse_off_x / s.GlyphWidth) : (size_t)-1;
posdiff = 0;
size_t mouseAddr = addr + (size_t)(posdiff / s.GlyphWidth);
ImGui::PushID(line_i); ImGui::PushID(line_i);
if (ImGui::InvisibleButton("ascii", ImVec2(s.PosAsciiEnd - s.PosAsciiStart, s.LineHeight))) if (ImGui::InvisibleButton("ascii", ImVec2(s.PosAsciiEnd - s.PosAsciiStart, s.LineHeight)))
{ {
DataEditingAddr = DataPreviewAddr = mouseAddr; DataEditingAddr = DataPreviewAddr = mouse_addr;
DataEditingTakeFocus = true; DataEditingTakeFocus = true;
} }
if (InteractFn && ImGui::IsItemHovered()) if (ImGui::IsItemHovered())
{ {
if (!interact_invoked) MouseHovered = true;
{ MouseHoveredAddr = mouse_addr;
// Revert padding/spacing to let users draw popups/windows without interference
ImGui::PopStyleVar(2);
InteractFn(mem_data, mouseAddr, InteractFnUserData);
interact_invoked = true;
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0));
}
} }
ImGui::PopID(); ImGui::PopID();
for (int n = 0; n < Cols && addr < mem_size; n++, addr++) for (int n = 0; n < Cols && addr < mem_size; n++, addr++)
@ -471,17 +464,15 @@ struct MemoryEditor
} }
else if (BgColorFn) else if (BgColorFn)
{ {
draw_list->AddRectFilled(pos, ImVec2(pos.x + s.GlyphWidth, pos.y + s.LineHeight), BgColorFn(mem_data, addr, BgColorFnUserData)); draw_list->AddRectFilled(pos, ImVec2(pos.x + s.GlyphWidth, pos.y + s.LineHeight), BgColorFn(mem_data, addr, UserData));
} }
unsigned char c = ReadFn ? ReadFn(mem_data, addr) : mem_data[addr]; unsigned char c = ReadFn ? ReadFn(mem_data, addr, UserData) : mem_data[addr];
char display_c = (c < 32 || c >= 128) ? '.' : c; char display_c = (c < 32 || c >= 128) ? '.' : c;
draw_list->AddText(pos, (display_c == c) ? color_text : color_disabled, &display_c, &display_c + 1); draw_list->AddText(pos, (display_c == c) ? color_text : color_disabled, &display_c, &display_c + 1);
pos.x += s.GlyphWidth; pos.x += s.GlyphWidth;
} }
} }
} }
}
ImGui::PopStyleVar(2); ImGui::PopStyleVar(2);
const float child_width = ImGui::GetWindowSize().x; const float child_width = ImGui::GetWindowSize().x;
ImGui::EndChild(); ImGui::EndChild();
@ -571,6 +562,12 @@ struct MemoryEditor
} }
GotoAddr = (size_t)-1; GotoAddr = (size_t)-1;
} }
//if (MouseHovered)
//{
// ImGui::SameLine();
// ImGui::Text("Hovered: %p", MouseHoveredAddr);
//}
} }
void DrawPreviewLine(const Sizes& s, void* mem_data_void, size_t mem_size, size_t base_display_addr) void DrawPreviewLine(const Sizes& s, void* mem_data_void, size_t mem_size, size_t base_display_addr)
@ -582,11 +579,16 @@ struct MemoryEditor
ImGui::Text("Preview as:"); ImGui::Text("Preview as:");
ImGui::SameLine(); ImGui::SameLine();
ImGui::SetNextItemWidth((s.GlyphWidth * 10.0f) + style.FramePadding.x * 2.0f + style.ItemInnerSpacing.x); ImGui::SetNextItemWidth((s.GlyphWidth * 10.0f) + style.FramePadding.x * 2.0f + style.ItemInnerSpacing.x);
static const ImGuiDataType supported_data_types[] = { ImGuiDataType_S8, ImGuiDataType_U8, ImGuiDataType_S16, ImGuiDataType_U16, ImGuiDataType_S32, ImGuiDataType_U32, ImGuiDataType_S64, ImGuiDataType_U64, ImGuiDataType_Float, ImGuiDataType_Double };
if (ImGui::BeginCombo("##combo_type", DataTypeGetDesc(PreviewDataType), ImGuiComboFlags_HeightLargest)) if (ImGui::BeginCombo("##combo_type", DataTypeGetDesc(PreviewDataType), ImGuiComboFlags_HeightLargest))
{ {
for (int n = 0; n < ImGuiDataType_COUNT; n++) for (int n = 0; n < IM_ARRAYSIZE(supported_data_types); n++)
if (ImGui::Selectable(DataTypeGetDesc((ImGuiDataType)n), PreviewDataType == n)) {
PreviewDataType = (ImGuiDataType)n; ImGuiDataType data_type = supported_data_types[n];
if (ImGui::Selectable(DataTypeGetDesc(data_type), PreviewDataType == data_type))
PreviewDataType = data_type;
}
ImGui::EndCombo(); ImGui::EndCombo();
} }
ImGui::SameLine(); ImGui::SameLine();
@ -608,18 +610,19 @@ struct MemoryEditor
ImGui::Text("Bin"); ImGui::SameLine(x); ImGui::TextUnformatted(has_value ? buf : "N/A"); ImGui::Text("Bin"); ImGui::SameLine(x); ImGui::TextUnformatted(has_value ? buf : "N/A");
} }
// Utilities for Data Preview // Utilities for Data Preview (since we don't access imgui_internal.h)
// FIXME: This technically depends on ImGuiDataType order.
const char* DataTypeGetDesc(ImGuiDataType data_type) const const char* DataTypeGetDesc(ImGuiDataType data_type) const
{ {
const char* descs[] = { "Int8", "Uint8", "Int16", "Uint16", "Int32", "Uint32", "Int64", "Uint64", "Float", "Double" }; const char* descs[] = { "Int8", "Uint8", "Int16", "Uint16", "Int32", "Uint32", "Int64", "Uint64", "Float", "Double" };
IM_ASSERT(data_type >= 0 && data_type < ImGuiDataType_COUNT); IM_ASSERT(data_type >= 0 && data_type < IM_ARRAYSIZE(descs));
return descs[data_type]; return descs[data_type];
} }
size_t DataTypeGetSize(ImGuiDataType data_type) const size_t DataTypeGetSize(ImGuiDataType data_type) const
{ {
const size_t sizes[] = { 1, 1, 2, 2, 4, 4, 8, 8, sizeof(float), sizeof(double) }; const size_t sizes[] = { 1, 1, 2, 2, 4, 4, 8, 8, sizeof(float), sizeof(double) };
IM_ASSERT(data_type >= 0 && data_type < ImGuiDataType_COUNT); IM_ASSERT(data_type >= 0 && data_type < IM_ARRAYSIZE(sizes));
return sizes[data_type]; return sizes[data_type];
} }
@ -672,8 +675,8 @@ struct MemoryEditor
void* EndiannessCopy(void* dst, void* src, size_t size) const void* EndiannessCopy(void* dst, void* src, size_t size) const
{ {
static void* (*fp)(void*, void*, size_t, int) = NULL; static void* (*fp)(void*, void*, size_t, int) = nullptr;
if (fp == NULL) if (fp == nullptr)
fp = IsBigEndian() ? EndiannessCopyBigEndian : EndiannessCopyLittleEndian; fp = IsBigEndian() ? EndiannessCopyBigEndian : EndiannessCopyLittleEndian;
return fp(dst, src, size, PreviewEndianness); return fp(dst, src, size, PreviewEndianness);
} }
@ -703,7 +706,7 @@ struct MemoryEditor
size_t size = addr + elem_size > mem_size ? mem_size - addr : elem_size; size_t size = addr + elem_size > mem_size ? mem_size - addr : elem_size;
if (ReadFn) if (ReadFn)
for (int i = 0, n = (int)size; i < n; ++i) for (int i = 0, n = (int)size; i < n; ++i)
buf[i] = ReadFn(mem_data, addr + i); buf[i] = ReadFn(mem_data, addr + i, UserData);
else else
memcpy(buf, mem_data + addr, size); memcpy(buf, mem_data + addr, size);
@ -798,6 +801,7 @@ struct MemoryEditor
if (data_format == DataFormat_Hex) { ImSnprintf(out_buf, out_buf_size, "%a", data); return; } if (data_format == DataFormat_Hex) { ImSnprintf(out_buf, out_buf_size, "%a", data); return; }
break; break;
} }
default:
case ImGuiDataType_COUNT: case ImGuiDataType_COUNT:
break; break;
} // Switch } // Switch