all repos — mgba @ 1fce327192bef81e4ddc1ce1acde3688c74fab59

mGBA Game Boy Advance Emulator

src/util/gui/menu.c (view raw)

  1/* Copyright (c) 2013-2015 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "menu.h"
  7
  8#include "util/gui.h"
  9#include "util/gui/font.h"
 10
 11#ifdef _3DS
 12#include <3ds.h>
 13#endif
 14
 15DEFINE_VECTOR(GUIMenuItemList, struct GUIMenuItem);
 16
 17enum GUIMenuExitReason GUIShowMenu(struct GUIParams* params, struct GUIMenu* menu, struct GUIMenuItem** item) {
 18	size_t start = 0;
 19	size_t lineHeight = GUIFontHeight(params->font);
 20	size_t pageSize = params->height / lineHeight;
 21	if (pageSize > 4) {
 22		pageSize -= 4;
 23	} else {
 24		pageSize = 1;
 25	}
 26	int cursorOverItem = 0;
 27
 28	GUIInvalidateKeys(params);
 29	while (true) {
 30#ifdef _3DS
 31		if (!aptMainLoop()) {
 32			return GUI_MENU_EXIT_CANCEL;
 33		}
 34#endif
 35		uint32_t newInput = 0;
 36		GUIPollInput(params, &newInput, 0);
 37		unsigned cx, cy;
 38		enum GUICursorState cursor = GUIPollCursor(params, &cx, &cy);
 39
 40		if (newInput & (1 << GUI_INPUT_UP) && menu->index > 0) {
 41			--menu->index;
 42		}
 43		if (newInput & (1 << GUI_INPUT_DOWN) && menu->index < GUIMenuItemListSize(&menu->items) - 1) {
 44			++menu->index;
 45		}
 46		if (newInput & (1 << GUI_INPUT_LEFT)) {
 47			struct GUIMenuItem* item = GUIMenuItemListGetPointer(&menu->items, menu->index);
 48			if (item->validStates) {
 49				if (item->state > 0) {
 50					unsigned oldState = item->state;
 51					do {
 52						--item->state;
 53					} while (!item->validStates[item->state] && item->state > 0);
 54					if (!item->validStates[item->state]) {
 55						item->state = oldState;
 56					}
 57				}
 58			} else if (menu->index >= pageSize) {
 59				menu->index -= pageSize;
 60			} else {
 61				menu->index = 0;
 62			}
 63		}
 64		if (newInput & (1 << GUI_INPUT_RIGHT)) {
 65			struct GUIMenuItem* item = GUIMenuItemListGetPointer(&menu->items, menu->index);
 66			if (item->validStates) {
 67				if (item->state < item->nStates - 1) {
 68					unsigned oldState = item->state;
 69					do {
 70						++item->state;
 71					} while (!item->validStates[item->state] && item->state < item->nStates - 1);
 72					if (!item->validStates[item->state]) {
 73						item->state = oldState;
 74					}
 75				}
 76			} else if (menu->index + pageSize < GUIMenuItemListSize(&menu->items)) {
 77				menu->index += pageSize;
 78			} else {
 79				menu->index = GUIMenuItemListSize(&menu->items) - 1;
 80			}
 81		}
 82		if (cursor != GUI_CURSOR_NOT_PRESENT) {
 83			if (cx < params->width - 16) {
 84				int index = (cy / lineHeight) - 2;
 85				if (index >= 0 && index + start < GUIMenuItemListSize(&menu->items)) {
 86					if (menu->index != index + start || !cursorOverItem) {
 87						cursorOverItem = 1;
 88					}
 89					menu->index = index + start;
 90				} else {
 91					cursorOverItem = 0;
 92				}
 93			} else if (cursor == GUI_CURSOR_DOWN || cursor == GUI_CURSOR_DRAGGING) {
 94				if (cy <= 2 * lineHeight && cy > lineHeight && menu->index > 0) {
 95					--menu->index;
 96				} else if (cy <= params->height && cy > params->height - lineHeight && menu->index < GUIMenuItemListSize(&menu->items) - 1) {
 97					++menu->index;
 98				} else if (cy <= params->height - lineHeight && cy > 2 * lineHeight) {
 99					size_t location = cy - 2 * lineHeight;
100					location *= GUIMenuItemListSize(&menu->items) - 1;
101					menu->index = location / (params->height - 3 * lineHeight);
102				}
103			}
104		}
105
106		if (menu->index < start) {
107			start = menu->index;
108		}
109		while ((menu->index - start + 4) * lineHeight > params->height) {
110			++start;
111		}
112		if (newInput & (1 << GUI_INPUT_CANCEL)) {
113			break;
114		}
115		if (newInput & (1 << GUI_INPUT_SELECT) || (cursorOverItem == 2 && cursor == GUI_CURSOR_CLICKED)) {
116			*item = GUIMenuItemListGetPointer(&menu->items, menu->index);
117			if ((*item)->submenu) {
118				enum GUIMenuExitReason reason = GUIShowMenu(params, (*item)->submenu, item);
119				if (reason != GUI_MENU_EXIT_BACK) {
120					return reason;
121				}
122			} else {
123				return GUI_MENU_EXIT_ACCEPT;
124			}
125		}
126		if (cursorOverItem == 1 && (cursor == GUI_CURSOR_UP || cursor == GUI_CURSOR_NOT_PRESENT)) {
127			cursorOverItem = 2;
128		}
129		if (newInput & (1 << GUI_INPUT_BACK)) {
130			return GUI_MENU_EXIT_BACK;
131		}
132
133		params->drawStart();
134		if (menu->background) {
135			menu->background->draw(menu->background, GUIMenuItemListGetPointer(&menu->items, menu->index)->data);
136		}
137		if (params->guiPrepare) {
138			params->guiPrepare();
139		}
140		unsigned y = lineHeight;
141		GUIFontPrint(params->font, 0, y, GUI_ALIGN_LEFT, 0xFFFFFFFF, menu->title);
142		if (menu->subtitle) {
143			GUIFontPrint(params->font, 0, y * 2, GUI_ALIGN_LEFT, 0xFFFFFFFF, menu->subtitle);
144		}
145		y += 2 * lineHeight;
146		size_t itemsPerScreen = (params->height - y) / lineHeight;
147		size_t i;
148		for (i = start; i < GUIMenuItemListSize(&menu->items); ++i) {
149			int color = 0xE0A0A0A0;
150			if (i == menu->index) {
151				color = 0xFFFFFFFF;
152				GUIFontDrawIcon(params->font, lineHeight * 0.8f, y, GUI_ALIGN_BOTTOM | GUI_ALIGN_RIGHT, GUI_ORIENT_0, 0xFFFFFFFF, GUI_ICON_POINTER);
153			}
154			struct GUIMenuItem* item = GUIMenuItemListGetPointer(&menu->items, i);
155			GUIFontPrint(params->font, lineHeight, y, GUI_ALIGN_LEFT, color, item->title);
156			if (item->validStates && item->validStates[item->state]) {
157				GUIFontPrintf(params->font, params->width, y, GUI_ALIGN_RIGHT, color, "%s ", item->validStates[item->state]);
158			}
159			y += lineHeight;
160			if (y + lineHeight > params->height) {
161				break;
162			}
163		}
164
165		if (itemsPerScreen < GUIMenuItemListSize(&menu->items)) {
166			size_t top = 2 * lineHeight;
167			size_t bottom = params->height - 8;
168			GUIFontDrawIcon(params->font, params->width - 8, top, GUI_ALIGN_HCENTER | GUI_ALIGN_BOTTOM, GUI_ORIENT_0, 0xFFFFFFFF, GUI_ICON_SCROLLBAR_BUTTON);
169			GUIFontDrawIconSize(params->font, params->width - 9, top, 2, bottom - top, 0xFFFFFFFF, GUI_ICON_SCROLLBAR_TRACK);
170			GUIFontDrawIcon(params->font, params->width - 8, bottom, GUI_ALIGN_HCENTER | GUI_ALIGN_TOP, GUI_ORIENT_0, 0xFFFFFFFF, GUI_ICON_SCROLLBAR_BUTTON);
171
172			y = menu->index * (bottom - top - 16) / GUIMenuItemListSize(&menu->items);
173			GUIFontDrawIcon(params->font, params->width - 8, top + y, GUI_ALIGN_HCENTER | GUI_ALIGN_TOP, GUI_ORIENT_0, 0xFFFFFFFF, GUI_ICON_SCROLLBAR_THUMB);
174		}
175
176		GUIDrawBattery(params);
177		GUIDrawClock(params);
178
179		if (cursor != GUI_CURSOR_NOT_PRESENT) {
180			GUIFontDrawIcon(params->font, cx, cy, GUI_ALIGN_HCENTER | GUI_ALIGN_TOP, GUI_ORIENT_0, 0xFFFFFFFF, GUI_ICON_CURSOR);
181		}
182
183		if (params->guiFinish) {
184			params->guiFinish();
185		}
186		params->drawEnd();
187	}
188	return GUI_MENU_EXIT_CANCEL;
189}
190
191enum GUICursorState GUIPollCursor(struct GUIParams* params, unsigned* x, unsigned* y) {
192	if (!params->pollCursor) {
193		return GUI_CURSOR_NOT_PRESENT;
194	}
195	enum GUICursorState state = params->pollCursor(x, y);
196	if (params->cursorState == GUI_CURSOR_DOWN) {
197		int dragX = *x - params->cx;
198		int dragY = *y - params->cy;
199		if (dragX * dragX + dragY * dragY > 25) {
200			params->cursorState = GUI_CURSOR_DRAGGING;
201			return GUI_CURSOR_DRAGGING;
202		}
203		if (state == GUI_CURSOR_UP || state == GUI_CURSOR_NOT_PRESENT) {
204			params->cursorState = GUI_CURSOR_UP;
205			return GUI_CURSOR_CLICKED;
206		}
207	} else {
208		params->cx = *x;
209		params->cy = *y;
210	}
211	if (params->cursorState == GUI_CURSOR_DRAGGING) {
212		if (state == GUI_CURSOR_UP || state == GUI_CURSOR_NOT_PRESENT) {
213			params->cursorState = GUI_CURSOR_UP;
214			return GUI_CURSOR_UP;
215		}
216		return GUI_CURSOR_DRAGGING;
217	}
218	params->cursorState = state;
219	return params->cursorState;
220}
221
222void GUIInvalidateKeys(struct GUIParams* params) {
223	for (int i = 0; i < GUI_INPUT_MAX; ++i) {
224		params->inputHistory[i] = 0;
225	}
226}
227
228void GUIDrawBattery(struct GUIParams* params) {
229	if (!params->batteryState) {
230		return;
231	}
232	int state = params->batteryState();
233	uint32_t color = 0xFF000000;
234	if (state == (BATTERY_CHARGING | BATTERY_FULL)) {
235		color |= 0xFFC060;
236	} else if (state & BATTERY_CHARGING) {
237		color |= 0x60FF60;
238	} else if (state >= BATTERY_HALF) {
239		color |= 0xFFFFFF;
240	} else if (state == BATTERY_LOW) {
241		color |= 0x30FFFF;
242	} else {
243		color |= 0x3030FF;
244	}
245
246	enum GUIIcon batteryIcon;
247	switch (state & ~BATTERY_CHARGING) {
248	case BATTERY_EMPTY:
249		batteryIcon = GUI_ICON_BATTERY_EMPTY;
250		break;
251	case BATTERY_LOW:
252		batteryIcon = GUI_ICON_BATTERY_LOW;
253		break;
254	case BATTERY_HALF:
255		batteryIcon = GUI_ICON_BATTERY_HALF;
256		break;
257	case BATTERY_HIGH:
258		batteryIcon = GUI_ICON_BATTERY_HIGH;
259		break;
260	case BATTERY_FULL:
261		batteryIcon = GUI_ICON_BATTERY_FULL;
262		break;
263	default:
264		batteryIcon = GUI_ICON_BATTERY_EMPTY;
265		break;
266	}
267
268	GUIFontDrawIcon(params->font, params->width, 0, GUI_ALIGN_RIGHT, GUI_ORIENT_0, color, batteryIcon);
269}
270
271void GUIDrawClock(struct GUIParams* params) {
272	char buffer[32];
273	time_t t = time(0);
274	struct tm tm;
275	localtime_r(&t, &tm);
276	strftime(buffer, sizeof(buffer), "%H:%M:%S", &tm);
277	GUIFontPrint(params->font, params->width / 2, GUIFontHeight(params->font), GUI_ALIGN_HCENTER, 0xFFFFFFFF, buffer);
278}