task TReplayScene{
	let bEndTitleScene = false;

	let pathTitle = dir ~ "./Image/menu.png";
	let pathFire = dir ~ "./Image/firesheet.png";

	let objTitleImage = ObjPrim_Create(OBJ_SPRITE_2D); 
	Obj_SetRenderPriorityI(objTitleImage, 20);
	ObjPrim_SetTexture(objTitleImage, pathTitle);
	ObjSprite2D_SetSourceRect(objTitleImage, 0, 0, 800, 600);
	ObjSprite2D_SetDestRect(objTitleImage, 0, 0, 640, 480);
	ObjRender_SetAlpha(objTitleImage,120);

	let INDEX_REPLAY_EXTRA = 0;
	let INDEX_REPLAY_ULTRA = 1;
	let INDEX_REPLAY_PRACTICE = 2;

	let selectIndex = 0;

	task TMenuItem(let index, let mx, let my, let text){

		function CreateTextObject(let mx, let my, let text){
			let obj = ObjText_Create();
			ObjText_SetText(obj, text);
			ObjText_SetFontSize(obj, 32);
			ObjText_SetFontType(obj,"Blogger Sans");
			ObjText_SetFontColorTop(obj, 255, 225, 255);
			ObjText_SetFontColorBottom(obj, 255, 255, 255);
			Obj_SetRenderPriorityI(obj, 20);
			ObjRender_SetX(obj, mx);
			ObjRender_SetY(obj, my);
			return obj;}
				
		function CreateArrow(let mx, let my){
			let obja = ObjPrim_Create(OBJ_SPRITE_2D);
			ObjPrim_SetTexture(obja, pathFire);
			Obj_SetRenderPriorityI(obja, 25);
			ObjRender_SetScaleXYZ(obja,0.8,0.8,0);
			ObjRender_SetPosition(obja, mx, my, 0);
			ObjRender_SetAlpha(obja,0);
			task tManage{
				let animFrame = 0;let w = 3;
				while(!Obj_IsDeleted(obja)){
				ObjSprite2D_SetDestRect(obja,-38,-70,38,70);
					if(animFrame < w){ObjSprite2D_SetSourceRect(obja,0,8,76,148);}
					if(animFrame >= w && animFrame < w*3){ObjSprite2D_SetSourceRect(obja,82,8,158,148);}
					if(animFrame >= w*3 && animFrame < w*5){ObjSprite2D_SetSourceRect(obja,160,8,236,148);}
					if(animFrame >= w*5 && animFrame < w*7){ObjSprite2D_SetSourceRect(obja,230,8,306,148);}
					if(animFrame >= w*7 && animFrame < w*9){ObjSprite2D_SetSourceRect(obja,310,8,386,148);}
					if(animFrame >= w*9 && animFrame < w*11){ObjSprite2D_SetSourceRect(obja,388,10,464,150);}
					if(animFrame >= w*11 && animFrame < w*13){ObjSprite2D_SetSourceRect(obja,462,7,538,147);}
					if(animFrame >= w*13 && animFrame < w*15){ObjSprite2D_SetSourceRect(obja,544,8,620,148);}
					if(animFrame >= w*15){ObjSprite2D_SetSourceRect(obja,622,8,698,148);}
					if(animFrame>=w*17){animFrame=0;}
				animFrame++;yield;}}
			tManage;
			return obja;}

		let objMenuText = CreateTextObject(mx, my, text);
		let objArrow = CreateArrow(mx-12,my-8);
		let alphaAR = 0;
		let scaleSS = 0;
		loop(20){
			if(index == selectIndex){
				ObjRender_SetAlpha(objArrow,alphaAR);}
			ObjRender_SetScaleX(objMenuText,scaleSS);
			scaleSS+=0.05;alphaAR+=255/20;yield;}

	let move1 = 0;
	let moveA = 0;
	let move2 = 0;
	let moveB = 0;
		
	while(!bEndTitleScene){
		if(index == selectIndex){
			ObjRender_SetAlpha(objArrow,255);
			ObjRender_SetAlpha(objMenuText,255);
			Obj_SetRenderPriority(objMenuText,0.2);
			ObjRender_SetPosition(objMenuText,mx-move1,my,0);
			move2=0;
			moveB=0;
			move1=15*sin(moveA);
			if(move1<4){moveA+=10;}}
		else{
			ObjRender_SetAlpha(objArrow,0);
			ObjRender_SetAlpha(objMenuText,155);
			ObjRender_SetPosition(objMenuText,mx-move1+move2,my,0);
			move1=0;
			moveA=0;
			move2=4*sin(moveB);
			if(move2<4){moveB+=10;}}
		if(GetVirtualKeyState(VK_OK) == KEY_PUSH){
			loop(20){
				if(index == selectIndex){
					ObjRender_SetAlpha(objArrow,alphaAR);}
				ObjRender_SetScaleX(objMenuText,scaleSS);
				scaleSS-=0.05;alphaAR-=255/20;yield;}
				Obj_Delete(objMenuText);
				Obj_Delete(objArrow);}
		if(GetVirtualKeyState(VK_CANCEL) == KEY_PUSH){
			loop(20){
				ObjRender_SetScaleX(objMenuText,scaleSS);
				scaleSS-=0.05;yield;}
				Obj_Delete(objMenuText);
				Obj_Delete(objArrow);}
		yield;}}

	let mx = 150;
	let my = 300;
	let texts = ["Extra","Ultra","Practice"];
	var countMenu = length(texts);
	ascent(var iText in 0 .. countMenu){
		TMenuItem(iText, mx, my, texts[iText]);
		my += 35;}

	while(GetVirtualKeyState(VK_OK) != KEY_FREE){yield;}

	let alphaTitle = 255;
	let frameKeyHold = 0;
	loop{
		if(GetVirtualKeyState(VK_CANCEL) == KEY_PUSH){
			SE(se_cancel);
			let scale = 1;
			let alpha = 120;
			loop(20){
				ObjRender_SetAlpha(objTitleImage,alpha);
				scale-=0.05;alpha+=255/20;yield;}
			Obj_Delete(objTitleImage);
			TTitleScene;
			return;}

		if(GetVirtualKeyState(VK_OK) == KEY_PUSH){
			if(selectIndex == INDEX_REPLAY_EXTRA){
			pathMain =  dir ~ "./Stage/Extra_Stage.txt";
			SE(se_ok);loop(20){yield;}
				TReplaySelectScene();}
				
			if(selectIndex == INDEX_REPLAY_ULTRA){
			pathMain =  dir ~ "./Stage/Ultra_Stage.txt";
			SE(se_ok);loop(20){yield;}
				TReplaySelectScene();}

			else if(selectIndex == INDEX_REPLAY_PRACTICE){
			pathMain =  dir ~ "./Stage/Practice_Stage.txt";
			SE(se_ok);loop(20){yield;}
				TReplaySelectScene();}
			break;}
			
		if(GetVirtualKeyState(VK_UP) == KEY_PUSH){
			SE(se_select);
			selectIndex--;}

		else if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH){
			SE(se_select);
			selectIndex++;}

		else if(GetVirtualKeyState(VK_UP) == KEY_HOLD){
			frameKeyHold++;
			if(frameKeyHold == 30 || (frameKeyHold > 30 && (frameKeyHold % 10 == 0))){
				SE(se_select);
				selectIndex--;}}

		else if(GetVirtualKeyState(VK_DOWN) == KEY_HOLD){
			frameKeyHold++;
			if(frameKeyHold == 30 || (frameKeyHold > 30 && (frameKeyHold % 10 == 0))){
				SE(se_select);
				selectIndex++;}}

		else{frameKeyHold = 0;}
		if(selectIndex < 0) {selectIndex = countMenu - 1;}
		else{selectIndex %= countMenu;}
		yield;}
	bEndTitleScene = true;
	Obj_Delete(objTitleImage);}