all repos — mgba @ f1fba591524169276a95043354601b969d4deeb4

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);
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, 2, y, GUI_ALIGN_BOTTOM | GUI_ALIGN_LEFT, GUI_ORIENT_0, 0xFFFFFFFF, GUI_ICON_POINTER);
153			}
154			struct GUIMenuItem* item = GUIMenuItemListGetPointer(&menu->items, i);
155			GUIFontPrintf(params->font, 0, y, GUI_ALIGN_LEFT, color, "  %s", 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			y = 2 * lineHeight;
167			GUIFontDrawIcon(params->font, params->width - 8, y, GUI_ALIGN_HCENTER | GUI_ALIGN_BOTTOM, GUI_ORIENT_VMIRROR, 0xFFFFFFFF, GUI_ICON_SCROLLBAR_BUTTON);
168			for (; y < params->height - 16; y += 16) {
169				GUIFontDrawIcon(params->font, params->width - 8, y, GUI_ALIGN_HCENTER | GUI_ALIGN_TOP, GUI_ORIENT_0, 0xFFFFFFFF, GUI_ICON_SCROLLBAR_TRACK);
170			}
171			GUIFontDrawIcon(params->font, params->width - 8, y, GUI_ALIGN_HCENTER | GUI_ALIGN_TOP, GUI_ORIENT_0, 0xFFFFFFFF, GUI_ICON_SCROLLBAR_BUTTON);
172
173			size_t top = 2 * lineHeight;
174			y = menu->index * (y - top - 16) / GUIMenuItemListSize(&menu->items);
175			GUIFontDrawIcon(params->font, params->width - 8, top + y, GUI_ALIGN_HCENTER | GUI_ALIGN_TOP, GUI_ORIENT_0, 0xFFFFFFFF, GUI_ICON_SCROLLBAR_THUMB);
176		}
177
178		GUIDrawBattery(params);
179		GUIDrawClock(params);
180
181		if (cursor != GUI_CURSOR_NOT_PRESENT) {
182			GUIFontDrawIcon(params->font, cx, cy, GUI_ALIGN_HCENTER | GUI_ALIGN_TOP, GUI_ORIENT_0, 0xFFFFFFFF, GUI_ICON_CURSOR);
183		}
184
185		if (params->guiFinish) {
186			params->guiFinish();
187		}
188		params->drawEnd();
189	}
190	return GUI_MENU_EXIT_CANCEL;
191}
192
193enum GUICursorState GUIPollCursor(struct GUIParams* params, unsigned* x, unsigned* y) {
194	if (!params->pollCursor) {
195		return GUI_CURSOR_NOT_PRESENT;
196	}
197	enum GUICursorState state = params->pollCursor(x, y);
198	if (params->cursorState == GUI_CURSOR_DOWN) {
199		int dragX = *x - params->cx;
200		int dragY = *y - params->cy;
201		if (dragX * dragX + dragY * dragY > 25) {
202			params->cursorState = GUI_CURSOR_DRAGGING;
203			return GUI_CURSOR_DRAGGING;
204		}
205		if (state == GUI_CURSOR_UP || state == GUI_CURSOR_NOT_PRESENT) {
206			params->cursorState = GUI_CURSOR_UP;
207			return GUI_CURSOR_CLICKED;
208		}
209	} else {
210		params->cx = *x;
211		params->cy = *y;
212	}
213	if (params->cursorState == GUI_CURSOR_DRAGGING) {
214		if (state == GUI_CURSOR_UP || state == GUI_CURSOR_NOT_PRESENT) {
215			params->cursorState = GUI_CURSOR_UP;
216			return GUI_CURSOR_UP;
217		}
218		return GUI_CURSOR_DRAGGING;
219	}
220	params->cursorState = state;
221	return params->cursorState;
222}
223
224void GUIInvalidateKeys(struct GUIParams* params) {
225	for (int i = 0; i < GUI_INPUT_MAX; ++i) {
226		params->inputHistory[i] = 0;
227	}
228}
229
230void GUIDrawBattery(struct GUIParams* params) {
231	if (!params->batteryState) {
232		return;
233	}
234	int state = params->batteryState();
235	uint32_t color = 0xFF000000;
236	if (state == (BATTERY_CHARGING | BATTERY_FULL)) {
237		color |= 0xFFC060;
238	} else if (state & BATTERY_CHARGING) {
239		color |= 0x60FF60;
240	} else if (state >= BATTERY_HALF) {
241		color |= 0xFFFFFF;
242	} else if (state == BATTERY_LOW) {
243		color |= 0x30FFFF;
244	} else {
245		color |= 0x3030FF;
246	}
247
248	enum GUIIcon batteryIcon;
249	switch (state & ~BATTERY_CHARGING) {
250	case BATTERY_EMPTY:
251		batteryIcon = GUI_ICON_BATTERY_EMPTY;
252		break;
253	case BATTERY_LOW:
254		batteryIcon = GUI_ICON_BATTERY_LOW;
255		break;
256	case BATTERY_HALF:
257		batteryIcon = GUI_ICON_BATTERY_HALF;
258		break;
259	case BATTERY_HIGH:
260		batteryIcon = GUI_ICON_BATTERY_HIGH;
261		break;
262	case BATTERY_FULL:
263		batteryIcon = GUI_ICON_BATTERY_FULL;
264		break;
265	default:
266		batteryIcon = GUI_ICON_BATTERY_EMPTY;
267		break;
268	}
269
270	GUIFontDrawIcon(params->font, params->width, 0, GUI_ALIGN_RIGHT, GUI_ORIENT_0, color, batteryIcon);
271}
272
273void GUIDrawClock(struct GUIParams* params) {
274	char buffer[32];
275	time_t t = time(0);
276	struct tm tm;
277	localtime_r(&t, &tm);
278	strftime(buffer, sizeof(buffer), "%H:%M:%S", &tm);
279	GUIFontPrint(params->font, params->width / 2, GUIFontHeight(params->font), GUI_ALIGN_HCENTER, 0xFFFFFFFF, buffer);
280}