//一時停止中スクリプト

InstallFont(GetCurrentScriptDirectory() ~ "../lib/Fonts/DTM.otf");
@Initialize
{
	SetAutoDeleteObject(true);
	TBackground();
	TMenu();
}

@MainLoop
{
	yield;
}

@Finalize
{
}


task TBackground
{
	let target = GetTransitionRenderTargetName();
	
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(obj, 4);
	Obj_SetRenderPriorityI(obj, 0);
	ObjPrim_SetTexture(obj, target);
	
	let left = 0; 
	let top = 0;
	let right = GetScreenWidth();
	let bottom = GetScreenHeight();

	ObjPrim_SetVertexPosition(obj, 0, left, top, 0);
	ObjPrim_SetVertexPosition(obj, 1, right, top, 0);
	ObjPrim_SetVertexPosition(obj, 2, left, bottom, 0);
	ObjPrim_SetVertexPosition(obj, 3, right, bottom, 0);

	ObjPrim_SetVertexUVT(obj, 0, left, top);
	ObjPrim_SetVertexUVT(obj, 1, right, top);
	ObjPrim_SetVertexUVT(obj, 2, left, bottom);
	ObjPrim_SetVertexUVT(obj, 3, right, bottom);
	
	let alpha = 255;
	while(alpha >= 128){
		ObjRender_SetAlpha(obj, alpha);
		alpha -= 255 / 20;
		yield;
	}

}

task TMenu
{
	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_SetFontType(obj, "Determination Sans");
			ObjText_SetFontCharacterSet(obj, 0x00000001);
			ObjText_SetFontSize(obj, 30);
			ObjText_SetFontBold(obj, true);
			ObjText_SetFontColorTop(obj, 200, 200, 200);
			ObjText_SetFontColorBottom(obj, 255, 255, 255);
			ObjText_SetFontBorderType(obj, BORDER_FULL);
			ObjText_SetFontBorderColor(obj, 0, 0, 0);
			ObjText_SetFontBorderWidth(obj, 2);
			Obj_SetRenderPriorityI(obj, 10);
			ObjRender_SetX(obj, mx);
			ObjRender_SetY(obj, my);
			return obj;
		}

		let objText = CreateTextObject(mx, my, text);
		let objSelect = CreateTextObject(mx, my, text);
		ObjText_SetFontColorTop(objSelect, 200, 200, 0);
		ObjText_SetFontColorBottom(objSelect, 255, 255, 0);
		ObjRender_SetBlendType(objSelect, BLEND_ALPHA);
		loop
		{
			Obj_SetVisible(objSelect, index == selectIndex);
			yield;
		}
	}

	//メニュー配置
	let mx = GetScreenWidth()/2.75;
	let my = GetScreenHeight()/2.75;
	let texts = ["\> Save Replay", "\> Quit", "\> Try Again"];
	var countMenu = length(texts);
	ascent(var iText in 0 .. countMenu)
	{
		TMenuItem(iText, mx, my, texts[iText]);
		my += 50;
	}

	//キー状態がリセットされるまで待機
	while(GetVirtualKeyState(VK_OK) != KEY_FREE){yield;}

	//メニュー選択処理
	let frameKeyHold = 0;//キー押しっぱなしフレーム数
	loop
	{
		//決定
		if(GetVirtualKeyState(VK_OK) == KEY_PULL)
		{
			let listResult = [RESULT_SAVE_REPLAY, RESULT_END, RESULT_RETRY];
			SetScriptResult(listResult[selectIndex]);
			CloseScript(GetOwnScriptID());
			return;
		}

		//カーソル移動
		if(GetVirtualKeyState(VK_UP) == KEY_PUSH)
		{
			selectIndex--;
		}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH)
		{
			selectIndex++;
		}
		else if(GetVirtualKeyState(VK_UP) == KEY_HOLD)
		{
			frameKeyHold++;
			if(frameKeyHold == 30 || (frameKeyHold > 30 && (frameKeyHold % 10 == 0)))
			{
				selectIndex--;
			}
		}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_HOLD)
		{
			frameKeyHold++;
			if(frameKeyHold == 30 || (frameKeyHold > 30 && (frameKeyHold % 10 == 0)))
			{
				selectIndex++;
			}
		}
		else
		{
			frameKeyHold = 0;
		}

		if(selectIndex < 0) 
		{
			selectIndex = countMenu - 1;
		}
		else
		{
			selectIndex %= countMenu;
		}

		yield;
	}
}



