[client] overlay: allow registration of runtime configuration options

This commit is contained in:
Geoffrey McRae 2021-08-05 00:47:00 +10:00
parent 4e81c7f724
commit f3f0157d3c
4 changed files with 75 additions and 15 deletions

View file

@ -820,11 +820,11 @@ static int lg_run(void)
DEBUG_INFO("Using font: %s", g_state.fontName);
g_state.overlays = ll_new();
app_registerOverlay(&LGOverlayConfig, NULL);
app_registerOverlay(&LGOverlayAlert , NULL);
app_registerOverlay(&LGOverlayFPS , NULL);
app_registerOverlay(&LGOverlayGraphs, NULL);
app_registerOverlay(&LGOverlayHelp , NULL);
app_registerOverlay(&LGOverlayConfig, NULL);
// initialize metrics ringbuffers
g_state.renderTimings = ringbuffer_new(256, sizeof(float));

View file

@ -21,27 +21,50 @@
#include "interface/overlay.h"
#include "cimgui.h"
#include "overlay_utils.h"
#include "ll.h"
#include "../main.h"
#include "../overlays.h"
#include "version.h"
#include "common/debug.h"
#include "common/appstrings.h"
typedef struct ConfigCallback
{
const char * title;
void * udata;
void (*callback)(void * udata);
}
ConfigCallback;
typedef struct OverlayConfig
{
struct ll * callbacks;
}
OverlayConfig;
static OverlayConfig cfg = { 0 };
static bool config_init(void ** udata, void * params)
{
cfg.callbacks = ll_new();
if (!cfg.callbacks)
{
DEBUG_ERROR("failed to allocate ram");
return false;
}
return true;
}
static void config_free(void * udata)
{
}
ConfigCallback * cb;
while(ll_shift(cfg.callbacks, (void **)&cb))
free(cb);
static void graphIterator(GraphHandle handle, const char * name, bool * enabled,
void * udata)
{
igTableNextColumn();
igCheckbox(name, enabled);
ll_free(cfg.callbacks);
}
static int config_render(void * udata, bool interactive, struct Rect * windowRects,
@ -132,14 +155,12 @@ static int config_render(void * udata, bool interactive, struct Rect * windowRec
}
}
if(igCollapsingHeaderBoolPtr("Performance Metrics", NULL, 0))
ConfigCallback * cb;
for (ll_reset(cfg.callbacks); ll_walk(cfg.callbacks, (void **)&cb); )
{
igCheckbox("Show Timing Graphs", &g_state.showTiming);
igSeparator();
igBeginTable("split", 2, 0, (ImVec2){}, 0);
overlayGraph_iterate(graphIterator, NULL);
igEndTable();
if (!igCollapsingHeaderBoolPtr(cb->title, NULL, 0))
continue;
cb->callback(cb->udata);
}
overlayGetImGuiRect(windowRects);
@ -154,3 +175,19 @@ struct LG_OverlayOps LGOverlayConfig =
.free = config_free,
.render = config_render
};
void overlayConfig_register(const char * title, void (*callback)(void * udata),
void * udata)
{
ConfigCallback * cb = calloc(1, sizeof(*cb));
if (!cb)
{
DEBUG_ERROR("failed to allocate ram");
return;
}
cb->title = title;
cb->udata = udata;
cb->callback = callback;
ll_push(cfg.callbacks, cb);
};

View file

@ -22,6 +22,7 @@
#include "cimgui.h"
#include "../main.h"
#include "../overlays.h"
#include "ll.h"
#include "common/debug.h"
@ -43,9 +44,28 @@ struct OverlayGraph
float max;
};
static void configCallback(void * udata)
{
igCheckbox("Show Timing Graphs", &g_state.showTiming);
igSeparator();
igBeginTable("split", 2, 0, (ImVec2){}, 0);
GraphHandle graph;
for (ll_reset(gs.graphs); ll_walk(gs.graphs, (void **)&graph); )
{
igTableNextColumn();
igCheckbox(graph->name, &graph->enabled);
}
igEndTable();
}
static bool graphs_init(void ** udata, void * params)
{
gs.graphs = ll_new();
overlayConfig_register("Performance Metrics", configCallback, NULL);
return true;
}

View file

@ -33,6 +33,9 @@ GraphHandle overlayGraph_register(const char * name, RingBuffer buffer,
float min, float max);
void overlayGraph_unregister();
void overlayGraph_iterate(void (*callback)(GraphHandle handle, const char * name,
bool * enabled, void * udata), void * udata);
bool * enabled, void * udata), void * udata);
void overlayConfig_register(const char * title, void (*callback)(void * udata),
void * udata);
#endif