//---------------------------------------------------------------------------------
//
//　メニュー用の関数・タスク群です。
//　
//
//---------------------------------------------------------------------------------

//ステージメインタスク　メニュー画面
//   @ 引数なし	
task StageTask_Menu{
	LoadGraphic_Menu();
	MenuBG();
	SelectObject();
	MenuTEXT();
	ForbidShot(true);
	ForbidBomb(true);
	while(nStageState == STAGE_STATE_MENU){
		SetPlayerX(DEFAULT_POSITION_X);
		SetPlayerY(DEFAULT_POSITION_Y);		//ポジション固定
		OutputDebugString(0, "Stage_Menu", 1);
		OutputDebugString(1, "nStageState", nStageState);
		OutputDebugString(2, "nMenuState", nMenuState);
		OutputDebugString(3, "nMenuSelect", nMenuSelect);
		OutputDebugString(4, "nSpellLank", nSpellLank);
		OutputDebugString(5, "nSpellCard", nSpellCard);
		
		//↑キーが押された時
		if(GetKeyState(VK_UP) == KEY_PUSH){
			alternative(nMenuState)
			case(MENU_STATE_INIT){
				if(nMenuSelect > SELECT_STATE_ALL){
					nMenuSelect--;
					fMenuSelectNext = true;
				}
			}
			case(MENU_STATE_LANK){
				if(nMenuSelect > SELECT_STATE_EASY){
					nMenuSelect--;
					fMenuSelectNext = true;
				}
			}
			PlaySE(SE_SELECT);
		}
		
		//↓キーが押された時
		if(GetKeyState(VK_DOWN) == KEY_PUSH){
			alternative(nMenuState)
			case(MENU_STATE_INIT){
				if(nMenuSelect < SELECT_STATE_QUIT){
					nMenuSelect++;
					fMenuSelectNext = true;
				}
			}
			case(MENU_STATE_LANK){
				if(nMenuSelect < SELECT_STATE_OMAKE){
					nMenuSelect++;
					fMenuSelectNext = true;
				}
			}
			PlaySE(SE_SELECT);
		}
		
		//Zキーが押された時
		if(GetKeyState(VK_SHOT) == KEY_PUSH){
			alternative(nMenuSelect)
			case(SELECT_STATE_ALL){
				//ALLのときは nSpellCard に SPELL_ALL を付加して nMenuState を MENU_STATE_LANK に遷移
				nSpellCard = SPELL_ALL;
				nMenuState = MENU_STATE_LANK;
				nMenuSelect = SELECT_STATE_EASY;
				fMenuSelectNext = true;
			}
			case(SELECT_STATE_014){
				//_014のときは nSpellCard に SPELL_014 を付加して nMenuState を MENU_STATE_LANK に遷移
				nSpellCard = SPELL_014;
				nMenuState = MENU_STATE_LANK;
				nMenuSelect = SELECT_STATE_EASY;
				fMenuSelectNext = true;
			}
			case(SELECT_STATE_017){
				//_017のときは nSpellCard に SPELL_017 を付加して nMenuState を MENU_STATE_LANK に遷移
				nSpellCard = SPELL_017;
				nMenuState = MENU_STATE_LANK;
				nMenuSelect = SELECT_STATE_EASY;
				fMenuSelectNext = true;
			}
			case(SELECT_STATE_022){
				//_022のときは nSpellCard に SPELL_022 を付加して nMenuState を MENU_STATE_LANK に遷移
				nSpellCard = SPELL_022;
				nMenuState = MENU_STATE_LANK;
				nMenuSelect = SELECT_STATE_EASY;
				fMenuSelectNext = true;
			}
			case(SELECT_STATE_QUIT){
				//QUITのときは nMenuState を STAGE_STATE_EXIT に遷移
				nStageState = STAGE_STATE_EXIT;
				fStageStateNext = true;
			}
			case(SELECT_STATE_EASY){
				//Easyのときは nSpellLank に SPELL_LANK_EASY を付加して nMenuState を MENU_STATE_NONE に遷移
				//さらに fStageStateNext trueにしてステージスイッチを解放。nStageState を STAGE_STATE_SPELL に遷移
				nSpellLank = SPELL_LANK_EASY;
				nMenuState = MENU_STATE_NONE;
				nMenuSelect = SELECT_STATE_NONE;
				nStageState = STAGE_STATE_SPELL;
				fMenuSelectNext = true;
				fStageStateNext = true;
			}
			case(SELECT_STATE_NORMAL){
				//Normalのときは nSpellLank に SPELL_LANK_NORMAL を付加して nMenuState を MENU_STATE_NONE に遷移
				//さらに fStageStateNext trueにしてステージスイッチを解放。nStageState を STAGE_STATE_SPELL に遷移
				nSpellLank = SPELL_LANK_NORMAL;
				nMenuState = MENU_STATE_NONE;
				nMenuSelect = SELECT_STATE_NONE;
				nStageState = STAGE_STATE_SPELL;
				fMenuSelectNext = true;
				fStageStateNext = true;
			}
			case(SELECT_STATE_HARD){
				//Hardのときは nSpellLank に SPELL_LANK_HARD を付加して nMenuState を MENU_STATE_NONE に遷移
				//さらに fStageStateNext trueにしてステージスイッチを解放。nStageState を STAGE_STATE_SPELL に遷移
				nSpellLank = SPELL_LANK_HARD;
				nMenuState = MENU_STATE_NONE;
				nMenuSelect = SELECT_STATE_NONE;
				nStageState = STAGE_STATE_SPELL;
				fMenuSelectNext = true;
				fStageStateNext = true;
			}
			case(SELECT_STATE_LUNATIC){
				//Lunaticのときは nSpellLank に SPELL_LANK_LUNATIC を付加して nMenuState を MENU_STATE_NONE に遷移
				//さらに fStageStateNext trueにしてステージスイッチを解放。nStageState を STAGE_STATE_SPELL に遷移
				nSpellLank = SPELL_LANK_LUNATIC;
				nMenuState = MENU_STATE_NONE;
				nMenuSelect = SELECT_STATE_NONE;
				nStageState = STAGE_STATE_SPELL;
				fMenuSelectNext = true;
				fStageStateNext = true;
			}
			case(SELECT_STATE_OMAKE){
				//Omakeのときは nSpellLank に SPELL_LANK_OMAKE を付加して nMenuState を MENU_STATE_NONE に遷移
				//さらに fStageStateNext trueにしてステージスイッチを解放。nStageState を STAGE_STATE_SPELL に遷移
				nSpellLank = SPELL_LANK_OMAKE;
				nMenuState = MENU_STATE_NONE;
				nMenuSelect = SELECT_STATE_NONE;
				nStageState = STAGE_STATE_SPELL;
				fMenuSelectNext = true;
				fStageStateNext = true;
			}
			PlaySE(SE_OK);
		}
		
		//Xキーが押されたとき
		if(GetKeyState(VK_BOMB) == KEY_PUSH){
			alternative(nMenuState)
			case(MENU_STATE_LANK){
				//MENU_STATE_LANKのときは nSpellCard をクリアして nMenuState を MENU_STATE_INIT に遷移
				nMenuSelect = (nSpellCard - SYS_SYSTEM) + SYS_STATE;
				nSpellCard = SPELL_NONE;
				nMenuState = MENU_STATE_INIT;
				fMenuSelectNext = true;
			}
			case(MENU_STATE_INIT){
				//MENU_STATE_INITのときは nMenuSelect を SELECT_STATE_QUIT に遷移
				nSpellCard = SPELL_NONE;
				nMenuSelect = SELECT_STATE_QUIT;
				fMenuSelectNext = true;
			}
			PlaySE(SE_BACK);
		}
		
		yield;
	}
}

//メニューの各選択部分のオブジェクトタスク
//   @ 引数なし	
task SelectObject{
	loop{
		let SObj = [];
		alternative(nMenuState)
		case(MENU_STATE_INIT){
			//ALL〜Quitまでの選択項目
			ascent(let i in 0..5){
				SObj = SObj ~ [i];
				
				SObj[i] = Obj_Create(OBJ_EFFECT);
				ObjEffect_SetPrimitiveType(SObj[i], PRIMITIVE_TRIANGLESTRIP);
				ObjEffect_CreateVertex(SObj[i], 4);
				ObjEffect_SetTexture(SObj[i],IMG_MENU_SELECT);
				ObjEffect_SetLayer(SObj[i],8);
				
				//色と位置
				if(nMenuSelect == i + SYS_STATE + 1){
					Obj_SetPosition(SObj[i], GetCenterX - 60, GetClipMinY + 32*i + 25);
					
					ObjEffect_SetVertexColor(SObj[i], 0, 255, YELLOW_R, YELLOW_G, YELLOW_B);
					ObjEffect_SetVertexColor(SObj[i], 1, 255, YELLOW_R, YELLOW_G, YELLOW_B);
					ObjEffect_SetVertexColor(SObj[i], 2, 255, YELLOW_R, YELLOW_G, YELLOW_B);
					ObjEffect_SetVertexColor(SObj[i], 3, 255, YELLOW_R, YELLOW_G, YELLOW_B);
				}else{
					Obj_SetPosition(SObj[i], GetCenterX - 55, GetClipMinY + 32*i + 25);
					
					ObjEffect_SetVertexColor(SObj[i], 0, 255, GREY_R, GREY_G, GREY_B);
					ObjEffect_SetVertexColor(SObj[i], 1, 255, GREY_R, GREY_G, GREY_B);
					ObjEffect_SetVertexColor(SObj[i], 2, 255, GREY_R, GREY_G, GREY_B);
					ObjEffect_SetVertexColor(SObj[i], 3, 255, GREY_R, GREY_G, GREY_B);
				}
				
				let nRight = 255;
				let nLeft = 0;
				let nTop = i * 32;
				let nBottom = 31 + nTop;

				let width = (|nRight|) - (|nLeft|);
				let length = (|nBottom|) - (|nTop|);

				let width2 = ceil(width /  2);
				let length2 = ceil(length / 2);

				let width3 = width - width2;
				let length3 = length - length2;

				//四角形状に各頂点の座標を設定
				ObjEffect_SetVertexXY(SObj[i], 0, -width3, -length3);
				ObjEffect_SetVertexXY(SObj[i], 1, -width3,  length2);
				ObjEffect_SetVertexXY(SObj[i], 2,  width2, -length3);
				ObjEffect_SetVertexXY(SObj[i], 3,  width2,  length2);
					
				//四角形状に各頂点のUV値を設定。
				ObjEffect_SetVertexUV(SObj[i], 0,  nLeft,  nTop);
				ObjEffect_SetVertexUV(SObj[i], 1,  nLeft, nBottom);
				ObjEffect_SetVertexUV(SObj[i], 2, nRight,  nTop);
				ObjEffect_SetVertexUV(SObj[i], 3, nRight, nBottom);
				
			}
			
			while(fMenuSelectNext == false && fMenuStateNext == false){ yield; }
			yield;
			fMenuSelectNext = false;
		}
		
		case(MENU_STATE_LANK){
			//ALL〜Quitまでの選択項目
			ascent(let i in 0..5){
				SObj = SObj ~ [i];
				
				SObj[i] = Obj_Create(OBJ_EFFECT);
				ObjEffect_SetPrimitiveType(SObj[i], PRIMITIVE_TRIANGLESTRIP);
				ObjEffect_CreateVertex(SObj[i], 4);
				ObjEffect_SetTexture(SObj[i],IMG_MENU_SELECT);
				ObjEffect_SetLayer(SObj[i],8);
				
				//色と位置
				if(nSpellCard == i + SYS_SYSTEM + 1){
					Obj_SetPosition(SObj[i], GetCenterX - 60, GetClipMinY + 32*i + 25);
					
					ObjEffect_SetVertexColor(SObj[i], 0, 255, WHITE_R, WHITE_G, WHITE_B);
					ObjEffect_SetVertexColor(SObj[i], 1, 255, WHITE_R, WHITE_G, WHITE_B);
					ObjEffect_SetVertexColor(SObj[i], 2, 255, WHITE_R, WHITE_G, WHITE_B);
					ObjEffect_SetVertexColor(SObj[i], 3, 255, WHITE_R, WHITE_G, WHITE_B);
				}else{
					Obj_SetPosition(SObj[i], GetCenterX - 55, GetClipMinY + 32*i + 25);
					
					ObjEffect_SetVertexColor(SObj[i], 0, 255, GREY_R, GREY_G, GREY_B);
					ObjEffect_SetVertexColor(SObj[i], 1, 255, GREY_R, GREY_G, GREY_B);
					ObjEffect_SetVertexColor(SObj[i], 2, 255, GREY_R, GREY_G, GREY_B);
					ObjEffect_SetVertexColor(SObj[i], 3, 255, GREY_R, GREY_G, GREY_B);
				}
				
				let nRight = 255;
				let nLeft = 0;
				let nTop = i * 32;
				let nBottom = 31 + nTop;

				let width = (|nRight|) - (|nLeft|);
				let length = (|nBottom|) - (|nTop|);

				let width2 = ceil(width /  2);
				let length2 = ceil(length / 2);

				let width3 = width - width2;
				let length3 = length - length2;

				//四角形状に各頂点の座標を設定
				ObjEffect_SetVertexXY(SObj[i], 0, -width3, -length3);
				ObjEffect_SetVertexXY(SObj[i], 1, -width3,  length2);
				ObjEffect_SetVertexXY(SObj[i], 2,  width2, -length3);
				ObjEffect_SetVertexXY(SObj[i], 3,  width2,  length2);
					
				//四角形状に各頂点のUV値を設定。
				ObjEffect_SetVertexUV(SObj[i], 0,  nLeft,  nTop);
				ObjEffect_SetVertexUV(SObj[i], 1,  nLeft, nBottom);
				ObjEffect_SetVertexUV(SObj[i], 2, nRight,  nTop);
				ObjEffect_SetVertexUV(SObj[i], 3, nRight, nBottom);
				
			}
			//EASY〜OMAKEまでの選択項目
			ascent(let i in 5..10){
				SObj = SObj ~ [i];
				
				SObj[i] = Obj_Create(OBJ_EFFECT);
				ObjEffect_SetPrimitiveType(SObj[i], PRIMITIVE_TRIANGLESTRIP);
				ObjEffect_CreateVertex(SObj[i], 4);
				ObjEffect_SetTexture(SObj[i],IMG_MENU_SELECT);
				ObjEffect_SetLayer(SObj[i],8);
				
				//色と位置
				if(nMenuSelect == i + SYS_STATE + 1){
					Obj_SetPosition(SObj[i], GetCenterX + 55, GetClipMinY + 32*(i-5) + 25);
					
					ObjEffect_SetVertexColor(SObj[i], 0, 255, YELLOW_R, YELLOW_G, YELLOW_B);
					ObjEffect_SetVertexColor(SObj[i], 1, 255, YELLOW_R, YELLOW_G, YELLOW_B);
					ObjEffect_SetVertexColor(SObj[i], 2, 255, YELLOW_R, YELLOW_G, YELLOW_B);
					ObjEffect_SetVertexColor(SObj[i], 3, 255, YELLOW_R, YELLOW_G, YELLOW_B);
				}else{
					Obj_SetPosition(SObj[i], GetCenterX + 60, GetClipMinY + 32*(i-5) + 25);
					
					ObjEffect_SetVertexColor(SObj[i], 0, 255, GREY_R, GREY_G, GREY_B);
					ObjEffect_SetVertexColor(SObj[i], 1, 255, GREY_R, GREY_G, GREY_B);
					ObjEffect_SetVertexColor(SObj[i], 2, 255, GREY_R, GREY_G, GREY_B);
					ObjEffect_SetVertexColor(SObj[i], 3, 255, GREY_R, GREY_G, GREY_B);
				}
				
				let nRight = 255;
				let nLeft = 0;
				let nTop = i * 32;
				let nBottom = 31 + nTop;

				let width = (|nRight|) - (|nLeft|);
				let length = (|nBottom|) - (|nTop|);

				let width2 = ceil(width /  2);
				let length2 = ceil(length / 2);

				let width3 = width - width2;
				let length3 = length - length2;

				//四角形状に各頂点の座標を設定
				ObjEffect_SetVertexXY(SObj[i], 0, -width3, -length3);
				ObjEffect_SetVertexXY(SObj[i], 1, -width3,  length2);
				ObjEffect_SetVertexXY(SObj[i], 2,  width2, -length3);
				ObjEffect_SetVertexXY(SObj[i], 3,  width2,  length2);
					
				//四角形状に各頂点のUV値を設定。
				ObjEffect_SetVertexUV(SObj[i], 0,  nLeft,  nTop);
				ObjEffect_SetVertexUV(SObj[i], 1,  nLeft, nBottom);
				ObjEffect_SetVertexUV(SObj[i], 2, nRight,  nTop);
				ObjEffect_SetVertexUV(SObj[i], 3, nRight, nBottom);
				
			}
			
			while(fMenuSelectNext == false && fMenuStateNext == false){ yield; }
			yield;
			fMenuSelectNext = false;
			
		}
		
		yield;
		ascent(let i in 0..length(SObj)){
			Obj_Delete(SObj[i]);
		}
		if(nStageState != STAGE_STATE_MENU){ break; }
	}
}

//メニューの背景部分のオブジェクトタスク
//   @ 引数なし	
task MenuBG{
	loop{
		let BGObj = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetPrimitiveType(BGObj, PRIMITIVE_TRIANGLESTRIP);//プリミティブタイプを設定
		ObjEffect_CreateVertex(BGObj, 4);//頂点を4個つくる
		ObjEffect_SetTexture(BGObj,IMG_MENU_BG);
		ObjEffect_SetLayer(BGObj,8);
		
		let nRight = 640;
		let nLeft = 0;
		let nTop = 0;
		let nBottom = 480;

		let width = (|nRight|) - (|nLeft|);
		let length = (|nBottom|) - (|nTop|);
		let width2 = ceil(width /  2);
		let length2 = ceil(length / 2);
		let width3 = width - width2;
		let length3 = length - length2;

		//四角形状に各頂点の座標を設定
		ObjEffect_SetVertexXY(BGObj, 0, -width3*1, -length3*1);
		ObjEffect_SetVertexXY(BGObj, 1, -width3*1,  length2*1);
		ObjEffect_SetVertexXY(BGObj, 2,  width2*1, -length3*1);
		ObjEffect_SetVertexXY(BGObj, 3,  width2*1,  length2*1);
			
		//四角形状に各頂点のUV値を設定。
		ObjEffect_SetVertexUV(BGObj, 0,  nLeft,  nTop);
		ObjEffect_SetVertexUV(BGObj, 1,  nLeft, nBottom);
		ObjEffect_SetVertexUV(BGObj, 2, nRight,  nTop);
		ObjEffect_SetVertexUV(BGObj, 3, nRight, nBottom);
		Obj_SetPosition(BGObj, 320, 240);
		while(1){
			yield;
			if(nStageState != STAGE_STATE_MENU){ Obj_Delete(BGObj); break; }
		}
		break;
	}
}

//メニューのテキスト部分のオブジェクトタスク
//   @ 引数なし	
task MenuTEXT{
	loop{
		let nSetMenuText = 0;
		//テキストの選択
		//nMenuSelectに値が_014〜_022ならばそこから決定
		//そうでないならnSpellCardから決定
		alternative(nMenuSelect)
		case(SELECT_STATE_014){
			nSetMenuText = 0;
		}
		case(SELECT_STATE_017){
			nSetMenuText = 1;
		}
		case(SELECT_STATE_022){
			nSetMenuText = 2;
		}
		others{
			alternative(nSpellCard)		case(SPELL_014)	{ nSetMenuText = 0; }
							case(SPELL_017)	{ nSetMenuText = 1; }
							case(SPELL_022)	{ nSetMenuText = 2; }
							others		{ nSetMenuText = 4; }	//真っ白テキスト
		}
		
		
		let TXObj = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetPrimitiveType(TXObj, PRIMITIVE_TRIANGLESTRIP);//プリミティブタイプを設定
		ObjEffect_CreateVertex(TXObj, 4);//頂点を4個つくる
		ObjEffect_SetTexture(TXObj,IMG_MENU_TEXT);
		ObjEffect_SetLayer(TXObj,8);
		
		let nRight = 620;
		let nLeft = 0;
		let nTop = 144 * nSetMenuText;
		let nBottom = nTop + 143;

		let width = (|nRight|) - (|nLeft|);
		let length = (|nBottom|) - (|nTop|);
		let width2 = ceil(width /  2);
		let length2 = ceil(length / 2);
		let width3 = width - width2;
		let length3 = length - length2;

		//四角形状に各頂点の座標を設定
		ObjEffect_SetVertexXY(TXObj, 0, -width3, -length3);
		ObjEffect_SetVertexXY(TXObj, 1, -width3,  length2);
		ObjEffect_SetVertexXY(TXObj, 2,  width2, -length3);
		ObjEffect_SetVertexXY(TXObj, 3,  width2,  length2);
			
		//四角形状に各頂点のUV値を設定。
		ObjEffect_SetVertexUV(TXObj, 0,  nLeft,  nTop);
		ObjEffect_SetVertexUV(TXObj, 1,  nLeft, nBottom);
		ObjEffect_SetVertexUV(TXObj, 2, nRight,  nTop);
		ObjEffect_SetVertexUV(TXObj, 3, nRight, nBottom);
		
		ObjEffect_SetVertexColor(TXObj, 0, 255, PINK_R, PINK_G, PINK_B);
		ObjEffect_SetVertexColor(TXObj, 1, 255, PINK_R, PINK_G, PINK_B);
		ObjEffect_SetVertexColor(TXObj, 2, 255, PINK_R, PINK_G, PINK_B);
		ObjEffect_SetVertexColor(TXObj, 3, 255, PINK_R, PINK_G, PINK_B);
		
		Obj_SetPosition(TXObj, 320, 360);
		
		while(fMenuSelectNext == false && fMenuStateNext == false && fStageStateNext == false){ yield; }
		yield;
		fMenuSelectNext = false;
		Obj_Delete(TXObj);
		if(nStageState != STAGE_STATE_MENU){ Obj_Delete(TXObj); break; }
		
	}
}

//この関数・タスク群で用いる画像の読み込み
//   @ 引数なし	
sub LoadGraphic_Menu{
	LoadGraphic(IMG_MENU_SELECT);
	LoadGraphic(IMG_MENU_TEXT);
	LoadGraphic(IMG_MENU_BG);
}




