class CustomAnimationsHudGameController extends inkHUDGameController
{
	[ category = "Animations" ]
	protected editable inlined var m_customAnimations : WidgetAnimationManager;
	[ category = "Animations" ]
	protected mutable editable var m_onSpawnAnimations : array< CName >;
	[ category = "Library" ]
	protected editable var m_defaultLibraryItemName : CName;
	[ category = "Library" ]
	protected editable var m_defaultLibraryItemAnchor : inkEAnchor;
	default m_defaultLibraryItemAnchor = inkEAnchor.Fill;
	protected var m_spawnedLibrararyItem : weak< inkWidget >;
	protected var m_curentLibraryItemName : CName;
	protected var m_currentLibraryItemAnchor : inkEAnchor;
	protected var m_root : weak< inkCompoundWidget >;
	protected var m_isInitialized : Bool;
	private var m_ownerID : EntityID;

	protected event OnInitialize()
	{
		m_ownerID = GetOwnerEntity().GetEntityID();
		m_root = ( ( inkCompoundWidget )( GetRootWidget() ) );
		if( !( ResolveLibraryItemSpawn( m_defaultLibraryItemName, m_defaultLibraryItemAnchor, true ) ) )
		{
			PlayOnSpawnAnimations();
		}
		m_isInitialized = true;
	}

	protected event OnCustomUIAnimationEvent( evt : CustomUIAnimationEvent )
	{
		if( m_ownerID != evt.ownerID )
		{
			return false;
		}
		if( m_customAnimations != NULL )
		{
			m_customAnimations.UpdateAnimationsList( evt.animationName, evt.animOptionsOverride );
			ResolveLibraryItemSpawn( evt.libraryItemName, evt.libraryItemAnchor, false, evt.forceRespawnLibraryItem );
			PlayAnimation( evt.animationName, evt.playbackOption );
		}
	}

	protected function PlayAnimation( animationName : CName, playbackOption : EInkAnimationPlaybackOption )
	{
		if( m_customAnimations != NULL )
		{
			m_customAnimations.TriggerAnimationByName( this, animationName, playbackOption, m_spawnedLibrararyItem );
		}
	}

	private function PlayOnSpawnAnimations()
	{
		var i : Int32;
		for( i = 0; i < m_onSpawnAnimations.Size(); i += 1 )
		{
			PlayAnimation( m_onSpawnAnimations[ i ], EInkAnimationPlaybackOption.PLAY );
		}
	}

	private function ResolveLibraryItemSpawn( itemName : CName, anchor : inkEAnchor, optional async : Bool, optional forceRespawnLibraryItem : Bool ) : Bool
	{
		if( !( m_root ) )
		{
			return false;
		}
		if( !( IsNameValid( itemName ) ) )
		{
			return false;
		}
		if( ( ( m_spawnedLibrararyItem && itemName == m_curentLibraryItemName ) && anchor == m_currentLibraryItemAnchor ) && !( forceRespawnLibraryItem ) )
		{
			return false;
		}
		if( !( HasLocalLibrary( itemName ) ) )
		{
			return false;
		}
		if( m_spawnedLibrararyItem )
		{
			m_root.RemoveChild( m_spawnedLibrararyItem );
			m_customAnimations.CleanAllAnimationsChachedData();
		}
		if( async )
		{
			AsyncSpawnFromLocal( m_root, itemName, this, 'OnInitialSpawnLibrararyItem' );
			m_curentLibraryItemName = itemName;
			m_currentLibraryItemAnchor = anchor;
		}
		else
		{
			m_spawnedLibrararyItem = SpawnFromLocal( m_root, itemName );
			m_spawnedLibrararyItem.SetAnchor( anchor );
			m_curentLibraryItemName = itemName;
			m_currentLibraryItemAnchor = anchor;
		}
		return true;
	}

	protected event OnInitialSpawnLibrararyItem( widget : inkWidget, userData : IScriptable )
	{
		m_spawnedLibrararyItem = widget;
		m_spawnedLibrararyItem.SetAnchor( m_currentLibraryItemAnchor );
		PlayOnSpawnAnimations();
	}

}

