src/gba/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 "gba/input.h"
9#include "util/gui.h"
10#include "util/gui/menu.h"
11
12void GBAGUIRemapKeys(struct GUIParams* params, struct GBAInputMap* map, const struct GUIInputKeys* keys) {
13 struct GUIMenu menu = {
14 .title = "Remap keys",
15 .index = 0,
16 .background = 0
17 };
18 GUIMenuItemListInit(&menu.items, 0);
19 const char* keyNames[keys->nKeys + 1];
20 memcpy(&keyNames[1], keys->keyNames, keys->nKeys * sizeof(keyNames[0]));
21 keyNames[0] = "Unmapped";
22 size_t i;
23 for (i = 0; i < GBA_KEY_MAX; ++i) {
24 *GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
25 .title = GBAKeyNames[i],
26 .data = (void*) (GUI_INPUT_MAX + i),
27 .submenu = 0,
28 .state = GBAInputQueryBinding(map, keys->id, i) + 1,
29 .validStates = keyNames,
30 .nStates = keys->nKeys + 1
31 };
32 }
33 *GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
34 .title = "Save",
35 .data = (void*) (GUI_INPUT_MAX + GBA_KEY_MAX + 2),
36 };
37 *GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
38 .title = "Cancel",
39 .data = 0,
40 };
41
42 struct GUIMenuItem* item;
43 while (true) {
44 enum GUIMenuExitReason reason;
45 reason = GUIShowMenu(params, &menu, &item);
46 if (reason != GUI_MENU_EXIT_ACCEPT || !item->data) {
47 break;
48 }
49 if (item->data == (void*) (GUI_INPUT_MAX + GBA_KEY_MAX + 2)) {
50 for (i = 0; i < GUIMenuItemListSize(&menu.items); ++i) {
51 item = GUIMenuItemListGetPointer(&menu.items, i);
52 if (i < GBA_KEY_MAX) {
53 GBAInputBindKey(map, keys->id, item->state - 1, i);
54 }
55 }
56 break;
57 }
58 if (item->validStates) {
59 // TODO: Open remap menu
60 }
61 }
62}