all repos — mgba @ c191ec88b83d9a989586d5590f5443031d82c68c

mGBA Game Boy Advance Emulator

src/feature/gui/remap.c (view raw)

 1/* Copyright (c) 2013-2016 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 "remap.h"
 7
 8#include "util/gui.h"
 9#include "util/gui/menu.h"
10
11void mGUIRemapKeys(struct GUIParams* params, struct mInputMap* map, const struct GUIInputKeys* keys) {
12	struct GUIMenu menu = {
13		.title = "Remap keys",
14		.index = 0,
15		.background = 0
16	};
17	GUIMenuItemListInit(&menu.items, 0);
18	const char* keyNames[keys->nKeys + 1];
19	memcpy(&keyNames[1], keys->keyNames, keys->nKeys * sizeof(keyNames[0]));
20	keyNames[0] = "Unmapped";
21	size_t i;
22	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
23		.title = "Game keys:",
24		.data = 0,
25	};
26	for (i = 0; i < map->info->nKeys; ++i) {
27		*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
28			.title = map->info->keyId[i],
29			.data = (void*) (GUI_INPUT_MAX + i + 1),
30			.submenu = 0,
31			.state = mInputQueryBinding(map, keys->id, i) + 1,
32			.validStates = keyNames,
33			.nStates = keys->nKeys + 1
34		};
35	}
36	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
37		.title = "Interface keys:",
38		.data = 0,
39	};
40	for (i = 0; i < params->keyMap.info->nKeys; ++i) {
41		if (!params->keyMap.info->keyId[i]) {
42			continue;
43		}
44		*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
45			.title = params->keyMap.info->keyId[i],
46			.data = (void*) i + 1,
47			.submenu = 0,
48			.state = mInputQueryBinding(&params->keyMap, keys->id, i) + 1,
49			.validStates = keyNames,
50			.nStates = keys->nKeys + 1
51		};
52	}
53	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
54		.title = "Save",
55		.data = (void*) (GUI_INPUT_MAX + map->info->nKeys + 2),
56	};
57	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
58		.title = "Cancel",
59		.data = 0,
60	};
61
62	struct GUIMenuItem* item;
63	while (true) {
64		enum GUIMenuExitReason reason;
65		reason = GUIShowMenu(params, &menu, &item);
66		if (reason != GUI_MENU_EXIT_ACCEPT || !item->data) {
67			break;
68		}
69		if (item->data == (void*) (GUI_INPUT_MAX + map->info->nKeys + 2)) {
70			for (i = 0; i < GUIMenuItemListSize(&menu.items); ++i) {
71				item = GUIMenuItemListGetPointer(&menu.items, i);
72				if ((uint32_t) item->data < 1) {
73					continue;
74				}
75				if ((uint32_t) item->data < GUI_INPUT_MAX + 1) {
76					mInputBindKey(&params->keyMap, keys->id, item->state - 1, (uint32_t) item->data - 1);
77				} else if ((uint32_t) item->data < GUI_INPUT_MAX + map->info->nKeys + 1) {
78					mInputBindKey(map, keys->id, item->state - 1, (uint32_t) item->data - GUI_INPUT_MAX - 1);
79				}
80			}
81			break;
82		}
83		if (item->validStates) {
84			// TODO: Open remap menu
85		}
86	}
87}