//
class UTCHUD extends Mutator;

#exec TEXTURE IMPORT NAME=Tape_AA  	FILE=Textures\Tape_AA.bmp GROUP="" MIPS=OFF FLAGS=2

var int iCompassScale, iPosY;
var PlayerPawn PlayerOwner;
var ChallengeHUD myHUD;
var Texture tTape, Tape_AA; //, tMarkers;
var int oldClipX, oldClipY, iGTArrayPos, iCompassOffset;
var bool bSetDefaults, bCheckedGame;
var TournamentGameReplicationInfo GRI;
var compassController controller;

simulated Function addMutator(mutator M)
{
	if(M.class==self.class)
		m.destroy();
	else super.addMutator(M);
}

simulated function getController()
{
	local inventory inv;
	if(PlayerOwner==none) return;

	inv=PlayerOwner.inventory;

	while(inv!=none)
	{
		if(inv.isA('CompassController'))
		{
			controller=compassController(inv);
			if(!bSetDefaults) bSetDefaults=true;
			return;
		}
		inv=inv.inventory;
	}
}

simulated function PostRender(canvas Canvas)
{
	playerOwner=Canvas.viewPort.Actor;
	if(playerOwner==none) PlayerOwner = PlayerPawn(Owner);

	if(playerOwner!=none)
	{
		if(myHUD == none) myHUD=challengeHUD(PlayerOwner.myHUD);
		else if(myHUD.bHideHUD)
		{
			if(nextHudMutator!=none) nextHUDMutator.PostRender(Canvas);
			return;
		}
		oldClipY=Canvas.clipY;
	
		if(Canvas.ClipX>500 && myHUD!=none && !myHUD.bHideHUD && playerOwner.playerReplicationInfo!=none)
		{
			if(canvas.clipX<1024) iCompassScale=1;
			drawCompass(canvas);
		}
	}
	else 
	{
		Canvas.setPos(3, Canvas.clipY-3);
		canvas.DrawText("UTCompass Error: Can't find controlling PlayerPawn!");
	}
	
	if(nextHudMutator!=none) nextHUDMutator.PostRender(Canvas);
}

simulated function timer()
{
	if(controller==none) getController();
	if(controller!=none)
		iCompassOffset = controller.iCompassOffset;
}

simulated event destroyed()
{
	local mutator m;

	if(bHUDMutator && myHUD!=none)
	{
		m=myHUD.HUDMutator;
		if(m==self) myHUD.HUDMutator=nextHUDMutator;
		else 
		{
			while(m!=none)
			{
				if(m.nextHUDMutator==self) m.nextHUDMutator=nextHUDMutator;
				m=m.nextHUDMutator;
			}
		}
	}
	super.destroyed();
}


event postBeginPlay()
{
	postNetBeginPlay();
	super.postBeginPlay();
}

simulated event postNetBeginPlay()
{
	bHUDMutator=true;
	super.postNetBeginPlay();
	setTimer(0.5, true);
}

// Draw large compass tape.
simulated function DrawCompass(canvas Canvas)
{
	local int i, j, k, iCanvasMid, iDirection, iSenderDir, iRight, iLeft, iCenter;
	local float fStrLen, fStrHgt, fBrightness;
	local playerReplicationInfo msgPRI;
	local pawn p;
	local vector vLocation, oLocation;
	local color drawColor;
	local String sName;
		
	icanvasMid=Canvas.clipX/2;
	if(icanvasMid<250) return;
	
	if(GRI==none) GRI = tournamentGameReplicationInfo(PlayerOwner.gameReplicationInfo);

	if(playerOwner.viewTarget!=none)
	{
		if(PlayerOwner.viewTarget.isA('Projectile'))
			iDirection=playerOwner.viewtarget.rotation.yaw % 65536;
		else
			iDirection = (((Canvas.viewport.actor.ViewRotation.Yaw) % 65536));
		oLocation=playerOwner.viewtarget.location;
	}
	else 
	{
		iDirection = ((PlayerOwner.ViewRotation.Yaw) % 65536);
		oLocation=playerOwner.location;
	}

	iDirection -= iCompassOffset * 182.044;

	while(iDirection < 0) iDirection += 65536;
	while(iDirection > 65536) iDirection -= 65536;
	iDirection = iDirection/45.51; // iDirection is now degrees*4
	DrawColor=myHUD.HUDColor;
	canvas.Style=ERenderStyle.STY_Translucent;
	tTape=Tape_AA;
	canvas.drawColor=drawColor;

	// Draw segments.
	drawSegment(iDirection-180, icanvasMid-(180*iCompassScale), iPosY, iCompassScale, Canvas);
	drawSegment(iDirection-60, icanvasMid-(60*iCompassScale), iPosY, iCompassScale, Canvas);
	drawSegment(iDirection+60, icanvasMid+(60*iCompassScale), iPosY, iCompassScale, Canvas);

	// Draw direction marker.
	Canvas.setPos(icanvasMid-(4*iCompassScale), iPosY);
	Canvas.DrawTile(tTape, (9*iCompassScale), (10*iCompassScale), 0, 240, 9, 10);

	// Draw endbars.
	Canvas.setPos(iCanvasMid-(182*iCompassScale), iPosY);
	Canvas.DrawTile(tTape, (7*iCompassScale), (33*iCompassScale), 245, 0, 7, 33);
	Canvas.setPos(iCanvasMid+(175*iCompassScale), iPosY);
	Canvas.DrawTile(tTape, (7*iCompassScale), (33*iCompassScale), 245, 40, 7, 33);
	
	iRight=0;
	iLeft=0;
}

simulated function drawSegment(int iDir, int offsX, int offsY, int iScale, canvas Canvas){

	While(iDir>1440) iDir-=1440;
	While(iDir<0) iDir+=1440;
	if(iDir==1440) iDir=0;

	canvas.setPos(offsX, offsY);
	if(iDir<240)
	{
		if(iDir<=120) Canvas.drawTile(tTape, (120*iCompassScale), (30*iCompassScale), iDir, 0, 120, 30);
		else
		{
			Canvas.drawTile(tTape, (240-iDir)*iCompassScale, (30*iCompassScale), iDir, 0, 240-iDir, 30);
			Canvas.setPos(offsX+((240-iDir)*iCompassScale), offsY);
			Canvas.drawTile(tTape, (iDir-120)*iCompassScale, (30*iCompassScale), 0, 40, iDir-120, 30);
		}
	}
	else if(iDir<480)
	{
		if(iDir<=360) Canvas.drawTile(tTape, (120*iCompassScale), (30*iCompassScale), iDir-240, 40, 120, 30);
		else
		{
			Canvas.drawTile(tTape, (480-iDir)*iCompassScale, (30*iCompassScale), iDir-240, 40, 480-iDir, 30);
			Canvas.setPos(offsX+(480-iDir)*iCompassScale, offsY);
			Canvas.drawTile(tTape, (iDir-360)*iCompassScale, (30*iCompassScale), 0, 80, iDir-360, 30);
		}
	}
	else if(iDir<720)
	{
		if(iDir<=600) Canvas.drawTile(tTape, (120*iCompassScale), (30*iCompassScale), iDir-480, 80, 120, 30);
		else
		{
			Canvas.drawTile(tTape, (720-iDir)*iCompassScale, (30*iCompassScale), iDir-480, 80, 720-iDir, 30);
			Canvas.setPos(offsX+(720-iDir)*iCompassScale, offsY);
			Canvas.drawTile(tTape, (iDir-600)*iCompassScale, (30*iCompassScale), 0, 120, iDir-600, 30);
		}
	}
	else if(iDir<960)
	{
		if(iDir<=840) Canvas.drawTile(tTape, (120*iCompassScale), (30*iCompassScale), iDir-720, 120, 120, 30);
		else
		{
			Canvas.drawTile(tTape, (960-iDir)*iCompassScale, (30*iCompassScale), iDir-720, 120, 960-iDir, 30);
			Canvas.setPos(offsX+(960-iDir)*iCompassScale, offsY);
			Canvas.drawTile(tTape, (iDir-840)*iCompassScale, (30*iCompassScale), 0, 160, iDir-840, 30);
		}
	}
	else if(iDir<1200)
	{
		if(iDir<=1080) Canvas.drawTile(tTape, (120*iCompassScale), (30*iCompassScale), iDir-960, 160, 120, 30);
		else
		{
			Canvas.drawTile(tTape, (1200-iDir)*iCompassScale, (30*iCompassScale), iDir-960, 160, 1200-iDir, 30);
			Canvas.setPos(offsX+(1200-iDir)*iCompassScale, offsY);
			Canvas.drawTile(tTape, (iDir-1080)*iCompassScale, (30*iCompassScale), 0, 200, iDir-1080, 30);
		}
	}
	else 
	{
		if(iDir<=1320) Canvas.drawTile(tTape, (120*iCompassScale), (30*iCompassScale), iDir-1200, 200, 120, 30);
		else
		{
			Canvas.drawTile(tTape, (1440-iDir)*iCompassScale, (30*iCompassScale), iDir-1200, 200, 1440-iDir, 30);
			Canvas.setPos(offsX+(1440-iDir)*iCompassScale, offsY);
			Canvas.drawTile(tTape, (iDir-1320)*iCompassScale, (30*iCompassScale), 0, 0, iDir-1320, 30);
		}
	}
}

defaultproperties
{
   iCompassScale=1
   iPosY=88
   Tape_AA=Texture'Tape_AA'
   bNetTemporary=True
   RemoteRole=ROLE_SimulatedProxy
}
