class InterpolatorsShowcaseController extends inkLogicController
{
	editable var interpolationType : inkanimInterpolationType;
	editable var interpolationMode : inkanimInterpolationMode;
	private var m_overlay : weak< inkWidget >;
	private var m_heightBar : weak< inkWidget >;
	private var m_widthBar : weak< inkWidget >;
	private var m_graphPointer : weak< inkWidget >;
	private var m_counterText : weak< inkText >;
	private var m_sizeWidget : weak< inkWidget >;
	private var m_rotationWidget : weak< inkWidget >;
	private var m_marginWidget : weak< inkWidget >;
	private var m_colorWidget : weak< inkWidget >;
	private var m_sizeAnim : inkAnimDef;
	private var m_rotationAnim : inkAnimDef;
	private var m_marginAnim : inkAnimDef;
	private var m_colorAnim : inkAnimDef;
	private var m_followTimelineAnim : inkAnimDef;
	private var m_interpolateAnim : inkAnimDef;
	private var m_startMargin : inkMargin;
	private var m_animLength : Float;
	private var m_animConstructor : AnimationsConstructor;

	protected event OnInitialize()
	{
		m_animConstructor = new AnimationsConstructor;
		FillWidgetsVariables();
		PrepareGraphPointer();
		m_animLength = 3.0;
		UpdateCounterText();
		ConstructAnimations();
	}

	private function FillWidgetsVariables()
	{
		m_overlay = GetWidget( 'Graph' );
		m_heightBar = GetWidget( 'Graph/HeightBar' );
		m_widthBar = GetWidget( 'Graph/WidthBar' );
		m_graphPointer = GetWidget( 'GraphPointer' );
		m_counterText = ( ( inkText )( GetWidget( 'TimerPanel/Timer/Text' ) ) );
		m_sizeWidget = GetWidget( 'AnimatedObjects/Anim_size' );
		m_rotationWidget = GetWidget( 'AnimatedObjects/Anim_rotation' );
		m_marginWidget = GetWidget( 'AnimatedObjects/Anim_margin' );
		m_colorWidget = GetWidget( 'AnimatedObjects/Anim_color' );
	}

	private function PrepareGraphPointer()
	{
		var tempVector : Vector2;
		m_startMargin = m_overlay.GetMargin();
		tempVector = m_widthBar.GetSize();
		m_startMargin.top -= tempVector.Y;
		tempVector = m_heightBar.GetSize();
		m_startMargin.left += tempVector.X;
		m_graphPointer.SetMargin( m_startMargin );
	}

	public function InterpolatorModeToIn( e : inkPointerEvent )
	{
		interpolationMode = inkanimInterpolationMode.EasyIn;
	}

	public function InterpolatorModeToOut( e : inkPointerEvent )
	{
		interpolationMode = inkanimInterpolationMode.EasyOut;
	}

	public function InterpolatorModeToInOut( e : inkPointerEvent )
	{
		interpolationMode = inkanimInterpolationMode.EasyInOut;
	}

	public function InterpolatorTypeToLinear( e : inkPointerEvent )
	{
		interpolationType = inkanimInterpolationType.Linear;
	}

	public function InterpolatorTypeToQuadratic( e : inkPointerEvent )
	{
		interpolationType = inkanimInterpolationType.Quadratic;
	}

	public function InterpolatorTypeToQubic( e : inkPointerEvent )
	{
		interpolationType = inkanimInterpolationType.Qubic;
	}

	public function InterpolatorTypeToQuartic( e : inkPointerEvent )
	{
		interpolationType = inkanimInterpolationType.Quartic;
	}

	public function InterpolatorTypeToQuintic( e : inkPointerEvent )
	{
		interpolationType = inkanimInterpolationType.Quintic;
	}

	public function InterpolatorTypeToSinusoidal( e : inkPointerEvent )
	{
		interpolationType = inkanimInterpolationType.Sinusoidal;
	}

	public function InterpolatorTypeToExponential( e : inkPointerEvent )
	{
		interpolationType = inkanimInterpolationType.Exponential;
	}

	public function InterpolatorTypeToElastic( e : inkPointerEvent )
	{
		interpolationType = inkanimInterpolationType.Elastic;
	}

	public function InterpolatorTypeToCircular( e : inkPointerEvent )
	{
		interpolationType = inkanimInterpolationType.Circular;
	}

	public function InterpolatorTypeToBack( e : inkPointerEvent )
	{
		interpolationType = inkanimInterpolationType.Back;
	}

	public function RiseTimer_1( e : inkPointerEvent )
	{
		m_animLength += 0.01;
		UpdateCounterText();
	}

	public function RiseTimer_2( e : inkPointerEvent )
	{
		m_animLength += 0.1;
		UpdateCounterText();
	}

	public function RiseTimer_3( e : inkPointerEvent )
	{
		m_animLength += 1.0;
		UpdateCounterText();
	}

	public function LowerTimer_1( e : inkPointerEvent )
	{
		m_animLength -= 0.01;
		UpdateCounterText();
	}

	public function LowerTimer_2( e : inkPointerEvent )
	{
		m_animLength -= 0.1;
		UpdateCounterText();
	}

	public function LowerTimer_3( e : inkPointerEvent )
	{
		m_animLength -= 1.0;
		UpdateCounterText();
	}

	private function UpdateCounterText()
	{
		m_counterText.SetText( FloatToString( m_animLength ) );
	}

	public function PlayAnimation( e : inkPointerEvent )
	{
		StopAllAnimations();
		ConstructAnimations();
		m_graphPointer.SetMargin( m_overlay.GetMargin() );
		m_graphPointer.PlayAnimation( m_interpolateAnim );
		m_graphPointer.PlayAnimation( m_followTimelineAnim );
		m_sizeWidget.PlayAnimation( m_sizeAnim );
		m_rotationWidget.PlayAnimation( m_rotationAnim );
		m_colorWidget.PlayAnimation( m_colorAnim );
		m_marginWidget.PlayAnimation( m_marginAnim );
	}

	private function StopAllAnimations()
	{
		m_graphPointer.StopAllAnimations();
		m_sizeWidget.StopAllAnimations();
		m_rotationWidget.StopAllAnimations();
		m_colorWidget.StopAllAnimations();
		m_marginWidget.StopAllAnimations();
	}

	private function ConstructAnimations()
	{
		m_animConstructor.SetGenericSettings( m_animLength, interpolationType, interpolationMode, true );
		ConstructInterpolatorAnim();
		ConstructTimelineFollow();
		m_animConstructor.SetGenericSettings( m_animLength, interpolationType, interpolationMode, false );
		ConstructShowcaseAnimations();
	}

	private function ConstructInterpolatorAnim()
	{
		var interpolatedMovement : inkAnimMargin;
		var endMargin : inkMargin;
		var tempVector : Vector2;
		tempVector = m_heightBar.GetSize();
		endMargin.top = -( tempVector.Y );
		tempVector = m_widthBar.GetSize();
		endMargin.top += tempVector.Y;
		m_interpolateAnim = new inkAnimDef;
		interpolatedMovement = m_animConstructor.NewMarginInterpolator( inkMargin( 0.0, 0.0, 0.0, 0.0 ), endMargin );
		m_interpolateAnim.AddInterpolator( interpolatedMovement );
	}

	private function ConstructTimelineFollow()
	{
		var linearMovementInterpolator : inkAnimMargin;
		var endMargin : inkMargin;
		var tempVector : Vector2;
		tempVector = m_widthBar.GetSize();
		endMargin.left = tempVector.X;
		tempVector = m_heightBar.GetSize();
		endMargin.left -= tempVector.X;
		m_followTimelineAnim = new inkAnimDef;
		linearMovementInterpolator = m_animConstructor.NewMarginInterpolator( inkMargin( 0.0, 0.0, 0.0, 0.0 ), endMargin );
		linearMovementInterpolator.SetType( inkanimInterpolationType.Linear );
		m_followTimelineAnim.AddInterpolator( linearMovementInterpolator );
	}

	private function ConstructShowcaseAnimations()
	{
		m_sizeAnim = new inkAnimDef;
		m_rotationAnim = new inkAnimDef;
		m_colorAnim = new inkAnimDef;
		m_marginAnim = new inkAnimDef;
		m_sizeAnim.AddInterpolator( m_animConstructor.NewSizeInterpolator( Vector2( 96.0, 96.0 ), Vector2( 192.0, 192.0 ) ) );
		m_rotationAnim.AddInterpolator( m_animConstructor.NewRotationInterpolator( 0.0, 180.0 ) );
		m_colorAnim.AddInterpolator( m_animConstructor.NewColorInterpolator( HDRColor( 1.0, 1.0, 1.0, 1.0 ), HDRColor( 1.0, 0.0, 0.0, 1.0 ) ) );
		m_marginAnim.AddInterpolator( m_animConstructor.NewMarginInterpolator( inkMargin( 475.0, 400.0, 0.0, 0.0 ), inkMargin( 875.0, 400.0, 0.0, 0.0 ) ) );
	}

}

