From be44249c05fddd0fdf3e821c23347c17e5017d95 Mon Sep 17 00:00:00 2001 From: Quantum Date: Fri, 23 Jul 2021 05:58:33 -0400 Subject: [PATCH] [client] imgui: converted alerts to use imgui --- client/CMakeLists.txt | 1 + client/src/app.c | 15 +++---- client/src/main.c | 3 ++ client/src/main.h | 6 +++ client/src/overlay/alert.c | 89 ++++++++++++++++++++++++++++++++++++++ client/src/overlay/help.c | 1 - client/src/overlays.h | 1 + 7 files changed, 107 insertions(+), 9 deletions(-) create mode 100644 client/src/overlay/alert.c diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index ecefb2fd..cdaefe0a 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -123,6 +123,7 @@ set(SOURCES src/overlay_utils.c src/overlay_utils.cpp + src/overlay/alert.c src/overlay/fps.c src/overlay/graphs.c src/overlay/help.c diff --git a/client/src/app.c b/client/src/app.c index b302d0df..5a21b694 100644 --- a/client/src/app.c +++ b/client/src/app.c @@ -39,6 +39,8 @@ #include #include +#define ALERT_TIMEOUT 2000000 + bool app_isRunning(void) { return @@ -511,14 +513,11 @@ void app_alert(LG_MsgAlert type, const char * fmt, ...) valloc_sprintf(&buffer, fmt, args); va_end(args); - g_state.lgr->on_alert( - g_state.lgrData, - type, - buffer, - NULL - ); - - free(buffer); + free(g_state.alertMessage); + g_state.alertMessage = buffer; + g_state.alertTimeout = microtime() + ALERT_TIMEOUT; + g_state.alertType = type; + g_state.alertShow = true; } KeybindHandle app_registerKeybind(int sc, KeybindFn callback, void * opaque, const char * description) diff --git a/client/src/main.c b/client/src/main.c index 5c164ec5..dc4c0783 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -192,6 +192,8 @@ static int renderThread(void * unused) ImFontAtlas_Clear(g_state.io->Fonts); ImFontAtlas_AddFontFromFileTTF(g_state.io->Fonts, g_state.fontName, g_params.uiSize * g_state.windowScale, NULL, NULL); + g_state.fontLarge = ImFontAtlas_AddFontFromFileTTF(g_state.io->Fonts, + g_state.fontName, 2 * g_params.uiSize * g_state.windowScale, NULL, NULL); ImFontAtlas_Build(g_state.io->Fonts); if (g_state.lgr) @@ -774,6 +776,7 @@ static int lg_run(void) DEBUG_INFO("Using font: %s", g_state.fontName); g_state.overlays = ll_new(); + app_registerOverlay(&LGOverlayAlert , NULL); app_registerOverlay(&LGOverlayFPS , NULL); app_registerOverlay(&LGOverlayGraphs, NULL); app_registerOverlay(&LGOverlayHelp , NULL); diff --git a/client/src/main.h b/client/src/main.h index aaa8861d..6d0cf3bf 100644 --- a/client/src/main.h +++ b/client/src/main.h @@ -51,6 +51,12 @@ struct AppState ImGuiStyle * style; struct ll * overlays; char * fontName; + ImFont * fontLarge; + + bool alertShow; + char * alertMessage; + LG_MsgAlert alertType; + uint64_t alertTimeout; struct LG_DisplayServerOps * ds; bool dsInitialized; diff --git a/client/src/overlay/alert.c b/client/src/overlay/alert.c new file mode 100644 index 00000000..5e7eac42 --- /dev/null +++ b/client/src/overlay/alert.c @@ -0,0 +1,89 @@ +/** + * Looking Glass + * Copyright (C) 2017-2021 The Looking Glass Authors + * https://looking-glass.io + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., 59 + * Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "interface/overlay.h" +#include "cimgui.h" +#include "overlay_utils.h" + +#include "../main.h" + +static bool alert_init(void ** udata, void * params) +{ + return true; +} + +static void alert_free(void * udata) +{ +} + +static const uint32_t colours[] = +{ + 0xCC0000, // LG_ALERT_INFO + 0x00CC00, // LG_ALERT_SUCCESS + 0x007FCC, // LG_ALERT_WARNING + 0x0000FF // LG_ALERT_ERROR +}; + +static int alert_render(void * udata, bool interactive, struct Rect * windowRects, + int maxRects) +{ + if (!g_state.alertShow) + return 0; + + if (g_state.alertTimeout < microtime()) + { + g_state.alertShow = false; + free(g_state.alertMessage); + g_state.alertMessage = NULL; + return 0; + } + + ImVec2 * screen = overlayGetScreenSize(); + igSetNextWindowBgAlpha(0.8f); + igSetNextWindowPos((ImVec2) { screen->x / 2.0f, screen->y / 2.0f }, 0, + (ImVec2) { 0.5f, 0.5f }); + igPushStyleColorU32(ImGuiCol_WindowBg, colours[g_state.alertType]); + + igBegin( + "Alert", + NULL, + ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize | + ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | + ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoTitleBar + ); + + igPushFont(g_state.fontLarge); + igText("%s", g_state.alertMessage); + igPopFont(); + + overlayGetImGuiRect(windowRects); + igEnd(); + igPopStyleColor(1); + + return 1; +} + +struct LG_OverlayOps LGOverlayAlert = +{ + .name = "alert", + .init = alert_init, + .free = alert_free, + .render = alert_render +}; diff --git a/client/src/overlay/help.c b/client/src/overlay/help.c index ae3badab..1f89ccc0 100644 --- a/client/src/overlay/help.c +++ b/client/src/overlay/help.c @@ -54,7 +54,6 @@ static int help_render(void * udata, bool interactive, struct Rect * windowRects ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoTitleBar ); - if (igBeginTable("Help", 2, 0, (ImVec2) { 0.0f, 0.0f }, 0.0f)) { const char * escapeName = xfree86_to_display[g_params.escapeKey]; diff --git a/client/src/overlays.h b/client/src/overlays.h index e121472b..f7213e8e 100644 --- a/client/src/overlays.h +++ b/client/src/overlays.h @@ -23,6 +23,7 @@ #include "interface/overlay.h" +extern struct LG_OverlayOps LGOverlayAlert; extern struct LG_OverlayOps LGOverlayFPS; extern struct LG_OverlayOps LGOverlayGraphs; extern struct LG_OverlayOps LGOverlayHelp;