class RipperdocScreenAnimationController extends inkLogicController
{
	private editable var m_doll : inkWidgetRef;
	private editable var m_defaultAnimationTab : inkWidgetRef;
	private editable var m_itemAnimationTab : inkWidgetRef;
	private editable var m_femaleHovers : inkWidgetRef;
	private editable var m_maleHovers : inkWidgetRef;
	private editable var m_F_immuneHoverTexture : inkWidgetRef;
	private editable var m_F_systemReplacementHoverTexture : inkWidgetRef;
	private editable var m_F_integumentaryHoverTexture : inkWidgetRef;
	private editable var m_F_musculoskeletalHoverTexture : inkWidgetRef;
	private editable var m_F_nervousHoverTexture : inkWidgetRef;
	private editable var m_F_eyesHoverTexture : inkImageRef;
	private editable var m_F_legsHoverTexture : inkWidgetRef;
	private editable var m_F_frontalCortexHoverTexture : inkWidgetRef;
	private editable var m_F_handsHoverTexture : inkWidgetRef;
	private editable var m_F_cardiovascularHoverTexture : inkWidgetRef;
	private editable var m_F_armsHoverTexture : inkWidgetRef;
	private editable var m_M_integumentaryHoverTexture : inkWidgetRef;
	private editable var m_M_armsHoverTexture : inkWidgetRef;
	private editable var m_M_cardiovascularHoverTexture : inkWidgetRef;
	private editable var m_M_handsHoverTexture : inkWidgetRef;
	private editable var m_M_frontalCortexHoverTexture : inkWidgetRef;
	private editable var m_M_immuneHoverTexture : inkWidgetRef;
	private editable var m_M_legsHoverTexture : inkWidgetRef;
	private editable var m_M_systemReplacementHoverTexture : inkWidgetRef;
	private editable var m_M_musculoskeletalHoverTexture : inkWidgetRef;
	private editable var m_M_nervousHoverTexture : inkWidgetRef;
	private editable var m_M_eyesHoverTexture : inkImageRef;
	private editable var m_man_wiresTexture : inkWidgetRef;
	private editable var m_woman_wiresTexture : inkWidgetRef;
	private var m_hoverAnimation : inkAnimProxy;
	private var m_hoverOverAnimation : inkAnimProxy;
	private var m_introDefaultAnimation : inkAnimProxy;
	private var m_outroDefaultAnimation : inkAnimProxy;
	private var m_introPaperdollAnimation : inkAnimProxy;
	private var m_outroPaperdollAnimation : inkAnimProxy;
	private var m_slideAnimation : inkAnimProxy;
	private var m_hoveredArea : gamedataEquipmentArea;
	private var m_introArea : gamedataEquipmentArea;
	private var m_isFemale : Bool;
	private var m_area : gamedataEquipmentArea;
	private var m_anim : inkAnimProxy;
	private var m_animHover : inkAnimProxy;
	private var m_animCancel : inkAnimProxy;
	private var m_isHovering : Bool;
	private var m_isSelected : Bool;
	private var m_animName : CName;
	private var m_position : inkMargin;
	private var m_positionOffset : Float;
	private var m_root : weak< inkWidget >;
	private var m_isSlidingOut : Bool;
	private var m_zoomOutAnim : inkAnimProxy;
	private var m_zoomInAnim : inkAnimProxy;
	private var m_slideOutAnim : inkAnimProxy;
	private var m_slideInAnim : inkAnimProxy;
	private var m_slideDirection : Bool;
	private var m_cancelSlideIn : Bool;
	private var m_isInside : Bool;
	private var m_currentArea : gamedataEquipmentArea;
	default m_currentArea = gamedataEquipmentArea.Invalid;
	private var m_nextArea : gamedataEquipmentArea;
	private var m_midArea : gamedataEquipmentArea;
	private const var SLIDE_DURATION : Float;
	default SLIDE_DURATION = 0.25f;

	public function StartSlide( isRight : Bool, nextArea : gamedataEquipmentArea )
	{
		m_slideDirection = isRight;
		m_nextArea = nextArea;
		m_isInside = true;
		if( m_currentArea == gamedataEquipmentArea.Invalid )
		{
			m_currentArea = m_area;
		}
		if( ( ( m_zoomOutAnim != NULL ) && m_zoomOutAnim.IsPlaying() ) || ( ( m_slideOutAnim != NULL ) && m_slideOutAnim.IsPlaying() ) )
		{
			m_cancelSlideIn = true;
			return;
		}
		if( ( m_slideInAnim != NULL ) && m_slideInAnim.IsPlaying() )
		{
			m_slideInAnim.Stop();
		}
		m_cancelSlideIn = false;
		SlideDoll( true );
	}

	private function ZoomDoll( isZoomOut : Bool )
	{
		var options : inkAnimOptions;
		if( m_zoomInAnim != NULL )
		{
			m_zoomInAnim.Stop();
		}
		if( isZoomOut )
		{
			options.customTimeDilation = 100.0;
			options.applyCustomTimeDilation = true;
			m_zoomOutAnim = PlayLibraryAnimation( GetName( m_currentArea, 'outro' ), options );
			m_zoomOutAnim.RegisterToCallback( inkanimEventType.OnFinish, this, 'OnZoomOutFinished' );
		}
		else
		{
			m_currentArea = m_nextArea;
			options.fromMarker = 'SkipIntro';
			m_zoomInAnim = PlayLibraryAnimation( GetName( m_nextArea, 'intro' ), options );
			m_zoomInAnim.RegisterToCallback( inkanimEventType.OnFinish, this, 'OnZoomInFinished' );
		}
	}

	protected event OnZoomOutFinished( anim : inkAnimProxy )
	{
		if( !( m_isInside ) )
		{
			StopSelect();
		}
		else if( m_cancelSlideIn )
		{
			if( m_slideOutAnim != NULL )
			{
				m_slideOutAnim.Stop();
			}
			ZoomDoll( false );
			StartSlide( m_slideDirection, m_nextArea );
		}
		else
		{
			ZoomDoll( false );
			SlideDoll( false );
		}
	}

	protected event OnZoomInFinished( anim : inkAnimProxy )
	{
		if( m_cancelSlideIn )
		{
			StartSlide( m_slideDirection, m_nextArea );
		}
	}

	private function SlideDoll( isSlideOut : Bool )
	{
		var animation : inkAnimDef;
		var marginInterpolator : inkAnimMargin;
		var opacityInterpolator : inkAnimTransparency;
		var startPosition : inkMargin;
		var endPosition : inkMargin;
		var startOpacity : Float;
		var endOpacity : Float;
		if( m_slideAnimation != NULL )
		{
			m_slideAnimation.Stop();
		}
		startOpacity = ( ( isSlideOut ) ? ( 1.0 ) : ( 0.0 ) );
		endOpacity = ( ( isSlideOut ) ? ( 0.0 ) : ( 1.0 ) );
		startPosition = m_position;
		startPosition.left += ( ( ( isSlideOut ) ? ( 0.0 ) : ( m_positionOffset ) ) * ( ( m_slideDirection ) ? ( 1.0 ) : ( -1.0 ) ) );
		endPosition = m_position;
		endPosition.left += ( ( ( isSlideOut ) ? ( m_positionOffset ) : ( 0.0 ) ) * ( ( m_slideDirection ) ? ( -1.0 ) : ( 1.0 ) ) );
		opacityInterpolator = new inkAnimTransparency;
		opacityInterpolator.SetDuration( SLIDE_DURATION );
		opacityInterpolator.SetStartTransparency( startOpacity );
		opacityInterpolator.SetEndTransparency( endOpacity );
		opacityInterpolator.SetType( inkanimInterpolationType.Sinusoidal );
		opacityInterpolator.SetMode( inkanimInterpolationMode.EasyOut );
		marginInterpolator = new inkAnimMargin;
		marginInterpolator.SetDuration( SLIDE_DURATION );
		marginInterpolator.SetStartMargin( startPosition );
		marginInterpolator.SetEndMargin( endPosition );
		marginInterpolator.SetType( inkanimInterpolationType.Sinusoidal );
		marginInterpolator.SetMode( inkanimInterpolationMode.EasyOut );
		animation = new inkAnimDef;
		animation.AddInterpolator( opacityInterpolator );
		animation.AddInterpolator( marginInterpolator );
		if( isSlideOut )
		{
			m_slideOutAnim = m_root.PlayAnimation( animation );
			m_slideOutAnim.RegisterToCallback( inkanimEventType.OnFinish, this, 'OnSlideOutFinished' );
		}
		else
		{
			m_slideInAnim = m_root.PlayAnimation( animation );
		}
	}

	protected event OnSlideOutFinished( anim : inkAnimProxy )
	{
		if( m_isInside )
		{
			ZoomDoll( true );
		}
	}

	protected event OnInitialize()
	{
		HideAllBodyParts();
		m_root = inkWidgetRef.Get( m_doll );
		m_position = m_root.GetMargin();
		m_positionOffset = 500.0;
	}

	public function SetMask( hasMask : Bool )
	{
		if( hasMask )
		{
			inkImageRef.SetTexturePart( m_M_eyesHoverTexture, 'ma_ocular_mask' );
			inkImageRef.SetTexturePart( m_F_eyesHoverTexture, 'wo_ocular_mask' );
		}
		else
		{
			inkImageRef.SetTexturePart( m_M_eyesHoverTexture, 'ma_ocular' );
			inkImageRef.SetTexturePart( m_F_eyesHoverTexture, 'wo_ocular' );
		}
	}

	public function ForceRestartFaceAnimation()
	{
		m_anim = PlayLibraryAnimation( ( ( !( m_isFemale ) ) ? ( 'M_ocular_intro' ) : ( 'ocular_intro' ) ) );
	}

	public function SetGender( female : Bool )
	{
		m_isFemale = female;
		inkWidgetRef.SetVisible( m_femaleHovers, m_isFemale );
		inkWidgetRef.SetVisible( m_maleHovers, !( m_isFemale ) );
		inkWidgetRef.SetVisible( m_woman_wiresTexture, m_isFemale );
		inkWidgetRef.SetVisible( m_man_wiresTexture, !( m_isFemale ) );
	}

	public function StartHover( area : gamedataEquipmentArea )
	{
		var target : weak< inkWidget >;
		if( area != gamedataEquipmentArea.Invalid && area != m_area )
		{
			if( ( m_animHover != NULL ) && m_animHover.IsPlaying() )
			{
				m_animHover.GotoEndAndStop();
			}
			m_isHovering = true;
			m_area = area;
			target = GetHoverAnimationTarget( m_area );
			m_animHover = PlayLibraryAnimationOnTargets( 'hover_area', SelectWidgets( target ) );
		}
	}

	public function StopHover()
	{
		var target : weak< inkWidget >;
		if( m_isHovering )
		{
			if( ( m_animHover != NULL ) && m_animHover.IsPlaying() )
			{
				m_animHover.GotoEndAndStop();
			}
			target = GetHoverAnimationTarget( m_area );
			m_animHover = PlayLibraryAnimationOnTargets( 'hoverover_area', SelectWidgets( target ) );
			m_area = gamedataEquipmentArea.Invalid;
		}
	}

	public function StartSelect()
	{
		if( !( m_isInside ) )
		{
			TryStartAnimation( GetName( m_area, 'intro' ) );
			m_isInside = true;
		}
	}

	public function StopSelect()
	{
		if( m_isInside )
		{
			return;
		}
		if( m_anim != NULL )
		{
			m_anim.Stop();
			m_anim = NULL;
		}
		if( ( m_slideOutAnim != NULL ) && m_slideOutAnim.IsPlaying() )
		{
			m_slideOutAnim.Stop();
			m_slideOutAnim = NULL;
			ZoomDoll( true );
			return;
		}
		if( m_slideInAnim != NULL )
		{
			m_slideInAnim.Stop();
			m_slideInAnim = NULL;
		}
		m_root.SetOpacity( 1.0 );
		m_root.SetMargin( m_position );
		m_currentArea = gamedataEquipmentArea.Invalid;
		m_anim = PlayLibraryAnimation( GetName( m_area, 'outro' ) );
	}

	public function SetOutside()
	{
		m_isInside = false;
	}

	private function TryStartAnimation( name : CName )
	{
		var options : inkAnimOptions;
		if( ( m_anim != NULL ) && m_anim.IsPlaying() )
		{
			m_animName = name;
			options.playReversed = true;
			m_anim.Continue( options );
			m_anim.RegisterToCallback( inkanimEventType.OnFinish, this, 'AnimQueue' );
		}
		else
		{
			m_animName = name;
			m_anim = PlayLibraryAnimation( m_animName );
			m_animName = '';
		}
	}

	private function AnimQueue( anim : inkAnimProxy )
	{
		if( m_animName != '' )
		{
			m_anim = PlayLibraryAnimation( m_animName );
		}
	}

	private function GetName( area : gamedataEquipmentArea, suffix : CName ) : CName
	{
		var animName : CName;
		switch( area )
		{
			case gamedataEquipmentArea.FrontalCortexCW:
				animName = 'frontalCortex_';
			break;
			case gamedataEquipmentArea.EyesCW:
				animName = 'ocular_';
			break;
			case gamedataEquipmentArea.CardiovascularSystemCW:
				animName = 'circlatory_';
			break;
			case gamedataEquipmentArea.ImmuneSystemCW:
				animName = 'immune_';
			break;
			case gamedataEquipmentArea.NervousSystemCW:
				animName = 'nervous_';
			break;
			case gamedataEquipmentArea.IntegumentarySystemCW:
				animName = 'integumentary_';
			break;
			case gamedataEquipmentArea.SystemReplacementCW:
				animName = 'operating_';
			break;
			case gamedataEquipmentArea.MusculoskeletalSystemCW:
				animName = 'skeleton_';
			break;
			case gamedataEquipmentArea.HandsCW:
				animName = 'hands_';
			break;
			case gamedataEquipmentArea.ArmsCW:
				animName = 'arms_';
			break;
			case gamedataEquipmentArea.LegsCW:
				animName = 'legs_';
			break;
		}
		animName = animName + suffix;
		if( !( m_isFemale ) )
		{
			animName = 'M_' + animName;
		}
		return animName;
	}

	private function GetHoverAnimationTarget( area : gamedataEquipmentArea ) : weak< inkWidget >
	{
		var target : weak< inkWidget >;
		switch( area )
		{
			case gamedataEquipmentArea.FrontalCortexCW:
				target = ( ( m_isFemale ) ? ( inkWidgetRef.Get( m_F_frontalCortexHoverTexture ) ) : ( inkWidgetRef.Get( m_M_frontalCortexHoverTexture ) ) );
			break;
			case gamedataEquipmentArea.EyesCW:
				target = ( ( m_isFemale ) ? ( inkImageRef.Get( m_F_eyesHoverTexture ) ) : ( inkImageRef.Get( m_M_eyesHoverTexture ) ) );
			break;
			case gamedataEquipmentArea.CardiovascularSystemCW:
				target = ( ( m_isFemale ) ? ( inkWidgetRef.Get( m_F_cardiovascularHoverTexture ) ) : ( inkWidgetRef.Get( m_M_cardiovascularHoverTexture ) ) );
			break;
			case gamedataEquipmentArea.ImmuneSystemCW:
				target = ( ( m_isFemale ) ? ( inkWidgetRef.Get( m_F_immuneHoverTexture ) ) : ( inkWidgetRef.Get( m_M_immuneHoverTexture ) ) );
			break;
			case gamedataEquipmentArea.NervousSystemCW:
				target = ( ( m_isFemale ) ? ( inkWidgetRef.Get( m_F_nervousHoverTexture ) ) : ( inkWidgetRef.Get( m_M_nervousHoverTexture ) ) );
			break;
			case gamedataEquipmentArea.IntegumentarySystemCW:
				target = ( ( m_isFemale ) ? ( inkWidgetRef.Get( m_F_integumentaryHoverTexture ) ) : ( inkWidgetRef.Get( m_M_integumentaryHoverTexture ) ) );
			break;
			case gamedataEquipmentArea.SystemReplacementCW:
				target = ( ( m_isFemale ) ? ( inkWidgetRef.Get( m_F_systemReplacementHoverTexture ) ) : ( inkWidgetRef.Get( m_M_systemReplacementHoverTexture ) ) );
			break;
			case gamedataEquipmentArea.MusculoskeletalSystemCW:
				target = ( ( m_isFemale ) ? ( inkWidgetRef.Get( m_F_musculoskeletalHoverTexture ) ) : ( inkWidgetRef.Get( m_M_musculoskeletalHoverTexture ) ) );
			break;
			case gamedataEquipmentArea.HandsCW:
				target = ( ( m_isFemale ) ? ( inkWidgetRef.Get( m_F_handsHoverTexture ) ) : ( inkWidgetRef.Get( m_M_handsHoverTexture ) ) );
			break;
			case gamedataEquipmentArea.ArmsCW:
				target = ( ( m_isFemale ) ? ( inkWidgetRef.Get( m_F_armsHoverTexture ) ) : ( inkWidgetRef.Get( m_M_armsHoverTexture ) ) );
			break;
			case gamedataEquipmentArea.LegsCW:
				target = ( ( m_isFemale ) ? ( inkWidgetRef.Get( m_F_legsHoverTexture ) ) : ( inkWidgetRef.Get( m_M_legsHoverTexture ) ) );
			break;
		}
		return target;
	}

	private function HideAllBodyParts()
	{
		inkWidgetRef.SetOpacity( m_F_frontalCortexHoverTexture, 0.0 );
		inkImageRef.SetOpacity( m_F_eyesHoverTexture, 0.0 );
		inkWidgetRef.SetOpacity( m_F_cardiovascularHoverTexture, 0.0 );
		inkWidgetRef.SetOpacity( m_F_immuneHoverTexture, 0.0 );
		inkWidgetRef.SetOpacity( m_F_nervousHoverTexture, 0.0 );
		inkWidgetRef.SetOpacity( m_F_integumentaryHoverTexture, 0.0 );
		inkWidgetRef.SetOpacity( m_F_systemReplacementHoverTexture, 0.0 );
		inkWidgetRef.SetOpacity( m_F_musculoskeletalHoverTexture, 0.0 );
		inkWidgetRef.SetOpacity( m_F_handsHoverTexture, 0.0 );
		inkWidgetRef.SetOpacity( m_F_armsHoverTexture, 0.0 );
		inkWidgetRef.SetOpacity( m_F_legsHoverTexture, 0.0 );
		inkWidgetRef.SetOpacity( m_M_frontalCortexHoverTexture, 0.0 );
		inkImageRef.SetOpacity( m_M_eyesHoverTexture, 0.0 );
		inkWidgetRef.SetOpacity( m_M_cardiovascularHoverTexture, 0.0 );
		inkWidgetRef.SetOpacity( m_M_immuneHoverTexture, 0.0 );
		inkWidgetRef.SetOpacity( m_M_nervousHoverTexture, 0.0 );
		inkWidgetRef.SetOpacity( m_M_integumentaryHoverTexture, 0.0 );
		inkWidgetRef.SetOpacity( m_M_systemReplacementHoverTexture, 0.0 );
		inkWidgetRef.SetOpacity( m_M_musculoskeletalHoverTexture, 0.0 );
		inkWidgetRef.SetOpacity( m_M_handsHoverTexture, 0.0 );
		inkWidgetRef.SetOpacity( m_M_armsHoverTexture, 0.0 );
		inkWidgetRef.SetOpacity( m_M_legsHoverTexture, 0.0 );
	}

}

