
#include"./AudioFunctions.h"

@Initialize{
	LoadMenuSounds;
	MenuBackground;
	Menu;
}

@MainLoop{
	yield;
}

@Finalize{
}


task MenuBackground{
	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);

		if(left >= 32 && right <= 416 && top >=16 && bottom <= 464){
			let alpha = 255;
			while(alpha >= 128){
				ObjPrim_SetVertexAlpha(obj, index + 0, alpha);
				ObjPrim_SetVertexAlpha(obj, index + 1, alpha/2);
				ObjPrim_SetVertexAlpha(obj, index + 2, alpha/2);
				ObjPrim_SetVertexAlpha(obj, index + 3, alpha/2);
				ObjPrim_SetVertexAlpha(obj, index + 4, alpha/2);
				ObjPrim_SetVertexAlpha(obj, index + 5, alpha);
				alpha -= 255 / frame;

				yield;
			}
		}
	}

	let frame = 20;
	let countH = 1; 
	let countV = 1; 
	let width = 640 / countH;
	let height = 480 / countV;
	let target = GetTransitionRenderTargetName;
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_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, top, right, bottom);
		}
	}
	
	
	let objDim = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(objDim, BLEND_ALPHA);
	Obj_SetRenderPriorityI(objDim, 1);
	ObjPrim_SetTexture(objDim, current~"img/frame.png");
	ObjSprite2D_SetSourceRect(objDim, 148, 32, 150, 34);
	ObjSprite2D_SetDestRect(objDim, 0, 0, 800, 800);
	ObjRender_SetPosition(objDim, 0, 0, 0);
	ObjRender_SetAlpha(objDim, 0);
	
	let alpha = 0;
	ascent (i in 0..15){
		ObjRender_SetAlpha(objDim, alpha);
		alpha+=10;
		yield;
	}
}

task Menu{
	let selectIndex = 0;
	task TMenuItem(index, mx, my, text){
		function CreateTextObject(mx, my, text, r, g, b){
			let obj = ObjText_Create;
			ObjText_SetText(obj, text);
			ObjText_SetFontSize(obj, 16);
			ObjText_SetFontBold(obj, true);
			ObjText_SetFontColorTop(obj, r, g, b);
			ObjText_SetFontColorBottom(obj, r, g, b);
			//ObjText_SetFontBorderType(obj, BORDER_FULL);
			ObjText_SetFontBorderColor(obj, 255, 255, 255);
			ObjText_SetFontBorderWidth(obj, 0);
			Obj_SetRenderPriorityI(obj, 10);
			ObjRender_SetX(obj, mx);
			ObjRender_SetY(obj, my);
			return obj;
		}

		let objText = CreateTextObject(mx, my, text, 255, 255, 255);
		let objSelect = CreateTextObject(mx, my, text, 255, 131, 131);
		ObjRender_SetBlendType(objSelect, BLEND_ALPHA);
		let color = 100;
		let colorset = 0;
		let selection = 0;
		let move = 0;
		loop{
			if(selection != selectIndex){
				colorset = 0;
				color = 125;
				move = 15;
			}
			selection = selectIndex;
			
			if(move>0 && index == selection){
				let randx = mx + rand(-move/3, move/3);
				let randy = my + rand(-move/3, move/3);
				ObjRender_SetPosition(objText, randx, randy, 0);
				ObjRender_SetPosition(objSelect, randx, randy, 0);
				move--;
			}
			else{
				ObjRender_SetPosition(objText, mx, my, 0);
				ObjRender_SetPosition(objSelect, mx, my, 0);
			}
			
			if(colorset == 0){color+=5}
			if(colorset == 1){color-=5}
			if(color>=255){colorset = 1}
			if(color<=15){colorset = 0}
			ObjRender_SetAlpha(objSelect, color);
			Obj_SetVisible(objSelect, index == selectIndex);
			yield;
		}
	}

	let mx = 148;
	let my = 314;
	let texts = ["Save replay", "Return to menu", "Give up and retry"];
	var countMenu = length(texts);
	ascent(var iText in 0 .. countMenu){
		TMenuItem(iText, mx, my, texts[iText]);
		my += 24;
	}

	let frameKeyHold = 0;
	let listResult = [RESULT_SAVE_REPLAY, RESULT_END, RESULT_RETRY];
	loop{
		if(GetVirtualKeyState(VK_OK) == KEY_PULL || GetVirtualKeyState(VK_USER1) == KEY_PULL){
			SetScriptResult(listResult[selectIndex]);
			loop(30){yield;}
			return;
		}

		if(GetVirtualKeyState(VK_UP) == KEY_PUSH){
			PlaySound("select");
			selectIndex--;
		}
		
		else if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH){
			PlaySound("select");
			selectIndex++;
		}
		
		else if(GetVirtualKeyState(VK_UP) == KEY_HOLD){
			frameKeyHold++;
			if(frameKeyHold == 25 || (frameKeyHold > 25 && (frameKeyHold % 10 == 0))){
				PlaySound("select");
				selectIndex--;
			}
		}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_HOLD){
			frameKeyHold++;
			if(frameKeyHold == 25 || (frameKeyHold > 25 && (frameKeyHold % 10 == 0))){
				PlaySound("select");
				selectIndex++;
			}
		}
		else{
			frameKeyHold = 0;
		}

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

		yield;
	}
}



