//////////////////////////////////////////////////////////////////////////////
//
// Asura_EffectVolume_EffectState.cpp
//
// Copyright (c) 2008 Rebellion. All rights reserved.
//
// Created by Emil Popovic - 19/12/2008
//
//////////////////////////////////////////////////////////////////////////////

// I N C L U D E S ///////////////////////////////////////////////////////////

// Core
#include "Core/Asura.h"

// This file's header.
#include "Asura_EffectVolume_EffectState.h"

// Engine independent.
#include "FSFX/Asura_FSFX_System.h"

// Tools
#ifdef ASURA_TOOLS
#include "Tools_System.h"
#endif



#if defined(ASURA_USE_FSFX)

Asura_EffectVolume_EffectState_FSFX::Asura_EffectVolume_EffectState_FSFX() 
: PARENT( uEFFECTVOLUME_EFFECTSTATE_FSFX )
{
}

void Asura_EffectVolume_EffectState_FSFX::BlendStrengthOnly( float fStrength )
{
	// Also don't forget to set correct strength to the effect. The final strength is multiplied 
	// strength of Effect Global Strength and Effect Parameter Strength.
	float fFinalStrength = m_xTemplateParameter.GetStrength() * fStrength;

	m_xTemplateParameter.SetStrength( fFinalStrength );
}


void Asura_EffectVolume_EffectState_FSFX::ApplyToCamera( u_int uCameraIndex )
{
	Asura_Hash_ID uTemplateHash = m_xTemplateParameter.GetTemplateHash();

	if( uTemplateHash!=ASURA_HASH_ID_UNSET )
	{
		float fStrength = m_xTemplateParameter.GetStrength();

//		float fExistingStrength = Asura_FSFX_System::GetFirstValueFromAllHandlers( uTemplateHash );

//		Currently this blend type is disabled. It is because Asura_FSFX_System is not refreshing effect systems every frame, so this algorithm will take value from previous frame. So most commonly the value is bigger then current value, therefore no blending will happen. Instead the last frame value will be used.
//		if( fStrength<fExistingStrength )		// Use the highest value for blend.
//		{
//			fStrength = fExistingStrength;
//		}

		Asura_FSFX_System::SetValueForAllHandlers( uTemplateHash, fStrength, 0.0f );
	}
}

bool Asura_EffectVolume_EffectState_FSFX::Blend( Asura_EffectVolume_EffectState& xNewEffectState, float fStrength )
{
	// In case that this effect is empty, we can simply copy new effect to this one.
	if( m_xTemplateParameter.GetTemplateHash()==ASURA_HASH_ID_UNSET )
	{
		Asura_EffectVolume_EffectState_FSFX& xCurrentFSFX = static_cast< Asura_EffectVolume_EffectState_FSFX& >( xNewEffectState );

		xCurrentFSFX.BlendStrengthOnly( fStrength );

		m_xTemplateParameter = xCurrentFSFX.m_xTemplateParameter;
		return true;
	}
	

	// We can't truly blend the effect. Because we want to apply all effects to final render. So
	// do nothing, but instead add this effect to the list. 
	return false;

}

#endif	//defined(ASURA_USE_FSFX)

//----------------------------------------------------------------------------------------------------------------------------

Asura_EffectVolume_EffectState_Fog::Asura_EffectVolume_EffectState_Fog() 
: PARENT( uEFFECTVOLUME_EFFECTSTATE_FOG )
{
}

void Asura_EffectVolume_EffectState_Fog::InitialiseToCurrentExistingFog()
{
	m_xFogParameters = Asura_Fog::GetFog();
}

void Asura_EffectVolume_EffectState_Fog::ApplyToCamera( u_int uCameraIndex )
{
	Asura_Fog::SetFog( m_xFogParameters );
}

bool Asura_EffectVolume_EffectState_Fog::Blend( Asura_EffectVolume_EffectState& xNewEffectState, float fStrength )
{
	Asura_EffectVolume_EffectState_Fog& xCurrentFog = static_cast< Asura_EffectVolume_EffectState_Fog& >( xNewEffectState );

	m_xFogParameters.BlendParameters( xCurrentFog.m_xFogParameters, fStrength );

	// Blend succeeded. Do not continue.
	return true;
}


//----------------------------------------------------------------------------------------------------------------------------

void Asura_EffectVolume_EffectState::ApplyToCamera( u_int uCameraIndex )
{
}

bool Asura_EffectVolume_EffectState::Blend( Asura_EffectVolume_EffectState& xNewEffectState, float fStrength )
{
	// Blend failed. Continue blending with next one.
	return false;
}


Asura_EffectVolume_EffectState::Asura_EffectVolume_EffectState( Asura_Hash_ID uEffectTypeHash )
: m_bHashData(ASURA_HASH_ID_UNSET)
, m_uEffectTypeHash(uEffectTypeHash)
{
}

Asura_EffectVolume_EffectState::~Asura_EffectVolume_EffectState()
{

}
