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

@Initialize
{
	SetAutoDeleteObject(true);
	TBackground();
	TMenu();
}

@MainLoop
{
	yield;
}

@Finalize
{
}


task TBackground
{
	task TVertex(var index, var left, var top, var right, var bottom)
	{
		ObjPrim_SetVertexPosition(obj, index + 0, left, top, 0);
		ObjPrim_SetVertexPosition(obj, index + 1, left, bottom, 0);
		ObjPrim_SetVertexPosition(obj, index + 2, right, top, 0);
		ObjPrim_SetVertexPosition(obj, index + 3, right, top, 0);
		ObjPrim_SetVertexPosition(obj, index + 4, left, bottom, 0);
		ObjPrim_SetVertexPosition(obj, index + 5, right, bottom, 0);

		ObjPrim_SetVertexUVT(obj, index + 0, left, top);
		ObjPrim_SetVertexUVT(obj, index + 1, left, bottom);
		ObjPrim_SetVertexUVT(obj, index + 2, right, top);
		ObjPrim_SetVertexUVT(obj, index + 3, right, top);
		ObjPrim_SetVertexUVT(obj, index + 4, left, bottom);
		ObjPrim_SetVertexUVT(obj, index + 5, right, bottom);

		//STGシーン内のみアニメーション
		
	}

	

	//分割設定
	let frame = 30;
	let countH = 1; //分割数
	let countV = 1; 
	let width = 640 / countH;
	let height = 480 / countV;
	let target = GetTransitionRenderTargetName();
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D); //2D頂点ブジェクト生成
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLELIST);
	ObjPrim_SetVertexCount(obj, countH * countV * 6);
	Obj_SetRenderPriorityI(obj, 0); //描画優先度を設定
	ObjPrim_SetTexture(obj, target); //テクスチャを設定

	ascent(ix in 0.. countH)
	{
		ascent(iy in 0.. countV)
		{
			let index = (ix + iy * countH) * 6;
			let left = ix * width;
			let right = left + width;
			let top = iy * height;
			let bottom = top + height;
			TVertex(index, left * rez, top * rez, right* rez, bottom* rez);
		}
	}
	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);

	Obj_SetRenderPriorityI(obj2, 01);
	ObjRender_SetBlendType(obj2, BLEND_SUBTRACT);
	ObjSprite2D_SetSourceRect(obj2, 0, 0, 384,448);
	ObjSprite2D_SetDestRect(obj2, 0, 0, 384, 448);
	ObjSprite2D_SetDestCenter(obj2);
	ObjRender_SetPosition(obj2, 32.5*rez, 17*rez, 1);
	ObjRender_SetScaleXYZ(obj2, rez, rez, 1);
	ObjRender_SetAlpha(obj2, 0);
	let alpha = 0;
	let ap = 50/wt;
	loop(wt){
		alpha += ap;
		ObjRender_SetAlpha(obj2, alpha);
		yield;
	}
}

let endstate = 0;
let confstate = 0;
let wt = 15;
let rez = GetScreenHeight()/480;

let CSD = GetCurrentScriptDirectory();

let spause = CSD ~ "se/pause.wav";
let sselect = CSD ~ "se/select.wav";
let sok = CSD ~ "se/ok.wav";

let spausev = 70;
let sselectv = 55;
let sokv = 50;

let seobj = ObjSound_Create;
task SE_Play(path, vl) {
	ObjSound_Load(seobj,path);
	ObjSound_Play(seobj);
	ObjSound_SetVolumeRate(seobj,vl);
}


task TMenu
{
	SE_Play(spause, spausev);
	let selectIndex = 0;//選択位置
	let selectIndex2 = 0;
	pauseimg;

	task pauseimg{
		let x = 80;
		let y = 286;
		let alpha = 150;
		let path = GetCurrentScriptDirectory~"img/pause_back.png";
		let img = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(img, path);
		ObjSprite2D_SetSourceRect(img, 0, 0, 97, 448);
		Obj_SetRenderPriorityI(img, 5);
		ObjSprite2D_SetDestCenter(img);
		ObjRender_SetColor(img, 255, 0, 0);
		ObjRender_SetScaleXYZ(img, 0.5*rez, 0.5*rez, 1);

		let calpha = 0;
		let xp = 3;
		let yp = xp;
		let cx = x - xp*wt;
		let cy = y - yp*wt;
		let alp = alpha/wt;
		loop(wt){
			calpha += alp;
			cx += xp;
			cy += yp;
			ObjRender_SetAlpha(img, calpha);
			ObjRender_SetPosition(img, cx*rez, cy*rez, 1);
			yield;
		}
		ObjRender_SetAlpha(img, alpha);
		ObjRender_SetPosition(img, x*rez, y*rez, 1);
		let angle = 0;
		let at = 90;
		let ap = 10;
		let posneg = 1;
		rotate;
		
		task rotate{
			while(endstate == 0){
				ObjRender_SetAngleZ(img, angle);
				angle += -ap/at * posneg;
				if(angle < -ap){posneg = -1; yield;}
				else if (angle > 0){posneg = 1; yield;}
				yield;
			}
		}
		while(endstate == 0){yield;}
		loop(wt){
			calpha -= alp;
			cx -= xp;
			ObjRender_SetAlpha(img, calpha);
			ObjRender_SetPosition(img, cx*rez, cy*rez, 1);
			yield;
		}
	}

	task TMenuItem(c, y, iy){
		let x = 200;
		let path = GetCurrentScriptDirectory~"img/pause.png";
		let img = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(img, path);
		ObjSprite2D_SetSourceRect(img, 0, iy, 512, iy+64);
		Obj_SetRenderPriorityI(img, 6);
		ObjSprite2D_SetDestCenter(img);
		ObjRender_SetScaleXYZ(img, 0.5*rez, 0.5*rez, 1);
		ObjRender_SetPosition(img, 200*rez, y*rez, 1);
		
		let gc = 100;
		let r = 255;
		let gb = 255;
		ObjRender_SetColor(img, gc, gc, gc);

		let calpha = 0;
		let xp = -3;
		let cx = x - xp*wt;
		let alp = 255/wt;
		loop(wt){
			calpha += alp;
			cx += xp;
			ObjRender_SetAlpha(img, calpha);
			ObjRender_SetPosition(img, cx*rez, y*rez, 1);
			yield;
		}
		ObjRender_SetAlpha(img, 255);
		ObjRender_SetPosition(img, x*rez, y*rez, 1);

		let ct = 40;
		let gbm = 130;
		let posneg = -1;
		let csel = 0;
		while(endstate == 0){
			if(c == selectIndex){
				ObjRender_SetColor(img, r, gb, gb);
				if(csel != selectIndex){
					movemenu(img);
				}
			}
			else{
				ObjRender_SetColor(img, gc, gc, gc);
			}
			csel = selectIndex;
			gb += gbm/ct * posneg;
			if(gb < 255-gbm){posneg = 1;}
			else if (gb == 255){posneg = -1;}

			yield;
		}
		alp = calpha/wt;
		loop(wt){
			calpha -= alp;
			cx -= xp;
			ObjRender_SetAlpha(img, calpha);
			ObjRender_SetPosition(img, cx*rez, y*rez, 1);
			yield;
		}
		task movemenu(img){
			let xp = -3;
			let t = 3;
			let x = 200;
			loop(t){
				x += xp;
				ObjRender_SetPosition(img, x*rez, y*rez, 1);
				yield;
			}
			loop(t){
				x -= xp;
				ObjRender_SetPosition(img, x*rez, y*rez, 1);
				yield;
			}
			ObjRender_SetPosition(img, 200*rez, y*rez, 1);

		}
	}

	let my = 256;
	let texts = ["Continue", "Quit", "Retry"];
	let yn = [1, 6, 4];
	let countMenu = length(texts);
	ascent(i in 0 .. countMenu)
	{
		TMenuItem(i, my, yn[i]*64);
		my += 33;
	}

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

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

		//キャンセル
		/*if(GetVirtualKeyState(VK_CANCEL) == KEY_PULL || GetVirtualKeyState(VK_PAUSE) == KEY_PULL)
		{	
			endstate = 1;
			loop(wt){yield;}
			SetScriptResult(RESULT_CANCEL);
			CloseScript(GetOwnScriptID());
			return;
		}*/

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

		if(selectIndex < 0) 
		{
			selectIndex = 2;
		}
		else if(selectIndex > 2)
		{
			selectIndex = 0;
		}

		yield;
	}
}



