all repos — mgba @ 31f40b05ba0ca4be6bc0a1988ef81cac3e49307f

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
11DEFINE_VECTOR(GUIMenuItemList, struct GUIMenuItem);
12
13enum GUIMenuExitReason GUIShowMenu(struct GUIParams* params, struct GUIMenu* menu, struct GUIMenuItem* item) {
14	size_t start = 0;
15	size_t pageSize = params->height / GUIFontHeight(params->font);
16	if (pageSize > 4) {
17		pageSize -= 4;
18	} else {
19		pageSize = 1;
20	}
21
22	GUIInvalidateKeys(params);
23	while (true) {
24		int newInput = 0;
25		GUIPollInput(params, &newInput, 0);
26
27		if (newInput & (1 << GUI_INPUT_UP) && menu->index > 0) {
28			--menu->index;
29		}
30		if (newInput & (1 << GUI_INPUT_DOWN) && menu->index < GUIMenuItemListSize(&menu->items) - 1) {
31			++menu->index;
32		}
33		if (newInput & (1 << GUI_INPUT_LEFT)) {
34			if (menu->index >= pageSize) {
35				menu->index -= pageSize;
36			} else {
37				menu->index = 0;
38			}
39		}
40		if (newInput & (1 << GUI_INPUT_RIGHT)) {
41			if (menu->index + pageSize < GUIMenuItemListSize(&menu->items)) {
42				menu->index += pageSize;
43			} else {
44				menu->index = GUIMenuItemListSize(&menu->items) - 1;
45			}
46		}
47
48		if (menu->index < start) {
49			start = menu->index;
50		}
51		while ((menu->index - start + 4) * GUIFontHeight(params->font) > params->height) {
52			++start;
53		}
54		if (newInput & (1 << GUI_INPUT_CANCEL)) {
55			break;
56		}
57		if (newInput & (1 << GUI_INPUT_SELECT)) {
58			*item = *GUIMenuItemListGetPointer(&menu->items, menu->index);
59			return GUI_MENU_EXIT_ACCEPT;
60		}
61		if (newInput & (1 << GUI_INPUT_BACK)) {
62			return GUI_MENU_EXIT_BACK;
63		}
64
65		params->drawStart();
66		unsigned y = GUIFontHeight(params->font);
67		GUIFontPrint(params->font, 0, y, GUI_TEXT_LEFT, 0xFFFFFFFF, menu->title);
68		y += 2 * GUIFontHeight(params->font);
69		size_t i;
70		for (i = start; i < GUIMenuItemListSize(&menu->items); ++i) {
71			int color = 0xE0A0A0A0;
72			char bullet = ' ';
73			if (i == menu->index) {
74				color = 0xFFFFFFFF;
75				bullet = '>';
76			}
77			GUIFontPrintf(params->font, 0, y, GUI_TEXT_LEFT, color, "%c %s", bullet, GUIMenuItemListGetPointer(&menu->items, i)->title);
78			y += GUIFontHeight(params->font);
79			if (y + GUIFontHeight(params->font) > params->height) {
80				break;
81			}
82		}
83		y += GUIFontHeight(params->font) * 2;
84
85		params->drawEnd();
86	}
87	return GUI_MENU_EXIT_CANCEL;
88}