#include "MW4Headers.hpp"
#include "hudcomp.hpp"
#include "hudcomp2.hpp"
#include "vehicle.hpp"
#include "mwapplication.hpp"
#include "..\missionlang\resource.h"

//#include <MLR\MLR.hpp>
//#include <MLR\MLRTexturePool.hpp>
//#include <MLR\MLRTexture.hpp>

// jcem - begin
extern Scalar g_fLastStartTime;
extern Adept::ReplicatorID g_LastTarget;
// jcem - end

using namespace MechWarrior4;

HUDTargetArrow::HUDTargetArrow()
{

	AddTexture ("hud\\hud5",0,7,58,34,114);				 	// arrow
	AddTexture ("hud\\hud5",1,65,12,82,29);					// tl
	AddTexture ("hud\\hud5",2,103,12,120,29);					// tr
	AddTexture ("hud\\hud5",3,103,50,120,67);					// br
	AddTexture ("hud\\hud5",4,65,50,82,67);					// bl

	AddTexture ("hud\\hud5",5,64,11,121,67);				// whole

//	Location (Point3D (30.0f,0.5f,0.9f));
	Location (Point3D (33.0f,275.0f,0.9f));

	Alignment (LEFT_ALIGN);
	Size (m_Textures[0]->Size ());
	m_NavAlphaTime = 0;

	m_LockColor[0] = MakeColor (0,0,0,250);
	m_LockColor[1] = MakeColor (0,0,255,250);
	m_LockColor[2] = MakeColor (255,0,0,250);

	m_TargetAlignment = 0;
	m_Textures[1]->Clip (true);
	m_Textures[2]->Clip (true);
	m_Textures[3]->Clip (true);
	m_Textures[4]->Clip (true);
	Point3D size = m_Textures[1]->Size ();
	m_Textures[1]->Origin (0,0);
	m_Textures[2]->Origin (size.x,0);
	m_Textures[3]->Origin (size.x,size.y);
	m_Textures[4]->Origin (0,size.y);
	size = m_Textures[5]->Size ();
	m_Textures[5]->Origin (size.x/2,size.y/2);
	size = m_Textures[0]->Size ();
	m_Textures[0]->Origin (size.x*0.5f,size.y*0.50f);
	arrowTextureWidth_Half = m_Textures[0]->Size().x;
	arrowTextureHeight_Half = m_Textures[0]->Size().y;;

	m_ZoomLeft = 1;
	m_ZoomRight = 0;
	m_ZoomTop = 1;
	m_ZoomBottom = 0;
	m_IndicatorInZoom = false;

}

HUDTargetArrow::~HUDTargetArrow()
{
}

void HUDTargetArrow::Update (Stuff::Time till)
{

	if (m_NavAlphaTime == 0)
		m_NavAlphaTime = till;

	while (m_NavAlphaTime <= till)
	{
		m_NavAlpha +=20;
		if (m_NavAlpha >= 255)
			m_NavAlpha = 15;
		m_NavAlphaTime += 1.0/30.0;
	}

	inherited::Update (till);
}

void HUDTargetArrow::SetZoomWindow(Stuff::Scalar left,Stuff::Scalar top,Stuff::Scalar right,Stuff::Scalar bottom)
{
	m_ZoomLeft = left;
	m_ZoomRight = right;
	m_ZoomTop = top;
	m_ZoomBottom = bottom;
}


void HUDTargetArrow::DrawImplementation(void)
{
	//sanghoon - need to find where blink handling is done.
	Point3D loc,size;
	DWORD color;
	int textx,texty,ty2;

	textx = -1;
	texty = -1;
	ty2 = -1;
	size = m_Textures[0]->Size ();
	if ((!m_LeftArrow) && (!m_RightArrow) && (!m_OnScreen) && (!m_TopArrow) && (!m_BottomArrow) )
		return;
	loc = Location ();
	if (m_LeftArrow)
	{
		if (m_IndicatorInZoom)
		{
			loc.x = loc.x + (1.0f - m_ZoomLeft)*800.0f;
			loc.y = (m_ZoomRight + m_ZoomLeft) * 0.5f * 600.0f;
		}
		textx = (int) loc.x;
		texty = (int) (loc.y + (size.y/2.0f));
	}
	else if (m_RightArrow)
	{
		if (m_IndicatorInZoom)
		{
			loc.x = ((1.0f-m_ZoomRight)*800.0f) - loc.x*0.75f;
			loc.y = (m_ZoomRight + m_ZoomLeft) * 0.5f * 600.0f;
		}
		else
			loc.x = 800.0f - loc.x;
		textx = (int) loc.x;
		texty = (int) (loc.y + (size.y/2.0f));
	}
	else if (m_TopArrow)
	{
		if (m_IndicatorInZoom)
		{
			loc.y = (1.0f - m_ZoomTop)*600.0f + loc.x*0.75f;
			loc.x = (m_ZoomLeft + m_ZoomRight)*800.0f * 0.5f;
			textx = (int) loc.x;
			texty = (int) (loc.y + (size.x/2.0f));		// arrow gets rotated;
		}
	}
	else if (m_BottomArrow)
	{
		if (m_IndicatorInZoom)
		{
			loc.y = (1.0f-m_ZoomBottom)*600.0f - loc.x*0.75f;
			loc.x = (m_ZoomLeft + m_ZoomRight)*800.0f * 0.5f;
			textx = (int) loc.x;
			texty = (int) (loc.y - (size.x/2.0f));		// arrow gets rotated
		}
	}

    if (g_pCurTOC) {
        color = MakeColor (255,255,0,250);
    } else {
	    color = m_LockColor[m_TargetAlignment];
    }

	color = (m_NavAlpha << 24) + (color & 0x00FFFFFF);
	m_Textures[0]->Rotate(0.0f);
	if (m_LeftArrow)
	{
		m_Textures[0]->Draw (loc,size,color,HUDTexture::XFLIP);
	}
	else if (m_RightArrow)
	{
		m_Textures[0]->Draw (loc,size,color);
	}
	else if (m_TopArrow)
	{
		m_Textures[0]->Rotate(-90.0f *Stuff::Radians_Per_Degree);
		m_Textures[0]->Draw (loc,size,color);
	}
	else if (m_BottomArrow)
	{
		m_Textures[0]->Rotate(90.0f *Stuff::Radians_Per_Degree);
		m_Textures[0]->Draw (loc,size,color);
	}

	//
	// Need to rotate the texture instead of flipping
	//
	else if (m_OnScreen)
	{
// MSL 5.02 Hot Box Masking
// If Console Radar Setting is Not Unlimited then you don't get a Hot Box.
// MSL 5.05 Advance Mode
		MWApplication *m_App;
		m_App = MWApplication::GetInstance ();
		m_AdvanceMode = m_App->GetLocalNetParams()->m_advancemodeOn;

		if (m_AdvanceMode)
		{
			if (g_pCurTOC)
		        color = MakeColor (255,255,0,250);
			else
				color = MakeColor (0,0,0,0);
		}
		else
		{
			color = (m_NavAlpha << 24) + (color & 0x00FFFFFF);
		}
		m_TargetX /= g_AdjustMultX;
		m_TargetY /= g_AdjustMultY;
		m_TargetWidth /= g_AdjustMultX;
		m_TargetHeight /= g_AdjustMultY;
		if ((m_TargetWidth == 0) || (m_TargetHeight == 0))
		{
			size = m_Textures[5]->Size ();
			m_Textures[5]->Draw (Point3D (m_TargetX,m_TargetY,0.9f),size,color);
			//Texture of four angle brackets merged into one...
		}
		else
		{
			size = m_Textures[1]->Size ();
//			size.x = m_TargetWidth;
//			size.y = m_TargetHeight;
//			size.z = 0.0f;
			m_Textures[1]->Draw (Point3D (m_TargetX-m_TargetWidth/2.0f,m_TargetY-m_TargetHeight/2.0f,0.9f),size,color);
			m_Textures[2]->Draw (Point3D (m_TargetX+m_TargetWidth/2.0f,m_TargetY-m_TargetHeight/2.0f,0.9f),size,color);
			m_Textures[3]->Draw (Point3D (m_TargetX+m_TargetWidth/2.0f,m_TargetY+m_TargetHeight/2.0f,0.9f),size,color);
			m_Textures[4]->Draw (Point3D (m_TargetX-m_TargetWidth/2.0f,m_TargetY+m_TargetHeight/2.0f,0.9f),size,color);
//			m_Textures[1]->Draw (Point3D (m_TargetX,m_TargetY,0.9f),size,color/*, HUDTexture::NO_FLIP, true*/);
//			DrawFrame (Point3D (m_TargetX - m_TargetWidth/2.0f,m_TargetY - m_TargetHeight/2.0f,0.9f),Point3D (m_TargetWidth,m_TargetHeight,0.9f),color);
		}
	}
}


HUDTorsoBar::HUDTorsoBar()
{
	m_TorsoTwist = 0;
	m_TorsoPitch = 0;
	m_TwistSize = Point3D (33,5,0.9f);
	m_PitchSize = Point3D (5,25,0.9f);
	AddTexture ("hud\\hud4",0,245,240,250,243);				// left pitch
	AddTexture ("hud\\hud4",1,245,240,250,243);				// right pitch
	AddTexture ("hud\\hud4",2,245,240,250,243);				// top twist
	AddTexture ("hud\\hud4",3,245,240,250,243);				// bottom twist
	AddTexture ("hud\\hud5",4,132,10,137,53);				// pitch bar
	AddTexture ("hud\\hud5",5,132,10,137,53);				// twist bar
	Point3D size;
	size = m_Textures[0]->Size ();
	m_Textures[0]->Origin (size.x/2.0f,0);
	m_Textures[1]->Origin (size.x/2.0f,0);
	m_Textures[2]->Origin (size.x/2.0f,0);
	m_Textures[3]->Origin (size.x/2.0f,0);
	size = m_Textures[4]->Size ();
	m_Textures[4]->Origin (0,size.y/2.0f);
	m_Textures[5]->Origin (0,size.y/2.0f);

	m_TwistSize.x = m_Textures[5]->Size ().y/2.0f;
	m_PitchSize.y = m_Textures[5]->Size ().y/2.0f;

	m_Textures[0]->Rotate (Pi_Over_2);
	m_Textures[1]->Rotate (Pi_Over_2 + Pi);
	m_Textures[2]->Rotate (Pi);

	m_Textures[5]->Rotate (Pi_Over_2);
	Color (0,250,0,255);
	m_TwistText = new HUDText ();
// Changed Twist Text Color to Yellow
// MSL 5.00
//  m_TwistText->Color (Color ());
    m_TwistText->Color (MakeColor (255,255,0,250));
	m_TwistText->Justification (HUDText::LEFT_ALIGN);
	m_TwistText->TopLeft (0,0);
	m_TwistText->BottomRight (5000,5000);
}

HUDTorsoBar::~HUDTorsoBar()
{
	delete m_TwistText;
}

void HUDTorsoBar::DrawImplementation(void)
{
	Scalar currentxval,currentyval;
	Point3D size,loc,texsize;

	//Routine to draw the graph representing the x-axis...
	//_________________________________________________________________________
	size = TwistSize ();
	texsize = m_Textures[0]->Size ();
	currentxval = (m_TorsoTwist*size.x)/100.0f;			// 33 is the width of the bar

	loc.y = 360;
	loc.x = 400;
	if (currentxval > 0)
		loc.x += 1.0f;
	DWORD color = DarkerColor (Color ());
	//Draw horizontal ruler. (arrows not included)
	m_Textures[5]->Draw (loc,m_Textures[5]->Size (),color,HUDTexture::NO_FLIP,true);
	color = Color ();
	if (currentxval != 0)
	{
		int min,max;
		MWApplication *app;
		app = MWApplication::GetInstance ();
		if (currentxval > 0)
		{
			min = (int) (loc.x-currentxval);
			max = (int) loc.x;
			m_TwistText->UpdateText (app->GetLocString (IDS_TWISTLEFT));
		}
		else
		{
			max = (int) (loc.x-currentxval);
			min = (int) loc.x;
			m_TwistText->UpdateText (app->GetLocString (IDS_TWISTRIGHT));
		}
		DWORD tx,ty;
		m_TwistText->DrawSize (tx,ty);
		tx = 400 - (tx/2);
		ty= (DWORD) (loc.y+size.y+10);
		if ((currentxval > 5) || (currentxval < -5))
		{
			//Shows direction indicator when torso twist is excessive..
			m_TwistText->Draw (Point3D ((Scalar) tx,(Scalar) ty,0.9f));
		}
		//Upper body indicator rectangle..
// Changed Torso Bar from Green to Yellow
// MSL 5.00
        color = MakeColor (255,255,0,250);
		my_DrawRect (min,(int) loc.y,max,(int) (loc.y+size.y),color);

		color = DarkerColor (Color ());
		//Top arrow indicating upper body direction..
		m_Textures[2]->Draw (Point3D (loc.x-currentxval,loc.y-1,0.9f),texsize,color);
		//Bottom arrow indicating center position..
		m_Textures[3]->Draw (Point3D (loc.x-1,loc.y+size.y+1,0.9f),texsize,color);
//		DrawLine ((int) (loc.x-currentxval),(int) (loc.y-1),(int) (loc.x-currentxval),(int) (loc.y+size.y),color);
	}
	else
	{
		color = DarkerColor (Color ());
		m_Textures[2]->Draw (Point3D (loc.x,loc.y-1,0.9f),texsize,color);
		m_Textures[3]->Draw (Point3D (loc.x,loc.y+size.y+1,0.9f),texsize,color);
//		DrawLine ((int) (loc.x),(int) (loc.y-1),(int) (loc.x),(int) (loc.y+size.y),color);
	}

	//Routine to draw graph for y-axis... <== no excessive up/down text here.
	//_________________________________________________________________________
	size = PitchSize ();
	currentyval = (m_TorsoPitch*size.y)/100.0f;			// 33 is the width of the bar

	loc.x = 400-61.0f;
	loc.y = 300;
	if (currentyval > 0)
		loc.y += 1.0f;
	color = DarkerColor (Color ());
	m_Textures[4]->Draw (loc,m_Textures[4]->Size (),color,HUDTexture::NO_FLIP,true);
	color = Color ();
	if (currentyval != 0)
	{
		gos_SetRenderState( gos_State_Texture, 0);
		int min,max;
		if (currentyval < 0)
		{
			min = (int) (loc.y+currentyval);
			max = (int) loc.y;
		}
		else
		{
			max = (int) (loc.y+currentyval);
			min = (int) loc.y;
		}
// Changed Torso Bar from Green to Yellow
// MSL 5.00
        color = MakeColor (255,255,0,250);
		my_DrawRect ((int) (loc.x-1),min,(int) (loc.x+size.x),max,color);

		color = DarkerColor (Color ());
		m_Textures[0]->Draw (Point3D (loc.x-1,loc.y+currentyval,0.9f),texsize,color);
		m_Textures[1]->Draw (Point3D (loc.x+size.x,loc.y,0.9f),texsize,color);
//		DrawLine ((int) (loc.x+size.x),(int) (loc.y+currentyval),(int) (loc.x+size.x),(int) (loc.y),color);
	}
	else
	{
		color = DarkerColor (Color ());
		m_Textures[0]->Draw (Point3D (loc.x-1,loc.y,0.9f),texsize,color);
		m_Textures[1]->Draw (Point3D (loc.x+size.x,loc.y,0.9f),texsize,color);
//		DrawLine ((int) (loc.x-1),(int) (loc.y),(int) (loc.x+size.x+2),(int) (loc.y),color);
	}
	//End.. DrawImplementation.
}


HUDZoom::HUDZoom()
	: m_ZoomLevel(5)
{
	Location (Point3D (0.35f,0.35f,0.9f));
	Size (Point3D (0.3f,0.3f,0.9f));
//	m_Text = new HUDText ();

//	char buf[10];
//	sprintf(buf,"%dx",m_ZoomLevel);
//	m_Text->UpdateText (buf);
//	m_Text->Justification (HUDText::LEFT_ALIGN);
//	m_Text->Color (0xFF00AA00);
}

HUDZoom::~HUDZoom()
{
//	delete m_Text;
}

void HUDZoom::SetZoomLevel(int zoom_level)
{
	m_ZoomLevel = zoom_level;
#if 0
	char buf[10];
	sprintf(buf,"%dx",m_ZoomLevel);
	m_Text->UpdateText (buf);
#endif
}

void HUDZoom::SetWindow (Stuff::Scalar left,Stuff::Scalar top,Stuff::Scalar right,Stuff::Scalar bottom)
{
	m_Left = left*800;
	m_Right = right*800;
	m_Top = top*600;
	m_Bottom = bottom*600;
	if (m_Left > m_Right)
	{
		Scalar temp;
		temp = m_Left;
		m_Left = m_Right;
		m_Right = temp;
	}
	if (m_Top > m_Bottom)
	{
		Scalar temp;
		temp = m_Top;
		m_Top = m_Bottom;
		m_Bottom = temp;
	}
	Location (Point3D (m_Left,m_Top,0.9f));
	Size (Point3D (m_Right-m_Left,m_Bottom - m_Top,0.9f));
}

void HUDZoom::DrawImplementation(void)
{
	Point3D loc,size;

	loc = Location ();
	size = Size ();

// MSL 5.02 Zoom Border Size
	size.x -= 1.0f;
	size.y -= 1.0f;

// MSL 5.02 Zoom Border Color
	DWORD color = MakeColor (175,0,0,150);

	DrawFrame (loc,size,color);

	loc.x += 3.0f;
	loc.y += 3.0f;
//	m_Text->Draw (loc);
}



HUDMP::HUDMP ()
{
// MSL 5.00
//	Changed the icon position of KOTH and Flag Icons
	//AddTexture ("hud\\hud5",0,224,167,255,198);				// flag
	//AddTexture ("hud\\hud5",1,224,129,255,160);			// KOTF
	AddTexture ("hud\\hud5",0,223,167,255,198);				// flag
	AddTexture ("hud\\hud5",1,223,129,255,160);			// KOTF
	AddTexture ("hud\\hud5",2,223,205,255,236);			// Fire
	AddTexture ("hud\\hud5",3,186,129,217,160);			// Jam

	m_Alpha = 255;
	m_AlphaTime = 0;
	Color (MakeColor (0,175,0,150));				
	m_FlagVisible = false;
	m_KOTFVisible = false;
	m_FireVisible = false;
	m_JamVisible = false;
//	Location (Point3D (657,466,0));
//	Size (m_Textures[0]->Size ());
}

void HUDMP::Update (Stuff::Time till)
{
	if (m_AlphaTime == 0)
		m_AlphaTime = till;

	while (m_AlphaTime <= till)
	{
		m_Alpha +=5;
		if (m_Alpha >= 255)
			m_Alpha = 15;
		m_AlphaTime += 1.0/30.0;
	}

	inherited::Update (till);
}

void HUDMP::DrawImplementation(void)
{
	Point3D loc,size1,size2,size3,size4;
	DWORD color;

	loc.y = loc.z = 0;
	size1 = m_Textures[0]->Size ();
	size2 = m_Textures[1]->Size ();
// MSL 5.03 Ammo Bay Fire Indicator
	size3 = m_Textures[2]->Size ();
// MSL 5.03 Jam Indicator
	size4 = m_Textures[3]->Size ();
    //MSL: change 603 to 400 (middle of screen)
	loc.x = 400 - (size1.x/2);
    //MSL: change the color of icon to solid-RED, no more flashing
    color = MakeColor(255,0,0,255);
	//color = Color ();
	//color &= 0x00ffffff;
	//color |= (m_Alpha << 24);

// MSL 5.03 Ammo Bay Fire Indicator
	if (m_FireVisible)
	{
		Scalar oldheatlevel;
		oldheatlevel = g_HUDHeatLevel;
		g_HUDHeatLevel = 0;

		loc.x = 468 - (size3.x/2);
		loc.y = 310 - size3.y - 5;
	    color = MakeColor(255,0,0,255);
		m_Textures[2]->Draw (loc,size3,color);

		g_HUDHeatLevel = oldheatlevel;
	}	
	
// MSL 5.03 Jam Indicator
	if (m_JamVisible)
	{
		Scalar oldheatlevel;
		oldheatlevel = g_HUDHeatLevel;
		g_HUDHeatLevel = 0;

		loc.x = 468 - (size4.x/2);
		loc.y = 340 - size4.y - 5;
	    color = MakeColor(255,0,0,255);
		m_Textures[3]->Draw (loc,size4,color);

		g_HUDHeatLevel = oldheatlevel;
	}	

	if (m_FlagVisible && m_KOTFVisible)
	{
	    color = MakeColor(255,0,0,255);
		loc.x = 400 - (size1.x/2);
        //MSL: change 599 to 180 (1/4 from top of screen)
		loc.y = 180 - size1.y - size2.y - 4;
		m_Textures[0]->Draw (loc,size1,color);
		loc.y += size2.y+2;
		m_Textures[1]->Draw (loc,size2,color);
	}
	else if (m_FlagVisible)
	{
	    color = MakeColor(255,0,0,255);
        loc.x = 400 - (size1.x/2);
		//MSL: change 599 to 180 (1/4 from top of screen)
		loc.y = 180 - size1.y - 5;
		m_Textures[0]->Draw (loc,size1,color);
	}
	else if (m_KOTFVisible)
	{
	    color = MakeColor(255,0,0,255);
        loc.x = 400 - (size1.x/2);
		//MSL: change 599 to 180 (1/4 from top of screen)
		loc.y = 180 - size2.y - 5;
		m_Textures[1]->Draw (loc,size2,color);
	}
}

