/*********************************************************************
*	HUD Value holder
*	SOURCE FILE
*	Autor:	Michal Jirouš
*	Datum: 10.9.2008
*	Soubor: HUD_valueholder.cpp
*	Popis: Tento prvek se pouziva k animaci vykreslovani ciselnych hodnot
*			u HUD.
**********************************************************************/

#include "HUD.h"
#include "globaltime.h"
#include <sstream>

using namespace HUD;

HUDValueHolder::HUDValueHolder()
{
	m_iAnimStepLeft = 0;
	m_iValue = 0;
	m_uiTimePeriod = 0;
	m_uiBackTime = 0;
	m_bValueSet = false;
}

void HUDValueHolder::setString()
{
	ostringstream buf;
	buf << m_iValue;
	m_sValue = buf.str();
}

void HUDValueHolder::saveValue( int value )
{
	m_iValue = value;
	setString();
}

void HUDValueHolder::setValue( int value )
{
	if( m_iValue != value )
	{
		saveValue( value );
		determineTargetColor( m_ColorCurrent );
	}
}

void HUDValueHolder::reInitValue()
{
	m_bValueSet = false;
}

void HUDValueHolder::setValueHighLighted( int value )
{
	if( !m_bValueSet )
	{
		m_bValueSet = true;
		saveValue( value );
		determineTargetColor( m_ColorCurrent );
	}
	else if( m_iValue != value )
	{
		saveValue( value );
		m_fScaleValue = HUD_VALUE_HOLDER_ANIM_MAX;
		m_fScaleStep = ( HUD_VALUE_HOLDER_ANIM_MAX - HUD_VALUE_HOLDER_ANIM_MIN ) / (float)HUD_VALUE_HOLDER_ANIM_NUM_STEPS;
		m_uiTimePeriod = global_time::getGlobalTime();
		m_uiBackTime = global_time::getGlobalTime();
		m_ColorCurrent = m_ColorHighLighted;
		m_iAnimStepLeft = HUD_VALUE_HOLDER_ANIM_NUM_STEPS;
		createColorChangeVector();
	}
}

void HUDValueHolder::determineTargetColor( Color4I &store_here )
{
	if( m_iValue > m_iCriticalValue )
		store_here = m_ColorDefault;
	else
		store_here = m_ColorCritical;
}

void HUDValueHolder::createColorChangeVector()
{
	Color4I targetColor;
	determineTargetColor( targetColor );
	
	for( int i = 0; i < 4; i++ )
		m_vecColorChange.values[i] = ( targetColor.values[i] - m_ColorHighLighted.values[i] ) / (float)HUD_VALUE_HOLDER_ANIM_NUM_STEPS;

}

void HUDValueHolder::update()
{
	for( int i = 0; i < 4; i++ )
		m_ColorCurrent.values[i] += m_vecColorChange.values[i];
	m_fScaleValue -= m_fScaleStep;
}


void HUDValueHolder::draw( float x, float y, std::string font, float aplha  )
{
	Font *tmpFont = fontLibrary::getFont( font );
	if( !tmpFont )
		return;
	
	int fWidth = 0;
	for( size_t i = 0; i < m_sValue.length(); i++ )
		fWidth += tmpFont->getCharacterWidth( m_sValue.at( i ) );

	glColor4f( m_ColorCurrent.values[0], m_ColorCurrent.values[1], m_ColorCurrent.values[2], m_ColorCurrent.values[3]* aplha );
	
	if( m_fScaleValue > 1.0f )
	{
		glPushMatrix();
		glTranslatef( x, y, 0.0f );
		glScalef( m_fScaleValue, m_fScaleValue, 0.0f );

		fontLibrary::simpleDrawTextFloated( m_sValue,
			-(((float)fWidth - (float)fWidth / m_fScaleValue) / 2.0f),
			-(((float)tmpFont->m_iLetterHeight - (float)tmpFont->m_iLetterHeight / m_fScaleValue) / 2.0f), tmpFont );
		
		glPopMatrix();
	}
	else
		fontLibrary::simpleDrawText( m_sValue, x, y, tmpFont );
}

void HUDValueHolder::run()
{
	if( m_uiBackTime + HUD_VALUE_HOLDER_BACK_TIME_PERIOD < global_time::getGlobalTime() )
	{
		if( m_uiTimePeriod + HUD_VALUE_HOLDER_TIME_PERIOD < global_time::getGlobalTime() )
		{
			m_uiTimePeriod = global_time::getGlobalTime();

			if( m_iAnimStepLeft > 0 )
			{
				m_iAnimStepLeft--;
				update();
			}
		}
	}
}
