//=============================================================================
// TeamSelectionMenu
//=============================================================================
class TeamSelectionMenu expands UnrealListMenu
	localized;

#exec TEXTURE IMPORT NAME="RedTeamIcon" FILE="Textures\team-r.pcx" MIPS=off
#exec TEXTURE IMPORT NAME="BlueTeamIcon" FILE="Textures\team-b.pcx" MIPS=off

var bool bSetup;
var BotInfo BotConfig;
var int NumBots;
var GameInfo GameType;
var byte CurrentTeam;

var string[32] Team0List[33];
var string[32] Team1List[33];
var localized string[32] RightTitle0;
var localized string[32] RightTitle1;
var localized string[32] Team0;
var localized string[32] Team1;

var int Team0Max;
var int Team1Max;

function InitConfig(GameInfo G)
{
	GameType = G;
	if ( Level.Game.IsA('DeathMatchGame') )
		BotConfig = DeathMatchGame(Level.Game).BotConfig;
	else
		BotConfig = Spawn(DeathMatchGame(GameType).Default.BotConfigType);
	NumBots = 32;
	CurrentTeam = 0;
	LeftSelection = 1;
	RightMax = 0;
	Team0Max = 0;
	Team1Max = 0;
	RightSelection = 1;
}

function LeftMenuProcessInput( byte KeyNum, byte ActionNum )
{
	local float XL, YL;
	local int Overflow;

	if ( KeyNum == EInputKey.IK_Escape )
	{
		PlayEnterSound();
		ExitMenu();
		return;
	}
	else if ( KeyNum == EInputKey.IK_Up )
	{
		PlaySelectSound();
		Selection--;
		if ( Selection < 1 )
			Selection = 1;
		else if ( Selection < LeftOffset + 1 )
			LeftOffset--;
	}
	else if ( KeyNum == EInputKey.IK_Down )
	{
		PlaySelectSound();
		Selection++;
		if ( Selection > LeftMax )
			Selection = LeftMax;
		else if ( Selection >= LeftNumDrawn )
		{
			Overflow = Selection - LeftNumDrawn;
			if (Overflow > 0)
				LeftOffset = Overflow;
		}
	}
	else if ( KeyNum == EInputKey.IK_Enter )
	{
		bConfigChanged=true;
		if ( ProcessSelection() )
			PlayEnterSound();
	}
	else if ( KeyNum == EInputKey.IK_Left )
	{
		bConfigChanged=true;
		if ( ProcessLeft() )
			PlayModifySound();
	}
	else if ( KeyNum == EInputKey.IK_Right )
	{
		bConfigChanged=true;
		if ( ProcessRight() )
			PlayModifySound();
	}
	else if ( KeyNum == EInputKey.IK_Y )
	{
		bConfigChanged=true;
		if ( ProcessYes() )
			PlayModifySound();
	}
	else if ( KeyNum == EInputKey.IK_N )
	{
		bConfigChanged=true;
		if ( ProcessNo() )
			PlayModifySound();
	}
	if ( bExitAllMenus )
		ExitAllMenus(); 
}

function RightMenuProcessInput( byte KeyNum, byte ActionNum )
{
	local float XL, YL;
	local int Overflow;

	if ( KeyNum == EInputKey.IK_Escape )
	{
		PlayEnterSound();
		ExitMenu();
		return;
	}
	else if ( KeyNum == EInputKey.IK_Up )
	{
		PlaySelectSound();
		Selection--;
		if ( Selection < 1 )
			Selection = 1;
		else if ( Selection < RightOffset + 1 )
			RightOffset--;
	}
	else if ( KeyNum == EInputKey.IK_Down )
	{
		PlaySelectSound();
		Selection++;
		if ( Selection > RightMax )
			Selection = RightMax;
		else if ( Selection >= RightNumDrawn )
		{
			Overflow = Selection - RightNumDrawn;
			if (Overflow > 0)
				RightOffset = Overflow;
		}
	}
	else if ( KeyNum == EInputKey.IK_Enter )
	{
		bConfigChanged=true;
		if ( ProcessSelection() )
			PlayEnterSound();
	}
	else if ( KeyNum == EInputKey.IK_Left )
	{
		bConfigChanged=true;
		if ( ProcessLeft() )
			PlayModifySound();
	}
	else if ( KeyNum == EInputKey.IK_Right )
	{
		bConfigChanged=true;
		if ( ProcessRight() )
			PlayModifySound();
	}
	else if ( KeyNum == EInputKey.IK_Y )
	{
		bConfigChanged=true;
		if ( ProcessYes() )
			PlayModifySound();
	}
	else if ( KeyNum == EInputKey.IK_N )
	{
		bConfigChanged=true;
		if ( ProcessNo() )
			PlayModifySound();
	}
	if ( bExitAllMenus )
		ExitAllMenus(); 
}

function CenterMenuProcessInput( byte KeyNum, byte ActionNum )
{
	if ( KeyNum == EInputKey.IK_Escape )
	{
		PlayEnterSound();
		ExitMenu();
		return;
	}	

	if ( KeyNum == EInputKey.IK_Up )
	{
		PlaySelectSound();
		Selection--;
		if ( Selection < 1 )
			Selection = MenuLength;
		if ( (Selection == 3) || (Selection == 5) )
			Selection--;
	}
	else if ( KeyNum == EInputKey.IK_Down )
	{
		PlaySelectSound();
		Selection++;
		if ( Selection > MenuLength )
			Selection = 1;
		if ( (Selection == 3) || (Selection == 5) )
			Selection++;
	}
	else if ( KeyNum == EInputKey.IK_Enter )
	{
		bConfigChanged=true;
		if ( ProcessSelection() )
			PlayEnterSound();
	}
	else if ( KeyNum == EInputKey.IK_Left )
	{
		bConfigChanged=true;
		if ( ProcessLeft() )
			PlayModifySound();
	}
	else if ( KeyNum == EInputKey.IK_Right )
	{
		bConfigChanged=true;
		if ( ProcessRight() )
			PlayModifySound();
	}
	else if ( KeyNum == EInputKey.IK_Y )
	{
		bConfigChanged=true;
		if ( ProcessYes() )
			PlayModifySound();
	}
	else if ( KeyNum == EInputKey.IK_N )
	{
		bConfigChanged=true;
		if ( ProcessNo() )
			PlayModifySound();
	}

	if ( bExitAllMenus )
		ExitAllMenus(); 
}

function ProcessMenuEscape()
{
}

function Menu ExitMenu()
{
	Super.ExitMenu();
}

function bool ProcessLeft()
{
	switch (Focus)
	{
		case -1:
			break;
		case 0:
			if (LeftMax > 0)
			{
				Focus--;
				CenterSelection = Selection;
				Selection = LeftSelection;
			}
			break;
		case 1:
			Focus--;
			RightSelection = Selection;
			Selection = CenterSelection;
			break;
	}
	return true;
}

function bool ProcessRight()
{
	switch (Focus)
	{
		case -1:
			Focus++;
			LeftSelection = Selection;
			Selection = CenterSelection;
			break;
		case 0:
			if (RightMax > 0)
			{
				Focus++;
				CenterSelection = Selection;
				Selection = RightSelection;
			}
			break;
		case 1:
			break;
	}
	return true;
}

function bool ProcessSelection()
{
	local Menu ChildMenu;
	local int i;
	
	switch (Focus)
	{
		case -1:
			// Open a bot edit screen for the current bot.
			ChildMenu = spawn(class'ChallengeIndivBotMenu', owner);
			ChallengeIndivBotMenu(ChildMenu).InitConfig(GameType);
			ChallengeIndivBotMenu(ChildMenu).GotoBot(LeftSelection);
			break;
		case 0:
			if ( Selection == 1 ) {
				// Move left selection to right list.
				AddBot();
			} else if ( Selection == 2 ) {
				// Move right selection to left list.
				RemoveBot();
			} else if ( Selection == 4 ) {
				// Select next team.
				switch (CurrentTeam)
				{
					case 0:
						CurrentTeam = 1;
						// Switch Lists
						for (i=0; i<32; i++)
							Team0List[i] = RightList[i];
						for (i=0; i<32; i++)
							RightList[i] = Team1List[i];
						Team0Max = RightMax;
						RightMax = Team1Max;
						break;
					case 1:
						CurrentTeam = 0;
						// Switch Lists
						for (i=0; i<32; i++)
							Team1List[i] = RightList[i];
						for (i=0; i<32; i++)
							RightList[i] = Team0List[i];
						Team1Max = RightMax;
						RightMax = Team0Max;
						break;
				}
			} else if ( Selection == 6 ) {
				// Save changes and exit.
				switch (CurrentTeam)
				{
					case 0:
						for (i=0; i<32; i++)
							Team0List[i] = RightList[i];
						break;
					case 1:
						for (i=0; i<32; i++)
							Team1List[i] = RightList[i];
						break;
				}
				for (i=0; i<32; i++)
					if (Team0List[i] != "")
						BotConfig.SetBotTeam(0, BotConfig.GetBotIndex(Team0List[i]));
				for (i=0; i<32; i++)
					if (Team1List[i] != "")
						BotConfig.SetBotTeam(1, BotConfig.GetBotIndex(Team1List[i]));
				ExitAllMenus();
			}
			break;
		case 1:
			// Open a bot edit screen for the current bot.
			ChildMenu = spawn(class'ChallengeIndivBotMenu', owner);
			ChallengeIndivBotMenu(ChildMenu).InitConfig(GameType);
			ChallengeIndivBotMenu(ChildMenu).GotoBot(RightSelection);
			break;
	}

	if ( ChildMenu != None )
	{
		HUD(Owner).MainMenu = ChildMenu;
		ChildMenu.ParentMenu = self;
		ChildMenu.PlayerOwner = PlayerOwner;
	}

	return true;
}

function AddBot()
{
	local int i;
	local string[32] BotName;

	if (LeftList[LeftSelection] == "")
		return;
	
	BotName = LeftList[LeftSelection];
	for ( i=LeftSelection; i<LeftMax; i++ )
		LeftList[i] = LeftList[i+1];
	LeftList[LeftMax] = "";

	for ( i=1; i<32; i++ )
		if (RightList[i] == "") {
			RightList[i] = BotName;
			break;
		}
	LeftMax--;
	RightMax++;
	RightSelection = RightMax;
	
	if (LeftSelection > LeftMax)
		LeftSelection = LeftMax;
}

function RemoveBot()
{
	local int i;
	local string[32] BotName;

	if (RightList[RightSelection] == "")
		return;
		
	BotName = RightList[RightSelection];
	for ( i=RightSelection; i<RightMax; i++ )
		RightList[i] = RightList[i+1];
	RightList[RightMax] = "";

	for ( i=1; i<32; i++ )
		if (LeftList[i] == "") {
			LeftList[i] = BotName;
			break;
		}
	LeftMax++;
	RightMax--;
	LeftSelection = LeftMax;

	if (RightSelection > RightMax)
		RightSelection = RightMax;
}

function MenuTick(float DeltaTime)
{
	Super.MenuTick(DeltaTime);
	
	// Update FadeTimes.
	if (TitleFadeTime >= 0.0)
		TitleFadeTime += DeltaTime;
}

function DrawCenterPanel(canvas Canvas)
{
	local int i;
	local float XL, YL;

	Canvas.bCenter = true;
	for ( i=0; i<MenuLength+1; i++ )
	{
		if ( (Focus == 0) && (Selection == i) )
		{
			switch (CurrentTeam)
			{
				case 0:
					Canvas.DrawColor.R = 255;
					Canvas.DrawColor.G = PulseTime * 10;
					Canvas.DrawColor.B = PulseTime * 10;
					break;
				case 1:
					Canvas.DrawColor.R = PulseTime * 10;
					Canvas.DrawColor.G = PulseTime * 10;
					Canvas.DrawColor.B = 255;
					break;
			}
		} else {
			Canvas.DrawColor.R = 192;
			Canvas.DrawColor.G = 192;
			Canvas.DrawColor.B = 192;
		}
		Canvas.StrLen(MenuList[i], XL, YL);
		Canvas.SetPos(4, Canvas.ClipY/2 + i*2*YL);
		Canvas.DrawText(MenuList[i], true);
	}
	Canvas.bCenter = false;

	Canvas.DrawColor.R = 255;
	Canvas.DrawColor.G = 255;
	Canvas.DrawColor.B = 255;

	// Team specific stuff...
	switch (CurrentTeam)
	{
		case 0:
			Canvas.Style = 2;
			Canvas.SetPos(Canvas.ClipX / 2 - 128, Canvas.ClipY / 8);
			Canvas.DrawIcon(texture'RedTeamIcon', 1.0);
			Canvas.Style = 1;
			break;
		case 1:
			Canvas.Style = 2;
			Canvas.SetPos(Canvas.ClipX / 2 - 128, Canvas.ClipY / 8);
			Canvas.DrawIcon(texture'BlueTeamIcon', 1.0);
			Canvas.Style = 1;
			break;
	}
}

function SetUpDisplay(canvas Canvas)
{
	local int i;
	local float XL, YL;

	for (i=1; i<NumBots; i++) {
		LeftList[i] = BotConfig.GetBotName(i-1);
		if (LeftList[i] != "")
			LeftMax++;
	}
	PanelWidth = Canvas.ClipX/6;
	PanelHeight = Canvas.ClipY/3 + Canvas.ClipY/3;
	//PanelHeight = Canvas.ClipY/3;
	Canvas.Font = Font'WhiteFont';
	Canvas.StrLen("Test", XL, YL);
	ColumnLines = (PanelHeight / YL) / 2;
	ColumnLines -= 1;
	bSetup = true;
}

function DrawLeftTitle(canvas Canvas)
{
	local float XL, YL;

	Canvas.DrawColor.R = 255;
	Canvas.DrawColor.G = 255;
	Canvas.DrawColor.B = 255;
	Canvas.StrLen(LeftTitle$LeftMax, XL, YL);
	Canvas.SetPos(64 + (PanelWidth/2 - XL/2), 126-YL);
	Canvas.DrawText(LeftTitle$LeftMax, false);
}

function DrawLeftList(canvas Canvas)
{
	local float XL, YL;
	local int i;

	LeftNumDrawn = 0;
	for ( i=LeftOffset+1; i<ColumnLines+LeftOffset; i++ )
	{
		if ( (Focus == -1) && (Selection == i) )
		{
			switch (CurrentTeam)
			{
				case 0:
					Canvas.DrawColor.R = 255;
					Canvas.DrawColor.G = PulseTime * 10;
					Canvas.DrawColor.B = PulseTime * 10;
					break;
				case 1:
					Canvas.DrawColor.R = PulseTime * 10;
					Canvas.DrawColor.G = PulseTime * 10;
					Canvas.DrawColor.B = 255;
					break;
			}
		} else if ( (Focus != -1) && (LeftSelection == i) ) {
			switch (CurrentTeam)
			{
				case 0:
					Canvas.DrawColor.R = 255;
					Canvas.DrawColor.G = 0;
					Canvas.DrawColor.B = 0;
					break;
				case 1:
					Canvas.DrawColor.R = 0;
					Canvas.DrawColor.G = 0;
					Canvas.DrawColor.B = 255;
					break;
			}
		} else {
			Canvas.DrawColor.R = 192;
			Canvas.DrawColor.G = 192;
			Canvas.DrawColor.B = 192;
		}
		Canvas.Font = Font'WhiteFont';
		Canvas.StrLen(LeftList[i], XL, YL);
		Canvas.SetPos(64 + (PanelWidth/2 - XL/2), 128 + (i-LeftOffset)*2*YL);
		Canvas.DrawText(LeftList[i], false);
		LeftNumDrawn++;
	}
	LeftNumDrawn--;
}

function DrawRightTitle(canvas Canvas)
{
	local float XL, YL;

	switch (CurrentTeam)
	{
		case 0:
			Canvas.DrawColor.R = 255;
			Canvas.DrawColor.G = 0;
			Canvas.DrawColor.B = 0;
			RightTitle = RightTitle0;
			break;
		case 1:
			Canvas.DrawColor.R = 0;
			Canvas.DrawColor.G = 0;
			Canvas.DrawColor.B = 255;
			RightTitle = RightTitle1;
			break;
	}
	Canvas.StrLen(RightTitle$RightMax, XL, YL);
	Canvas.SetPos(Canvas.ClipX - 64 - PanelWidth + (PanelWidth/2 - XL/2), 126-YL);
	Canvas.DrawText(RightTitle$RightMax, false);
}

function DrawRightList(canvas Canvas)
{
	local float XL, YL;
	local int i;

	RightNumDrawn = 0;
	for ( i=RightOffset+1; i<ColumnLines+RightOffset; i++ )
	{
		if ( (Focus == 1) && (Selection == i) )
		{
			switch (CurrentTeam)
			{
				case 0:
					Canvas.DrawColor.R = 255;
					Canvas.DrawColor.G = PulseTime * 10;
					Canvas.DrawColor.B = PulseTime * 10;
					break;
				case 1:
					Canvas.DrawColor.R = PulseTime * 10;
					Canvas.DrawColor.G = PulseTime * 10;
					Canvas.DrawColor.B = 255;
					break;
			}
		} else if ( (Focus != 1) && (RightSelection == i) ) {
			switch (CurrentTeam)
			{
				case 0:
					Canvas.DrawColor.R = 255;
					Canvas.DrawColor.G = 0;
					Canvas.DrawColor.B = 0;
					break;
				case 1:
					Canvas.DrawColor.R = 0;
					Canvas.DrawColor.G = 0;
					Canvas.DrawColor.B = 255;
					break;
			}
		} else {
			Canvas.DrawColor.R = 192;
			Canvas.DrawColor.G = 192;
			Canvas.DrawColor.B = 192;
		}
		Canvas.Font = Font'WhiteFont';
		Canvas.StrLen(RightList[i], XL, YL);
		Canvas.SetPos(Canvas.ClipX - 64 - PanelWidth + (PanelWidth/2 - XL/2), 128 + ((i-RightOffset)*2*YL));
		Canvas.DrawText(RightList[i], false);
		RightNumDrawn++;
	}
	RightNumDrawn--;
}

function DrawMenu(canvas Canvas)
{
	if (!bSetup)
		SetUpDisplay(Canvas);

	// Title...
	DrawFadeTitle(Canvas);

	// Top Center Panel...

	// Left Panel...
	DrawLeftPanel(Canvas);

	// Center Panel...
	DrawCenterPanel(Canvas);

	// Right Panel...
	DrawRightPanel(Canvas);

	// Draw Arrows...
	Canvas.DrawColor.R = 255;
	Canvas.DrawColor.G = 255;
	Canvas.DrawColor.B = 255;
	Canvas.Style = 2;
	if (LeftOffset > 0)
	{
		Canvas.SetPos(64 + PanelWidth - 8, 128);
		Canvas.DrawIcon(texture'ArrowUR', 1.0);
	}
	if (LeftOffset + LeftNumDrawn < LeftMax)
	{
		Canvas.SetPos(64 + PanelWidth - 8, 128 + PanelHeight - 32);
		Canvas.DrawIcon(texture'ArrowDR', 1.0);
	}
	if (RightOffset > 0)
	{
		Canvas.SetPos(Canvas.ClipX - 64 - PanelWidth - 32 + 4, 128);
		Canvas.DrawIcon(texture'ArrowUL', 1.0);
	}

	if (RightOffset + RightNumDrawn < RightMax)
	{
		Canvas.SetPos(Canvas.ClipX - 64 - PanelWidth - 32, 128 + PanelHeight - 32);
		Canvas.DrawIcon(texture'ArrowDL', 1.0);
	}
	Canvas.Style = 1;

	// Help Panel...
	Canvas.DrawColor.R = 192;
	Canvas.DrawColor.G = 192;
	Canvas.DrawColor.B = 192;
	DrawHelpPanel(Canvas, Canvas.ClipY - 64, 228);
	Canvas.DrawColor.R = 255;
	Canvas.DrawColor.G = 255;
	Canvas.DrawColor.B = 255;
}

// The help panel for list menus.  Oriented somewhat differently
// because it is not restricted by the green background.
function DrawHelpPanel(canvas Canvas, int StartY, int XClip)
{
	local int StartX;

	StartX = 0.5 * Canvas.ClipX - 128;

	Canvas.bCenter = false;
	Canvas.SetOrigin(StartX + 18, StartY + 16);
	Canvas.SetClip(XClip,64);
	Canvas.SetPos(0,0);
	Canvas.Style = 1;
	switch (Focus)
	{
		case -1:
			Canvas.DrawText(LeftHelpMessage, False);
			break;
		case 0:
			if ( Selection < 20 )
				Canvas.DrawText(HelpMessage[Selection], False);
			break;
		case 1:
			Canvas.DrawText(RightHelpMessage, False);
			break;
	}
	Canvas.SetPos(0,32);
}


defaultproperties
{
     LeftHelpMessage="Hit enter to modify this bot."
     LeftTitle="Bot Pool: "
	 RightTitle0="Red Team: "
	 RightTitle1="Blue Team: "
	 Team0="Red"
	 Team1="Blue"
     RightHelpMessage="Hit enter to modify this bot."
     MenuLength=6
     HelpMessage(1)="Select to add the currently selected bot to this team."
     HelpMessage(2)="Select to remove the currently selected bot from this team."
     HelpMessage(4)="Select to view the next team."
     HelpMessage(6)="Select to save changes and exit."
     MenuList(1)="Add -->"
     MenuList(2)="<-- Remove"
     MenuList(4)="Next Team"
     MenuList(6)="Done"
     MenuTitle="Team Selection Menu"
}
