all repos — mgba @ c3aededf05edf9337f89191dfdbfc3189885fb14

mGBA Game Boy Advance Emulator

GUI: Header with time and battery level
Jeffrey Pfau jeffrey@endrift.com
Sat, 19 Sep 2015 00:32:49 -0700
commit

c3aededf05edf9337f89191dfdbfc3189885fb14

parent

91fb4407b9a3bc64c84d74eaffe9673d89059a10

M src/platform/3ds/main.csrc/platform/3ds/main.c

@@ -85,6 +85,21 @@ gfxSwapBuffersGpu();

gspWaitForEvent(GSPEVENT_VBlank0, false); } +static int _batteryState(void) { + u8 charge; + u8 adapter; + PTMU_GetBatteryLevel(0, &charge); + PTMU_GetBatteryChargeState(0, &adapter); + int state = 0; + if (adapter) { + state |= BATTERY_CHARGING; + } + if (charge > 0) { + --charge; + } + return state | charge; +} + static void _guiPrepare(void) { guiDrawn = GUI_ACTIVE | GUI_THIS_FRAME; int screen = screenMode < SM_PA_TOP ? GFX_BOTTOM : GFX_TOP;

@@ -385,6 +400,7 @@ }

} int main() { + ptmInit(); hasSound = !csndInit(); rotation.d.sample = _sampleRotation;

@@ -444,12 +460,13 @@ 320, 240,

font, "/", _drawStart, _drawEnd, _pollInput, _pollCursor, + _batteryState, _guiPrepare, _guiFinish, GUI_PARAMS_TRAIL }, .configExtra = (struct GUIMenuItem[]) { - { + { .title = "Screen mode", .data = "screenMode", .submenu = 0,

@@ -496,5 +513,6 @@ linearFree(audioLeft);

linearFree(audioRight); } csndExit(); + ptmExit(); return 0; }
M src/platform/psp2/main.csrc/platform/psp2/main.c

@@ -85,6 +85,7 @@ .params = {

PSP2_HORIZONTAL_PIXELS, PSP2_VERTICAL_PIXELS, font, "cache0:", _drawStart, _drawEnd, _pollInput, _pollCursor, + 0, 0, 0, GUI_PARAMS_TRAIL
M src/platform/wii/main.csrc/platform/wii/main.c

@@ -158,6 +158,7 @@ 352, 230,

font, "/", _drawStart, _drawEnd, _pollInput, _pollCursor, + 0, _guiPrepare, _guiFinish, GUI_PARAMS_TRAIL
M src/util/gui.csrc/util/gui.c

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

*heldInput = input; } } - -enum GUICursorState GUIPollCursor(struct GUIParams* params, int* x, int* 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.hsrc/util/gui.h

@@ -36,6 +36,16 @@ GUI_CURSOR_CLICKED,

GUI_CURSOR_DRAGGING }; +enum { + BATTERY_EMPTY = 0, + BATTERY_LOW = 1, + BATTERY_HALF = 2, + BATTERY_HIGH = 3, + BATTERY_FULL = 4, + + BATTERY_CHARGING = 8 +}; + struct GUIBackground { void (*draw)(struct GUIBackground*, void* context); };

@@ -50,6 +60,7 @@ void (*drawStart)(void);

void (*drawEnd)(void); uint32_t (*pollInput)(void); enum GUICursorState (*pollCursor)(int* x, int* y); + int (*batteryState)(void); void (*guiPrepare)(void); void (*guiFinish)(void);
M src/util/gui/file-select.csrc/util/gui/file-select.c

@@ -103,8 +103,8 @@ params->drawStart();

if (params->guiPrepare) { params->guiPrepare(); } - GUIFontPrintf(params->font, 0, GUIFontHeight(params->font), GUI_TEXT_LEFT, 0xFFFFFFFF, "%s", currentPath); - GUIFontPrintf(params->font, 0, GUIFontHeight(params->font) * 2, GUI_TEXT_LEFT, 0xFFFFFFFF, "(scanning item %zu of %zu)", i, items); + GUIFontPrintf(params->font, 0, GUIFontHeight(params->font), GUI_TEXT_LEFT, 0xFFFFFFFF, "(scanning item %zu of %zu)", i, items); + GUIFontPrintf(params->font, 0, GUIFontHeight(params->font) * 2, GUI_TEXT_LEFT, 0xFFFFFFFF, "%s", currentPath); if (params->guiFinish) { params->guiFinish(); }

@@ -130,7 +130,8 @@ }

bool GUISelectFile(struct GUIParams* params, char* outPath, size_t outLen, bool (*filter)(struct VFile*)) { struct GUIMenu menu = { - .title = params->currentPath, + .title = "Select file", + .subtitle = params->currentPath, .index = params->fileIndex, }; GUIMenuItemListInit(&menu.items, 0);
M src/util/gui/menu.csrc/util/gui/menu.c

@@ -106,6 +106,9 @@ params->guiPrepare();

} unsigned y = lineHeight; GUIFontPrint(params->font, 0, y, GUI_TEXT_LEFT, 0xFFFFFFFF, menu->title); + if (menu->subtitle) { + GUIFontPrint(params->font, 0, y * 2, GUI_TEXT_LEFT, 0xFFFFFFFF, menu->subtitle); + } y += 2 * lineHeight; size_t i; for (i = start; i < GUIMenuItemListSize(&menu->items); ++i) {

@@ -125,6 +128,10 @@ if (y + lineHeight > params->height) {

break; } } + + GUIDrawBattery(params); + GUIDrawClock(params); + if (params->guiFinish) { params->guiFinish(); }

@@ -132,3 +139,91 @@ params->drawEnd();

} return GUI_MENU_EXIT_CANCEL; } + +enum GUICursorState GUIPollCursor(struct GUIParams* params, int* x, int* 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; + } +} + +void GUIDrawBattery(struct GUIParams* params) { + if (!params->batteryState) { + return; + } + int state = params->batteryState(); + uint32_t color = 0xFF000000; + if (state == (BATTERY_CHARGING | BATTERY_FULL)) { + color |= 0x2020FF; + } else if (state & BATTERY_CHARGING) { + color |= 0x20FF20; + } else if (state >= BATTERY_HALF) { + color |= 0xFFFFFF; + } else if (state == BATTERY_LOW) { + color |= 0x20FFFF; + } else { + color |= 0xFF2020; + } + + const char* batteryText; + switch (state & ~BATTERY_CHARGING) { + case BATTERY_EMPTY: + batteryText = "[ ]"; + break; + case BATTERY_LOW: + batteryText = "[I ]"; + break; + case BATTERY_HALF: + batteryText = "[II ]"; + break; + case BATTERY_HIGH: + batteryText = "[III ]"; + break; + case BATTERY_FULL: + batteryText = "[IIII]"; + break; + default: + batteryText = "[????]"; + break; + } + + GUIFontPrint(params->font, params->width, GUIFontHeight(params->font), GUI_TEXT_RIGHT, color, batteryText); +} + +void GUIDrawClock(struct GUIParams* params) { + char buffer[32]; + time_t t = time(0); + struct tm* tm = localtime(&t); + strftime(buffer, sizeof(buffer), "%H:%M:%S", tm); + GUIFontPrint(params->font, params->width / 2, GUIFontHeight(params->font), GUI_TEXT_CENTER, 0xFFFFFFFF, buffer); +}
M src/util/gui/menu.hsrc/util/gui/menu.h

@@ -22,6 +22,7 @@

struct GUIBackground; struct GUIMenu { const char* title; + const char* subtitle; struct GUIMenuItemList items; size_t index; struct GUIBackground* background;

@@ -35,5 +36,8 @@ };

struct GUIParams; enum GUIMenuExitReason GUIShowMenu(struct GUIParams* params, struct GUIMenu* menu, struct GUIMenuItem** item); + +void GUIDrawBattery(struct GUIParams* params); +void GUIDrawClock(struct GUIParams* params); #endif