//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose: This is a panel which is rendered image on top of an entity
//
// $Revision: $
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
//#pragma warning (disable: 4514)

#include "vgui_bitmapimage.h"
#include "hl2wars/vgui_BitmapButtonrts.h"
#include <KeyValues.h>
#include <vgui/Cursor.h>
#include <vgui/ISurface.h>
#include <vgui/IScheme.h>

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

//-----------------------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------------------
CBitmapButtonRTS::CBitmapButtonRTS( vgui::Panel *pParent, const char *pName, const char *pText ) : 
	BaseClass( pParent, pName, pText )
{
	SetPaintBackgroundEnabled( false );
	for ( int i = 0; i < BUTTON_STATE_COUNT; ++i )
	{
		m_bImageLoaded[i] = false;
	}

	SetCursor( GetParent()->GetCursor() );

	m_iButtonFlags = 0;

	m_iAmount = 0;

	SetScheme("ClientScheme.res");
	vgui::IScheme *pScheme = vgui::scheme()->GetIScheme(GetScheme()); 
	m_hNumberFont = pScheme->GetFont( "Default" ); 

	m_bIsArmedIgnoreEnabled = false;
	_actionMessage2 = NULL;
}

CBitmapButtonRTS::~CBitmapButtonRTS()
{
	if( _actionMessage2 )
		_actionMessage2->deleteThis();
}


//-----------------------------------------------------------------------------
// initialization
//-----------------------------------------------------------------------------
bool CBitmapButtonRTS::Init( KeyValues* pInitData )
{
	// Unimplemented
	Assert( 0 );
	return true;
}


//-----------------------------------------------------------------------------
// initialization from build-mode dialog style .res files
//-----------------------------------------------------------------------------
void CBitmapButtonRTS::ApplySettings(KeyValues *pInitData)
{
	BaseClass::ApplySettings(pInitData);

	/*COMPILE_TIME_ASSERT( BUTTON_STATE_COUNT == 4 );
	const char *pSectionName[BUTTON_STATE_COUNT] = 
	{
		"enabledImage",
		"mouseOverImage",
		"pressedImage",
		"disabledImage"
	};

	for ( int i = 0; i < BUTTON_STATE_COUNT; ++i )
	{
		m_bImageLoaded[i] = InitializeImage(pInitData, pSectionName[i], this, m_pImage[i] );
	}*/
}


//-----------------------------------------------------------------------------
// Sets the image
//-----------------------------------------------------------------------------
void CBitmapButtonRTS::SetImage( ButtonImageType_t type, BitmapImage *pMaterialImage, color32 color )
{
	m_pImage[type] = pMaterialImage;
	if( m_pImage[type] != NULL)
		m_bImageLoaded[type] = true;
	else
		m_bImageLoaded[type] = false;
	if (m_bImageLoaded[type])
	{
		Color vcol( color.r, color.g, color.b, color.a );
		m_pImage[type]->SetColor( vcol );
	}
}

bool CBitmapButtonRTS::IsImageLoaded( ButtonImageType_t type ) const
{
	return m_bImageLoaded[type];
}


//-----------------------------------------------------------------------------
// Draws the puppy
//-----------------------------------------------------------------------------
void CBitmapButtonRTS::Paint( void )
{
	int nCurrentImage = BUTTON_ENABLED;

	// do we change images ?
	if( !HasButtonFlags( F_ONEIMAGE ) )
	{
		if (IsArmed())
		{
			if (IsDepressed())
			{
				nCurrentImage = BUTTON_PRESSED;
			}
			else
			{
				nCurrentImage = BUTTON_ENABLED_MOUSE_OVER;
			}
		}
	}

	// draw the healthbar behind the image
	if( HasButtonFlags( F_SHOWHEALTHBAR ) )
	{ 
		// calculate the colors
		int red = 255 - (int)(m_fHealth * 255.0f);
		int green = (int)(m_fHealth * 255.0f);

		int x, y, xpos, ypos, width, ypos2;
		GetSize( x, y );

		xpos = 0.0625 * x;
		ypos = 0.8125 * y;
		ypos2 = 0.9375 * y;
		width = x - ( 2 * 0.0625 * x );

		// draw how much health we still got
		vgui::surface()->DrawSetColor( Color( red, green, 0, 255 ) );
		vgui::surface()->DrawFilledRect( xpos, ypos, 
			xpos + m_fHealth * width, ypos2 );		
	}

	// if not enabled, we always change the image to disabled version
	if (!IsEnabled())
	{
		nCurrentImage = BUTTON_DISABLED;
	}

	// paint the image
	if (m_bImageLoaded[nCurrentImage])
	{
		m_pImage[nCurrentImage]->DoPaint( GetVPanel() );
	}

	BaseClass::Paint();

	// show progress
	if( HasButtonFlags( F_SHOWPROGRESS ) )
	{
		int x, y;
		GetSize( x, y );
		vgui::surface()->DrawSetColor( Color( 0, 0, 230, 125 ) );
		vgui::surface()->DrawFilledRect( 0, 0, x * m_fProgress, y );
	}

	if( HasButtonFlags( F_SELECTED ) )
	{
		int x, y;
		GetSize( x, y );

		if( IsDepressed() ) {
			vgui::surface()->DrawSetTexture( m_pImageSelected );
		}
		else {
			vgui::surface()->DrawSetTexture( m_pImageHover );
		}

		vgui::surface()->DrawTexturedRect( 0, 0, x, y );
	}

	if (IsArmed() && !HasButtonFlags( F_SELECTED ) )
	{	
		if( !IsDepressed() ) {
			int x, y;
			GetSize( x, y );
			vgui::surface()->DrawSetTexture( m_pImageHover );
			vgui::surface()->DrawTexturedRect( 0, 0, x, y );
		}
	}

	if( HasButtonFlags( F_SHOWAMOUNT ) )
	{
		if( m_iAmount > 0 )
			PaintNumbers( m_hNumberFont, 8, 5, m_iAmount );
	}
}

//-----------------------------------------------------------------------------
// Purpose: paints a number at the specified position
//-----------------------------------------------------------------------------
void CBitmapButtonRTS::PaintNumbers(vgui::HFont font, int xpos, int ypos, int value)
{
	vgui::surface()->DrawSetTextColor( Color( 255, 255, 255, 200 ) );
	vgui::surface()->DrawSetTextFont(font);
	wchar_t unicode[6];
	swprintf(unicode, L"%d", value);
	// adjust the position to take into account 3 characters

	vgui::surface()->DrawSetTextPos(xpos, ypos);
	vgui::surface()->DrawUnicodeString( unicode );
}


void CBitmapButtonRTS::SetHighLightImages( const char *pMaterialImageHover, const char *pMaterialImageSelected )
{
	//m_pImageHover = pMaterialImageHover;
	m_pImageHover = vgui::surface()->CreateNewTextureID(); 
	vgui::surface()->DrawSetTextureFile( m_pImageHover, pMaterialImageHover , true, false); 

	//m_pImageSelected = pMaterialImageSelected; 
	m_pImageSelected = vgui::surface()->CreateNewTextureID(); 
	vgui::surface()->DrawSetTextureFile( m_pImageSelected, pMaterialImageSelected , true, false); 
}

void CBitmapButtonRTS::SetProgress( float weight )
{
	m_fProgress = weight;
}

void CBitmapButtonRTS::SetHealth( float weight )
{
	m_fHealth = weight;
}

void CBitmapButtonRTS::SetAmount( int amount )
{
	m_iAmount = amount;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CBitmapButtonRTS::OnMouseDoublePressed( vgui::MouseCode code)
{
	BaseClass::OnMouseDoublePressed( code );

	// message-based action signal
	if (_actionMessage2)
		PostActionSignal(_actionMessage2->MakeCopy());
}

//-----------------------------------------------------------------------------
// Purpose: sets the command to send when the button is pressed
//-----------------------------------------------------------------------------
void CBitmapButtonRTS::SetCommand2( const char *command )
{
	SetCommand2(new KeyValues("Command", "command", command));
}

//-----------------------------------------------------------------------------
// Purpose: sets the message to send when the button is pressed
//-----------------------------------------------------------------------------
void CBitmapButtonRTS::SetCommand2( KeyValues *message )
{
	// delete the old message
	if (_actionMessage2)
	{
		_actionMessage2->deleteThis();
	}

	_actionMessage2 = message;
}


void CBitmapButtonRTS::OnMouseReleased(vgui::MouseCode code)
{
	BaseClass::OnMouseReleased( code );

	if( GetParent() )	// let our parent handle this
		GetParent()->OnMouseReleased( code );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CBitmapButtonRTS::OnCursorEntered()
{
	BaseClass::OnCursorEntered();

	m_bIsArmedIgnoreEnabled = true;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CBitmapButtonRTS::OnCursorExited()
{
	BaseClass::OnCursorExited();

	m_bIsArmedIgnoreEnabled = false;
}

