//========= Copyright © 1996-2005, Sander Corporation, All rights reserved. ============//
//
// Purpose: hud_rts_unitpanel.cpp
//
// $NoKeywords: $
//=============================================================================//

#include "cbase.h"
#include "hud.h"
#include "hl2wars/hud_interface.h"
#include <vgui/ISurface.h>
#include "hl2wars/c_hl2wars_player.h"
#include "hl2wars/in_buttons_rts.h"
#include "hud_macros.h"
#include <filesystem.h>
#include <keyvalues.h>
#include "inputsystem/ButtonCode.h"
#include "hl2wars/hl2wars_convars.h"
#include "hl2wars/c_rts_globalresource.h"
#include "iinput.h"
#include "hl2wars/c_rts_buildings.h"

// test
#include <vgui_controls/Tooltip.h>

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

using namespace vgui;

DECLARE_HUD_COMMAND_NAME(CHudInterface, ShowObjectives, "CHudInterface");
DECLARE_HUD_COMMAND_NAME(CHudInterface, KillSelected, "CHudInterface");
DECLARE_HUD_COMMAND_NAME(CHudInterface, OrderCancel, "CHudInterface");

HOOK_COMMAND( showobjectives, ShowObjectives );
HOOK_COMMAND( killselected, KillSelected );
HOOK_COMMAND( order_cancel, OrderCancel );

DECLARE_HUD_MESSAGE( CHudInterface, AbilityStateUpdate );

DECLARE_HUDELEMENT( CHudInterface );

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CHudInterface::CHudInterface( const char *pElementName ) :
  CHudElement( pElementName ), BaseClass( NULL, "HudInterface" )
{
	vgui::Panel *pParent = g_pClientMode->GetViewport();
	SetParent( pParent );

	SetHiddenBits( HIDEHUD_RTS | HIDEHUD_PLAYERDEAD );

	SetProportional( true );
	SetPaintBackgroundEnabled( true );
	SetKeyBoardInputEnabled( false );	
	SetMouseInputEnabled( true );	
	SetScheme( vgui::scheme()->LoadSchemeFromFile("resource/HL2Wars.res", "HL2Wars") );
	SetCursor( GetParent()->GetCursor() );

	m_pHudPanel = NULL;

	// ----------------- create objectives frame
	m_pObjectivesFrame = new CRTSObjectiveDialog();

	m_pMiniMap = new CHudMiniMap("MiniMap");
	m_pMiniMap->SetParent(pParent);
	//m_pMiniMap->SetParent(this);

	m_pUnitPanel = new CHudRTSUnitPanel();
	m_pUnitPanel->SetParent(this);

	m_pAbilityPanel = new CHudRTSAbilityPanel();
	m_pAbilityPanel->SetParent(this);

	m_pBuildingPanel = new CHudRTSBuildingPanel();
	m_pBuildingPanel->SetParent(this);

	m_pConstructionPanel = new CHudRTSConstructionPanel();
	m_pConstructionPanel->SetParent(this);

	// ------------ create labels
	// unit counter
	m_pAmountUnits = vgui::SETUP_PANEL(new vgui::Label(this,"UnitsAmount",""));		
	m_pAmountUnits->SetScheme(vgui::scheme()->LoadSchemeFromFile("resource/ClientScheme.res", "ClientScheme"));	//Set it's scheme...
	m_pAmountUnits->SetBgColor(Color(0,0,0,0));	//Translucent BG
	m_pAmountUnits->SetPaintBackgroundEnabled( false );
	m_pAmountUnits->SetPaintBorderEnabled( false );	
	m_pAmountUnits->SetContentAlignment( vgui::Label::a_west );
	m_pAmountUnits->SetCursor( GetParent()->GetCursor() );

	// resource counter
	m_pAmountResources = vgui::SETUP_PANEL(new vgui::Label(this,"ResourcesAmount",""));		
	m_pAmountResources->SetScheme(vgui::scheme()->LoadSchemeFromFile("resource/ClientScheme.res", "ClientScheme"));	//Set it's scheme...
	m_pAmountResources->SetBgColor(Color(0,0,0,0));	//Translucent BG
	m_pAmountResources->SetPaintBackgroundEnabled( false );
	m_pAmountResources->SetPaintBorderEnabled( false );	
	m_pAmountResources->SetContentAlignment( vgui::Label::a_west );
	m_pAmountResources->SetCursor( GetParent()->GetCursor() );
}

//-----------------------------------------------------------------------------
// Purpose: Deconstructor
//-----------------------------------------------------------------------------
CHudInterface::~CHudInterface()
{

}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CHudInterface::Init( void )
{
	HOOK_HUD_MESSAGE( CHudInterface, AbilityStateUpdate );

	m_pMiniMap->ShowPanel( true );
	m_pMiniMap->Init();

	m_pUnitPanel->SetVisible( true );
	m_pUnitPanel->Init();

	m_pAbilityPanel->SetUnitPanel( m_pUnitPanel );
	m_pAbilityPanel->SetVisible( true );
	m_pAbilityPanel->Init();

	m_pBuildingPanel->SetVisible( false );
	m_pBuildingPanel->SetUnitPanel( m_pUnitPanel );
	m_pBuildingPanel->Init();

	m_pConstructionPanel->SetVisible( false );
	m_pConstructionPanel->Init();

	// create the buttons
	CreateButtons( );
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CHudInterface::LevelShutdown( void )
{	
	m_pUnitPanel->LevelShutdown();
	//m_pAbilityPanel->LevelShutdown();
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CHudInterface::ApplySchemeSettings( IScheme *scheme )
{
	BaseClass::ApplySchemeSettings( scheme );

	RecalculateSizePos();

	// set fonts and colors or labels
	HFont font = scheme->GetFont("InterfaceInfoFont", IsProportional() );
	m_pAmountUnits->SetFont( font );
	m_pAmountResources->SetFont( font );

	m_pAmountUnits->SetFgColor(GetSchemeColor("hudinterface.TextColor", scheme));
	m_pAmountResources->SetFgColor(GetSchemeColor("hudinterface.TextColor", scheme));
}

bool CHudInterface::ShouldDraw()
{
	bool draw = CHudElement::ShouldDraw();

	if( draw )
	{
		m_pMiniMap->ShowPanel( true );
	}
	else
	{
		m_pMiniMap->ShowPanel( false );
	}

	return draw;
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CHudInterface::OnThink()
{
	UpdateStats();
	UpdateMiddlePanel();
	UpdateBehaviorButtons();
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CHudInterface::RecalculateSizePos( void )
{
	// save the screensize
	surface()->GetScreenSize( 
		m_iScreenWidth, 
		m_iScreenHeight );

	// get the aspect ratio
	m_iAspect = (int)( ((float)m_iScreenHeight / 
		(float)m_iScreenWidth) * 100 );

	KeyValues *pInterfaceMainData = new KeyValues( "data" );
	//if ( !pInterfaceMainData->LoadFromFile( filesystem, "resource/ui/HudInterface.txt", "GAME" ) )
	if ( !pInterfaceMainData->LoadFromFile( filesystem, "resource/ui/HudInterface.res", "GAME" ) )
	{
		Warning("Error! CHudInterface::RecalculateSizePos: couldn't load file resource/ui/HudInterface.txt.\n" );
		return;
	}

	// retrieve the right values for the screensize
	KeyValues *pData;
	if( ( (float)m_iScreenWidth / (float)m_iScreenHeight ) < 1.45f )
		pData = pInterfaceMainData->FindKey("normal");
	else
		pData = pInterfaceMainData->FindKey("wide");

	if( !pData )
	{
		Warning("Failed to load the screen data settings! \n");
		return;
	}
	
	// load the right background
	m_pHudPanel = new CHudTexture();
	m_pHudPanel = gHUD.GetIcon(  pData->GetString("texture") );
	m_clrHudPanel =  Color(255, 255, 255, 255);

	float tex_width, tex_height;
	tex_width = (float)m_pHudPanel->Width();
	tex_height = (float)m_pHudPanel->Height();

	float size_fix = 1.0f;
	if( tex_width != 0 )
		size_fix = m_iScreenWidth / tex_width;
	int size_y = size_fix * tex_height;

	SetPos( 0, m_iScreenHeight - size_y );
	SetSize( m_iScreenWidth, size_y );
	GetSize( m_iPanelWidth, m_iPanelHeight);

	m_iRadarX1 = ( pData->GetFloat( "minimap_posx") / tex_width ) * (float)m_iPanelWidth;
	m_iRadarY1 = ( ( pData->GetFloat( "minimap_posy" ) / tex_height ) * (float)m_iPanelHeight) + (m_iScreenHeight - m_iPanelHeight);
	m_iRadarX2 = m_iRadarX1 + (( pData->GetFloat( "minimap_sizex") / tex_width )  * (float)m_iPanelWidth);
	m_iRadarY2 = m_iRadarY1 + (( pData->GetFloat( "minimap_sizey" ) / tex_height )  * (float)m_iPanelHeight);

	m_pMiniMap->SetPos( ( pData->GetFloat( "minimap_posx") / tex_width ) * (float)m_iPanelWidth, 
		 ( ( pData->GetFloat( "minimap_posy" ) / tex_height ) * (float)m_iPanelHeight) + (m_iScreenHeight - m_iPanelHeight) );
	m_pMiniMap->SetSize( ( pData->GetFloat( "minimap_sizex") / tex_width )  * (float)m_iPanelWidth, 
		( pData->GetFloat( "minimap_sizey" ) / tex_height )  * (float)m_iPanelHeight );

	// create unit panel
	m_pUnitPanel->SetPos( ( pData->GetFloat( "unit_posx") / tex_width ) * (float)m_iPanelWidth, 
		( pData->GetFloat( "unit_posy" ) / tex_height ) * (float)m_iPanelHeight );
	m_pUnitPanel->SetSize( ( pData->GetFloat( "unit_sizex") / tex_width )  * (float)m_iPanelWidth, 
		( pData->GetFloat( "unit_sizey" ) / tex_height ) * (float)m_iPanelHeight );
	m_pUnitPanel->CalculateSizePos();

	// create ability panel
	m_pAbilityPanel->SetPos( ( pData->GetFloat( "ability_posx") / tex_width ) * (float)m_iPanelWidth, 
		( pData->GetFloat( "ability_posy" ) / tex_height ) * (float)m_iPanelHeight );
	m_pAbilityPanel->SetSize( ( pData->GetFloat( "ability_sizex") / tex_width )  * (float)m_iPanelWidth, 
		( pData->GetFloat( "ability_sizey" ) / tex_height ) * (float)m_iPanelHeight );
	m_pAbilityPanel->CalculateSizePos();	// now we setted the size, we need to do the buttons of the ability panel

	// create building panel
	m_pBuildingPanel->SetPos( ( pData->GetFloat( "building_posx") / tex_width ) * (float)m_iPanelWidth, 
		( pData->GetFloat( "building_posy" ) / tex_height ) * (float)m_iPanelHeight );
	m_pBuildingPanel->SetSize( ( pData->GetFloat( "building_sizex") / tex_width )  * (float)m_iPanelWidth, 
		( pData->GetFloat( "building_sizey" ) / tex_height ) * (float)m_iPanelHeight );
	m_pBuildingPanel->CalculatePosSize();

	// create construction panel
	m_pConstructionPanel->SetPos( ( pData->GetFloat( "building_posx") / tex_width ) * (float)m_iPanelWidth, 
		( pData->GetFloat( "building_posy" ) / tex_height ) * (float)m_iPanelHeight );
	m_pConstructionPanel->SetSize( ( pData->GetFloat( "building_sizex") / tex_width )  * (float)m_iPanelWidth, 
		( pData->GetFloat( "building_sizey" ) / tex_height ) * (float)m_iPanelHeight );
	m_pConstructionPanel->CalculatePosSize();

	// -- set size and pos labels
	m_pAmountUnits->SetPos( ( pData->GetFloat( "AmountUnits_posx") / tex_width ) * (float)m_iPanelWidth, 
		( pData->GetFloat( "AmountUnits_posy" ) / tex_height ) * (float)m_iPanelHeight );
	m_pAmountUnits->SetSize( vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 45 ), 
		vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 20 ) );

	m_pAmountResources->SetPos( ( pData->GetFloat( "AmountResources_posx") / tex_width ) * (float)m_iPanelWidth, 
		( pData->GetFloat( "AmountResources_posy" ) / tex_height ) * (float)m_iPanelHeight );
	m_pAmountResources->SetSize( vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 45 ), 
		vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 20 ) );

	// position all buttons ( pfff )
	state_normal->SetPos( ( pData->GetFloat( "statenormal_posx") / tex_width )  * (float)m_iPanelWidth, 
		( pData->GetFloat( "statenormal_posy" ) / tex_height ) * (float)m_iPanelHeight );
    state_normal->SetSize( ( pData->GetFloat( "statenormal_sizex") / tex_width )   * (float)m_iPanelWidth, 
		( pData->GetFloat( "statenormal_sizey" ) / tex_height ) * (float)m_iPanelHeight );

	state_defense->SetPos( ( pData->GetFloat( "statedefense_posx") / tex_width )  * (float)m_iPanelWidth, 
		( pData->GetFloat( "statedefense_posy" ) / tex_height ) * (float)m_iPanelHeight );
    state_defense->SetSize( ( pData->GetFloat( "statedefense_sizex") / tex_width )   * (float)m_iPanelWidth, 
		( pData->GetFloat( "statedefense_sizey" ) / tex_height ) * (float)m_iPanelHeight );

	level_up->SetPos( ( pData->GetFloat( "levelup_posx") / tex_width )  * (float)m_iPanelWidth, 
		( pData->GetFloat( "levelup_posy" ) / tex_height ) * (float)m_iPanelHeight );
    level_up->SetSize( ( pData->GetFloat( "levelup_sizex") / tex_width )   * (float)m_iPanelWidth, 
		( pData->GetFloat( "levelup_sizey" ) / tex_height ) * (float)m_iPanelHeight );

	level_down->SetPos( ( pData->GetFloat( "leveldown_posx") / tex_width )  * (float)m_iPanelWidth, 
		( pData->GetFloat( "leveldown_posy" ) / tex_height ) * (float)m_iPanelHeight );
    level_down->SetSize( ( pData->GetFloat( "leveldown_sizex") / tex_width )   * (float)m_iPanelWidth, 
		( pData->GetFloat( "leveldown_sizey" ) / tex_height ) * (float)m_iPanelHeight );

	main_menu->SetPos( ( pData->GetFloat( "mainmenu_posx") / tex_width ) * (float)m_iPanelWidth, 
		( pData->GetFloat( "mainmenu_posy" ) / tex_height )  * (float)m_iPanelHeight );
    main_menu->SetSize( ( pData->GetFloat( "mainmenu_sizex") / tex_width )  * (float)m_iPanelWidth, 
		( pData->GetFloat( "mainmenu_sizey" ) / tex_height )  * (float)m_iPanelHeight );

	obj_button->SetPos( ( pData->GetFloat( "objbutton_posx") / tex_width ) * (float)m_iPanelWidth, 
		( pData->GetFloat( "objbutton_posy" ) / tex_height ) * (float)m_iPanelHeight );
    obj_button->SetSize( ( pData->GetFloat( "objbutton_sizex") / tex_width )  * (float)m_iPanelWidth, 
		( pData->GetFloat( "objbutton_sizey" ) / tex_height ) * (float)m_iPanelHeight );

	mp3_button->SetPos( ( pData->GetFloat( "mp3button_posx") / tex_width ) * (float)m_iPanelWidth, 
		( pData->GetFloat( "mp3button_posy" ) / tex_height ) * (float)m_iPanelHeight );
    mp3_button->SetSize( ( pData->GetFloat( "mp3button_sizex") / tex_width )  * (float)m_iPanelWidth, 
		( pData->GetFloat( "mp3button_sizey" ) / tex_height ) * (float)m_iPanelHeight );

	// clean up keyvalues
	pInterfaceMainData->deleteThis();
}

//-----------------------------------------------------------------------------
// Purpose: Paint
//-----------------------------------------------------------------------------
/*void CHudInterface::Paint( void )
{
	DrawStats();
}*/

//-----------------------------------------------------------------------------
// Purpose: Save CPU cycles by letting the HUD system early cull
// costly traversal.  Called per frame, return true if thinking and 
// painting need to occur.
//-----------------------------------------------------------------------------
/*bool CHudInterface::ShouldDraw( void )
{
	if( engine->IsLevelMainMenuBackground() )
		return false;

}*/

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudInterface::PaintBackground()
{
	DrawHudPanel();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudInterface::DrawHudPanel( void )
{
	if ( !m_pHudPanel )
		return;

	m_pHudPanel->DrawSelf( 0, 0, m_iPanelWidth, m_iPanelHeight, m_clrHudPanel);
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudInterface::UpdateStats( void )
{
	C_HL2Wars_Player* pPlayer = C_HL2Wars_Player::GetLocalHL2WarsPlayer();
	if( !pPlayer ) 
		return;

	// update amount of units
	int amountUnits = g_RTSR->GetSize( pPlayer->GetTeamRTSID() );
	char amountUnitsText[MAX_PATH];
	Q_snprintf( amountUnitsText, sizeof( amountUnitsText ), "%d / %d", amountUnits, sv_max_units.GetInt() );
	m_pAmountUnits->SetText( amountUnitsText );

	// update amount of resources
	int amountResources = pPlayer->m_HLWLocal.m_iResources;
	char amountResourcesText[MAX_PATH];
	Q_snprintf( amountResourcesText, sizeof( amountResourcesText ), "%d", amountResources );
	m_pAmountResources->SetText( amountResourcesText );
}

//-----------------------------------------------------------------------------
// Purpose: find out which panel we need to show in the middle ( unit, building 
//			or capture point )
//-----------------------------------------------------------------------------
void CHudInterface::UpdateMiddlePanel()
{
	C_HL2Wars_Player* pPlayer = C_HL2Wars_Player::GetLocalHL2WarsPlayer();
	if( !pPlayer ) 
		return;

	switch( pPlayer->GetSelectionType() ) {
		case SELECTION_UNIT:
			m_pUnitPanel->SetVisible( true );
			m_pBuildingPanel->SetVisible( false );
			m_pConstructionPanel->SetVisible( false );
			break;
		case SELECTION_BUILDING:
			m_pUnitPanel->SetVisible( false );
			if( pPlayer->GetSelectedBuilding() && pPlayer->GetSelectedBuilding()->IsRTSModel() )
			{
				C_BaseBuildingModel *m_pSelected = dynamic_cast<C_BaseBuildingModel *>(pPlayer->GetSelectedBuilding());
				if( !m_pSelected || !m_pSelected->IsAlive() )
					return;

				if( m_pSelected->IsUnderConstruction() )
				{
					m_pBuildingPanel->SetVisible( false );
					m_pConstructionPanel->SetVisible( true );
				}
				else
				{	
					m_pConstructionPanel->SetVisible( false );
					m_pBuildingPanel->SetVisible( true );
				}
			}
			else
			{	
				m_pBuildingPanel->SetVisible( true );
			}
			break;
		default:
			m_pUnitPanel->SetVisible( true );
			DevMsg("Invalid selection type! \n");
			break;
	}
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CHudInterface::UpdateBehaviorButtons( void )
{
	C_HL2Wars_Player* pPlayer = C_HL2Wars_Player::GetLocalHL2WarsPlayer();
	if( !pPlayer ) 
		return;

	color32 color;
	color.r = color.g = color.b = color.a = 1.0f;

	if( pPlayer->m_HLWLocal.m_nBehaviorButtonsState & IN_BUTTON_NORMAL )
		state_normal->SetImage( CBitmapButtonRTS::BUTTON_ENABLED, &m_pImageStatesNormal[1], color);
	else
		state_normal->SetImage( CBitmapButtonRTS::BUTTON_ENABLED, &m_pImageStatesNormal[0], color);

	if( pPlayer->m_HLWLocal.m_nBehaviorButtonsState & IN_BUTTON_AGGRESSIVE )
		state_defense->SetImage( CBitmapButtonRTS::BUTTON_ENABLED, &m_pImageStatesAggressive[1], color);
	else
		state_defense->SetImage( CBitmapButtonRTS::BUTTON_ENABLED, &m_pImageStatesAggressive[0], color);

}

//-----------------------------------------------------------------------------
// Purpose: Handles things like behavior buttons
//-----------------------------------------------------------------------------
void CHudInterface::OnCommand(const char *command) 
{ 
	if (!stricmp(command, "state_normal")) 
	{ 
		C_HL2Wars_Player* pPlayer = C_HL2Wars_Player::GetLocalHL2WarsPlayer();
		if( !pPlayer ) 
			return;

		int *m_nNewRTSCommands2 = pPlayer->GetRTSCommands2();
		*m_nNewRTSCommands2 |= IN_BEHAVIOR_NORM;
	} 
	else if( !stricmp(command, "state_aggressive") )
	{
		C_HL2Wars_Player* pPlayer = C_HL2Wars_Player::GetLocalHL2WarsPlayer();
		if( !pPlayer ) 
			return;

		int *m_nNewRTSCommands2 = pPlayer->GetRTSCommands2();
		*m_nNewRTSCommands2 |= IN_BEHAVIOR_AGG;
	}
	else if( !stricmp(command, "level_up") ) {
		C_HL2Wars_Player* pPlayer = C_HL2Wars_Player::GetLocalHL2WarsPlayer();
		if( !pPlayer ) 
			return;

		int *m_nNewRTSCommands2 = pPlayer->GetRTSCommands2();
		*m_nNewRTSCommands2 |= IN_LEVELUP;
	}
	else if( !stricmp(command, "level_down") ) {
		C_HL2Wars_Player* pPlayer = C_HL2Wars_Player::GetLocalHL2WarsPlayer();
		if( !pPlayer ) 
			return;

		// we can't go lower then 0
		if( pPlayer->m_HLWLocal.m_iLevelRTS == 0 )
			return;

		int *m_nNewRTSCommands2 = pPlayer->GetRTSCommands2();
		*m_nNewRTSCommands2 |= IN_LEVELDOWN;
	}
	else if( !stricmp(command, "main_menu") ) {
		//C_HL2Wars_Player* pPlayer = C_HL2Wars_Player::GetLocalHL2WarsPlayer();
		//if( !pPlayer ) 
		//	return;

		//::input->KeyEvent(1, KEY_ESCAPE, NULL);
		//engine->Key_Event( KEY_ESCAPE, 1 );
		//clientdll->IN_KeyEvent(0, KEY_ESCAPE, NULL);
		//engine->ExecuteClientCmd("escape");
	}
	else if( !stricmp(command, "obj_button") ) {
		engine->ClientCmd("showobjectives");
	}
	else if( !stricmp(command, "mp3_button") ) {
		engine->ClientCmd("mp3");
	}


	BaseClass::OnCommand(command); 
} 

//-----------------------------------------------------------------------------
// Purpose: Create all buttons
//-----------------------------------------------------------------------------
void CHudInterface::CreateButtons( )
{
	// first load the images

	m_pImageStatesNormal[0].Init( GetVPanel(), "VGUI/buttons/behaviour_normalButton_off.vmt");
	m_pImageStatesNormal[1].Init( GetVPanel(), "VGUI/buttons/behaviour_normalButton_on.vmt");
	m_pImageStatesNormal[2].Init( GetVPanel(), "VGUI/buttons/behaviour_normalButton_down.vmt");
	m_pImageStatesNormal[3].Init( GetVPanel(), "VGUI/buttons/behaviour_normalButton_over.vmt");

	m_pImageStatesAggressive[0].Init( GetVPanel(), "VGUI/buttons/behaviour_runButton_off.vmt");
	m_pImageStatesAggressive[1].Init( GetVPanel(), "VGUI/buttons/behaviour_runButton_on.vmt");
	m_pImageStatesAggressive[2].Init( GetVPanel(), "VGUI/buttons/behaviour_runButton_down.vmt");
	m_pImageStatesAggressive[3].Init( GetVPanel(), "VGUI/buttons/behaviour_runButton_over.vmt");
	/*m_pImageStates[0].Init( GetVPanel(), "VGUI/buttons/behaviour_normalButton.vmt");
	m_pImageStates[1].Init( GetVPanel(), "VGUI/buttons/behaviour_runButton.vmt");
	//m_pImageStates[2].Init( GetVPanel(), "VGUI/buttons/behaviour_runButton.vmt");

	m_pImageStatesDown[0].Init( GetVPanel(), "VGUI/buttons/behaviour_normalButton_down.vmt");
	m_pImageStatesDown[1].Init( GetVPanel(), "VGUI/buttons/behaviour_runButton_down.vmt");
	//m_pImageStatesDown[2].Init( GetVPanel(), "VGUI/buttons/behaviour_runButton_down.vmt");

	m_pImageStatesOver[0].Init( GetVPanel(), "VGUI/buttons/behaviour_normalButton_over.vmt");
	m_pImageStatesOver[1].Init( GetVPanel(), "VGUI/buttons/behaviour_runButton_over.vmt");
	//m_pImageStatesOver[2].Init( GetVPanel(), "VGUI/buttons/behaviour_runButton_over.vmt");*/

	m_pImageLevels[0].Init( GetVPanel(), "VGUI/buttons/level_down.vmt");
	m_pImageLevels[1].Init( GetVPanel(), "VGUI/buttons/level_up.vmt");
	m_pImageLevelsDown[0].Init( GetVPanel(), "VGUI/buttons/level_down.vmt");
	m_pImageLevelsDown[1].Init( GetVPanel(), "VGUI/buttons/level_up.vmt");
	m_pImageLevelsOver[0].Init( GetVPanel(), "VGUI/buttons/level_down.vmt");
	m_pImageLevelsOver[1].Init( GetVPanel(), "VGUI/buttons/level_up.vmt");

	m_pImageMenu[0].Init( GetVPanel(), "VGUI/buttons/mm_button.vmt" );
	m_pImageMenu[1].Init( GetVPanel(), "VGUI/buttons/mm_button_onOver.vmt" );
	m_pImageMenu[2].Init( GetVPanel(), "VGUI/buttons/mm_button_onPressed.vmt" );
	m_pImageobj[0].Init( GetVPanel(), "VGUI/buttons/obj_button.vmt" );
	m_pImageobj[1].Init( GetVPanel(), "VGUI/buttons/obj_button_onOver.vmt" );
	m_pImageobj[2].Init( GetVPanel(), "VGUI/buttons/obj_button_onPressed.vmt" );
	m_pImagemp3[0].Init( GetVPanel(), "VGUI/buttons/mp3player_button.vmt" );
	m_pImagemp3[1].Init( GetVPanel(), "VGUI/buttons/mp3player_button_onOver.vmt" );
	m_pImagemp3[2].Init( GetVPanel(), "VGUI/buttons/mp3player_button_onPressed.vmt" );

	color32 color;
	color.r = color.g = color.b = color.a = 1.0f;

	// normal behavior button
	state_normal = new CBitmapButtonRTS( this, "normal", ""); 
	state_normal->SetImage( CBitmapButtonRTS::BUTTON_ENABLED, &m_pImageStatesNormal[0], color);
	state_normal->SetImage( CBitmapButtonRTS::BUTTON_ENABLED_MOUSE_OVER, &m_pImageStatesNormal[3], color);
	state_normal->SetImage( CBitmapButtonRTS::BUTTON_PRESSED, &m_pImageStatesNormal[2], color);
	state_normal->SetVisible( true );
	state_normal->SetPaintBackgroundEnabled( false );
	state_normal->SetPaintBorderEnabled( false );
	state_normal->SetParent(this);
	state_normal->AddActionSignalTarget(this);
	state_normal->SetCommand("state_normal");

	Tooltip *tool = state_normal->GetTooltip();
	tool->SetText("Set your selected units to normal behavior");

	// defense behavior button
	state_defense = new CBitmapButtonRTS( this, "aggressive", ""); 
	state_defense->SetImage( CBitmapButtonRTS::BUTTON_ENABLED, &m_pImageStatesAggressive[0], color);
	state_defense->SetImage( CBitmapButtonRTS::BUTTON_ENABLED_MOUSE_OVER, &m_pImageStatesAggressive[3], color);
	state_defense->SetImage( CBitmapButtonRTS::BUTTON_PRESSED, &m_pImageStatesAggressive[2], color);
	state_defense->SetVisible( true );
	state_defense->SetPaintBackgroundEnabled( false );
	state_defense->SetPaintBorderEnabled( false );
	state_defense->SetParent(this);
	state_defense->AddActionSignalTarget(this);
	state_defense->SetCommand("state_aggressive");

	tool = state_defense->GetTooltip();
	tool->SetText("Set your selected units to aggresive behavior");

	// level up or down
	level_up = new CBitmapButtonRTS( this, "levelup", ""); 
	level_up->SetImage( CBitmapButtonRTS::BUTTON_ENABLED, &m_pImageLevels[1], color);
	level_up->SetImage( CBitmapButtonRTS::BUTTON_ENABLED_MOUSE_OVER, &m_pImageLevelsDown[1], color);
	level_up->SetImage( CBitmapButtonRTS::BUTTON_PRESSED, &m_pImageLevelsOver[1], color);
	level_up->SetVisible( true );
	level_up->SetPaintBackgroundEnabled( false );
	level_up->SetPaintBorderEnabled( false );
	level_up->SetParent(this);
	level_up->AddActionSignalTarget(this);
	level_up->SetCommand("level_up");

	level_down = new CBitmapButtonRTS( this, "leveldown", ""); 
	level_down->SetImage( CBitmapButtonRTS::BUTTON_ENABLED, &m_pImageLevels[0], color);
	level_down->SetImage( CBitmapButtonRTS::BUTTON_ENABLED_MOUSE_OVER, &m_pImageLevelsDown[0], color);
	level_down->SetImage( CBitmapButtonRTS::BUTTON_PRESSED, &m_pImageLevelsOver[0], color);
	level_down->SetVisible( true );
	level_down->SetPaintBackgroundEnabled( false );
	level_down->SetPaintBorderEnabled( false );
	level_down->SetParent(this);
	level_down->AddActionSignalTarget(this);
	level_down->SetCommand("level_down");

	main_menu = new CBitmapButtonRTS( this, "main_menu", ""); 
	main_menu->SetImage( CBitmapButtonRTS::BUTTON_ENABLED, &m_pImageMenu[0], color);
	main_menu->SetImage( CBitmapButtonRTS::BUTTON_ENABLED_MOUSE_OVER, &m_pImageMenu[1], color);
	main_menu->SetImage( CBitmapButtonRTS::BUTTON_PRESSED, &m_pImageMenu[2], color);
	main_menu->SetVisible( true );
	main_menu->SetPaintBackgroundEnabled( false );
	main_menu->SetPaintBorderEnabled( false );
	main_menu->SetParent(this);
	main_menu->AddActionSignalTarget(this);
	main_menu->SetCommand("main_menu");

	obj_button = new CBitmapButtonRTS( this, "obj_button", ""); 
	obj_button->SetImage( CBitmapButtonRTS::BUTTON_ENABLED, &m_pImageobj[0], color);
	obj_button->SetImage( CBitmapButtonRTS::BUTTON_ENABLED_MOUSE_OVER, &m_pImageobj[1], color);
	obj_button->SetImage( CBitmapButtonRTS::BUTTON_PRESSED, &m_pImageobj[2], color);
	obj_button->SetVisible( true );
	obj_button->SetPaintBackgroundEnabled( false );
	obj_button->SetPaintBorderEnabled( false );
	obj_button->SetParent(this);
	obj_button->AddActionSignalTarget(this);
	obj_button->SetCommand("obj_button");

	mp3_button = new CBitmapButtonRTS( this, "mp3_button", ""); 
	mp3_button->SetImage( CBitmapButtonRTS::BUTTON_ENABLED, &m_pImagemp3[0], color);
	mp3_button->SetImage( CBitmapButtonRTS::BUTTON_ENABLED_MOUSE_OVER, &m_pImagemp3[1], color);
	mp3_button->SetImage( CBitmapButtonRTS::BUTTON_PRESSED, &m_pImagemp3[2], color);
	mp3_button->SetVisible( true );
	mp3_button->SetPaintBackgroundEnabled( false );
	mp3_button->SetPaintBorderEnabled( false );
	mp3_button->SetParent(this);
	mp3_button->AddActionSignalTarget(this);
	mp3_button->SetCommand("mp3_button");
}

// mouse listeners for the panel
void CHudInterface::OnCursorMoved(int x, int y)
{
	int xpos, ypos;
	::input->GetFullscreenMousePosRTS(&xpos, &ypos);
	if( xpos > m_iRadarX1 && xpos < m_iRadarX2 && ypos > m_iRadarY1 && ypos < m_iRadarY2 )
	{
		m_pMiniMap->OnCursorMoved( xpos - m_iRadarX1 , ypos - m_iRadarY1 );
		return;
	}
}

void CHudInterface::OnCursorEntered()
{
}

void CHudInterface::OnCursorExited()
{
}

//-----------------------------------------------------------------------------
// Purpose:  
//-----------------------------------------------------------------------------
void CHudInterface::OnMousePressed(MouseCode code)
{
	int x, y;
	::input->GetFullscreenMousePosRTS(&x, &y);
	if( x > m_iRadarX1 && x < m_iRadarX2 && y > m_iRadarY1 && y < m_iRadarY2 )
	{
		m_pMiniMap->OnMousePressed( code );
		return;
	}

	C_HL2Wars_Player *pPlayer = C_HL2Wars_Player::GetLocalHL2WarsPlayer();
	if( !pPlayer )
		return;

	pPlayer->SetActivePanel( this );
}

//-----------------------------------------------------------------------------
// Purpose:  
//-----------------------------------------------------------------------------
void CHudInterface::OnMouseDoublePressed(MouseCode code)
{
	int x, y;
	::input->GetFullscreenMousePosRTS(&x, &y);
	if( x > m_iRadarX1 && x < m_iRadarX2 && y > m_iRadarY1 && y < m_iRadarY2 )
	{
		m_pMiniMap->OnMouseDoublePressed( code );
		return;
	}
}

//-----------------------------------------------------------------------------
// Purpose: FIXME: the radar doesn't gets released sometimes. 
//-----------------------------------------------------------------------------
void CHudInterface::OnMouseReleased(MouseCode code)
{
	C_HL2Wars_Player *pPlayer = C_HL2Wars_Player::GetLocalHL2WarsPlayer();
	if( !pPlayer )
		return;

	// check both panels here, don't do this in the minimap. Otherwise a potential crash
	// when clicking a lot around the radar.
	if( !pPlayer->IsActivePanel( this ) && !pPlayer->IsActivePanel( m_pMiniMap ) )
	{
		if( pPlayer->ReleaseActivePanel( code ) )
			return;
	}	

	// check if need to redirect to radar
	int x, y;
	::input->GetFullscreenMousePosRTS(&x, &y);
	if( x > m_iRadarX1 && x < m_iRadarX2 && y > m_iRadarY1 && y < m_iRadarY2 )
	{
		m_pMiniMap->OnMouseReleased( code );
		return;
	}

	pPlayer->SetActivePanel( NULL );
}

//-----------------------------------------------------------------------------
// Purpose: Shows the current objectives
//-----------------------------------------------------------------------------
void CHudInterface::UserCmd_ShowObjectives(void)
{
	if( !m_pObjectivesFrame->IsVisible() ) {
		m_pObjectivesFrame->ShowPanel( true );
		m_pObjectivesFrame->MoveToFront();
	}
	else {
		m_pObjectivesFrame->ShowPanel( false );
	}
}

//-----------------------------------------------------------------------------
// Purpose: Kill selected units
//-----------------------------------------------------------------------------
void CHudInterface::UserCmd_KillSelected(void)
{
	engine->ServerCmd("hl2wars_killselected");	
}

//-----------------------------------------------------------------------------
// Purpose: Cancels the current given order of the selected units
//-----------------------------------------------------------------------------
void CHudInterface::UserCmd_OrderCancel(void)
{
	engine->ServerCmd("hl2wars_orderstop");	
}

//-----------------------------------------------------------------------------
// Purpose: Receive a message for a specific player about the tech tree 
//			Pass it through to the global resource
//-----------------------------------------------------------------------------
void CHudInterface::MsgFunc_AbilityStateUpdate( bf_read &msg )
{
	if( m_pAbilityPanel )
		m_pAbilityPanel->AbilityStateUpdate( msg );
	else
		Warning("NO m_pAbilityPanel\n");
}