/***************************************************************************

	menu.c

	PSP user interface menu functions.

***************************************************************************/

#include "ncdz.h"

#define MENU_BLANK	{ "\n", }

static int state_loaded;


/*------------------------------------------------------
	エミュレーションリセット
 -----------------------------------------------------*/

int menu_reset(void)
{
	if (messagebox(MB_RESETEMULATION))
	{
		video_clear_screen32();
		video_flip_screen(1);
		bLoop = LOOP_RESET;
		bSleep = 0;
		return 1;
	}
	return 0;
}


/*------------------------------------------------------
	エミュレーション再起動
 -----------------------------------------------------*/

int menu_restart(void)
{
	if (messagebox(MB_RESTARTEMULATION))
	{
		video_clear_screen32();
		video_flip_screen(1);
		bLoop = LOOP_RESTART;
		bSleep = 0;
	}
	return 1;
}


/*------------------------------------------------------
	ファイルブラウザに戻る
 -----------------------------------------------------*/

int menu_browser(void)
{
	bLoop = LOOP_BROWSER;
	bSleep = 0;
	return 1;
}


/*------------------------------------------------------
	エミュレータ終了
 -----------------------------------------------------*/

int menu_exit(void)
{
	if (messagebox(MB_EXITEMULATOR))
	{
		bLoop = LOOP_EXIT;
		bSleep = 0;
		return 1;
	}
	return 0;
}


/*------------------------------------------------------
	コマンドリスト表示
 -----------------------------------------------------*/

static int menu_cmdlist(void)
{
	commandlist(0);

	if (get_app_status()) return 1;
	return 0;
}


/*------------------------------------------------------
	ゲーム設定メニュー
 -----------------------------------------------------*/

typedef struct {
	const char label[32];
	int	*value;
	int flag;
	int old_value;
	int	value_max;
	char *values_label[32];
} gamecfg_t;


#define CFG_CONTINUE	0
#define CFG_RESTART		1
#define CFG_RESET		2

#define GAMECFG_MENU_ITEMS 22

#if JAPANESE_UI
gamecfg_t gamecfg_menu[GAMECFG_MENU_ITEMS] =
{
	{ "ネオジオ言語設定", &neogeo_region, CFG_RESTART, 0, 2, { "日本", "アメリカ", "ヨーロッパ" } },
	{ "ロード画面エミュレート", &neogeo_loadscreen, CFG_RESTART, 0, 1, { "オフ", "オン" } },
	{ "CD-ROM速度制限", &neogeo_cdspeed_limit, CFG_CONTINUE, 0, 1, { "オフ", "オン" } },
	MENU_BLANK,
	{ "拡大描画", &neogeo_blitter, CFG_CONTINUE, 0, 2, { "拡大しない (1:1)", "拡大 (アスペクト比保持)", "拡大 (全画面表\示)" } },
	{ "VBLANK同期", &neogeo_vsync, CFG_CONTINUE, 0, 1, { "オフ", "オン" } },
	{ "自動フレームスキップ", &neogeo_autoframeskip, CFG_CONTINUE, 0, 1, { "オフ", "オン" } },
	{ "フレームスキップ", &neogeo_frameskip, CFG_CONTINUE, 0, 11, { "0","1","2","3","4","5","6","7","8","9","10","11" } },
	{ "FPS表\示", &neogeo_showfps, CFG_CONTINUE, 0, 1, { "オフ", "オン" } },
	{ "60fpsフレーム制限", &neogeo_speedlimit, CFG_CONTINUE, 0, 1, { "オフ","オン" } },
	MENU_BLANK,
	{ "サウンド再生", &neogeo_sound_enable, CFG_RESTART, 0, 1, { "オフ","オン" } },
	{ "サンプルレート", &neogeo_samplerate, CFG_RESTART, 0, 2, {"11025Hz","22050Hz","44100Hz"} },
	{ "サウンド音量", &neogeo_sound_volume, CFG_CONTINUE, 0, 10, {"0%","10%","20%","30%","40%","50%","60%","70%","80%","90%","100%"} },
	{ "CDDA再生", &neogeo_cdda_enable, CFG_RESTART, 0, 1, {"オフ","オン"} },
	{ "CDDA音量", &neogeo_cdda_volume, CFG_CONTINUE, 0, 10, {"0%", "10%","20%","30%","40%","50%","60%","70%","80%","90%","100%"} },
	MENU_BLANK,
	{ "コントローラー", &neogeo_input_port, CFG_CONTINUE, 0, 1, {"プレイヤー1側","プレイヤー2側"} },
	MENU_BLANK,
	{ "PSPクロック", &psp_cpuclock, CFG_CONTINUE, 0, 2, {"222MHz","266MHz","333MHz"} },
	MENU_BLANK,
	{"メインメニューに戻る" }
};
#else
gamecfg_t gamecfg_menu[GAMECFG_MENU_ITEMS] =
{
	{ "Machine region", &neogeo_region, CFG_RESTART, 0, 2, { "Japan", "USA", "Europe" } },
	{ "Emulate load screen", &neogeo_loadscreen, CFG_RESTART, 0, 1, { "No", "Yes" } },
	{ "CD-ROM speed limit", &neogeo_cdspeed_limit, CFG_CONTINUE, 0, 1, { "No", "Yes" } },
	MENU_BLANK,
	{ "Render", &neogeo_blitter, CFG_CONTINUE, 0, 2, { "1:1", "Zoom (keep aspect ratio)", "Zoom (full screen)" } },
	{ "Video sync", &neogeo_vsync, CFG_CONTINUE, 0, 1, { "Off", "On" } },
	{ "Auto frameskip", &neogeo_autoframeskip, CFG_CONTINUE, 0, 1, { "Disable", "Enable" } },
	{ "Frameskip", &neogeo_frameskip, CFG_CONTINUE, 0, 11, { "0","1","2","3","4","5","6","7","8","9","10","11" } },
	{ "Show FPS", &neogeo_showfps, CFG_CONTINUE, 0, 1, { "Off", "On" } },
	{ "60fps limit", &neogeo_speedlimit, CFG_CONTINUE, 0, 1, { "Off","On" } },
	MENU_BLANK,
	{ "Enable Sound", &neogeo_sound_enable, CFG_RESTART, 0, 1, { "No","Yes" } },
	{ "Sample Rate", &neogeo_samplerate, CFG_RESTART, 0, 2, {"11025Hz","22050Hz","44100Hz"} },
	{ "Sound Volume", &neogeo_sound_volume, CFG_CONTINUE, 0, 10, {"0%","10%","20%","30%","40%","50%","60%","70%","80%","90%","100%"} },
	{ "Enable CDDA", &neogeo_cdda_enable, CFG_RESTART, 0, 1, {"No","Yes"} },
	{ "CDDA Volume", &neogeo_cdda_volume, CFG_CONTINUE, 0, 10, {"0%", "10%","20%","30%","40%","50%","60%","70%","80%","90%","100%"} },
	MENU_BLANK,
	{ "Controler", &neogeo_input_port, CFG_CONTINUE, 0, 1, {"Player 1","Player 2"} },
	MENU_BLANK,
	{ "PSP clock", &psp_cpuclock, CFG_CONTINUE, 0, 2, {"222MHz","266MHz","333MHz"} },
	MENU_BLANK,
	{"Return to main menu" }
};
#endif

int menu_gamecfg(void)
{
	int sel = 0, rows = 13, top = 0;
	int i, arrowl, arrowr, prev_sel, update = 1;

	for (i = 0; i < GAMECFG_MENU_ITEMS; i++)
	{
		if (gamecfg_menu[i].value)
		{
			if (*gamecfg_menu[i].value < 0)
				*gamecfg_menu[i].value = 0;
			if (*gamecfg_menu[i].value > gamecfg_menu[i].value_max)
				*gamecfg_menu[i].value = gamecfg_menu[i].value_max;

			gamecfg_menu[i].old_value = *gamecfg_menu[i].value;
		}
	}

	pad_wait_clear();
	load_background(WP_GAMECFG);
	ui_popup_reset();

	do
	{
		if (update)
		{
			update = 0;

		  	show_background();

			arrowl = 0;
			arrowr = 0;
			if (gamecfg_menu[sel].value)
			{
				int cur = *gamecfg_menu[sel].value;

				if (cur > 0) arrowl = 1;
				if (cur < gamecfg_menu[sel].value_max) arrowr = 1;
			}

			small_icon_shadow(8, 3, UI_COLOR(UI_PAL_TITLE), ICON_CONFIG);
#if JAPANESE_UI
			uifont_print_shadow(36, 5, UI_COLOR(UI_PAL_TITLE), "ゲーム設定メニュー");
#else
			uifont_print_shadow(36, 5, UI_COLOR(UI_PAL_TITLE), "Game configration menu");
#endif
			draw_battery_status(1);
			draw_scrollbar(470, 26, 479, 270, rows, GAMECFG_MENU_ITEMS, sel);

			for (i = 0; i < rows; i++)
			{
				if (top + i >= GAMECFG_MENU_ITEMS) break;

				if (gamecfg_menu[top + i].label[0] == '\n')
					continue;

				if ((top + i) == sel)
				{
					uifont_print_shadow(16, 40 + i * 17, UI_COLOR(UI_PAL_SELECT), gamecfg_menu[top + i].label);
					if (arrowl)
					{
						uifont_print_shadow(180, 40 + i * 17, UI_COLOR(UI_PAL_SELECT), FONT_LEFTTRIANGLE);
					}
				}
				else
					uifont_print(16, 40 + i * 17, UI_COLOR(UI_PAL_NORMAL), gamecfg_menu[top + i].label);

				if (gamecfg_menu[top + i].value)
				{
					int val = *gamecfg_menu[top + i].value;

					if ((top + i) == sel)
					{
						uifont_print_shadow(200, 40 + i * 17, UI_COLOR(UI_PAL_SELECT), gamecfg_menu[top + i].values_label[val]);
						if (arrowr)
						{
							int width = uifont_get_string_width(gamecfg_menu[top + i].values_label[val]);
							uifont_print_shadow(204 + width, 40 + i * 17, UI_COLOR(UI_PAL_SELECT), FONT_RIGHTTRIANGLE);
						}
					}
					else
						uifont_print(200, 40 + i * 17, UI_COLOR(UI_PAL_NORMAL), gamecfg_menu[top + i].values_label[val]);
				}
			}

			update = ui_show_popup(1);
			video_flip_screen(1);
		}
		else
		{
			update = ui_show_popup(0);
			video_wait_vsync();
		}

		prev_sel = sel;

		if (pad_pressed(PSP_CTRL_UP))
		{
			sel--;
			if (gamecfg_menu[sel].label[0] == '\n') sel--;
		}
		else if (pad_pressed(PSP_CTRL_DOWN))
		{
			sel++;
			if (gamecfg_menu[sel].label[0] == '\n') sel++;
		}
		else if (pad_pressed(PSP_CTRL_LEFT))
		{
			if (gamecfg_menu[sel].value)
			{
				int cur = *gamecfg_menu[sel].value;

				if (cur > 0)
				{
					*gamecfg_menu[sel].value = cur - 1;
					update = 1;
				}
			}
		}
		else if (pad_pressed(PSP_CTRL_RIGHT))
		{
			if (gamecfg_menu[sel].value)
			{
				int cur = *gamecfg_menu[sel].value;

				if (cur < gamecfg_menu[sel].value_max)
				{
					*gamecfg_menu[sel].value = cur + 1;
					update = 1;
				}
			}
		}
		else if (pad_pressed(PSP_CTRL_CIRCLE))
		{
			if (sel == GAMECFG_MENU_ITEMS - 1)
				break;
		}
		else if (pad_pressed(PSP_CTRL_START))
		{
			ui_save_screenshot("menu2");
			pad_wait_clear();
		}
		else if (pad_pressed(PSP_CTRL_RTRIGGER))
		{
			help(HELP_GAMECONFIG);
			update = 1;
		}

		if (top > GAMECFG_MENU_ITEMS - rows) top = GAMECFG_MENU_ITEMS - rows;
		if (top < 0) top = 0;
		if (sel >= GAMECFG_MENU_ITEMS) sel = 0;
		if (sel < 0) sel = GAMECFG_MENU_ITEMS - 1;
		if (sel >= top + rows) top = sel - rows + 1;
		if (sel < top) top = sel;

		if (prev_sel != sel) update = 1;

		pad_update();

   		if (get_app_status()) break;

	} while (!pad_pressed(PSP_CTRL_CROSS));

	for (i = 0; i < GAMECFG_MENU_ITEMS; i++)
	{
		if (gamecfg_menu[i].value && gamecfg_menu[i].flag == CFG_RESTART)
		{
			if (gamecfg_menu[i].old_value != *gamecfg_menu[i].value)
			{
				menu_restart();
				return 1;
			}
		}
	}

	pad_wait_clear();
	load_background(WP_LOGO);
	ui_popup_reset();

	return 0;
}


/*------------------------------------------------------
	ボタン設定メニュー
 -----------------------------------------------------*/

static const int button_value[13] =
{
	0,
	PSP_CTRL_UP,
	PSP_CTRL_DOWN,
	PSP_CTRL_LEFT,
	PSP_CTRL_RIGHT,
	PSP_CTRL_CIRCLE,
	PSP_CTRL_CROSS,
	PSP_CTRL_SQUARE,
	PSP_CTRL_TRIANGLE,
	PSP_CTRL_LTRIGGER,
	PSP_CTRL_RTRIGGER,
	PSP_CTRL_START,
	PSP_CTRL_SELECT
};

static const char *button_name[13] =
{
#if JAPANESE_UI
	"使用しない",
#else
	"Not use",
#endif
	FONT_UPARROW,
	FONT_DOWNARROW,
	FONT_LEFTARROW,
	FONT_RIGHTARROW,
	FONT_CIRCLE,
	FONT_CROSS,
	FONT_SQUARE,
	FONT_TRIANGLE,
#if JAPANESE_UI
	"Lトリガ",
	"Rトリガ",
#else
	"L TRIGGER",
	"R TRIGGER",
#endif
	"START",
	"SELECT"
};

typedef struct {
	const char label[64];
	int	type;
	int	code;
} keycfg_menu_t;


#define KEYCFG_MENU_ITEMS 34
#if JAPANESE_UI
keycfg_menu_t keycfg_menu[KEYCFG_MENU_ITEMS] =
{
	{ FONT_UPARROW,             1, P1_UP      },
	{ FONT_DOWNARROW,           1, P1_DOWN    },
	{ FONT_LEFTARROW,           1, P1_LEFT    },
	{ FONT_RIGHTARROW,          1, P1_RIGHT   },
	{ "Aボタン",                1, P1_BUTTONA },
	{ "Bボタン",                1, P1_BUTTONB },
	{ "Cボタン",                1, P1_BUTTONC },
	{ "Dボタン",                1, P1_BUTTOND },
	{ "スタートボタン",         1, P1_START   },
	{ "セレクトボタン",         1, P1_SELECT  },
	MENU_BLANK,
	{ "Aボタン連射",            1, P1_AF_A    },
	{ "Bボタン連射",            1, P1_AF_B    },
	{ "Cボタン連射",            1, P1_AF_C    },
	{ "Dボタン連射",            1, P1_AF_D    },
	{ "連射間隔(フレーム)",     2, 0          },
	MENU_BLANK,
	{ "A+B 同時押し",           1, P1_AB      },
	{ "A+C 同時押し",           1, P1_AC      },
	{ "A+D 同時押し",           1, P1_AD      },
	{ "B+C 同時押し",           1, P1_BC      },
	{ "B+D 同時押し",           1, P1_BD      },
	{ "C+D 同時押し",           1, P1_CD      },
	{ "A+B+C 同時押し",         1, P1_ABC     },
	{ "A+B+D 同時押し",         1, P1_ABD     },
	{ "A+C+D 同時押し",         1, P1_ACD     },
	{ "B+C+D 同時押し",         1, P1_BCD     },
	{ "A+B+C+D 同時押し",       1, P1_ABCD    },
	MENU_BLANK,
	{ "メニュー表\示",          1, SHOWMENU   },
	{ "スクリーンショット保存", 1, SNAPSHOT   },
	{ "プレイヤー切り替え",     1, SWPLAYER   },
	MENU_BLANK,
	{ "メインメニューに戻る", }
};
#else
keycfg_menu_t keycfg_menu[KEYCFG_MENU_ITEMS] =
{
	{ FONT_UPARROW,            1, P1_UP      },
	{ FONT_DOWNARROW,          1, P1_DOWN    },
	{ FONT_LEFTARROW,          1, P1_LEFT    },
	{ FONT_RIGHTARROW,         1, P1_RIGHT   },
	{ "Button A",              1, P1_BUTTONA },
	{ "Button B",              1, P1_BUTTONB },
	{ "Button C",              1, P1_BUTTONC },
	{ "Button D",              1, P1_BUTTOND },
	{ "Start",                 1, P1_START   },
	{ "Select",                1, P1_SELECT  },
	MENU_BLANK,
	{ "Autofire A",            1, P1_AF_A    },
	{ "Autofire B",            1, P1_AF_B    },
	{ "Autofire C",            1, P1_AF_C    },
	{ "Autofire D",            1, P1_AF_D    },
	{ "Autofire Interval",     2, 0          },
	MENU_BLANK,
	{ "Hotkey A+B",            1, P1_AB      },
	{ "Hotkey A+C",            1, P1_AC      },
	{ "Hotkey A+D",            1, P1_AD      },
	{ "Hotkey B+C",            1, P1_BC      },
	{ "Hotkey B+D",            1, P1_BD      },
	{ "Hotkey C+D",            1, P1_CD      },
	{ "Hotkey A+B+C",          1, P1_ABC     },
	{ "Hotkey A+B+D",          1, P1_ABD     },
	{ "Hotkey A+C+D",          1, P1_ACD     },
	{ "Hotkey B+C+D",          1, P1_BCD     },
	{ "Hotkey A+B+C+D",        1, P1_ABCD    },
	MENU_BLANK,
	{ "Show menu",             1, SHOWMENU   },
	{ "Save snapshot",         1, SNAPSHOT   },
	{ "Switch player",         1, SWPLAYER   },
	MENU_BLANK,
	{ "Return to main menu", }
};
#endif

static int keycfg_value[KEYCFG_MENU_ITEMS];


static int menu_keycfg(void)
{
	int sel = 0, prev_sel, rows = 13, top = 0;
	int i, j, arrowl, arrowr, update = 1;

	for (i = 0; i < KEYCFG_MENU_ITEMS; i++)
	{
		keycfg_value[i] = 0;

		if (keycfg_menu[i].type == 1)
		{
			int code = input_map[keycfg_menu[i].code];

			if (code)
			{
				for (j = 0; j < 13; j++)
				{
					if (code == button_value[j])
					{
						keycfg_value[i] = j;
						break;
					}
				}
			}
		}
		else if (keycfg_menu[i].type == 2)
		{
			keycfg_value[i] = af_interval;
		}
	}

	pad_wait_clear();
	load_background(WP_KEYCFG);
	ui_popup_reset();

	do
	{
		if (update)
		{
			update = 0;

		  	show_background();

			small_icon_shadow(8, 3, UI_COLOR(UI_PAL_TITLE), ICON_KEYCONFIG);
#if JAPANESE_UI
			uifont_print_shadow(36, 5, UI_COLOR(UI_PAL_TITLE), "入力ボタン設定メニュー");
#else
			uifont_print_shadow(36, 5, UI_COLOR(UI_PAL_TITLE), "Key configration menu");
#endif
			draw_battery_status(1);

			draw_scrollbar(470, 26, 479, 270, rows, KEYCFG_MENU_ITEMS, sel);

			arrowl = 0;
			arrowr = 0;
			if (keycfg_menu[sel].type)
			{
				if (keycfg_value[sel] > 0) arrowl = 1;
				if (keycfg_menu[sel].type == 1 && keycfg_value[sel] < 12) arrowr = 1;
				if (keycfg_menu[sel].type == 2 && keycfg_value[sel] < 10) arrowr = 1;
			}

			for (i = 0; i < rows; i++)
			{
				if ((top + i) >= KEYCFG_MENU_ITEMS) break;

				if (keycfg_menu[top + i].label[0] == '\n')
					continue;

				if ((top + i) == sel)
				{
					uifont_print_shadow(16, 40 + i * 17, UI_COLOR(UI_PAL_SELECT), keycfg_menu[top + i].label);
					if (arrowl)
						uifont_print_shadow(170, 40 + i * 17, UI_COLOR(UI_PAL_SELECT), FONT_LEFTTRIANGLE);
				}
				else
				{
					uifont_print(16, 40 + i * 17, UI_COLOR(UI_PAL_NORMAL), keycfg_menu[top + i].label);
				}

				if (keycfg_menu[top + i].type == 1)
				{
					if ((top + i) == sel)
					{
						uifont_print_shadow(190, 40 + i * 17, UI_COLOR(UI_PAL_SELECT), button_name[keycfg_value[top + i]]);
						if (arrowr)
						{
							int width = uifont_get_string_width(button_name[keycfg_value[top + i]]);
							uifont_print_shadow(194 + width, 40 + i * 17, UI_COLOR(UI_PAL_SELECT), FONT_RIGHTTRIANGLE);
						}
					}
					else
						uifont_print(190, 40 + i * 17, UI_COLOR(UI_PAL_NORMAL), button_name[keycfg_value[top + i]]);
				}
				else if (keycfg_menu[top + i].type == 2)
				{
					char s[8];

#if JAPANESE_UI
					sprintf(s, "%dフレーム", keycfg_value[top + i] + 1);
#else
					if (keycfg_value[top + i] == 0)
						sprintf(s, "%d frame", keycfg_value[top + i] + 1);
					else
						sprintf(s, "%d frames", keycfg_value[top + i] + 1);
#endif

					if ((top + i) == sel)
					{
						uifont_print_shadow(190, 40 + i * 17, UI_COLOR(UI_PAL_SELECT), s);
						if (arrowr)
						{
							int width = uifont_get_string_width(s);
							uifont_print_shadow(194 + width, 40 + i * 17, UI_COLOR(UI_PAL_SELECT), FONT_RIGHTTRIANGLE);
						}
					}
					else
						uifont_print(190, 40 + i * 17, UI_COLOR(UI_PAL_NORMAL), s);
				}
			}

			update = ui_show_popup(1);
			video_flip_screen(1);
		}
		else
		{
			update = ui_show_popup(0);
			video_wait_vsync();
		}

		prev_sel = sel;

		if (pad_pressed(PSP_CTRL_UP))
		{
			sel--;
			if (keycfg_menu[sel].label[0] == '\n') sel--;
		}
		else if (pad_pressed(PSP_CTRL_DOWN))
		{
			sel++;
			if (keycfg_menu[sel].label[0] == '\n') sel++;
		}
		else if (pad_pressed(PSP_CTRL_LEFT))
		{
			if (keycfg_menu[sel].type)
			{
				if (keycfg_value[sel] - 1 >= 0)
				{
					keycfg_value[sel]--;
					update = 1;
				}
			}
		}
		else if (pad_pressed(PSP_CTRL_RIGHT))
		{
			if (keycfg_menu[sel].type == 1)
			{
				if (keycfg_value[sel] + 1 < 13)
				{
					keycfg_value[sel]++;
					update = 1;
				}
			}
			else if (keycfg_menu[sel].type == 2)
			{
				if (keycfg_value[sel] + 1 < 11)
				{
					keycfg_value[sel]++;
					update = 1;
				}
			}
		}
		else if (pad_pressed(PSP_CTRL_CIRCLE))
		{
			if (sel == KEYCFG_MENU_ITEMS - 1)
				break;
		}
		else if (pad_pressed(PSP_CTRL_START))
		{
			ui_save_screenshot("menu3");
			pad_wait_clear();
		}
		else if (pad_pressed(PSP_CTRL_RTRIGGER))
		{
			help(HELP_KEYCONFIG);
			update = 1;
		}

		if (top > KEYCFG_MENU_ITEMS - rows) top = KEYCFG_MENU_ITEMS - rows;
		if (top < 0) top = 0;
		if (sel >= KEYCFG_MENU_ITEMS) sel = 0;
		if (sel < 0) sel = KEYCFG_MENU_ITEMS - 1;
		if (sel >= top + rows) top = sel - rows + 1;
		if (sel < top) top = sel;

		if (prev_sel != sel) update = 1;

		pad_update();

		if (get_app_status()) break;

	} while (!pad_pressed(PSP_CTRL_CROSS));

	for (i = 0; i < KEYCFG_MENU_ITEMS; i++)
	{
		if (keycfg_menu[i].type == 1)
		{
			input_map[keycfg_menu[i].code] = button_value[keycfg_value[i]];
		}
		else if (keycfg_menu[i].type == 2)
		{
			af_interval = keycfg_value[i];
		}
	}

	if (input_map[SHOWMENU] == 0)
		input_map[SHOWMENU] = PSP_CTRL_LTRIGGER;

	pad_wait_clear();
	load_background(WP_LOGO);
	ui_popup_reset();

	return 0;
}


/*------------------------------------------------------
	セーブ/ロードステート
 -----------------------------------------------------*/

#ifdef SAVE_STATE

static u8 slot[10];
static int st_which;
static int st_sel;


static void delete_state_slot(void)
{
	if (messagebox(MB_DELETESTATE))
	{
		char path[MAX_PATH];

		sprintf(path, "%sstate/%s.sv%d", launchDir, game_name, st_sel);
		sceIoRemove(path);
		slot[st_sel] = 0;
		st_which = 0;
	}
}


void draw_state_thumbnail(void)
{
	int x, y;
	u32 *vptr = video_frame_addr32(draw_frame, 298, 52);

	for (y = 0; y < 112; y++)
	{
		for (x = 0; x < 152; x++)
		{
			int color = thumbnail_buf[y * 152 + x];
			int r = GETR15(color);
			int g = GETG15(color);
			int b = GETB15(color);

			vptr[x] = MAKECOL32(r, g, b);
		}
		vptr += BUF_WIDTH;
	}
}


int draw_state_progress(int progress)
{
	if (st_which >= 2) return 0;

	if (progress == 1)
		ui_screen_backup(show_frame);
	else
		ui_screen_restore(draw_frame);

	if (st_which == 0 && progress == 9)
	{
		RECT clip = { 290, 186, 480, 240 };

		video_copy_rect32(work_frame, draw_frame, &clip, &clip);
		draw_state_thumbnail();
#if JAPANESE_UI
		uifont_print(290, 186, UI_COLOR(UI_PAL_NORMAL), "プレイ日付");
		uifont_print(368, 186, UI_COLOR(UI_PAL_NORMAL), date_str);
		uifont_print(290, 206, UI_COLOR(UI_PAL_NORMAL), "保存時刻");
		uifont_print(368, 206, UI_COLOR(UI_PAL_NORMAL), time_str);
		uifont_print(290, 226, UI_COLOR(UI_PAL_NORMAL), "バージョン");
		uifont_print(368, 226, UI_COLOR(UI_PAL_NORMAL), stver_str);
#else
		uifont_print(290, 186, UI_COLOR(UI_PAL_NORMAL), "Play Date");
		uifont_print(368, 186, UI_COLOR(UI_PAL_NORMAL), date_str);
		uifont_print(290, 206, UI_COLOR(UI_PAL_NORMAL), "Save Time");
		uifont_print(368, 206, UI_COLOR(UI_PAL_NORMAL), time_str);
		uifont_print(290, 226, UI_COLOR(UI_PAL_NORMAL), "Version");
		uifont_print(368, 226, UI_COLOR(UI_PAL_NORMAL), stver_str);
#endif
	}

	draw_dialog(80, 34, 399, 261);
#if JAPANESE_UI
	uifont_print_shadow_center(42, UI_COLOR(UI_PAL_INFO), st_which ? "ロードステート" : "セーブステート");
#else
	uifont_print_shadow_center(42, UI_COLOR(UI_PAL_INFO), st_which ? "Load state" : "Save state");
#endif

	if (progress == 1)
	{
#if JAPANESE_UI
		if (st_which)
			uifont_print_shadow(90, 62, UI_COLOR(UI_PAL_SELECT), FONT_CIRCLE "を押すとロードを開始します。(" FONT_CROSS "でキャンセル)");
		else
			uifont_print_shadow(90, 62, UI_COLOR(UI_PAL_SELECT), FONT_CIRCLE "を押すとセーブを開始します。(" FONT_CROSS "でキャンセル)");
#else
		if (st_which)
			uifont_print_shadow(90, 62, UI_COLOR(UI_PAL_SELECT), "Press " FONT_CIRCLE " to start load state. (" FONT_CROSS " to cancel)");
		else
			uifont_print_shadow(90, 62, UI_COLOR(UI_PAL_SELECT), "Press " FONT_CIRCLE " to start save state. (" FONT_CROSS " to cancel)");
#endif
		video_flip_screen(1);
		pad_wait_clear();

		do
		{
			if (pad_pressed(PSP_CTRL_CIRCLE))
			{
				pad_wait_clear();
				return 1;
			}

	   		if (get_app_status()) break;

			pad_update();

		} while (!pad_pressed(PSP_CTRL_CROSS));

		pad_wait_clear();
	}
	else
	{
		if (st_which == 1)
		{
#if JAPANESE_UI
			if (progress > 1) uifont_print_shadow(90, 62 + 18*0, UI_COLOR(UI_PAL_SELECT), "ヘッダ読み込み中...");
			if (progress > 2) uifont_print_shadow(90, 62 + 18*1, UI_COLOR(UI_PAL_SELECT), "スナップショットをスキップ...");
			if (progress > 3) uifont_print_shadow(90, 62 + 18*2, UI_COLOR(UI_PAL_SELECT), "ステートデータ読み込み中...");
			if (progress > 4) uifont_print_shadow(90, 62 + 18*3, UI_COLOR(UI_PAL_SELECT), "ステートデータを解凍中...");
			if (progress > 5) uifont_print_shadow(90, 62 + 18*4, UI_COLOR(UI_PAL_SELECT), "ステートデータを復帰中...");
			if (progress > 6)
			{
				uifont_print_shadow(90, 62 + 18*5, UI_COLOR(UI_PAL_SELECT), "完了しました。");
				uifont_print_shadow_center(239, UI_COLOR(UI_PAL_SELECT), "何かボタンを押すとゲームに戻ります。");
			}
#else
			if (progress > 1) uifont_print_shadow(90, 62 + 18*0, UI_COLOR(UI_PAL_SELECT), "Read header...");
			if (progress > 2) uifont_print_shadow(90, 62 + 18*1, UI_COLOR(UI_PAL_SELECT), "Skip snap shot...");
			if (progress > 3) uifont_print_shadow(90, 62 + 18*2, UI_COLOR(UI_PAL_SELECT), "Read state data...");
			if (progress > 4) uifont_print_shadow(90, 62 + 18*3, UI_COLOR(UI_PAL_SELECT), "Uncompress state data...");
			if (progress > 5) uifont_print_shadow(90, 62 + 18*4, UI_COLOR(UI_PAL_SELECT), "Restore state data...");
			if (progress > 6)
			{
				uifont_print_shadow(90, 62 + 18*5, UI_COLOR(UI_PAL_SELECT), "Complete.");
				uifont_print_shadow_center(239, UI_COLOR(UI_PAL_SELECT), "Press any button to return to game.");
			}
#endif
			video_flip_screen(1);

			if (progress == 7)
			{
				pad_wait_clear();
				pad_wait_press(-1);
				pad_wait_clear();
			}
		}
		else
		{
#if JAPANESE_UI
			if (progress > 1) uifont_print_shadow(90, 62 + 18*0, UI_COLOR(UI_PAL_SELECT), "ヘッダ書き込み中...");
			if (progress > 2) uifont_print_shadow(90, 62 + 18*1, UI_COLOR(UI_PAL_SELECT), "スナップショットを作成中...");
			if (progress > 3) uifont_print_shadow(90, 62 + 18*2, UI_COLOR(UI_PAL_SELECT), "スナップショットを圧縮中...");
			if (progress > 4) uifont_print_shadow(90, 62 + 18*3, UI_COLOR(UI_PAL_SELECT), "スナップショットを書き込み中...");
			if (progress > 5) uifont_print_shadow(90, 62 + 18*4, UI_COLOR(UI_PAL_SELECT), "ステートデータを登録中...");
			if (progress > 6) uifont_print_shadow(90, 62 + 18*5, UI_COLOR(UI_PAL_SELECT), "ステートデータを圧縮中...");
			if (progress > 7) uifont_print_shadow(90, 62 + 18*6, UI_COLOR(UI_PAL_SELECT), "ステートデータを書き込み中...");
			if (progress > 8)
			{
				uifont_print_shadow(92, 62 + 18*7, UI_COLOR(UI_PAL_SELECT), "完了しました。");
				uifont_print_shadow_center(221, UI_COLOR(UI_PAL_SELECT), FONT_CROSS "を押すとゲームに戻ります。");
				uifont_print_shadow_center(239, UI_COLOR(UI_PAL_SELECT), "その他のボタンを押すとメニューに戻ります。");
			}
			else
			{
				uifont_print_shadow_center(221, UI_COLOR(UI_PAL_WARNING), "ステートデータの保存は非常に遅いです。");
				uifont_print_shadow_center(239, UI_COLOR(UI_PAL_WARNING), "完了するまで電源を落とさないでください。");
			}
#else
			if (progress > 1) uifont_print_shadow(90, 62 + 18*0, UI_COLOR(UI_PAL_SELECT), "Write header...");
			if (progress > 2) uifont_print_shadow(90, 62 + 18*1, UI_COLOR(UI_PAL_SELECT), "Capture snap shot...");
			if (progress > 3) uifont_print_shadow(90, 62 + 18*2, UI_COLOR(UI_PAL_SELECT), "Compress snap shot...");
			if (progress > 4) uifont_print_shadow(90, 62 + 18*3, UI_COLOR(UI_PAL_SELECT), "Write snap shot...");
			if (progress > 5) uifont_print_shadow(90, 62 + 18*4, UI_COLOR(UI_PAL_SELECT), "Register state data...");
			if (progress > 6) uifont_print_shadow(90, 62 + 18*5, UI_COLOR(UI_PAL_SELECT), "Compress state data...");
			if (progress > 7) uifont_print_shadow(90, 62 + 18*6, UI_COLOR(UI_PAL_SELECT), "Write state data...");
			if (progress > 8)
			{
				uifont_print_shadow(90, 62 + 18*7, UI_COLOR(UI_PAL_SELECT), "Complete.");
				uifont_print_shadow_center(221, UI_COLOR(UI_PAL_SELECT), "Press " FONT_CROSS " to return to game.");
				uifont_print_shadow_center(239, UI_COLOR(UI_PAL_SELECT), "Press other button to return to menu.");
			}
			else
			{
				uifont_print_shadow_center(221, UI_COLOR(UI_PAL_WARNING), "Save state is very slow.");
				uifont_print_shadow_center(239, UI_COLOR(UI_PAL_WARNING), "Please wait for a while.");
			}
#endif
			video_flip_screen(1);

			if (progress == 9)
			{
				pad_wait_clear();

				do
				{
					if (pad_pressed(PSP_CTRL_CROSS))
					{
						pad_wait_clear();
						return 1;
					}

			   		if (get_app_status()) break;

					pad_update();

				} while (!pad_pressed_any(PSP_CTRL_CROSS));

				pad_wait_clear();
			}
		}
	}
	return 0;
}


int menu_state(void)
{
	int i, prev_sel = -1, prev_which = -1, update = 1;
	int thumbnail_loaded = 0;
	char name[16], state[32];

	st_which = 0;
	st_sel = 0;
	thumbnail_loaded = 0;

	pad_wait_clear();
	load_background(WP_STATE);
	ui_popup_reset();

	memset(slot, 0, sizeof(slot));

	find_state_file(slot);

	do
	{
		if (prev_sel != st_sel)
		{
			if (!slot[st_sel])
			{
				if (neogeo_region)
					strcpy(date_str, "--/--/----");
				else
					strcpy(date_str, "----/--/--");
				strcpy(time_str, "--:--:--");
				strcpy(stver_str, "--------");

				st_which = 0;
				memset(thumbnail_buf, 0, sizeof(thumbnail_buf));
			}
			else
				state_load_screen(st_sel);

			update = 1;
		}

		if (update & 1)
		{
			update = 0;

			show_background();
			draw_battery_status(0);
			draw_box_shadow(298, 52, 449, 163);
			draw_state_thumbnail();

#if JAPANESE_UI
			uifont_print_shadow(8, 5, UI_COLOR(UI_PAL_NORMAL), FONT_LTRIGGER " メインメニュー");
			uifont_print(118, 5, UI_COLOR(UI_PAL_NORMAL), "|");
			uifont_print(126, 5, UI_COLOR(UI_PAL_TITLE), "セーブ/ロードステート " FONT_RTRIGGER);

			for (i = 0; i < 10; i++)
			{
				sprintf(name, "スロット%d:", i);
				if (slot[i])
					sprintf(state, "%s.sv%d", game_name, i);
				else
					strcpy(state, "データなし");

				if (i == st_sel)
				{
					small_icon_light(12, 38 + i * 22, UI_COLOR(UI_PAL_SELECT), ICON_MEMSTICK);
					uifont_print_shadow(40, 40 + i * 22, UI_COLOR(UI_PAL_SELECT), name);
					uifont_print_shadow(104, 40 + i * 22, UI_COLOR(UI_PAL_SELECT), state);

					if (slot[st_sel])
					{
						if (st_which == 1)
						{
							uifont_print_shadow(188, 40 + i * 22, UI_COLOR(UI_PAL_SELECT), FONT_LEFTTRIANGLE);
							uifont_print_shadow(210, 40 + i * 22, UI_COLOR(UI_PAL_SELECT), "ロード");
							uifont_print_shadow(250, 40 + i * 22, UI_COLOR(UI_PAL_SELECT), FONT_RIGHTTRIANGLE);
						}
						else if (st_which == 2)
						{
							uifont_print_shadow(188, 40 + i * 22, UI_COLOR(UI_PAL_SELECT), FONT_LEFTTRIANGLE);
							uifont_print_shadow(210, 40 + i * 22, UI_COLOR(UI_PAL_SELECT), "削除");
						}
						else
						{
							uifont_print_shadow(210, 40 + i * 22, UI_COLOR(UI_PAL_SELECT), "セーブ");
							uifont_print_shadow(250, 40 + i * 22, UI_COLOR(UI_PAL_SELECT), FONT_RIGHTTRIANGLE);
						}
					}
					else
					{
						uifont_print_shadow(210, 40 + i * 22, UI_COLOR(UI_PAL_SELECT), "セーブ");
					}

					hline_gradation(40, 266, 56 + i * 22, UI_COLOR(UI_PAL_NORMAL), UI_COLOR(UI_PAL_SELECT), 14);
					hline_gradation(41, 267, 57 + i * 22, COLOR_BLACK, COLOR_BLACK, 8);
				}
				else
				{
					small_icon(12, 38 + i * 22, UI_COLOR(UI_PAL_NORMAL), ICON_MEMSTICK);
					uifont_print(40, 40 + i * 22, UI_COLOR(UI_PAL_NORMAL), name);
					uifont_print(104, 40 + i * 22, UI_COLOR(UI_PAL_NORMAL), state);
				}
			}

			uifont_print(290, 186, UI_COLOR(UI_PAL_NORMAL), "プレイ日付");
			uifont_print(368, 186, UI_COLOR(UI_PAL_NORMAL), date_str);
			uifont_print(290, 206, UI_COLOR(UI_PAL_NORMAL), "保存時刻");
			uifont_print(368, 206, UI_COLOR(UI_PAL_NORMAL), time_str);
			uifont_print(290, 226, UI_COLOR(UI_PAL_NORMAL), "バージョン");
			uifont_print(368, 226, UI_COLOR(UI_PAL_NORMAL), stver_str);

#else
			uifont_print(8, 5, UI_COLOR(UI_PAL_NORMAL), FONT_LTRIGGER " Main menu");
			uifont_print(98, 5, UI_COLOR(UI_PAL_NORMAL), "|");
			uifont_print_shadow(106, 5, UI_COLOR(UI_PAL_TITLE), "Save/Load state " FONT_RTRIGGER);

			for (i = 0; i < 10; i++)
			{
				sprintf(name, "Slot %d:", i);
				if (slot[i])
					sprintf(state, "%s.sv%d", game_name, i);
				else
					strcpy(state, "Empty");

				if (i == st_sel)
				{
					small_icon_light(12, 38 + i * 22, UI_COLOR(UI_PAL_SELECT), ICON_MEMSTICK);
					uifont_print_shadow(40, 40 + i * 22, UI_COLOR(UI_PAL_SELECT), name);
					uifont_print_shadow(90, 40 + i * 22, UI_COLOR(UI_PAL_SELECT), state);

					if (slot[st_sel])
					{
						if (st_which == 1)
						{
							uifont_print_shadow(192, 40 + i * 22, UI_COLOR(UI_PAL_SELECT), FONT_LEFTTRIANGLE);
							uifont_print_shadow(212, 40 + i * 22, UI_COLOR(UI_PAL_SELECT), "Load");
							uifont_print_shadow(244, 40 + i * 22, UI_COLOR(UI_PAL_SELECT), FONT_RIGHTTRIANGLE);
						}
						else if (st_which == 2)
						{
							uifont_print_shadow(192, 40 + i * 22, UI_COLOR(UI_PAL_SELECT), FONT_LEFTTRIANGLE);
							uifont_print_shadow(212, 40 + i * 22, UI_COLOR(UI_PAL_SELECT), "Delete");
						}
						else
						{
							uifont_print_shadow(212, 40 + i * 22, UI_COLOR(UI_PAL_SELECT), "Save");
							uifont_print_shadow(244, 40 + i * 22, UI_COLOR(UI_PAL_SELECT), FONT_RIGHTTRIANGLE);
						}
					}
					else
					{
						uifont_print_shadow(212, 40 + i * 22, UI_COLOR(UI_PAL_SELECT), "Save");
					}

					hline_gradation(40, 266, 56 + i * 22, UI_COLOR(UI_PAL_NORMAL), UI_COLOR(UI_PAL_SELECT), 14);
					hline_gradation(41, 267, 57 + i * 22, COLOR_BLACK, COLOR_BLACK, 8);
				}
				else
				{
					small_icon(12, 38 + i * 22, UI_COLOR(UI_PAL_NORMAL), ICON_MEMSTICK);
					uifont_print(40, 40 + i * 22, UI_COLOR(UI_PAL_NORMAL), name);
					uifont_print(90, 40 + i * 22, UI_COLOR(UI_PAL_NORMAL), state);
				}
			}

			uifont_print(290, 186, UI_COLOR(UI_PAL_NORMAL), "Play Date");
			uifont_print(368, 186, UI_COLOR(UI_PAL_NORMAL), date_str);
			uifont_print(290, 206, UI_COLOR(UI_PAL_NORMAL), "Save Time");
			uifont_print(368, 206, UI_COLOR(UI_PAL_NORMAL), time_str);
			uifont_print(290, 226, UI_COLOR(UI_PAL_NORMAL), "Version");
			uifont_print(368, 226, UI_COLOR(UI_PAL_NORMAL), stver_str);
#endif

			update = ui_show_popup(1);
			video_flip_screen(1);
		}
		else if (update & 2)
		{
			int x, y;
			u32 temp[26][32];
			u32 *vptr;

			update = 0;

			show_background();
			small_icon_light(12, 38 + st_sel * 22, UI_COLOR(UI_PAL_SELECT), ICON_MEMSTICK);

			vptr = video_frame_addr32(draw_frame, 8, 34 + st_sel * 22);
			for (y = 0; y < 26; y++)
			{
				for (x = 0; x < 32; x++)
				{
					temp[y][x] = vptr[x];
				}
				vptr += BUF_WIDTH;
			}

			video_copy_rect32(show_frame, draw_frame, &full_rect, &full_rect);

			vptr = video_frame_addr32(draw_frame, 8, 34 + st_sel * 22);
			for (y = 0; y < 26; y++)
			{
				for (x = 0; x < 32; x++)
				{
					vptr[x] = temp[y][x];
				}
				vptr += BUF_WIDTH;
			}

			update = ui_show_popup(0);
			video_flip_screen(1);
		}
		else
		{
			update = ui_show_popup(0);
			video_wait_vsync();
		}

		update |= ui_light_update();
		prev_sel = st_sel;
		prev_which = st_which;

		if (pad_pressed(PSP_CTRL_UP))
		{
			st_sel--;
			if (st_sel < 0) st_sel = 10 - 1;
		}
		else if (pad_pressed(PSP_CTRL_DOWN))
		{
			st_sel++;
			if (st_sel >= 10) st_sel = 0;
		}
		else if (pad_pressed(PSP_CTRL_LEFT))
		{
			if (st_which > 0) st_which--;
		}
		else if (pad_pressed(PSP_CTRL_RIGHT))
		{
			if (slot[st_sel])
			{
				if (st_which < 2) st_which++;
			}
		}
		else if (pad_pressed(PSP_CTRL_CIRCLE))
		{
			if (st_which == 1)
			{
				if (slot[st_sel])
				{
					if (draw_state_progress(1))
					{
						set_cpu_clock();
						state_load(st_sel);
						scePowerSetClockFrequency(222, 222, 111);

						state_loaded = 1;
						draw_state_progress(7);
						ui_screen_free();
						return 1;
					}
					ui_screen_free();
					update = 1;
				}
			}
			else if (st_which == 2)
			{
				if (slot[st_sel])
				{
					delete_state_slot();
					update = 1;
				}
			}
			else
			{
				if (draw_state_progress(1))
				{
					set_cpu_clock();
					state_save(st_sel);
					scePowerSetClockFrequency(222, 222, 111);

					find_state_file(slot);
					state_load_screen(st_sel);

					if (draw_state_progress(9))
					{
						ui_screen_free();
						return 1;
					}
				}
				ui_screen_free();
				update = 1;
			}
		}
		else if (pad_pressed(PSP_CTRL_START))
		{
			ui_save_screenshot("menu4");
			pad_wait_clear();
		}
		else if (pad_pressed(PSP_CTRL_RTRIGGER))
		{
			help(HELP_STATE);
			update = 1;
		}

		if (prev_which != st_which) update = 1;

		pad_update();

   		if (get_app_status()) break;

	} while (!pad_pressed(PSP_CTRL_LTRIGGER));

	pad_wait_clear();
	load_background(WP_LOGO);
	ui_popup_reset();

	return 0;
}

#endif


/*------------------------------------------------------
	メインメニュー
 -----------------------------------------------------*/

typedef struct
{
	const char label[64];
	int (*menu_func)(void);
	int	icon;
	const char help[128];
} menu_t;


#define MENU_ITEMS 7

menu_t main_menu[MENU_ITEMS] =
{
#if JAPANESE_UI
	{"ゲーム設定", menu_gamecfg, ICON_CONFIG, "ゲームの動作設定を変更します。" },
	{"入力ボタン設定", menu_keycfg, ICON_KEYCONFIG, "入力ボタンと連射/ホットキーの設定をします。" },
	{"コマンドリスト表\示", menu_cmdlist, ICON_CMDLIST, "このゲームのコマンド一覧を表\示します。" },
	{"エミュレーションをリセット", menu_reset, ICON_RESET, "NEO･GEO CDZのエミュレーションをリセットします。"},
	{"ファイルブラウザに戻る", menu_browser, ICON_FOLDER, "エミュレーションを終了し、ファイルブラウザに戻ります。" },
	{"ゲームに戻る", NULL, ICON_RETURN, "ゲーム画面に戻ります。" },
	{"エミュレータを終了", menu_exit, ICON_EXIT, "このソ\フトウェアを終了し、PSPのメニューに戻ります。" },
#else
	{"Game configration", menu_gamecfg, ICON_CONFIG, "Change game settings." },
	{"Key configration", menu_keycfg, ICON_KEYCONFIG, "Change buttons and autofire/hotkey settings." },
	{"Show command list", menu_cmdlist, ICON_CMDLIST, "Show command list. (MAME Plus! format \"command.dat\")" },
	{"Reset NEO･GEO", menu_reset, ICON_RESET, "Reset NEO･GEO CDZ emulation."},
	{"Return to file browser", menu_browser, ICON_FOLDER, "Stop emulation and return to file browser." },
	{"Return to game", NULL, ICON_RETURN, "Return to game emulation." },
	{"Exit emulator", menu_exit, ICON_EXIT, "Exit emulation and return to PSP menu." },
#endif
};


void showmenu(void)
{
	static int sel = 0;
	int i, prev_sel = 0, update = 1;
	int mp3_paused = 0;

	state_make_thumbnail();
	state_loaded = 0;

	if (mp3_get_status() == MP3_PLAY)
	{
		mp3_pause(1);
		mp3_paused = 1;
	}

	sound_enable(0);

	scePowerSetClockFrequency(222, 222, 111);

	video_clear_screen();
	video_exit();

	video_init32();
	video_clear_screen32();

	pad_wait_clear();
	load_background(WP_LOGO);
	ui_popup_reset();

	do
	{
		if (update & 1)
		{
			update = 0;

		  	show_background();

#ifdef SAVE_STATE
#if JAPANESE_UI
			uifont_print_shadow(8, 5, UI_COLOR(UI_PAL_TITLE), FONT_LTRIGGER " メインメニュー");
			uifont_print(118, 5, UI_COLOR(UI_PAL_NORMAL), "|");
			uifont_print(126, 5, UI_COLOR(UI_PAL_NORMAL), "セーブ/ロードステート " FONT_RTRIGGER);
#else
			uifont_print_shadow(8, 5, UI_COLOR(UI_PAL_TITLE), FONT_LTRIGGER " Main menu");
			uifont_print(98, 5, UI_COLOR(UI_PAL_NORMAL), "|");
			uifont_print(106, 5, UI_COLOR(UI_PAL_NORMAL), "Save/Load state " FONT_RTRIGGER);
#endif
#else
			small_icon_shadow(8, 3, UI_COLOR(UI_PAL_TITLE), ICON_RESET);
#if JAPANESE_UI
			uifont_print_shadow(36, 5, UI_COLOR(UI_PAL_TITLE), "メインメニュー");
#else
			uifont_print_shadow(36, 5, UI_COLOR(UI_PAL_TITLE), "Main menu");
#endif
#endif
			draw_battery_status(1);

			for (i = 0; i < MENU_ITEMS; i++)
			{
				const int sy = 40, dy = 32;

				if (i == sel)
				{
					large_icon_light(12, 40 + i * 32, UI_COLOR(UI_PAL_SELECT), main_menu[i].icon);
					uifont_print_shadow(54, 37 + i * 32, UI_COLOR(UI_PAL_SELECT), main_menu[i].label);
					uifont_print_shadow(54, 56 + i * 32, UI_COLOR(UI_PAL_SELECT), main_menu[i].help);

					hline_gradation(54, 460, 53 + i * 32, UI_COLOR(UI_PAL_NORMAL), UI_COLOR(UI_PAL_SELECT), 14);
					hline_gradation(55, 461, 54 + i * 32, COLOR_BLACK, COLOR_BLACK, 8);
				}
				else
				{
					small_icon(16, 42 + i * 32, UI_COLOR(UI_PAL_NORMAL), main_menu[i].icon);
					uifont_print(56, 44 + i * 32, UI_COLOR(UI_PAL_NORMAL), main_menu[i].label);
				}
			}

			update = ui_show_popup(1);
			video_flip_screen(1);
		}
		else if (update & 2)
		{
			int x, y;
			u32 temp[32][40];
			u32 *vptr;

			update = 0;

			show_background();

			large_icon_light(12, 40 + sel * 32, UI_COLOR(UI_PAL_SELECT), main_menu[sel].icon);

			vptr = video_frame_addr32(draw_frame, 8, 36 + sel * 32);
			for (y = 0; y < 32; y++)
			{
				for (x = 0; x < 40; x++)
				{
					temp[y][x] = vptr[x];
				}
				vptr += BUF_WIDTH;
			}

			video_copy_rect32(show_frame, draw_frame, &full_rect, &full_rect);

			vptr = video_frame_addr32(draw_frame, 8, 36 + sel * 32);
			for (y = 0; y < 32; y++)
			{
				for (x = 0; x < 40; x++)
				{
					vptr[x] = temp[y][x];
				}
				vptr += BUF_WIDTH;
			}

			update = ui_show_popup(0);
			video_flip_screen(1);
		}
		else
		{
			update = ui_show_popup(0);
			video_wait_vsync();
		}

		update |= ui_light_update();
		prev_sel = sel;

		if (pad_pressed(PSP_CTRL_UP))
		{
			sel--;
			if (sel < 0) sel = MENU_ITEMS - 1;
		}
		else if (pad_pressed(PSP_CTRL_DOWN))
		{
			sel++;
			if (sel >= MENU_ITEMS) sel = 0;
		}
		else if (pad_pressed(PSP_CTRL_CIRCLE))
		{
			if (main_menu[sel].menu_func)
			{
				if (main_menu[sel].menu_func())
					break;
				update = 1;
			}
			else break;
		}
		else if (pad_pressed(PSP_CTRL_RTRIGGER))
		{
			if (menu_state()) break;
			update = 1;
		}
		else if (pad_pressed(PSP_CTRL_START))
		{
			ui_save_screenshot("menu0");
			pad_wait_clear();
		}

		if (prev_sel != sel) update |= 1;

		pad_update();

   		if (get_app_status()) break;

	} while (!pad_pressed(PSP_CTRL_CROSS));

	pad_wait_clear();

	video_clear_screen32();
	video_exit32();

	video_init();
	video_clear_screen();

	set_cpu_clock();
	init_autoframeskip();
	blit_clear_all_cache();

	sound_set_volume();

	mp3_file_reopen();
	mp3_set_volume();

	if (state_loaded)
	{
		if (mp3_get_status() == MP3_SEEK)
		{
			mp3_seek_start();

			while (mp3_get_status() == MP3_SEEK)
				wait(100);
		}
	}
	else if (!neogeo_cdda_enable)
	{
		cdda_stop();
	}
	else if (mp3_paused)
	{
		mp3_pause(0);
	}

	if (neogeo_sound_enable)
	{
		timer_suspend_cpu(CPU_Z80, 1, SUSPEND_REASON_NOTSUSPEND);
		sound_enable(1);
	}
	else
	{
		timer_suspend_cpu(CPU_Z80, 0, SUSPEND_REASON_DISABLE);
		sound_enable(0);
	}
}


/*------------------------------------------------------
	カラー設定メニュー
 -----------------------------------------------------*/

#define COLOR_ITEMS 12

static const char *color_item[COLOR_ITEMS] =
{
#if JAPANESE_UI
	"背景画像の種類",
	"背景画像の明るさ",
	"タイトルバーの文字",
	"選択された文字",
	"標準の文字",
	"情報メッセージの文字",
	"警告メッセージの文字",
	"ファイル選択バー(開始)",
	"ファイル選択バー(終了)",
	"タイトルバー/メッセージボックス",
	"タイトルバー/メッセージボックスの枠",
	"背景色(背景画像未使用時)",
#else
	"Background image type",
	"Background image blightness",
	"Title bar text",
	"Selected text",
	"Normal text",
	"Information message text",
	"Warning message text",
	"File select bar (start)",
	"File select bar (end)",
	"Title bar/Message box",
	"Title bar/ Message box frame",
	"Background"
#endif
};

static const int palette[COLOR_ITEMS] =
{
	-1,
	-1,
	UI_PAL_TITLE,
	UI_PAL_SELECT,
	UI_PAL_NORMAL,
	UI_PAL_INFO,
	UI_PAL_WARNING,
	UI_PAL_FILESEL1,
	UI_PAL_FILESEL2,
	UI_PAL_BG1,
	UI_PAL_FRAME,
	UI_PAL_BG2
};

static char *rgb_name[3] =
{
#if JAPANESE_UI
	"赤輝度",
	"緑輝度",
	"青輝度"
#else
	"Red",
	"Green",
	"Blue"
#endif
};

char *bgimage_type_name[4] =
{
#if JAPANESE_UI
	"標準の画像",
	"ユーザの画像",
	"logo.pngのみ",
	"使用しない"
#else
	"Default image",
	"User's image",
	"load.png only",
	"Not use"
#endif
};

void show_color_menu(void)
{
	static int sel = 0;
	int i, x[3], sel_y = 0, update = 1, reload_bg = 0;
	UI_PALETTE *pal;
	char buf[256], rgb_value[3][4];

	pad_wait_clear();
  	load_background(WP_COLORCFG);
	ui_popup_reset();

	do
	{
		if (update)
		{
			update = 0;

		  	show_background();

			small_icon_shadow(8, 3, UI_COLOR(UI_PAL_TITLE), ICON_COLORCFG);
#if JAPANESE_UI
			uifont_print_shadow(36, 5, UI_COLOR(UI_PAL_TITLE), "カラー設定メニュー");
#else
			uifont_print_shadow(36, 5, UI_COLOR(UI_PAL_TITLE), "Color settings menu");
#endif

			for (i = 2; i <= 6; i++)
			{
				if (i < 4)
					uifont_print_shadow(8, 40 + (i - 2) * 18, UI_COLOR(palette[i]), color_item[i]);
				else
					uifont_print(8, 40 + (i - 2) * 18, UI_COLOR(palette[i]), color_item[i]);
			}
			boxfill_gradation(4, 38 + 5 * 18, 475, 58 + 5 * 18, UI_COLOR(UI_PAL_FILESEL1), UI_COLOR(UI_PAL_FILESEL2), 8, 0);
#if JAPANESE_UI
			uifont_print_shadow(8, 40 + 5 * 18, UI_COLOR(UI_PAL_SELECT), "ファイル選択バー");
#else
			uifont_print_shadow(8, 40 + 5 * 18, UI_COLOR(UI_PAL_SELECT), "File select bar");
#endif
			draw_battery_status(1);

			draw_dialog(48, 160, 431, 258);

			uifont_print_center(174, UI_COLOR(UI_PAL_INFO), color_item[sel]);
			if (!sel_y)
			{
				uifont_print(56, 174, UI_COLOR(UI_PAL_SELECT), FONT_LEFTTRIANGLE);
				uifont_print(409, 174, UI_COLOR(UI_PAL_SELECT), FONT_RIGHTTRIANGLE);
			}

			switch (sel)
			{
			case 0:
				if (sel_y)
				{
					if (bgimage_type > 0)  uifont_print(180, 213, UI_COLOR(UI_PAL_SELECT), FONT_LEFTTRIANGLE);
					if (bgimage_type < 3) uifont_print(282, 213, UI_COLOR(UI_PAL_SELECT), FONT_RIGHTTRIANGLE);
					uifont_print_center(213, UI_COLOR(UI_PAL_SELECT), bgimage_type_name[bgimage_type]);
				}
				else
					uifont_print_center(213, UI_COLOR(UI_PAL_NORMAL), bgimage_type_name[bgimage_type]);
				break;

			case 1:
				sprintf(buf, "%3d%%", bgimage_blightness);
				if (sel_y)
				{
					if (bgimage_blightness != 0)   uifont_print(200, 213, UI_COLOR(UI_PAL_SELECT), FONT_LEFTTRIANGLE);
					if (bgimage_blightness != 100) uifont_print(262, 213, UI_COLOR(UI_PAL_SELECT), FONT_RIGHTTRIANGLE);
					uifont_print_center(213, UI_COLOR(UI_PAL_SELECT), buf);
				}
				else
					uifont_print_center(213, UI_COLOR(UI_PAL_NORMAL), buf);
				break;

			default:
				pal = &ui_palette[palette[sel]];
				sprintf(rgb_value[0], "%d", pal->r);
				sprintf(rgb_value[1], "%d", pal->g);
				sprintf(rgb_value[2], "%d", pal->b);
				x[0] = 420 - uifont_get_string_width(rgb_value[0]);
				x[1] = 420 - uifont_get_string_width(rgb_value[1]);
				x[2] = 420 - uifont_get_string_width(rgb_value[2]);

				if (sel_y != 1) 
				{
					uifont_print(59, 198, UI_COLOR(UI_PAL_NORMAL), rgb_name[0]);
					uifont_print(x[0], 198, UI_COLOR(UI_PAL_NORMAL), rgb_value[0]);
				}
				else
				{
					uifont_print(59, 198, UI_COLOR(UI_PAL_SELECT), rgb_name[0]);
					uifont_print(x[0], 198, UI_COLOR(UI_PAL_SELECT), rgb_value[0]);
					box(121, 198, 121 + 255 + 1, 198 + 13, UI_COLOR(UI_PAL_SELECT));
					if (pal->r > 0)   uifont_print(105, 198, UI_COLOR(UI_PAL_SELECT), FONT_LEFTTRIANGLE);
					if (pal->r < 255) uifont_print(377, 198, UI_COLOR(UI_PAL_SELECT), FONT_RIGHTTRIANGLE);
				}
				if (sel_y != 2)
				{
					uifont_print(59, 214, UI_COLOR(UI_PAL_NORMAL), rgb_name[1]);
					uifont_print(x[1], 214, UI_COLOR(UI_PAL_NORMAL), rgb_value[1]);
				}
				else
				{
					uifont_print(59, 214, UI_COLOR(UI_PAL_SELECT), rgb_name[1]);
					uifont_print(x[1], 214, UI_COLOR(UI_PAL_SELECT), rgb_value[1]);
					box(121, 214, 121 + 255 + 1, 214 + 13, UI_COLOR(UI_PAL_SELECT));
					if (pal->g > 0)   uifont_print(105, 214, UI_COLOR(UI_PAL_SELECT), FONT_LEFTTRIANGLE);
					if (pal->g < 255) uifont_print(377, 214, UI_COLOR(UI_PAL_SELECT), FONT_RIGHTTRIANGLE);
				}
				if (sel_y != 3)
				{
					uifont_print(59, 230, UI_COLOR(UI_PAL_NORMAL), rgb_name[2]);
					uifont_print(x[2], 230, UI_COLOR(UI_PAL_NORMAL), rgb_value[2]);
				}
				else
				{
					uifont_print(59, 230, UI_COLOR(UI_PAL_SELECT), rgb_name[2]);
					uifont_print(x[2], 230, UI_COLOR(UI_PAL_SELECT), rgb_value[2]);
					box(121, 230, 121 + 255 + 1, 230 + 13, UI_COLOR(UI_PAL_SELECT));
					if (pal->b > 0)   uifont_print(105, 230, UI_COLOR(UI_PAL_SELECT), FONT_LEFTTRIANGLE);
					if (pal->b < 255) uifont_print(377, 230, UI_COLOR(UI_PAL_SELECT), FONT_RIGHTTRIANGLE);
				}
				boxfill(121 + 1, 198 + 1, 121 + pal->r, 198 + 12, COLOR_RED);
				boxfill(121 + 1, 214 + 1, 121 + pal->g, 214 + 12, COLOR_GREEN);
				boxfill(121 + 1, 230 + 1, 121 + pal->b, 230 + 12, COLOR_BLUE);
				break;
			}

			update = ui_show_popup(1);
			video_flip_screen(1);
		}
		else
		{
			update = ui_show_popup(0);
			video_wait_vsync();
		}

		if (pad_pressed(PSP_CTRL_UP))
		{
			if (sel_y > 0)
			{
				sel_y--;
				update = 1;
			}
		}
		else if (pad_pressed(PSP_CTRL_DOWN))
		{
			if (sel < 2)
			{
				if (sel_y == 0)
				{
					sel_y = 1;
					update = 1;
				}
			}
			else
			{
				if (sel_y < 3)
				{
					sel_y++;
					update = 1;
				}
			}
		}
		else if (pad_pressed(PSP_CTRL_LEFT))
		{
			if (sel_y == 0)
			{
				sel--;
				if (sel < 0) sel = COLOR_ITEMS - 1;
			}
			else
			{
				switch (sel)
				{
				case 0:
					if (bgimage_type > 0)
					{
						bgimage_type--;
						reload_bg = 1;
					}
					break;

				case 1:
					if (bgimage_blightness >= 5)
					{
						bgimage_blightness -= 5;
						reload_bg = 1;
					}
					break;

				default:
					switch (sel_y)
					{
					case 1: if (pal->r > 0) pal->r--; break;
					case 2: if (pal->g > 0) pal->g--; break;
					case 3: if (pal->b > 0) pal->b--; break;
					}
					if (sel == 2 || sel == 11) reload_bg = 1;
					break;
				}
			}
			update = 1;
		}
		else if (pad_pressed(PSP_CTRL_RIGHT))
		{
			if (sel_y == 0)
			{
				sel++;
				if (sel >= COLOR_ITEMS) sel = 0;
			}
			else
			{
				switch (sel)
				{
				case 0:
					if (bgimage_type < 3)
					{
						bgimage_type++;
						reload_bg = 1;
					}
					break;

				case 1:
					if (bgimage_blightness <= 95)
					{
						bgimage_blightness += 5;
						reload_bg = 1;
					}
					break;

				default:
					switch (sel_y)
					{
					case 1: if (pal->r < 255) pal->r++; break;
					case 2: if (pal->g < 255) pal->g++; break;
					case 3: if (pal->b < 255) pal->b++; break;
					}
					if (sel == 2 || sel == 11) reload_bg = 1;
					break;
				}
			}
			update = 1;
		}
		else if (pad_pressed(PSP_CTRL_CIRCLE))
		{
			if (sel >= 2)
			{
				switch (sel_y)
				{
				case 1: pal->r += 5; if (pal->r > 255) pal->r = 255; break;
				case 2: pal->g += 5; if (pal->g > 255) pal->g = 255; break;
				case 3: pal->b += 5; if (pal->b > 255) pal->b = 255; break;
				}
				if (sel == 2 || sel == 11) reload_bg = 1;
			}
			update = 1;
		}
		else if (pad_pressed(PSP_CTRL_SQUARE))
		{
			if (sel >= 2)
			{
				switch (sel_y)
				{
				case 1: pal->r -= 5; if (pal->r < 0) pal->r = 0; break;
				case 2: pal->g -= 5; if (pal->g < 0) pal->g = 0; break;
				case 3: pal->b -= 5; if (pal->b < 0) pal->b = 0; break;
				}
				if (sel == 2 || sel == 11) reload_bg = 1;
			}
			update = 1;
		}
		else if (pad_pressed(PSP_CTRL_TRIANGLE))
		{
			bgimage_type = BG_DEFAULT;
			bgimage_blightness = 75;
			set_ui_color(&ui_palette[UI_PAL_TITLE],    255, 255, 255);
			set_ui_color(&ui_palette[UI_PAL_SELECT],   255, 255, 255);
			set_ui_color(&ui_palette[UI_PAL_NORMAL],   180, 180, 180);
			set_ui_color(&ui_palette[UI_PAL_INFO],     255, 255,  64);
			set_ui_color(&ui_palette[UI_PAL_WARNING],  255,  64,  64);
			set_ui_color(&ui_palette[UI_PAL_BG1],       48,  48,  48);
			set_ui_color(&ui_palette[UI_PAL_BG2],       32,  32,  32);
			set_ui_color(&ui_palette[UI_PAL_FRAME],      0,   0,   0);
			set_ui_color(&ui_palette[UI_PAL_FILESEL1],  40,  40,  40);
			set_ui_color(&ui_palette[UI_PAL_FILESEL2], 120, 120, 120);
			reload_bg = 1;
			update = 1;
		}
		else if (pad_pressed(PSP_CTRL_START))
		{
			ui_save_screenshot("menu5");
			pad_wait_clear();
		}
		else if (pad_pressed(PSP_CTRL_RTRIGGER))
		{
			help(HELP_COLORSETTINGS);
			update = 1;
		}

		if (reload_bg)
		{
			reload_bg = 0;
			load_background(WP_COLORCFG);
		}

		pad_update();

   		if (get_app_status()) break;

	} while (!pad_pressed(PSP_CTRL_CROSS));

	pad_wait_clear();
	load_background(WP_FILER);
	ui_popup_reset();
}
