From c55d0a82f27f0b24c113c32652780334b0579181 Mon Sep 17 00:00:00 2001 From: Quantum Date: Fri, 18 Mar 2022 01:35:13 -0400 Subject: [PATCH] [client] overlay: add support for confirmation dialogs --- client/include/app.h | 5 +++++ client/src/app.c | 16 ++++++++++++-- client/src/overlay/msg.c | 48 ++++++++++++++++++++++++++++++++++------ client/src/overlay/msg.h | 3 ++- 4 files changed, 62 insertions(+), 10 deletions(-) diff --git a/client/include/app.h b/client/include/app.h index 46c2df9e..783449a4 100644 --- a/client/include/app.h +++ b/client/include/app.h @@ -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; diff --git a/client/src/app.c b/client/src/app.c index 738c804c..19202fff 100644 --- a/client/src/app.c +++ b/client/src/app.c @@ -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(); diff --git a/client/src/overlay/msg.c b/client/src/overlay/msg.c index 07aae88b..02694083 100644 --- a/client/src/overlay/msg.c +++ b/client/src/overlay/msg.c @@ -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; diff --git a/client/src/overlay/msg.h b/client/src/overlay/msg.h index f6bbd1ac..96c59844 100644 --- a/client/src/overlay/msg.h +++ b/client/src/overlay/msg.h @@ -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);