let CSD = GetCurrentScriptDirectory;
let SE_PAUSE = CSD ~ "./../SoundData/SE/SE_PAUSE.wav";

@Initialize{
	SetAutoDeleteObject(true);
	PauseThread_SetBackground;
	PauseThread_SetMenu;
// 	Pause Sound ----------------------------------
	LoadSound(SE_PAUSE);
	PlaySE(SE_PAUSE);
}

@MainLoop{
	yield;
}

@Finalize{
	/* Empty */
}

task PauseThread_SetBackground{
	let TransTarget = GetTransitionRenderTargetName;
	let objPause = ObjPrim_Create(OBJ_SPRITE_2D);

	ObjPrim_SetTexture(objPause,TransTarget);
	Obj_SetRenderPriority(objPause,0.00);
	ObjSprite2D_SetSourceRect(objPause,0,0,GetScreenWidth,GetScreenHeight);
	ObjSprite2D_SetDestCenter(objPause);
	
	ObjRender_SetPosition(objPause,GetScreenWidth/2,GetScreenHeight/2,0);
	
	let maxFrame = 30;
	descent(let d in 1..maxFrame+1){
		let decAlpha = 32+d*(255-32)/maxFrame;
		ObjRender_SetAlpha(objPause,decAlpha);
		yield;
	}
}

task PauseThread_SetMenu{
	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, 20);
			ObjText_SetFontBold(obj, true);
			ObjText_SetFontColorTop(obj, 128, 128, 255);
			ObjText_SetFontColorBottom(obj, 64, 64, 255);
			ObjText_SetFontBorderType(obj, BORDER_FULL);
			ObjText_SetFontBorderColor(obj,255, 255, 255);
			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);
		ObjRender_SetBlendType(objSelect, BLEND_ADD_ARGB);
		loop
		{
			Obj_SetVisible(objSelect, index == selectIndex);
			yield;
		}
	}

	//メニュー配置
	let mx = 48;
	let my = 32;
	let texts = ["Return", "Return to Menu", "Give Up and Retry"];
	var countMenu = length(texts);
	ascent(var iText in 0 .. countMenu)
	{
		TMenuItem(iText, mx, my, texts[iText]);
		my += 32;
	}

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

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

		//キャンセル
		if(GetVirtualKeyState(VK_CANCEL) == KEY_PULL || GetVirtualKeyState(VK_PAUSE) == KEY_PULL)
		{
			SetScriptResult(RESULT_CANCEL);
			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;
	}
}

function AbsMinX{ return 0; }

function AbsMinY{ return 0; }

function AbsMaxX{ return GetScreenWidth; }

function AbsMaxY{ return GetScreenHeight; }