all repos — mgba @ c45315b96b3322e4b29f933b27d461f2a73e7bc7

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 lineHeight = GUIFontHeight(params->font);
 16	size_t pageSize = params->height / lineHeight;
 17	if (pageSize > 4) {
 18		pageSize -= 4;
 19	} else {
 20		pageSize = 1;
 21	}
 22	int cursorOverItem = 0;
 23
 24	GUIInvalidateKeys(params);
 25	while (true) {
 26		uint32_t newInput = 0;
 27		GUIPollInput(params, &newInput, 0);
 28		int cx, cy;
 29		enum GUICursorState cursor = GUIPollCursor(params, &cx, &cy);
 30
 31		if (newInput & (1 << GUI_INPUT_UP) && menu->index > 0) {
 32			--menu->index;
 33		}
 34		if (newInput & (1 << GUI_INPUT_DOWN) && menu->index < GUIMenuItemListSize(&menu->items) - 1) {
 35			++menu->index;
 36		}
 37		if (newInput & (1 << GUI_INPUT_LEFT)) {
 38			struct GUIMenuItem* item = GUIMenuItemListGetPointer(&menu->items, menu->index);
 39			if (item->validStates) {
 40				if (item->state > 0) {
 41					--item->state;
 42				}
 43			} else if (menu->index >= pageSize) {
 44				menu->index -= pageSize;
 45			} else {
 46				menu->index = 0;
 47			}
 48		}
 49		if (newInput & (1 << GUI_INPUT_RIGHT)) {
 50			struct GUIMenuItem* item = GUIMenuItemListGetPointer(&menu->items, menu->index);
 51			if (item->validStates) {
 52				if (item->validStates[item->state + 1]) {
 53					++item->state;
 54				}
 55			} else if (menu->index + pageSize < GUIMenuItemListSize(&menu->items)) {
 56				menu->index += pageSize;
 57			} else {
 58				menu->index = GUIMenuItemListSize(&menu->items) - 1;
 59			}
 60		}
 61		if (cursor != GUI_CURSOR_NOT_PRESENT) {
 62			int index = (cy / lineHeight) - 2;
 63			if (index >= 0 && index + start < GUIMenuItemListSize(&menu->items)) {
 64				if (menu->index != index + start || !cursorOverItem) {
 65					cursorOverItem = 1;
 66				}
 67				menu->index = index + start;
 68			} else {
 69				cursorOverItem = 0;
 70			}
 71		}
 72
 73		if (menu->index < start) {
 74			start = menu->index;
 75		}
 76		while ((menu->index - start + 4) * lineHeight > params->height) {
 77			++start;
 78		}
 79		if (newInput & (1 << GUI_INPUT_CANCEL)) {
 80			break;
 81		}
 82		if (newInput & (1 << GUI_INPUT_SELECT) || (cursorOverItem == 2 && cursor == GUI_CURSOR_CLICKED)) {
 83			*item = GUIMenuItemListGetPointer(&menu->items, menu->index);
 84			if ((*item)->submenu) {
 85				enum GUIMenuExitReason reason = GUIShowMenu(params, (*item)->submenu, item);
 86				if (reason != GUI_MENU_EXIT_BACK) {
 87					return reason;
 88				}
 89			} else {
 90				return GUI_MENU_EXIT_ACCEPT;
 91			}
 92		}
 93		if (cursorOverItem == 1 && (cursor == GUI_CURSOR_UP || cursor == GUI_CURSOR_NOT_PRESENT)) {
 94			cursorOverItem = 2;
 95		}
 96		if (newInput & (1 << GUI_INPUT_BACK)) {
 97			return GUI_MENU_EXIT_BACK;
 98		}
 99
100		params->drawStart();
101		if (menu->background) {
102			menu->background->draw(menu->background, GUIMenuItemListGetPointer(&menu->items, menu->index)->data);
103		}
104		if (params->guiPrepare) {
105			params->guiPrepare();
106		}
107		unsigned y = lineHeight;
108		GUIFontPrint(params->font, 0, y, GUI_TEXT_LEFT, 0xFFFFFFFF, menu->title);
109		y += 2 * lineHeight;
110		size_t i;
111		for (i = start; i < GUIMenuItemListSize(&menu->items); ++i) {
112			int color = 0xE0A0A0A0;
113			char bullet = ' ';
114			if (i == menu->index) {
115				color = 0xFFFFFFFF;
116				bullet = '>';
117			}
118			struct GUIMenuItem* item = GUIMenuItemListGetPointer(&menu->items, i);
119			GUIFontPrintf(params->font, 0, y, GUI_TEXT_LEFT, color, "%c %s", bullet, item->title);
120			if (item->validStates) {
121				GUIFontPrintf(params->font, params->width, y, GUI_TEXT_RIGHT, color, "%s ", item->validStates[item->state]);
122			}
123			y += lineHeight;
124			if (y + lineHeight > params->height) {
125				break;
126			}
127		}
128		if (params->guiFinish) {
129			params->guiFinish();
130		}
131		params->drawEnd();
132	}
133	return GUI_MENU_EXIT_CANCEL;
134}