let pathtext = GetCurrentScriptDirectory ~ "./img/pause.png";
let waitKillDuration = 0;

@Initialize
{
	SetAutoDeleteObject(true);
	TBackground(30);
	TMenu();
	TImage;

	SetCommonData("IsPaused", true);
}

@MainLoop
{
	yield;
}

@Finalize
{
}

task TBackground(iterations){
	let target = GetTransitionRenderTargetName();
	let allObj = [];
	let prematureSkip = false;

	RenderFrame;

	ascent(i in 0 .. iterations){
		if(GetVirtualKeyState(VK_OK) == KEY_PUSH || GetVirtualKeyState(VK_CANCEL) == KEY_PUSH){
			prematureSkip = true;
			break;
		}

		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		Obj_SetRenderPriorityI(obj, 0);
		ObjPrim_SetTexture(obj, target);
		ObjSprite2D_SetSourceRect(obj, 128, 16, 128+384, 16+448);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetPosition(obj, 320, 240, 0);
		ObjRender_SetScaleXYZ(obj, 1 - (i/iterations), 1 - (i/iterations), 0);
		ObjRender_SetAlpha(obj, 64 - 64*i/(iterations));
		ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);

		allObj = allObj ~ [obj];
		waitKillDuration++;
		yield;
	}

	while(GetVirtualKeyState(VK_OK) != KEY_FREE){yield;}
	if(!prematureSkip){
		while(GetVirtualKeyState(VK_OK) != KEY_PULL && GetVirtualKeyState(VK_CANCEL) != KEY_PULL){yield;}
	}

	let i2 = 0;
	descent(i in 1 .. length(allObj)){
		Obj_Delete(allObj[i]);
		ObjRender_SetAlpha(allObj[0], 64 + (192/(iterations - 1)) * i2);
		i2++;
		yield;
	}
}

task RenderFrame{
	let target = GetTransitionRenderTargetName();

	let objLeft = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(objLeft, 0);
	ObjPrim_SetTexture(objLeft, target);
	ObjSprite2D_SetSourceRect(objLeft, 0, 0, 128, 480);
	ObjSprite2D_SetDestRect(objLeft, 0, 0, 128, 480);

	let objRight = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(objRight, 0);
	ObjPrim_SetTexture(objRight, target);
	ObjSprite2D_SetSourceRect(objRight, 512, 0, 640, 480);
	ObjSprite2D_SetDestRect(objRight, 512, 0, 640, 480);

	let objTop = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(objTop, 0);
	ObjPrim_SetTexture(objTop, target);
	ObjSprite2D_SetSourceRect(objTop, 128, 0, 512, 16);
	ObjSprite2D_SetDestRect(objTop, 128, 0, 512, 16);

	let objBottom = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(objBottom, 0);
	ObjPrim_SetTexture(objBottom, target);
	ObjSprite2D_SetSourceRect(objBottom, 128, 464, 512, 480);
	ObjSprite2D_SetDestRect(objBottom, 128, 464, 512, 480);
}


task TMenu
{
	let selectIndex = 0;//選択位置
	task TMenuItem(index, mx, my, rect)
	{
		function CreateTextObject(mx, my, rect)
		{
			let obj = ObjPrim_Create(OBJ_SPRITE_2D);
			ObjPrim_SetTexture(obj, pathtext);
			ObjSprite2D_SetSourceRect(obj, rect[0], rect[1], rect[2], rect[3]);
			ObjSprite2D_SetDestRect(obj, 0, -8, 256, 8);
			Obj_SetRenderPriorityI(obj, 100);
			ObjRender_SetX(obj, mx);
			ObjRender_SetY(obj, my);
			return obj;
		}

		let objText = CreateTextObject(mx, my, rect);
		let c = 0;
		let rr = 255;
		loop{
			if(index == selectIndex){
				MovePrimitiveObject(objText, mx + 16, my, 5);

				ObjRender_SetColor(objText, 255, rr, rr);
				rr -= 8*sin(c*4);
				c++;
			}else{
				MovePrimitiveObject(objText, mx, my, 5);

				ObjRender_SetColor(objText, 255, 255, 255);
				rr = 255;
				c = 0;
			}
			yield;
		}
	}

	//メニュー配置
	let mx = 192;
	let my = 256;
	let rects = [[0, 512, 1024, 576], [0, 320, 1024, 384], /*[0, 576, 1024, 640],*/ [0, 256, 1024, 320]]; //Return to game, give up, save replay & return to title, retry
	var countMenu = length(rects);
	ascent(var i in 0 .. countMenu)
	{
		TMenuItem(i, mx, my, rects[i]);
		my += 16;
		mx += 8;
	}

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

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

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

task TImage{
	let text = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(text, pathtext);
	ObjSprite2D_SetSourceRect(text, 0, 64, 1024, 127);
	ObjSprite2D_SetDestRect(text, 0, -31.5, 1024, 31.5);
	ObjRender_SetScaleXYZ(text, 0.35, 0.35, 0);
	Obj_SetRenderPriorityI(text, 100);
	ObjRender_SetPosition(text, 192, 192, 0);
	ObjRender_SetColor(text, 0, 192, 0);

	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory ~ "./img/flower.png");
	ObjSprite2D_SetSourceRect(obj, 0, 0, 256, 1024);
	ObjSprite2D_SetDestCenter(obj);
	Obj_SetRenderPriorityI(obj, 99);
	ObjRender_SetPosition(obj, 192, 256, 0);
	ObjRender_SetScaleXYZ(obj, 0.25, 0.25, 0);
	ObjRender_SetColor(obj, 0, 255, 64);

	let z = 0;
	while(!Obj_IsDeleted(obj)){
		ObjRender_SetAngleZ(obj, ObjRender_GetAngleZ(obj) - sin(z*2)/4);
		z++;
		yield;
	}
}

task MovePrimitiveObject(obj, destX, destY, frame){
	let x = ObjRender_GetX(obj);
	let y = ObjRender_GetY(obj);
	loop(frame){
		ObjRender_SetPosition(obj, x, y, 0);
		x += (destX - x)/frame;
		y += (destY - y)/frame;
		yield;
	}
}