Skip to content

Commit

Permalink
implement dear imgui settings handler to save window-open state
Browse files Browse the repository at this point in the history
  • Loading branch information
floooh committed Dec 27, 2024
1 parent fbebbe5 commit 76ae7ac
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ set(SOURCES
asm.c asm.h
ui.cc ui.h
ui_asm.cc ui_asm.h
imgui_util.cc imgui_util.h
common.h
)

Expand Down
64 changes: 64 additions & 0 deletions src/imgui_util.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "imgui.h"
#include "imgui_internal.h"
#include "imgui_util.h"
#include "ui.h"
#include "util.h"

static void imgui_ClearAllFn(ImGuiContext* ctx, ImGuiSettingsHandler* handler) {
(void)ctx; (void)handler;
}

// read: Called before reading (in registration order)
static void imgui_ReadInitFn(ImGuiContext* ctx, ImGuiSettingsHandler* handler) {
(void)ctx; (void)handler;
}

// read: Called when entering into a new ini entry e.g. "[Window][Name]"
static void* imgui_ReadOpenFn(ImGuiContext* ctx, ImGuiSettingsHandler* handler, const char* name) {
(void)ctx; (void)handler;
return (void*)ui_window_by_id(name);
}

// read: Called for every line of text within an ini entry
static void imgui_ReadLineFn(ImGuiContext* ctx, ImGuiSettingsHandler* handler, void* entry, const char* line) {
(void)ctx; (void)handler; (void)entry;
ui_window_t win = (ui_window_t)(uintptr_t)entry;
if ((win > UI_WINDOW_INVALID) && (win < UI_WINDOW_NUM)) {
int is_open = 0;
if (sscanf(line, "IsOpen=%i", &is_open) == 1) {
ui_init_window_open(win, true);
}
}
}

// read: Called after reading (in registration order)
static void imgui_ApplyAllFn(ImGuiContext* ctx, ImGuiSettingsHandler* handler) {
(void)ctx; (void)handler;
}

// write: output all entries into 'out_buf'
static void imgui_WriteAllFn(ImGuiContext* ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buf) {
(void)ctx; (void)handler;
for (int i = UI_WINDOW_INVALID + 1; i < UI_WINDOW_NUM; i++) {
ui_window_t win = (ui_window_t)i;
buf->appendf("[%s][%s]\n", handler->TypeName, ui_window_id(win));
if (ui_is_window_open(win)) {
buf->append("IsOpen=1\n");
}
}
buf->append("\n");
}

void imgui_register_settings_handler(void) {
const char* type_name = "floooh.v6502r";
ImGuiSettingsHandler ini_handler;
ini_handler.TypeName = type_name;;
ini_handler.TypeHash = ImHashStr(type_name);
ini_handler.ClearAllFn = imgui_ClearAllFn;
ini_handler.ReadInitFn = imgui_ReadInitFn;
ini_handler.ReadOpenFn = imgui_ReadOpenFn;
ini_handler.ReadLineFn = imgui_ReadLineFn;
ini_handler.ApplyAllFn = imgui_ApplyAllFn;
ini_handler.WriteAllFn = imgui_WriteAllFn;
ImGui::AddSettingsHandler(&ini_handler);
}
11 changes: 11 additions & 0 deletions src/imgui_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#if defined(__cplusplus)
extern "C" {
#endif

void imgui_register_settings_handler(void);

#if defined(__cplusplus)
} // extern "C"
#endif
16 changes: 9 additions & 7 deletions src/ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "pick.h"
#include "trace.h"
#include "util.h"
#include "imgui_util.h"
#include "TextEditor.h"
#include "nodenames.h"

Expand Down Expand Up @@ -251,36 +252,36 @@ static void ui_ioedit(void);
static ImGui::MarkdownConfig md_conf;

void ui_check_dirty(ui_window_t win) {
assert((win >= 0) && (win < UI_WINDOW_NUM));
assert((win > UI_WINDOW_INVALID) && (win < UI_WINDOW_NUM));
if (ui.window_open[win] != ui.prev_window_open[win]) {
ui.prev_window_open[win] = ui.window_open[win];
ImGui::MarkIniSettingsDirty();
}
}

void ui_init_window_open(ui_window_t win, bool state) {
assert((win >= 0) && (win < UI_WINDOW_NUM));
assert((win > UI_WINDOW_INVALID) && (win < UI_WINDOW_NUM));
ui.window_open[win] = state;
ui.prev_window_open[win] = state;
}

void ui_set_window_open(ui_window_t win, bool state) {
assert((win >= 0) && (win < UI_WINDOW_NUM));
assert((win > UI_WINDOW_INVALID) && (win < UI_WINDOW_NUM));
ui.window_open[win] = state;
}

bool ui_is_window_open(ui_window_t win) {
assert((win >= 0) && (win < UI_WINDOW_NUM));
assert((win > UI_WINDOW_INVALID) && (win < UI_WINDOW_NUM));
return ui.window_open[win];
}

void ui_toggle_window(ui_window_t win) {
assert((win >= 0) && (win < UI_WINDOW_NUM));
assert((win > UI_WINDOW_INVALID) && (win < UI_WINDOW_NUM));
ui.window_open[win] = !ui.window_open[win];
}

bool* ui_window_open_ptr(ui_window_t win) {
assert((win >= 0) && (win < UI_WINDOW_NUM));
assert((win > UI_WINDOW_INVALID) && (win < UI_WINDOW_NUM));
return &ui.window_open[win];
}

Expand Down Expand Up @@ -324,7 +325,7 @@ const char* ui_window_name(ui_window_t win) {

ui_window_t ui_window_by_id(const char* id) {
assert(id);
for (int i = 0; i < UI_WINDOW_NUM; i++) {
for (int i = UI_WINDOW_INVALID + 1; i < UI_WINDOW_NUM; i++) {
ui_window_t win = (ui_window_t)i;
if (0 == strcmp(ui_window_id(win), id)) {
return win;
Expand Down Expand Up @@ -372,6 +373,7 @@ void ui_init() {
simgui_desc.no_default_font = true;
simgui_desc.disable_paste_override = true;
simgui_setup(&simgui_desc);
imgui_register_settings_handler();
auto& style = ImGui::GetStyle();
style.WindowRounding = 0.0f;
style.WindowBorderSize = 1.0f;
Expand Down
5 changes: 3 additions & 2 deletions src/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ typedef struct {
} ui_diffview_t;

typedef enum {
UI_WINDOW_FLOATING_CONTROLS = 0,
UI_WINDOW_INVALID = 0,
UI_WINDOW_FLOATING_CONTROLS, // keep at start
UI_WINDOW_CPU_CONTROLS,
UI_WINDOW_TRACELOG,
UI_WINDOW_TIMING_DIAGRAM,
Expand All @@ -31,7 +32,6 @@ typedef enum {
UI_WINDOW_AUDIO,
#endif
UI_WINDOW_NUM,
UI_WINDOW_INVALID,
} ui_window_t;

void ui_init(void);
Expand All @@ -41,6 +41,7 @@ void ui_set_window_open(ui_window_t win, bool state);
void ui_toggle_window(ui_window_t win);
bool ui_is_window_open(ui_window_t win);
const char* ui_window_id(ui_window_t win);
ui_window_t ui_window_by_id(const char* id);
void ui_check_dirty(ui_window_t win);
bool* ui_window_open_ptr(ui_window_t win);
void ui_frame(void);
Expand Down

0 comments on commit 76ae7ac

Please sign in to comment.