[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; typedef void * MsgBoxHandle;
MsgBoxHandle app_msgBox(const char * caption, const char * fmt, ...); 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); void app_msgBoxClose(MsgBoxHandle * handle);
typedef struct KeybindHandle * KeybindHandle; typedef struct KeybindHandle * KeybindHandle;

View File

@ -644,8 +644,20 @@ MsgBoxHandle app_msgBox(const char * caption, const char * fmt, ...)
{ {
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
MsgBoxHandle handle = MsgBoxHandle handle = overlayMsg_show(caption, NULL, NULL, fmt, args);
overlayMsg_show(caption, 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); va_end(args);
core_updateOverlayState(); core_updateOverlayState();

View File

@ -35,6 +35,8 @@ struct Msg
char * caption; char * caption;
char * message; char * message;
StringList lines; StringList lines;
MsgBoxConfirmCallback confirm;
void * opaque;
}; };
struct MsgState struct MsgState
@ -115,13 +117,42 @@ static int msg_render(void * udata, bool interactive, struct Rect * windowRects,
} }
igNewLine(); 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); ll_shift(l_msg.messages, NULL);
freeMsg(msg); freeMsg(msg);
@ -149,7 +180,8 @@ bool overlayMsg_modal(void)
} }
MsgBoxHandle overlayMsg_show( 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)); struct Msg * msg = malloc(sizeof(*msg));
if (!msg) if (!msg)
@ -160,6 +192,8 @@ MsgBoxHandle overlayMsg_show(
msg->caption = strdup(caption); msg->caption = strdup(caption);
msg->lines = stringlist_new(false); msg->lines = stringlist_new(false);
msg->confirm = confirm;
msg->opaque = opaque;
valloc_sprintf(&msg->message, fmt, args); valloc_sprintf(&msg->message, fmt, args);
char * token = msg->message; char * token = msg->message;

View File

@ -29,7 +29,8 @@
bool overlayMsg_modal(void); bool overlayMsg_modal(void);
MsgBoxHandle overlayMsg_show( 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); void overlayMsg_close(MsgBoxHandle * handle);