[client] overlay/help: switch to using ImGui's table facility

This commit is contained in:
Quantum 2021-07-23 02:55:23 -04:00 committed by Geoffrey McRae
parent 5153d35bb5
commit efb5019176

View File

@ -27,89 +27,20 @@
#include "../kb.h"
#include "../main.h"
struct HelpData
{
char * helpText;
};
static bool help_init(void ** udata, void * params)
{
*udata = calloc(1, sizeof(struct HelpData));
if (!udata)
{
DEBUG_ERROR("Out of memory");
return false;
}
return true;
}
static void help_free(void * udata)
{
free(udata);
}
static char * buildHelpText(void)
{
size_t size = 50;
size_t offset = 0;
char * buffer = malloc(size);
if (!buffer)
return NULL;
const char * escapeName = xfree86_to_display[g_params.escapeKey];
offset += snprintf(buffer, size, "%s %-10s Toggle capture mode\n", escapeName, "");
if (offset >= size)
{
DEBUG_ERROR("Help string somehow overflowed. This should be impossible.");
return NULL;
}
for (int i = 0; i < KEY_MAX; ++i)
{
if (g_state.keyDescription[i])
{
const char * keyName = xfree86_to_display[i];
const char * desc = g_state.keyDescription[i];
int needed = snprintf(buffer + offset, size - offset, "%s+%-10s %s\n", escapeName, keyName, desc);
if (offset + needed < size)
offset += needed;
else
{
size = size * 2 + needed;
void * new = realloc(buffer, size);
if (!new) {
free(buffer);
DEBUG_ERROR("Out of memory when constructing help text");
return NULL;
}
buffer = new;
offset += snprintf(buffer + offset, size - offset, "%s+%-10s %s\n", escapeName, keyName, desc);
}
}
}
return buffer;
}
static int help_render(void * udata, bool interactive, struct Rect * windowRects,
int maxRects)
{
struct HelpData * data = udata;
if (!g_state.escapeHelp)
{
if (data->helpText)
{
free(data->helpText);
data->helpText = NULL;
}
return 0;
}
if (!data->helpText)
data->helpText = buildHelpText();
ImVec2 * screen = overlayGetScreenSize();
igSetNextWindowBgAlpha(0.6f);
@ -123,7 +54,27 @@ static int help_render(void * udata, bool interactive, struct Rect * windowRects
ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoTitleBar
);
igText("%s", data->helpText);
if (igBeginTable("Help", 2, 0, (ImVec2) { 0.0f, 0.0f }, 0.0f))
{
const char * escapeName = xfree86_to_display[g_params.escapeKey];
igTableNextColumn();
igText("%s", escapeName);
igTableNextColumn();
igText("Toggle capture mode");
for (int i = 0; i < KEY_MAX; ++i)
if (g_state.keyDescription[i])
{
igTableNextColumn();
igText("%s+%s", escapeName, xfree86_to_display[i]);
igTableNextColumn();
igText(g_state.keyDescription[i]);
}
igEndTable();
}
overlayGetImGuiRect(windowRects);
igEnd();