class RipperdocFillBar extends inkLogicController
{
	private var m_root : weak< inkWidget >;
	private var m_fillStart : Float;
	private var m_fillEnd : Float;
	private var m_maxSize : Vector2;
	private var m_sizeAnimation : inkAnimProxy;
	private var m_marginAnimation : inkAnimProxy;

	protected event OnInitialize()
	{
		var size : Vector2;
		m_root = GetRootWidget();
		m_maxSize = m_root.GetSize();
		size.X = m_maxSize.X;
		m_root.SetSize( size );
	}

	public function SetStart( start : Float, optional duration : Float )
	{
		m_fillStart = ClampF( start, 0.0, 1.0 );
		AnimateMargin( duration );
	}

	public function SetEnd( end : Float, optional duration : Float )
	{
		m_fillStart = ClampF( end, 0.0, 1.0 );
		AnimateSize( duration );
	}

	private function AnimateMargin( duration : Float )
	{
		var margin : inkMargin;
		var animation : inkAnimDef;
		var marginInterpolator : inkAnimMargin;
		if( m_marginAnimation != NULL )
		{
			m_marginAnimation.Stop();
		}
		margin.bottom = m_fillStart * m_maxSize.Y;
		if( duration == 0.0 )
		{
			m_marginAnimation = NULL;
			m_root.SetMargin( margin );
			return;
		}
		marginInterpolator = new inkAnimMargin;
		marginInterpolator.SetDuration( duration );
		marginInterpolator.SetStartMargin( m_root.GetMargin() );
		marginInterpolator.SetEndMargin( margin );
		marginInterpolator.SetType( inkanimInterpolationType.Sinusoidal );
		marginInterpolator.SetMode( inkanimInterpolationMode.EasyOut );
		animation = new inkAnimDef;
		animation.AddInterpolator( marginInterpolator );
		m_marginAnimation = m_root.PlayAnimation( animation );
	}

	private function AnimateSize( duration : Float )
	{
		var size : Vector2;
		var animation : inkAnimDef;
		var sizeInterpolator : inkAnimSize;
		if( m_sizeAnimation != NULL )
		{
			m_sizeAnimation.Stop();
		}
		size.X = m_maxSize.X;
		size.Y = AbsF( m_fillEnd - m_fillStart ) * m_maxSize.Y;
		if( duration == 0.0 )
		{
			m_sizeAnimation = NULL;
			m_root.SetSize( size );
			return;
		}
		sizeInterpolator = new inkAnimSize;
		sizeInterpolator.SetDuration( duration );
		sizeInterpolator.SetStartSize( m_root.GetSize() );
		sizeInterpolator.SetEndSize( size );
		sizeInterpolator.SetType( inkanimInterpolationType.Sinusoidal );
		sizeInterpolator.SetMode( inkanimInterpolationMode.EasyOut );
		animation = new inkAnimDef;
		animation.AddInterpolator( sizeInterpolator );
		m_sizeAnimation = m_root.PlayAnimation( animation );
	}

}

