[client] overlay: add support for confirmation dialogs

This commit is contained in:
Quantum 2022-03-18 01:35:13 -04:00 committed by Geoffrey McRae
parent f28084e653
commit c55d0a82f2
4 changed files with 62 additions and 10 deletions

View file

@ -140,6 +140,11 @@ void app_alert(LG_MsgAlert type, const char * fmt, ...);
typedef void * MsgBoxHandle;
MsgBoxHandle app_msgBox(const char * caption, const char * fmt, ...);
typedef void (*MsgBoxConfirmCallback)(bool yes, void * opaque);
MsgBoxHandle app_confirmMsgBox(const char * caption,
MsgBoxConfirmCallback callback, void * opaque, const char * fmt, ...);
void app_msgBoxClose(MsgBoxHandle * handle);
typedef struct KeybindHandle * KeybindHandle;

View file

@ -644,8 +644,20 @@ MsgBoxHandle app_msgBox(const char * caption, const char * fmt, ...)
{
va_list args;
va_start(args, fmt);
MsgBoxHandle handle =
overlayMsg_show(caption, fmt, args);
MsgBoxHandle handle = overlayMsg_show(caption, NULL, NULL, fmt, args);
va_end(args);
core_updateOverlayState();
return handle;
}
MsgBoxHandle app_confirmMsgBox(const char * caption,
MsgBoxConfirmCallback callback, void * opaque, const char * fmt, ...)
{
va_list args;
va_start(args, fmt);
MsgBoxHandle handle = overlayMsg_show(caption, callback, opaque, fmt, args);
va_end(args);
core_updateOverlayState();

View file

@ -35,6 +35,8 @@ struct Msg
char * caption;
char * message;
StringList lines;
MsgBoxConfirmCallback confirm;
void * opaque;
};
struct MsgState
@ -115,13 +117,42 @@ static int msg_render(void * udata, bool interactive, struct Rect * windowRects,
}
igNewLine();
igCalcTextSize(&textSize, "OK", NULL, false, 0.0);
ImGuiStyle * style = igGetStyle();
textSize.x += (style->FramePadding.x * 2.0f) * 8.0f;
textSize.y += (style->FramePadding.y * 2.0f) * 1.5f;
igSetCursorPosX((igGetWindowWidth() * 0.5f) - (textSize.x * 0.5f));
if (igButton("OK", textSize))
bool destroy = false;
if (msg->confirm)
{
igCalcTextSize(&textSize, "Yes", NULL, false, 0.0);
ImGuiStyle * style = igGetStyle();
textSize.x += (style->FramePadding.x * 2.0f) * 8.0f;
textSize.y += (style->FramePadding.y * 2.0f) * 1.5f;
igSetCursorPosX((igGetWindowWidth() * 0.5f) - textSize.x);
if (igButton("Yes", textSize))
{
destroy = true;
msg->confirm(true, msg->opaque);
}
igSameLine(0.0f, -1.0f);
if (igButton("No", textSize))
{
destroy = true;
msg->confirm(false, msg->opaque);
}
}
else
{
igCalcTextSize(&textSize, "OK", NULL, false, 0.0);
ImGuiStyle * style = igGetStyle();
textSize.x += (style->FramePadding.x * 2.0f) * 8.0f;
textSize.y += (style->FramePadding.y * 2.0f) * 1.5f;
igSetCursorPosX((igGetWindowWidth() * 0.5f) - (textSize.x * 0.5f));
if (igButton("OK", textSize))
destroy = true;
}
if (destroy)
{
ll_shift(l_msg.messages, NULL);
freeMsg(msg);
@ -149,7 +180,8 @@ bool overlayMsg_modal(void)
}
MsgBoxHandle overlayMsg_show(
const char * caption, const char * fmt, va_list args)
const char * caption, MsgBoxConfirmCallback confirm, void * opaque,
const char * fmt, va_list args)
{
struct Msg * msg = malloc(sizeof(*msg));
if (!msg)
@ -160,6 +192,8 @@ MsgBoxHandle overlayMsg_show(
msg->caption = strdup(caption);
msg->lines = stringlist_new(false);
msg->confirm = confirm;
msg->opaque = opaque;
valloc_sprintf(&msg->message, fmt, args);
char * token = msg->message;

View file

@ -29,7 +29,8 @@
bool overlayMsg_modal(void);
MsgBoxHandle overlayMsg_show(
const char * caption, const char * fmt, va_list args);
const char * caption, MsgBoxConfirmCallback confirm, void * opaque,
const char * fmt, va_list args);
void overlayMsg_close(MsgBoxHandle * handle);