all repos — mgba @ c45315b96b3322e4b29f933b27d461f2a73e7bc7

mGBA Game Boy Advance Emulator

src/gba/gui/gui-config.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 "gui-config.h"
 7
 8#include "gba/gui/gui-runner.h"
 9#include "util/gui/file-select.h"
10#include "util/gui/menu.h"
11
12void GBAGUIShowConfig(struct GBAGUIRunner* runner, struct GUIMenuItem* extra, size_t nExtra) {
13	struct GUIMenu menu = {
14		.title = "Configure",
15		.index = 0,
16		.background = &runner->background.d
17	};
18	GUIMenuItemListInit(&menu.items, 0);
19	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
20		.title = "Use BIOS if found",
21		.data = "useBios",
22		.submenu = 0,
23		.state = true,
24		.validStates = (const char*[]) {
25			"Off", "On", 0
26		}
27	};
28	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
29		.title = "Select BIOS path",
30		.data = "bios",
31	};
32	size_t i;
33	for (i = 0; i < nExtra; ++i) {
34		*GUIMenuItemListAppend(&menu.items) = extra[i];
35	}
36	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
37		.title = "Save",
38		.data = "[SAVE]",
39	};
40	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
41		.title = "Cancel",
42		.data = 0,
43	};
44	enum GUIMenuExitReason reason;
45	char biosPath[256] = "";
46
47	struct GUIMenuItem* item;
48	for (i = 0; i < GUIMenuItemListSize(&menu.items); ++i) {
49		item = GUIMenuItemListGetPointer(&menu.items, i);
50		if (!item->validStates || !item->data) {
51			continue;
52		}
53		GBAConfigGetUIntValue(&runner->context.config, item->data, &item->state);
54	}
55
56	while (true) {
57		reason = GUIShowMenu(&runner->params, &menu, &item);
58		if (reason != GUI_MENU_EXIT_ACCEPT || !item->data) {
59			break;
60		}
61		if (!strcmp(item->data, "[SAVE]")) {
62			if (biosPath[0]) {
63				GBAConfigSetValue(&runner->context.config, "bios", biosPath);
64			}
65			for (i = 0; i < GUIMenuItemListSize(&menu.items); ++i) {
66				item = GUIMenuItemListGetPointer(&menu.items, i);
67				if (!item->validStates || !item->data) {
68					continue;
69				}
70				GBAConfigSetUIntValue(&runner->context.config, item->data, item->state);
71			}
72			GBAConfigSave(&runner->context.config);
73			break;
74		}
75		if (!strcmp(item->data, "bios")) {
76			// TODO: show box if failed
77			if (!GUISelectFile(&runner->params, biosPath, sizeof(biosPath), GBAIsBIOS)) {
78				biosPath[0] = '\0';
79			}
80			continue;
81		}
82		if (item->validStates) {
83			++item->state;
84			if (!item->validStates[item->state]) {
85				item->state = 0;
86			}
87		}
88	}
89}