all repos — mgba @ c191ec88b83d9a989586d5590f5443031d82c68c

mGBA Game Boy Advance Emulator

src/feature/gui/gui-config.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 "gui-config.h"
  7
  8#include "core/config.h"
  9#include "feature/gui/gui-runner.h"
 10#include "feature/gui/remap.h"
 11#include "gba/gba.h"
 12#include "util/gui/file-select.h"
 13#include "util/gui/menu.h"
 14
 15#ifndef GUI_MAX_INPUTS
 16#define GUI_MAX_INPUTS 7
 17#endif
 18
 19void mGUIShowConfig(struct mGUIRunner* runner, struct GUIMenuItem* extra, size_t nExtra) {
 20	struct GUIMenu menu = {
 21		.title = "Configure",
 22		.index = 0,
 23		.background = &runner->background.d
 24	};
 25	GUIMenuItemListInit(&menu.items, 0);
 26	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
 27		.title = "Frameskip",
 28		.data = "frameskip",
 29		.submenu = 0,
 30		.state = 0,
 31		.validStates = (const char*[]) {
 32			"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
 33		},
 34		.nStates = 10
 35	};
 36	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
 37		.title = "Show framerate",
 38		.data = "fpsCounter",
 39		.submenu = 0,
 40		.state = false,
 41		.validStates = (const char*[]) {
 42			"Off", "On"
 43		},
 44		.nStates = 2
 45	};
 46	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
 47		.title = "Use BIOS if found",
 48		.data = "useBios",
 49		.submenu = 0,
 50		.state = true,
 51		.validStates = (const char*[]) {
 52			"Off", "On"
 53		},
 54		.nStates = 2
 55	};
 56	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
 57		.title = "Select BIOS path",
 58		.data = "bios",
 59	};
 60	size_t i;
 61	const char* mapNames[GUI_MAX_INPUTS + 1];
 62	if (runner->keySources) {
 63		for (i = 0; runner->keySources[i].id && i < GUI_MAX_INPUTS; ++i) {
 64			mapNames[i] = runner->keySources[i].name;
 65		}
 66		if (i == 1) {
 67			// Don't display a name if there's only one input source
 68			i = 0;
 69		}
 70		*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
 71			.title = "Remap controls",
 72			.data = "*REMAP",
 73			.state = 0,
 74			.validStates = i ? mapNames : 0,
 75			.nStates = i
 76		};
 77	}
 78	for (i = 0; i < nExtra; ++i) {
 79		*GUIMenuItemListAppend(&menu.items) = extra[i];
 80	}
 81	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
 82		.title = "Save",
 83		.data = "*SAVE",
 84	};
 85	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
 86		.title = "Cancel",
 87		.data = 0,
 88	};
 89	enum GUIMenuExitReason reason;
 90	char biosPath[256] = "";
 91
 92	struct GUIMenuItem* item;
 93	for (i = 0; i < GUIMenuItemListSize(&menu.items); ++i) {
 94		item = GUIMenuItemListGetPointer(&menu.items, i);
 95		if (!item->validStates || !item->data) {
 96			continue;
 97		}
 98		mCoreConfigGetUIntValue(&runner->config, item->data, &item->state);
 99	}
100
101	while (true) {
102		reason = GUIShowMenu(&runner->params, &menu, &item);
103		if (reason != GUI_MENU_EXIT_ACCEPT || !item->data) {
104			break;
105		}
106		if (!strcmp(item->data, "*SAVE")) {
107			if (biosPath[0]) {
108				mCoreConfigSetValue(&runner->config, "bios", biosPath);
109			}
110			for (i = 0; i < GUIMenuItemListSize(&menu.items); ++i) {
111				item = GUIMenuItemListGetPointer(&menu.items, i);
112				if (!item->validStates || !item->data) {
113					continue;
114				}
115				mCoreConfigSetUIntValue(&runner->config, item->data, item->state);
116			}
117			mCoreConfigSave(&runner->config);
118			break;
119		}
120		if (!strcmp(item->data, "*REMAP")) {
121			mGUIRemapKeys(&runner->params, &runner->core->inputMap, &runner->keySources[item->state]);
122			continue;
123		}
124		if (!strcmp(item->data, "bios")) {
125			// TODO: show box if failed
126			if (!GUISelectFile(&runner->params, biosPath, sizeof(biosPath), GBAIsBIOS)) {
127				biosPath[0] = '\0';
128			}
129			continue;
130		}
131		if (item->validStates) {
132			++item->state;
133			if (item->state >= item->nStates) {
134				item->state = 0;
135			}
136		}
137	}
138}