class PulseAnimation
{
	var m_root : weak< inkWidget >;
	var m_anim : inkAnimProxy;
	var m_top : Float;
	var m_bot : Float;
	var m_time : Float;
	var m_delay : Float;

	public function Configure( root : weak< inkWidget >, topOpacity, bottomOpacity, pulseRate : Float, optional delay : Float )
	{
		if( m_anim != NULL )
		{
			m_anim.Stop();
			m_anim = NULL;
		}
		m_root = root;
		m_top = topOpacity;
		m_bot = bottomOpacity;
		m_time = pulseRate;
		m_delay = delay;
	}

	public function Configure( root : weak< inkWidget >, params : PulseAnimationParams )
	{
		if( m_anim != NULL )
		{
			m_anim.Stop();
			m_anim = NULL;
		}
		m_root = root;
		m_top = params.topOpacity;
		m_bot = params.bottomOpacity;
		m_time = params.pulseRate;
		m_delay = params.delay;
	}

	public virtual function Start( optional singleLoop : Bool )
	{
		var animDef : inkAnimDef;
		var animData : inkAnimTransparency;
		var animOptions : inkAnimOptions;
		if( m_anim != NULL )
		{
			m_anim.Stop();
		}
		m_root.SetOpacity( m_top );
		animData = new inkAnimTransparency;
		animData.SetDuration( m_time );
		animData.SetStartTransparency( m_top );
		animData.SetEndTransparency( m_bot );
		if( m_delay > 0.0 )
		{
			animData.SetStartDelay( m_delay );
		}
		animData.SetType( inkanimInterpolationType.Linear );
		animData.SetMode( inkanimInterpolationMode.EasyInOut );
		animOptions.loopType = inkanimLoopType.PingPong;
		animOptions.loopInfinite = !( singleLoop );
		animDef = new inkAnimDef;
		animDef.AddInterpolator( animData );
		m_anim = m_root.PlayAnimationWithOptions( animDef, animOptions );
	}

	public function Stop()
	{
		var time : Float;
		var animDef : inkAnimDef;
		var animData : inkAnimTransparency;
		if( m_anim != NULL )
		{
			m_anim.Stop();
		}
		time = m_root.GetOpacity();
		time = ( m_top - time ) * m_time;
		animData = new inkAnimTransparency;
		animData.SetDuration( time );
		animData.SetStartTransparency( m_root.GetOpacity() );
		animData.SetEndTransparency( m_top );
		animData.SetType( inkanimInterpolationType.Quintic );
		animData.SetMode( inkanimInterpolationMode.EasyInOut );
		animDef = new inkAnimDef;
		animDef.AddInterpolator( animData );
		m_anim = m_root.PlayAnimation( animDef );
	}

	public function ForceStop()
	{
		if( m_anim != NULL )
		{
			m_anim.Stop();
		}
	}

}

struct PulseAnimationParams
{
	var topOpacity : Float;
	var bottomOpacity : Float;
	var pulseRate : Float;
	var delay : Float;
	default delay = 0;
}

class PulseScaleAnimation extends PulseAnimation
{

	public override function Start( optional singleLoop : Bool )
	{
		var animDef : inkAnimDef;
		var animData : inkAnimScale;
		var animOptions : inkAnimOptions;
		if( m_anim != NULL )
		{
			m_anim.Stop();
		}
		m_root.SetOpacity( m_top );
		animData = new inkAnimScale;
		animData.SetDuration( m_time );
		animData.SetStartScale( Vector2( m_top, m_top ) );
		animData.SetEndScale( Vector2( m_bot, m_bot ) );
		animData.SetType( inkanimInterpolationType.Linear );
		animData.SetMode( inkanimInterpolationMode.EasyInOut );
		animOptions.loopType = inkanimLoopType.PingPong;
		animOptions.loopInfinite = !( singleLoop );
		animDef = new inkAnimDef;
		animDef.AddInterpolator( animData );
		m_anim = m_root.PlayAnimationWithOptions( animDef, animOptions );
	}

}

