//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================//

#include "cbase.h"
#include "hl2wars/c_rts_mouse_brush.h"

#include "fx_line.h"
#include "ClientEffectPrecacheSystem.h"
//#include "fx_impact.h"
//#include "ieffects.h"
//#include "simple_keys.h"
#include "iefx.h"
//#include "dlight.h"
//#include "c_sprite.h"
//#include <bitbuf.h>

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

// link data table DT_MouseRTSBrush to client class and map variables (RecvProps)
IMPLEMENT_CLIENTCLASS_DT( C_MouseRTSBrush, DT_MouseRTSBrush, CMouseRTSBrush )
	RecvPropFloat( RECVINFO( m_fYawSelection ) ),
	RecvPropBool( RECVINFO( m_bSolid ) ),
END_RECV_TABLE()

CLIENTEFFECT_REGISTER_BEGIN( PrecacheMouseBaseFX2)
CLIENTEFFECT_MATERIAL( "effects/line" )
CLIENTEFFECT_REGISTER_END()

// ##################################################################################
//	>> Mouse brush
//
// ##################################################################################

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
C_MouseRTSBrush::C_MouseRTSBrush()
{
	m_bDrawSelection = false;
	//AddEFlags( EFL_USE_PARTITION_WHEN_NOT_SOLID );
	//Msg("C_MouseRTSBrush constructor\n");
}

C_MouseRTSBrush::~C_MouseRTSBrush()
{
}

void C_MouseRTSBrush::Spawn( void )
{
	BaseClass::Spawn();	
}

void C_MouseRTSBrush::OnCursorEntered()
{
	//if( !IsSelected() )
	//	SetDrawSelection( true );
}

void C_MouseRTSBrush::OnCursorExited()
{
	if( !IsSelected() )
		SetDrawSelection( false );
}

color32 C_MouseRTSBrush::GetColor()
{
	color32 color = { 255, 255, 255, 200 };
	return color;
}

QAngle C_MouseRTSBrush::GetTargetSelectionAngle( void)
{
	QAngle angle = GetAbsAngles();
	angle[YAW] += m_fYawSelection;
	return angle;
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int C_MouseRTSBrush::DrawModel( int flags )
{
	if( m_bDrawSelection )
	{
		// get size of quad
		Vector mins, maxs; 
		Vector point1, point2, point3, point4;
		ICollideable* pCol = GetCollideable(); 
		mins = pCol->OBBMins();
		maxs = pCol->OBBMaxs();

		mins[0] -= 4.0f;
		mins[1] -= 4.0f;

		maxs[0] += 4.0f;
		maxs[1] += 4.0f;

		color32 clrColor32= GetColor();

		Vector origin = GetAbsOrigin();
		point1[2] = point2[2] = point3[2] = point4[2] = origin[2];

		point1[0] = mins[0];
		point1[1] = mins[1];
		point2[0] = mins[0];
		point2[1] = maxs[1];
		point3[0] = maxs[0];
		point3[1] = maxs[1];
		point4[0] = maxs[0];
		point4[1] = mins[1];

		//QAngle angle = GetAbsAngles();
		QAngle angle = GetTargetSelectionAngle();

		VectorYawRotate( point1, angle[YAW], point1 );
		VectorYawRotate( point2, angle[YAW], point2 );
		VectorYawRotate( point3, angle[YAW], point3 );
		VectorYawRotate( point4, angle[YAW], point4 );

		point1[0] += origin[0];
		point1[1] += origin[1];

		point2[0] += origin[0];
		point2[1] += origin[1];

		point3[0] += origin[0];
		point3[1] += origin[1];

		point4[0] += origin[0];
		point4[1] += origin[1];

		IMaterial *pMat = materials->FindMaterial( "effects/line", TEXTURE_GROUP_CLIENT_EFFECTS );

		FX_DrawLine( point1, point2, 16.0f, pMat, clrColor32 );
		FX_DrawLine( point2, point3, 16.0f, pMat, clrColor32 );
		FX_DrawLine( point3, point4, 16.0f, pMat, clrColor32 );
		FX_DrawLine( point4, point1, 16.0f, pMat, clrColor32 );
	}

	if( m_bBaseClassDraw )
		return BaseClass::DrawModel( flags );

	return 1;
}

