From c032d60f9e398ba961be952e0d56b9fdb8ecdf76 Mon Sep 17 00:00:00 2001 From: silverweed Date: Thu, 11 Jul 2024 14:27:19 +0200 Subject: [PATCH] prettier colors --- imgui.ini | 2 +- src/app_state.h | 3 ++- src/render.cpp | 49 +++++++++++++++++++++++++++++++++++------------ src/render.h | 10 ++++++++++ src/rntuple.cpp | 14 +++++++------- src/rntviewer.cpp | 2 ++ 6 files changed, 59 insertions(+), 21 deletions(-) create mode 100644 src/render.h diff --git a/imgui.ini b/imgui.ini index 47ace67..5c0aa5e 100644 --- a/imgui.ini +++ b/imgui.ini @@ -12,5 +12,5 @@ Size=1775,680 [Window][Hex View] Pos=91,62 -Size=986,681 +Size=1002,939 diff --git a/src/app_state.h b/src/app_state.h index 6891964..2097d3f 100644 --- a/src/app_state.h +++ b/src/app_state.h @@ -1,7 +1,8 @@ struct App_State { Window_Data win_data; User_Input user_input; - RNTuple_Info *rntinfo; + RNTuple_Info rntinfo; + Viewer_Settings vsettings; FILE *inspected_file; u8 *inspected_fmem; diff --git a/src/render.cpp b/src/render.cpp index 7148b8d..5d32aa3 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -1,26 +1,38 @@ internal u32 mem_edit_bg_color_fn(const u8 *data, u64 off, const void *user_data) { - const RNTuple_Info *ntinfo = reinterpret_cast(user_data); + const App_State *app = reinterpret_cast(user_data); + + #define COL(c) (ImColor((c)[0], (c)[1], (c)[2])) + if (app->rntinfo.header.start <= off && off <= app->rntinfo.header.end()) return COL(app->vsettings.col_header); + if (app->rntinfo.footer.start <= off && off <= app->rntinfo.footer.end()) return COL(app->vsettings.col_footer); + #undef COL - if (ntinfo->header.start <= off && off <= ntinfo->header.end()) - return IM_COL32(200, 0, 50, 255); - if (ntinfo->footer.start <= off && off <= ntinfo->footer.end()) - return IM_COL32(50, 0, 200, 255); return IM_COL32(0, 0, 0, 0); } internal -MemoryEditor make_memory_editor(const RNTuple_Info &ntpl_info) +MemoryEditor make_memory_editor(const App_State &app) { MemoryEditor mem_edit; // mem_edit.ReadOnly = true; mem_edit.Cols = 32; + mem_edit.OptShowDataPreview = true; mem_edit.BgColorFn = mem_edit_bg_color_fn; - mem_edit.BgColorFnUserData = &ntpl_info; + mem_edit.BgColorFnUserData = &app; return mem_edit; } +Viewer_Settings make_viewer_settings() +{ + Viewer_Settings settings {}; +#define COL(c, r, g, b) settings.c[0] = r/255.0, settings.c[1] = g/255.0, settings.c[2] = b/255.0 + COL(col_header, 150, 0, 50); + COL(col_footer, 50, 0, 150); +#undef COL + return settings; +} + internal void update_and_render(Arena *arena, App_State &app, f32 delta_time) { @@ -41,13 +53,26 @@ void update_and_render(Arena *arena, App_State &app, f32 delta_time) sprintf(&text_buf[2 * i], "%02X", app.inspected_fmem[i]); text_buf[text_buf_size] = 0; - if (ImGui::Begin("main")) { + const auto main_win_flags = ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoDecoration; + if (ImGui::Begin("main", nullptr, main_win_flags)) { ImGui::Text("Inspecting RNTuple '%s' (%s)", app.ntpl_name, - (const char *)rntuple_description(scratch.arena, app.rntinfo->anchor)); + (const char *)rntuple_description(scratch.arena, app.rntinfo.anchor)); + ImGui::Separator(); + + { + static MemoryEditor mem_edit = make_memory_editor(app); + ImGui::BeginTable("Hex View", 2); + + ImGui::TableNextColumn(); + mem_edit.DrawContents(app.inspected_fmem, app.inspected_file_size); + + ImGui::TableNextColumn(); + ImGui::ColorEdit3("Header", app.vsettings.col_header, ImGuiColorEditFlags_NoInputs); + ImGui::ColorEdit3("Footer", app.vsettings.col_footer, ImGuiColorEditFlags_NoInputs); + + ImGui::EndTable(); + } - static MemoryEditor mem_edit = make_memory_editor(*app.rntinfo); - mem_edit.DrawWindow("Hex View", app.inspected_fmem, app.inspected_file_size); - ImGui::End(); } } diff --git a/src/render.h b/src/render.h new file mode 100644 index 0000000..d07ffc2 --- /dev/null +++ b/src/render.h @@ -0,0 +1,10 @@ +struct Viewer_Settings { + float col_header[3]; + float col_footer[3]; +}; + +struct Edit_Bg_Color_Data { + RNTuple_Info *ntinfo; + Viewer_Settings vsettings; +}; + diff --git a/src/rntuple.cpp b/src/rntuple.cpp index e7af72b..46c346f 100644 --- a/src/rntuple.cpp +++ b/src/rntuple.cpp @@ -10,9 +10,9 @@ String8 rntuple_description(Arena *arena, const RNTuple &anchor) } internal -RNTuple_Info *get_rntuple_info(Arena *arena, const char *fname, const char *ntpl_name) +RNTuple_Info get_rntuple_info(Arena *arena, const char *fname, const char *ntpl_name) { - RNTuple_Info *info = arena_push_zeroed(arena); + RNTuple_Info info = {}; // Open the TFile TFile *tfile = TFile::Open(fname, "READ"); @@ -25,11 +25,11 @@ RNTuple_Info *get_rntuple_info(Arena *arena, const char *fname, const char *ntpl // Get the RNTuple information const RNTuple *anchor = tfile->Get(ntpl_name); if (anchor) { - info->anchor = *anchor; - info->header.start = anchor->GetSeekHeader(); - info->header.len = anchor->GetNBytesHeader(); - info->footer.start = anchor->GetSeekFooter(); - info->footer.len = anchor->GetNBytesFooter(); + info.anchor = *anchor; + info.header.start = anchor->GetSeekHeader(); + info.header.len = anchor->GetNBytesHeader(); + info.footer.start = anchor->GetSeekFooter(); + info.footer.len = anchor->GetNBytesFooter(); } else { fprintf(stderr, "RNTuple '%s' not found in %s.\n", ntpl_name, fname); } diff --git a/src/rntviewer.cpp b/src/rntviewer.cpp index 5d7f75c..72de955 100644 --- a/src/rntviewer.cpp +++ b/src/rntviewer.cpp @@ -24,6 +24,7 @@ #include "str.h" #include "rntuple.h" #include "window.h" +#include "render.h" #include "app_state.h" // @Platform @@ -143,6 +144,7 @@ int main(int argc, char **argv) app.ntpl_name = ntpl_name; app.rntinfo = get_rntuple_info(arena, fname, ntpl_name); + app.vsettings = make_viewer_settings(); // Start main loop run_main_loop(window, arena, app);