//===========================================================================//
// File:	Blanderizer.hpp                                                 
// Project: MechWarrior 4
// Contents: 
//			AngleAxis class for animation.  This is just an unlimited rotation
//	quaternion
//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               //
// --------  ---  ---------------------------------------------------------- //
// 06/02/99  JSE  Initial coding, 
//---------------------------------------------------------------------------//
// Copyright (C) 1998, Fasa Interactive, Inc.
// All Rights reserved worldwide
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL
//===========================================================================//

#pragma once

namespace MechWarrior4 {


	//##########################################################################
	//#########################    Blanderizer    ###############################
	//##########################################################################
	
	template <class T> class BlanderizerOf 
	{
	public:
				
		BlanderizerOf()
		{
			currentTime = 0.0f;
			fadeTime = 0.0f;
			targetDampen = 1.0f;
			lastDampen = 1.0f;
		};

		~BlanderizerOf()
			{};

		void FadeToNewDampen(Stuff::Scalar dampen_amount, Scalar fade_time)
		{
			Verify(fadeTime >= 0.0f);
			Verify(dampen_amount >= 0.0f);
			Verify(dampen_amount <= 1.0f);

			lastDampen = GetCurrentDampenValue();
			fadeTime = fade_time;
			currentTime = 0.0f;
			targetDampen = dampen_amount;
		}

		void SetNewDampen(Stuff::Scalar dampen_amount)
		{
			fadeTime = 0.0f;
			currentTime = 0.0f;
			lastDampen = dampen_amount;
			targetDampen = dampen_amount;
		}

		T DampenValue(T &home_position, T& value_position)
		{
			Scalar lerp_value = GetCurrentDampenValue();

			T new_value;

			return new_value.Lerp(value_position, home_position, lerp_value);
		}
		

		Scalar GetCurrentDampenValue()
		{
			Stuff::Scalar lerp_value = lastDampen;
			if (currentTime >= fadeTime)
			{
				lerp_value = targetDampen;
			}
			else if (currentTime != 0.0f)
			{
				Stuff::Scalar to_fade_ratio = currentTime / fadeTime;
 
				lerp_value = Stuff::Lerp(lastDampen, targetDampen, to_fade_ratio);
			}

			return lerp_value;
		}

		void AdvanceTime(Scalar time_slice)
		{	
			if (currentTime < fadeTime)
			{
				currentTime += time_slice;
			}
		}

		Scalar fadeTime;
		Scalar currentTime;

		Scalar lastDampen;
		Scalar targetDampen;
	};

}

