all repos — mgba @ f836a67863e66499c812c8d963d37ff56ad10638

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 = "Frameskip",
 21		.data = "frameskip",
 22		.submenu = 0,
 23		.state = 0,
 24		.validStates = (const char*[]) {
 25			"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", 0
 26		}
 27	};
 28	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
 29		.title = "Show framerate",
 30		.data = "fpsCounter",
 31		.submenu = 0,
 32		.state = false,
 33		.validStates = (const char*[]) {
 34			"Off", "On", 0
 35		}
 36	};
 37	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
 38		.title = "Use BIOS if found",
 39		.data = "useBios",
 40		.submenu = 0,
 41		.state = true,
 42		.validStates = (const char*[]) {
 43			"Off", "On", 0
 44		}
 45	};
 46	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
 47		.title = "Select BIOS path",
 48		.data = "bios",
 49	};
 50	size_t i;
 51	for (i = 0; i < nExtra; ++i) {
 52		*GUIMenuItemListAppend(&menu.items) = extra[i];
 53	}
 54	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
 55		.title = "Save",
 56		.data = "[SAVE]",
 57	};
 58	*GUIMenuItemListAppend(&menu.items) = (struct GUIMenuItem) {
 59		.title = "Cancel",
 60		.data = 0,
 61	};
 62	enum GUIMenuExitReason reason;
 63	char biosPath[256] = "";
 64
 65	struct GUIMenuItem* item;
 66	for (i = 0; i < GUIMenuItemListSize(&menu.items); ++i) {
 67		item = GUIMenuItemListGetPointer(&menu.items, i);
 68		if (!item->validStates || !item->data) {
 69			continue;
 70		}
 71		GBAConfigGetUIntValue(&runner->context.config, item->data, &item->state);
 72	}
 73
 74	while (true) {
 75		reason = GUIShowMenu(&runner->params, &menu, &item);
 76		if (reason != GUI_MENU_EXIT_ACCEPT || !item->data) {
 77			break;
 78		}
 79		if (!strcmp(item->data, "[SAVE]")) {
 80			if (biosPath[0]) {
 81				GBAConfigSetValue(&runner->context.config, "bios", biosPath);
 82			}
 83			for (i = 0; i < GUIMenuItemListSize(&menu.items); ++i) {
 84				item = GUIMenuItemListGetPointer(&menu.items, i);
 85				if (!item->validStates || !item->data) {
 86					continue;
 87				}
 88				GBAConfigSetUIntValue(&runner->context.config, item->data, item->state);
 89			}
 90			GBAConfigSave(&runner->context.config);
 91			break;
 92		}
 93		if (!strcmp(item->data, "bios")) {
 94			// TODO: show box if failed
 95			if (!GUISelectFile(&runner->params, biosPath, sizeof(biosPath), GBAIsBIOS)) {
 96				biosPath[0] = '\0';
 97			}
 98			continue;
 99		}
100		if (item->validStates) {
101			++item->state;
102			if (!item->validStates[item->state]) {
103				item->state = 0;
104			}
105		}
106	}
107}