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

#include "cbase.h"
#include "hl2wars/c_rts_mouse_model.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_MouseRTSModel, DT_MouseRTSModel, CMouseRTSModel )
	RecvPropFloat( RECVINFO( m_fYawSelection ) ),
END_RECV_TABLE()

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

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

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
C_MouseRTSModel::C_MouseRTSModel()
{
	m_bDrawSelection = false;

	m_PTSelection = CLIENTSHADOW_INVALID_HANDLE;
	m_bForceCheapSelection = false;

	// init the material reference for the selection circle
	m_SelectionShadow.Init("decals/bselection", TEXTURE_GROUP_DECAL);
}

C_MouseRTSModel::~C_MouseRTSModel()
{
}

void C_MouseRTSModel::UpdateOnRemove( void )
{
	BaseClass::UpdateOnRemove();

	DestroySelection();
	m_SelectionShadow.Shutdown();
}

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

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

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
void C_MouseRTSModel::CreateSelection()
{
	int flags;
	if( m_PTSelection == CLIENTSHADOW_INVALID_HANDLE ) 
	{
		color32 clrColor32= GetColor();
		flags = SHADOW_FLAGS_SHADOW|SHADOW_FLAGS_PROJECTTEXTUREUNLIT;

		Vector mins, maxs; 
		ICollideable* pCol = GetCollideable(); 
		mins = pCol->OBBMins();
		maxs = pCol->OBBMaxs();

		m_Info.m_PTUnlitShadow = &m_SelectionShadow;
		m_Info.m_PTUnlitMins = mins;
		m_Info.m_PTUnlitMaxs = maxs;
		m_Info.m_cR = clrColor32.r;
		m_Info.m_cG = clrColor32.g;
		m_Info.m_cB = clrColor32.b;
		m_Info.m_cA = 180;
		m_Info.m_fPTShadowDistance = maxs.z - mins.z;
		m_Info.m_vOrigin = GetRenderOrigin();
		m_Info.m_Angle = GetRenderAngles();

		m_PTSelection = g_pClientShadowMgr->CreatePTUnlit(m_Info, INVALID_CLIENTENTITY_HANDLE, flags);
	}
}

void C_MouseRTSModel::DestroySelection()
{
	if(m_PTSelection != CLIENTSHADOW_INVALID_HANDLE)
	{
		g_pClientShadowMgr->DestroyPTUnlit(m_PTSelection);
		m_PTSelection = CLIENTSHADOW_INVALID_HANDLE;
	}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void C_MouseRTSModel::SetDrawSelection( bool state )
{
	m_bDrawSelection = state;

	// disable/enable
	if( !m_bForceCheapSelection ) 
	{
		if(m_bDrawSelection ) 
		{
			CreateSelection();
		}
		else if(!m_bDrawSelection)
		{
			DestroySelection();
		}
	}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void C_MouseRTSModel::Simulate( void )
{
	// update our selection circle, if we have one
	if ( !m_bForceCheapSelection && m_PTSelection != CLIENTSHADOW_INVALID_HANDLE )
	{
		m_Info.m_vOrigin = GetRenderOrigin();
		m_Info.m_Angle = GetRenderAngles();
	
		g_pClientShadowMgr->UpdatePTUnlitState( m_PTSelection, m_Info, true );
	}

	BaseClass::Simulate();
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int C_MouseRTSModel::DrawModel( int flags )
{
	if( m_bDrawSelection && m_bForceCheapSelection )
	{
		// 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();

		// Get all points
		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];

		// apply rotation
		QAngle angle = GetTargetSelectionAngle();

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

		// Transform to world
		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];

		// draw the lines
		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;
}

