//リプレイ保存スクリプト

@Initialize
{
	SetAutoDeleteObject(true);
	LoadReplayList();

	TBackground_2(0,0,255);
	TBlack;
	ascent(j in -3..4)
	{
	ascent(i in -3..4)
	{
		TBackground(i*0.5,j*0.5,5);
	}
	}

//	TBackground();
	UnderExplain;

	Title();
	TReplayIndexSelection();
}

@MainLoop
{
	yield;
}

@Finalize
{
}

let MENU_INDEX_SELECTION = 1;
let MENU_NAME_ENTRY = 2;
let menuMode = MENU_INDEX_SELECTION;

function CreateTextObject(let mx, let my, let size, let text)
{
	let obj = ObjText_Create();
	ObjText_SetText(obj, text);
	ObjText_SetFontSize(obj, size);
	ObjText_SetFontBold(obj, true);
	ObjText_SetFontColorTop(obj, 255, 255, 255);
	ObjText_SetFontColorBottom(obj, 160, 160, 255);
//	ObjText_SetFontBorderType(obj, BORDER_FULL);
	ObjText_SetFontBorderType(obj, BORDER_NONE);
	ObjText_SetFontBorderColor(obj,255, 255, 255);
	ObjText_SetFontBorderWidth(obj, 2*0);
	Obj_SetRenderPriorityI(obj, 100);
	ObjRender_SetX(obj, mx);
	ObjRender_SetY(obj, my);
	ObjText_SetFontType(obj,"ＭＳ 明朝");

	return obj;
}

/*
task TBackground
{
	let target = GetTransitionRenderTargetName();
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, target);
	Obj_SetRenderPriority(obj, 0.1);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 640, 480);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj, 320, 240, 0);
	ObjRender_SetAlpha(obj, 96);
}
*/


task TBackground(dx,dy,alpha)
{
	let target = GetTransitionRenderTargetName();
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D); //2D頂点ブジェクト生成
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
	ObjPrim_SetVertexCount(obj, 4);
	Obj_SetRenderPriorityI(obj, 100); //描画優先度を設定
	if(alpha==255){Obj_SetRenderPriorityI(obj, 0);}
	ObjPrim_SetTexture(obj, target); //テクスチャを設定

	let index=0;
	ObjPrim_SetVertexPosition(obj, index + 0, 32, 16, 0);
	ObjPrim_SetVertexPosition(obj, index + 1, 32, 464, 0);
	ObjPrim_SetVertexPosition(obj, index + 2, 416, 464, 0);
	ObjPrim_SetVertexPosition(obj, index + 3, 416,16, 0);

	ObjPrim_SetVertexUVT(obj, index + 0, 32, 16);
	ObjPrim_SetVertexUVT(obj, index + 1, 32, 464);
	ObjPrim_SetVertexUVT(obj, index + 2, 416, 464);
	ObjPrim_SetVertexUVT(obj, index + 3, 416, 16);
	ObjRender_SetX(obj,dx);
	ObjRender_SetY(obj,dy);

	ObjRender_SetAlpha(obj,alpha*0.4);
}

task TBlack
{
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"../img/effect/Black.png");
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(obj, 4);
//	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	Obj_SetRenderPriorityI(obj,100);
	ObjRender_SetAngleZ(obj,0);

	ObjPrim_SetVertexPosition(obj, 0, -192, -224, -0);
	ObjPrim_SetVertexPosition(obj, 1, -192, 224, -0);
	ObjPrim_SetVertexPosition(obj, 2, 192, -224, 0);
	ObjPrim_SetVertexPosition(obj, 3, 192,224, 0);

	ObjPrim_SetVertexUVT(obj, 0, 0, 0);
	ObjPrim_SetVertexUVT(obj, 1, 0, 128);
	ObjPrim_SetVertexUVT(obj, 2, 128, 0);
	ObjPrim_SetVertexUVT(obj, 3, 128, 128);

	ObjRender_SetX(obj,192+32);
	ObjRender_SetY(obj,224+16);
	loop
	{
		yield;
	}
	Obj_Delete(obj);
}

task TBackground_2(dx,dy,alpha)
{

	let target = GetTransitionRenderTargetName();
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D); //2D頂点ブジェクト生成
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
	ObjPrim_SetVertexCount(obj, 4);
	Obj_SetRenderPriorityI(obj, 100); //描画優先度を設定
	if(alpha==255){Obj_SetRenderPriorityI(obj, 100);}
	ObjPrim_SetTexture(obj, target); //テクスチャを設定

	let index=0;
	ObjPrim_SetVertexPosition(obj, index + 0, 0, 0, 0);
	ObjPrim_SetVertexPosition(obj, index + 1, 0, 480, 0);
	ObjPrim_SetVertexPosition(obj, index + 2, 640, 480, 0);
	ObjPrim_SetVertexPosition(obj, index + 3, 640,0, 0);

	ObjPrim_SetVertexUVT(obj, index + 0, 0, 0);
	ObjPrim_SetVertexUVT(obj, index + 1, 0, 480);
	ObjPrim_SetVertexUVT(obj, index + 2, 640, 480);
	ObjPrim_SetVertexUVT(obj, index + 3, 640, 0);
	ObjRender_SetX(obj,dx);
	ObjRender_SetY(obj,dy);

	ObjRender_SetAlpha(obj,alpha*0.4);
}

task Title
{
	let objText = ObjText_Create();
	ObjText_SetText(objText, "リプレイ保存先選択");
	ObjText_SetFontSize(objText, 24);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 160, 160, 255);
//	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderType(objText, BORDER_NONE);
	ObjText_SetFontBorderColor(objText,255, 255, 255);
	ObjText_SetFontBorderWidth(objText, 2*0);
	Obj_SetRenderPriorityI(objText, 100);
	ObjRender_SetX(objText, 14);
	ObjRender_SetY(objText, 14);

	ObjText_SetFontType(objText,"ＭＳ 明朝");
}

task TReplayIndexSelection()
{
	let cursorY = 0;
	let page = 0;
	let countMaxItem = REPLAY_INDEX_DIGIT_MAX - REPLAY_INDEX_DIGIT_MIN + 1;
	let countItemPerPage = 16;
	let pageMax = trunc((countMaxItem - 1) / countItemPerPage);
	pageMax = max(pageMax, 1);
	let lastPageMaxCursorY = trunc(countMaxItem / countItemPerPage);

	task TMenuItem(let itemY)
	{
		let objText = CreateTextObject(14, 13+39 + 25 * itemY, 18, "");

		let oldPage = -1;
		while(menuMode == MENU_INDEX_SELECTION)
		{
			if(page != oldPage)
			{
				let index = page * countItemPerPage + itemY + 1;
				let text="";
				if(IsValidReplayIndex(index))
				{
					text = rtos("000", GetReplayInfo(index, REPLAY_COMMENT)) ~ " ";
					text = text ~ vtos("-8s", GetReplayInfo(index, REPLAY_USER_NAME)) ~ " ";
				//	text = text ~ vtos("-8s", GetReplayInfo(index, REPLAY_USER_NAME)) ~ " ";
				//	text = text ~ GetReplayInfo(index, REPLAY_DATE_TIME) ~ " ";
					text = text ~ rtos("0000000000000", GetReplayInfo(index, REPLAY_TOTAL_SCORE)) ~ " ";

					text = text ~ vtos("-8s", GetReplayInfo(index, REPLAY_PLAYER_NAME)) ~ " ";
					text = text ~ vtos("03.2f", GetReplayInfo(index, REPLAY_FPS_AVERAGE)) ~ "fps ";
					text = text ~ GetReplayInfo(index, REPLAY_DATE_TIME) ~ " ";

				}
				else
				{
					text = text ~ "No Data";
				}
				ObjText_SetText(objText, text);
				oldPage = page;
			}
			
			if(page == pageMax && itemY >= lastPageMaxCursorY)
			{
				Obj_SetVisible(objText, false);
			//	Obj_SetVisible(objSelect, false);
			}
			else
			{
				Obj_SetVisible(objText, true);
			//	Obj_SetVisible(objSelect, itemY == cursorY);
			}
			if(cursorY==itemY)
			{
				ObjRender_SetColor(objText,255,255,255);
			}
			else
			{
				ObjRender_SetColor(objText,150,150,150);
			}
			yield;
		}
		Obj_Delete(objText);
	}

	ascent(let iItem in 0 .. countItemPerPage)
	{
		TMenuItem(iItem);
	}

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

	//キー処理
	let frameKeyHold = 0;//キー押しっぱなしフレーム数
	SelectEffect;
	PageDraw;

	while(menuMode == MENU_INDEX_SELECTION)
	{
		//決定
		if(GetVirtualKeyState(VK_OK) == KEY_PULL)
		{
			menuMode = MENU_NAME_ENTRY;
			let index = page * countItemPerPage + cursorY + 1;
			TNameEntry(index);
			
			break;
		}

		//保存キャンセル
		if(GetVirtualKeyState(VK_USER1) == KEY_PUSH)
		{
			yield;
			SetScriptResult(RESULT_END);
			CloseScript(GetOwnScriptID());
			break;
		}

		//カーソル移動
		if(GetVirtualKeyState(VK_UP) == KEY_PUSH || GetVirtualKeyState(VK_UP) == KEY_HOLD)
		{
			frameKeyHold++;
			if(GetVirtualKeyState(VK_UP) == KEY_PUSH ||
				frameKeyHold == 20 ||
				 (frameKeyHold > 20 && (frameKeyHold % 8 == 0)))
			{
				cursorY--;
			}
		}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH || GetVirtualKeyState(VK_DOWN) == KEY_HOLD)
		{
			frameKeyHold++;
			if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH ||
				frameKeyHold == 20 ||
				 (frameKeyHold > 20 && (frameKeyHold % 8 == 0)))
			{
				cursorY++;
			}
		}
		else if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_LEFT) == KEY_HOLD)
		{
			frameKeyHold++;
			if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH ||
				frameKeyHold == 20 ||
				 (frameKeyHold > 20 && (frameKeyHold % 8 == 0)))
			{
				page--;
			}
		}
		else if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH || GetVirtualKeyState(VK_RIGHT) == KEY_HOLD)
		{
			frameKeyHold++;
			if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH ||
				frameKeyHold == 20 ||
				 (frameKeyHold > 20 && (frameKeyHold % 8 == 0)))
			{
				page++;
			}
		}
		else
		{
			frameKeyHold = 0;
		}

		if(page < 0)
		{
			page = pageMax;
		}
		else if(page > pageMax)
		{
			page = 0;
		}

		if(page != pageMax)
		{
			if(cursorY < 0)
			{
				cursorY = countItemPerPage - 1;
			}
			else if(cursorY >= countItemPerPage)
			{
				cursorY = 0;
			}
		}
		else
		{
			if(cursorY < 0)
			{
				cursorY = lastPageMaxCursorY - 1;
			}
			else if(cursorY >= lastPageMaxCursorY)
			{
				cursorY = 0;
			}
		}

		yield;
	}

	task PageDraw
	{
	let objText = CreateTextObject(480, 14, 24,"Page　"~"1 / "~IntToString(pageMax+1));
	loop
	{
	ObjText_SetText(objText, "Page　"~IntToString(page+1)~" / "~IntToString(pageMax+1));
	yield;
	}
	}

	task SelectEffect
	{
		let x=0;
		let y=0;
		while(menuMode != MENU_NAME_ENTRY)
		{
			y=13+39+25*cursorY;
		//	Light(rand(0,640),y+rand(0,16),rand(1,3),180+rand(-5,5));
			Light(rand(0,640),y+rand(15,16),rand(1,2)/3,270+rand(-5,5));
			Light(rand(0,640),y+rand(1,0),rand(1,2)/3,90+rand(-5,5));
			yield;
		}

		task Band
		{

		}

		task Light(X,Y,speed,angle)
		{
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"../img/effect/star.png");
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
		ObjPrim_SetVertexCount(obj, 4);
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		ObjRender_SetAngleZ(obj,0);
		Obj_SetRenderPriorityI(obj, 100);

		ObjPrim_SetVertexPosition(obj, 0, -16, -16, -0);
		ObjPrim_SetVertexPosition(obj, 1, -16, 16, -0);
		ObjPrim_SetVertexPosition(obj, 2, 16, -16, 0);
		ObjPrim_SetVertexPosition(obj, 3, 16,16, 0);

		ObjPrim_SetVertexUVT(obj, 0, 0, 0);
		ObjPrim_SetVertexUVT(obj, 1, 0, 32);
		ObjPrim_SetVertexUVT(obj, 2, 32, 0);
		ObjPrim_SetVertexUVT(obj, 3, 32, 32);
		ObjRender_SetPosition(obj,X,Y,0);

		let scale=0.5;
		let Alpha=180;
		let Zangle=0;
		let ZanglePlus=rand(-5,5);

		loop(30)
		{
			scale-=0.5/30;
			Zangle+=ZanglePlus;
			ObjRender_SetPosition(obj,ObjRender_GetX(obj)+speed*cos(angle),ObjRender_GetY(obj)+speed*sin(angle),0);
			ObjRender_SetScaleXYZ(obj,scale,scale,scale);
			ObjRender_SetAngleZ(obj,Zangle);
			ObjRender_SetColor(obj,Alpha*0.75,Alpha*0.75,Alpha);
			yield;
		}
		Obj_Delete(obj);
		}
	}
}


task TNameEntry(let replayIndex)
{
	let strTextIn = 
	[
		["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P"],
		["Q","R","S","T","U","V","W","X","Y","Z",".",",",":",";","_","@"],
		["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p"],
		["q","r","s","t","u","v","w","x","y","z","+","-","/","*","=","%"],
		["0","1","2","3","4","5","6","7","8","9","0","!","?","'","\"","$"],
		["(",")","{","}","[","]","<",">","&","#","|","~","^"," "," ","終"]
	];

	let strTextView = 
	[
		["Ａ","Ｂ","Ｃ","Ｄ","Ｅ","Ｆ","Ｇ","Ｈ","Ｉ","Ｊ","Ｋ","Ｌ","Ｍ","Ｎ","Ｏ","Ｐ"],
		["Ｑ","Ｒ","Ｓ","Ｔ","Ｕ","Ｖ","Ｗ","Ｘ","Ｙ","Ｚ","．","，","：","；","＿","＠"],
		["ａ","ｂ","ｃ","ｄ","ｅ","ｆ","ｇ","ｈ","ｉ","ｊ","ｋ","ｌ","ｍ","ｎ","ｏ","ｐ"],
		["ｑ","ｒ","ｓ","ｔ","ｕ","ｖ","ｗ","ｘ","ｙ","ｚ","＋","−","／","＊","＝","％"],
		["０","１","２","３","４","５","６","７","８","９","０","！","？","’","”","＄"],
		["（","）","｛","｝","［","］","＜","＞","＆","＃","｜","〜","＾"," "," ","終了"]
	];

	let cursorX = 0;
	let cursorY = 0;
	let maxCursorX = length(strTextIn[0]);
	let maxCursorY = length(strTextIn);

	task TMenuItem(let itemX, let itemY)
	{
		let objText = CreateTextObject(120 + itemX * 24, 200 + itemY * 24, 22, strTextView[itemY][itemX]);
	//	let objSelect = CreateTextObject(120 + itemX * 24, 200 + itemY * 24, 22, strTextView[itemY][itemX]);
	//	ObjRender_SetBlendType(objSelect, BLEND_ADD_RGB);

		while(menuMode == MENU_NAME_ENTRY)
		{
		//	Obj_SetVisible(objSelect, itemX == cursorX && itemY == cursorY);
			if(itemX == cursorX && itemY == cursorY)
			{
				ObjRender_SetColor(objText,255,255,255);
			}
			else
			{
				ObjRender_SetColor(objText,150,150,150);
			}
			yield;
		}
		Obj_Delete(objText);
	//	Obj_Delete(objSelect);
	}

	ascent(let iY in 0..maxCursorY)
	{
		ascent(let iX in 0 .. maxCursorX)
		{
			TMenuItem(iX, iY);
		}
	}

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

	//入力済み文字
	let userName = "";
	if(GetAreaCommonData("TUNING","PlayerName","NEMO")!="NEMO")
	{
		userName=GetAreaCommonData("TUNING","PlayerName","NEMO");
	}
	let objName = CreateTextObject(160, 96, 28, "");
	let CursorAngle=90;
	task TNameCursor()
	{
		let objCursor = CreateTextObject(0, 96, 28, "_");
		while(menuMode == MENU_NAME_ENTRY)
		{
			let nameLength = length(userName);
			ObjRender_SetX(objCursor, 160 + nameLength * 15);
			Obj_SetVisible(objCursor, nameLength < 8);
			ObjRender_SetAlpha(objCursor,200+55*cos(CursorAngle));
			CursorAngle+=2;
			yield;
		}
		Obj_Delete(objCursor);
	}
	TNameCursor;

	//キー処理
	let frameKeyHold = 0;//キー押しっぱなしフレーム数


	while(menuMode == MENU_NAME_ENTRY)
	{
		//エントリーキャンセル
		if(GetVirtualKeyState(VK_USER1) == KEY_PUSH)
		{
			yield;
			Obj_Delete(objName);
			menuMode = MENU_INDEX_SELECTION;
			TReplayIndexSelection();
			return;
		}
		
		if(GetVirtualKeyState(VK_OK) == KEY_PULL)
		{
			//決定
			let nameLength = length(userName);
			if(cursorX == maxCursorX-1 && cursorY == maxCursorY-1)
			{
				//終了キー
				if(nameLength == 0)
				{
					userName = "No Name";
				}
				else
				{
					let ID=IntToString(GetCommonData("SpellCardID",0));
					SetAreaCommonData("TUNING","PlayerName",userName);
					SetReplayInfo(REPLAY_COMMENT,ID);
					SaveReplay(replayIndex, userName);
					SetScriptResult(RESULT_END);
					CloseScript(GetOwnScriptID());
					return;
				}
			}
			else if(nameLength < 8)
			{
				userName = userName ~ strTextIn[cursorY][cursorX];
			}
		}
		if(GetVirtualKeyState(VK_CANCEL) == KEY_PULL)
		{
			//キャンセル
			let nameLength = length(userName);
			if(nameLength > 0)
			{
				userName = userName[0..nameLength-1];
			}
		}
		ObjText_SetText(objName, userName);

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

		if(cursorX < 0)
		{
			cursorX = maxCursorX-1;
		}
		else if(cursorX >= maxCursorX)
		{
			cursorX = 0;
		}

		if(cursorY < 0)
		{
			cursorY = maxCursorY-1;
		}
		else if(cursorY >= maxCursorY)
		{
			cursorY = 0;
		}

		yield;
	}
}

	task UnderExplain
	{
		let obj = ObjText_Create();
		ObjText_SetText(obj, "Shot Decide  User1 Cancel");
		ObjText_SetFontSize(obj, 12);
		ObjText_SetFontBold(obj, true);
//		ObjText_SetFontColorTop(obj, 128, 128, 255);
//		ObjText_SetFontColorBottom(obj, 64, 64, 255);
		ObjText_SetFontBorderType(obj, BORDER_NONE);
		ObjText_SetFontBorderColor(obj,255, 255, 255);
		ObjText_SetFontBorderWidth(obj, 0);
		Obj_SetRenderPriorityI(obj, 100);
		ObjRender_SetX(obj, 34+96);
		ObjRender_SetY(obj, 450);
		let Alpha=255;
		let AlphaAngle=0;
		loop
		{
			ObjRender_SetAlpha(obj,200+55*cos(AlphaAngle));
			if(menuMode == MENU_INDEX_SELECTION)
			{
				ObjText_SetText(obj, "Shot Decide  User1 Cancel");
			}
			else
			{
				ObjText_SetText(obj, "Shot Decide  User1 Cancel 　Bomb Back");
			}
			AlphaAngle+=2;
			yield;
		}
		Obj_Delete(obj);

	}