From 00066f225be31cdc4f6f950584f8c2976202b19b Mon Sep 17 00:00:00 2001 From: Simon Gellis Date: Fri, 1 Nov 2024 00:52:27 -0400 Subject: [PATCH] Change how active option is rendered --- ui.c | 32 ++++++++++++++------------------ window.c | 32 ++++++++++++++++++++++++++++++++ window.h | 4 ++++ 3 files changed, 50 insertions(+), 18 deletions(-) diff --git a/ui.c b/ui.c index 1dbdd76..70414eb 100644 --- a/ui.c +++ b/ui.c @@ -136,57 +136,53 @@ int uiRun(UIContext *ui, bool running) { if (windowGuiBegin(&ui->win, "Shrooms VB")) { windowMenubarBegin(&ui->win, MENU_ITEMS); - if (nk_menu_begin_label(ctx, "File", NK_TEXT_ALIGN_CENTERED, nk_vec2(windowScaleX(&ui->win, 100), windowGetScreenHeight(&ui->win)))) { - nk_layout_row_dynamic(ctx, windowGetMenuHeight(&ui->win), 1); - if (nk_menu_item_label(ctx, "Open ROM", NK_TEXT_ALIGN_LEFT)) { + if (windowMenuBegin(&ui->win, "File", 100)) { + if (windowMenuItemLabel(&ui->win, "Open ROM")) { char *file = tinyfd_openFileDialog("Pick a ROM", NULL, 1, ROM_EXTENSIONS, "Virtual Boy ROM files", false); if (file) { uiLoadGame(ui, file); status = status_running; } } - if (nk_menu_item_label(ctx, "Quit", NK_TEXT_ALIGN_LEFT)) { + if (windowMenuItemLabel(&ui->win, "Quit")) { SDL_Event QuitEvent; QuitEvent.type = SDL_QUIT; QuitEvent.quit.timestamp = SDL_GetTicks(); SDL_PushEvent(&QuitEvent); } - nk_menu_end(ctx); + windowMenuEnd(&ui->win); } - if (nk_menu_begin_label(ctx, "Emulation", NK_TEXT_ALIGN_CENTERED, nk_vec2(windowScaleX(&ui->win, 100), windowGetScreenHeight(&ui->win)))) { + if (windowMenuBegin(&ui->win, "Emulation", 100)) { const char *label = status == status_paused ? "Resume" : "Pause"; - nk_layout_row_dynamic(ctx, windowGetMenuHeight(&ui->win), 1); - if (nk_menu_item_label(ctx, label, NK_TEXT_ALIGN_LEFT)) { + if (windowMenuItemLabel(&ui->win, label)) { if (status == status_paused) status = status_running; else status = status_paused; } - if (nk_menu_item_label(ctx, "Reset", NK_TEXT_ALIGN_LEFT)) { + if (windowMenuItemLabel(&ui->win, "Reset")) { emuReset(&ui->emu); status = emuIsGameLoaded(&ui->emu) ? status_running : status_paused; } - nk_menu_end(ctx); + windowMenuEnd(&ui->win); } - if (nk_menu_begin_label(ctx, "Video", NK_TEXT_ALIGN_CENTERED, nk_vec2(windowScaleX(&ui->win, 100), windowGetScreenHeight(&ui->win)))) { + if (windowMenuBegin(&ui->win, "Video", 100)) { float multiplier = windowGetScreenSizeMultiplier(&ui->win); - - nk_layout_row_dynamic(ctx, windowGetMenuHeight(&ui->win), 1); - if (nk_menu_item_label(ctx, multiplier == 1.0f ? "x1 *" : "x1", NK_TEXT_ALIGN_LEFT)) { + if (windowMenuItemLabelChecked(&ui->win, "x1", multiplier == 1.0f)) { windowSetScreenSizeMultiplier(&ui->win, 1.0f); } - if (nk_menu_item_label(ctx, multiplier == 2.0f ? "x2 *" : "x2", NK_TEXT_ALIGN_LEFT)) { + if (windowMenuItemLabelChecked(&ui->win, "x2", multiplier == 2.0f)) { windowSetScreenSizeMultiplier(&ui->win, 2.0f); } - if (nk_menu_item_label(ctx, multiplier == 3.0f ? "x3 *" : "x3", NK_TEXT_ALIGN_LEFT)) { + if (windowMenuItemLabelChecked(&ui->win, "x3", multiplier == 3.0f)) { windowSetScreenSizeMultiplier(&ui->win, 3.0f); } - if (nk_menu_item_label(ctx, multiplier == 4.0f ? "x4 *" : "x4", NK_TEXT_ALIGN_LEFT)) { + if (windowMenuItemLabelChecked(&ui->win, "x4", multiplier == 4.0f)) { windowSetScreenSizeMultiplier(&ui->win, 4.0f); } - nk_menu_end(ctx); + windowMenuEnd(&ui->win); } windowMenubarEnd(&ui->win); diff --git a/window.c b/window.c index 19658bc..e66f3be 100644 --- a/window.c +++ b/window.c @@ -1,5 +1,6 @@ #include "assets.h" #include "nuklear.h" +#include #include "window.h" #define MENU_HEIGHT 20 @@ -256,4 +257,35 @@ void windowMenubarBegin(WindowContext *win, const char **items) { void windowMenubarEnd(WindowContext *win) { nk_menubar_end(win->nk); +} + +bool windowMenuBegin(WindowContext *win, const char *label, int width) { + if (!nk_menu_begin_label(win->nk, label, NK_TEXT_ALIGN_CENTERED, nk_vec2(windowScaleX(win, width), windowGetScreenHeight(win)))) { + return false; + } + nk_layout_row_dynamic(win->nk, windowGetMenuHeight(win), 1); + return true; +} + +void windowMenuEnd(WindowContext *win) { + nk_menu_end(win->nk); +} + +bool windowMenuItemLabel(WindowContext *win, const char *label) { + return nk_menu_item_label(win->nk, label, NK_TEXT_ALIGN_LEFT); +} + +bool windowMenuItemLabelChecked(WindowContext *win, const char *label, bool checked) { + char buffer[80]; + bool result; + if (!checked) { + return windowMenuItemLabel(win, label); + } + strcpy(buffer, " * "); + strncpy(buffer + 5, label, 74); + buffer[79] = '\0'; + nk_style_push_vec2(win->nk, &win->nk->style.contextual_button.padding, nk_vec2(4 * win->screenScaleX, 4 * win->screenScaleY)); + result = nk_menu_item_label(win->nk, buffer, NK_TEXT_ALIGN_LEFT); + nk_style_pop_vec2(win->nk); + return result; } \ No newline at end of file diff --git a/window.h b/window.h index 4775638..24f588e 100644 --- a/window.h +++ b/window.h @@ -33,5 +33,9 @@ bool windowGuiBegin(WindowContext *win, const char *title); void windowGuiEnd(WindowContext *win); void windowMenubarBegin(WindowContext *win, const char **items); void windowMenubarEnd(WindowContext *win); +bool windowMenuBegin(WindowContext *win, const char *label, int width); +void windowMenuEnd(WindowContext *win); +bool windowMenuItemLabel(WindowContext *win, const char *label); +bool windowMenuItemLabelChecked(WindowContext *win, const char *label, bool checked); #endif \ No newline at end of file