all repos — mgba @ 956f63bba31032ff1e955afa96ccbde0486b016a

mGBA Game Boy Advance Emulator

GUI: Add message box API
Jeffrey Pfau jeffrey@endrift.com
Mon, 22 Aug 2016 17:30:56 -0700
commit

956f63bba31032ff1e955afa96ccbde0486b016a

parent

5aec67a0f7c874189217efb8b771f1fccf0f8b24

4 files changed, 95 insertions(+), 43 deletions(-)

jump to
M src/feature/gui/gui-runner.csrc/feature/gui/gui-runner.c

@@ -261,7 +261,6 @@ *GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Configure", .data = (void*) RUNNER_CONFIG };

*GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Reset game", .data = (void*) RUNNER_RESET }; *GUIMenuItemListAppend(&pauseMenu.items) = (struct GUIMenuItem) { .title = "Exit game", .data = (void*) RUNNER_EXIT }; - // TODO: Message box API runner->params.drawStart(); if (runner->params.guiPrepare) { runner->params.guiPrepare();

@@ -288,18 +287,7 @@ }

if (!found) { mLOG(GUI_RUNNER, WARN, "Failed to find core for %s!", path); - int i; - for (i = 0; i < 240; ++i) { - runner->params.drawStart(); - if (runner->params.guiPrepare) { - runner->params.guiPrepare(); - } - GUIFontPrint(runner->params.font, runner->params.width / 2, (GUIFontHeight(runner->params.font) + runner->params.height) / 2, GUI_ALIGN_HCENTER, 0xFFFFFFFF, "Load failed!"); - if (runner->params.guiFinish) { - runner->params.guiFinish(); - } - runner->params.drawEnd(); - } + GUIShowMessageBox(&runner->params, GUI_MESSAGE_BOX_OK, 240, "Load failed!"); return; } if (runner->core->platform(runner->core) == PLATFORM_GBA) {
M src/util/gui.csrc/util/gui.c

@@ -30,3 +30,40 @@ if (heldInput) {

*heldInput = input; } } + +enum GUICursorState GUIPollCursor(struct GUIParams* params, unsigned* x, unsigned* y) { + if (!params->pollCursor) { + return GUI_CURSOR_NOT_PRESENT; + } + enum GUICursorState state = params->pollCursor(x, y); + if (params->cursorState == GUI_CURSOR_DOWN) { + int dragX = *x - params->cx; + int dragY = *y - params->cy; + if (dragX * dragX + dragY * dragY > 25) { + params->cursorState = GUI_CURSOR_DRAGGING; + return GUI_CURSOR_DRAGGING; + } + if (state == GUI_CURSOR_UP || state == GUI_CURSOR_NOT_PRESENT) { + params->cursorState = GUI_CURSOR_UP; + return GUI_CURSOR_CLICKED; + } + } else { + params->cx = *x; + params->cy = *y; + } + if (params->cursorState == GUI_CURSOR_DRAGGING) { + if (state == GUI_CURSOR_UP || state == GUI_CURSOR_NOT_PRESENT) { + params->cursorState = GUI_CURSOR_UP; + return GUI_CURSOR_UP; + } + return GUI_CURSOR_DRAGGING; + } + params->cursorState = state; + return params->cursorState; +} + +void GUIInvalidateKeys(struct GUIParams* params) { + for (int i = 0; i < GUI_INPUT_MAX; ++i) { + params->inputHistory[i] = 0; + } +}
M src/util/gui/menu.csrc/util/gui/menu.c

@@ -193,41 +193,60 @@ }

return GUI_MENU_EXIT_CANCEL; } -enum GUICursorState GUIPollCursor(struct GUIParams* params, unsigned* x, unsigned* y) { - if (!params->pollCursor) { - return GUI_CURSOR_NOT_PRESENT; - } - enum GUICursorState state = params->pollCursor(x, y); - if (params->cursorState == GUI_CURSOR_DOWN) { - int dragX = *x - params->cx; - int dragY = *y - params->cy; - if (dragX * dragX + dragY * dragY > 25) { - params->cursorState = GUI_CURSOR_DRAGGING; - return GUI_CURSOR_DRAGGING; +enum GUIMenuExitReason GUIShowMessageBox(struct GUIParams* params, int buttons, int frames, const char* format, ...) { + va_list args; + va_start(args, format); + char message[256] = {0}; + vsnprintf(message, sizeof(message) - 1, format, args); + va_end(args); + + while (true) { + if (frames) { + --frames; + if (!frames) { + break; + } } - if (state == GUI_CURSOR_UP || state == GUI_CURSOR_NOT_PRESENT) { - params->cursorState = GUI_CURSOR_UP; - return GUI_CURSOR_CLICKED; + params->drawStart(); + if (params->guiPrepare) { + params->guiPrepare(); } - } else { - params->cx = *x; - params->cy = *y; - } - if (params->cursorState == GUI_CURSOR_DRAGGING) { - if (state == GUI_CURSOR_UP || state == GUI_CURSOR_NOT_PRESENT) { - params->cursorState = GUI_CURSOR_UP; - return GUI_CURSOR_UP; + GUIFontPrint(params->font, params->width / 2, (GUIFontHeight(params->font) + params->height) / 2, GUI_ALIGN_HCENTER, 0xFFFFFFFF, message); + if (params->guiFinish) { + params->guiFinish(); } - return GUI_CURSOR_DRAGGING; - } - params->cursorState = state; - return params->cursorState; -} + params->drawEnd(); -void GUIInvalidateKeys(struct GUIParams* params) { - for (int i = 0; i < GUI_INPUT_MAX; ++i) { - params->inputHistory[i] = 0; + uint32_t input = 0; + GUIPollInput(params, &input, 0); + if (input) { + if (input & (1 << GUI_INPUT_SELECT)) { + if (buttons & GUI_MESSAGE_BOX_OK) { + return GUI_MENU_EXIT_ACCEPT; + } + if (buttons & GUI_MESSAGE_BOX_CANCEL) { + return GUI_MENU_EXIT_CANCEL; + } + } + if (input & (1 << GUI_INPUT_BACK)) { + if (buttons & GUI_MESSAGE_BOX_CANCEL) { + return GUI_MENU_EXIT_BACK; + } + if (buttons & GUI_MESSAGE_BOX_OK) { + return GUI_MENU_EXIT_ACCEPT; + } + } + if (input & (1 << GUI_INPUT_CANCEL)) { + if (buttons & GUI_MESSAGE_BOX_CANCEL) { + return GUI_MENU_EXIT_CANCEL; + } + if (buttons & GUI_MESSAGE_BOX_OK) { + return GUI_MENU_EXIT_ACCEPT; + } + } + } } + return GUI_MENU_EXIT_CANCEL; } void GUIDrawBattery(struct GUIParams* params) {
M src/util/gui/menu.hsrc/util/gui/menu.h

@@ -35,8 +35,16 @@ GUI_MENU_EXIT_BACK,

GUI_MENU_EXIT_CANCEL, }; +enum GUIMessageBoxButtons { + GUI_MESSAGE_BOX_OK = 1, + GUI_MESSAGE_BOX_CANCEL = 2 +}; + struct GUIParams; enum GUIMenuExitReason GUIShowMenu(struct GUIParams* params, struct GUIMenu* menu, struct GUIMenuItem** item); + +ATTRIBUTE_FORMAT(printf, 4, 5) +enum GUIMenuExitReason GUIShowMessageBox(struct GUIParams* params, int buttons, int frames, const char* format, ...); void GUIDrawBattery(struct GUIParams* params); void GUIDrawClock(struct GUIParams* params);