/// <summary> /// Function to set an animation playing. /// </summary> /// <param name="animatedObject">The object to apply the animation onto.</param> /// <param name="animation">Animation to play.</param> /// <exception cref="System.ArgumentNullException">Thrown when the <paramref name="animation"/> or <paramref name="animatedObject"/> parameters are NULL (Nothing in VB.Net).</exception> /// <exception cref="System.Collections.Generic.KeyNotFoundException">Thrown when the animation could not be found in the collection.</exception> public void Play(T animatedObject, GorgonAnimation <T> animation) { GorgonDebug.AssertNull(animation, "animation"); GorgonDebug.AssertNull(animatedObject, "animatedObject"); #if DEBUG if (!Contains(animation)) { throw new KeyNotFoundException(string.Format(Resources.GORANM_ANIMATION_DOES_NOT_EXIST, animation.Name)); } #endif // This animation is already playing. if (animation == CurrentAnimation) { return; } // Stop the current animation. if (CurrentAnimation != null) { Stop(); } AnimatedObject = animatedObject; CurrentAnimation = animation; // Update to the first frame. CurrentAnimation.UpdateObject(); }
/// <summary> /// Function to clone the animation. /// </summary> /// <returns>A clone of the animation.</returns> public GorgonAnimation <T> Clone() { var clone = new GorgonAnimation <T>(AnimationController, Name, Length) { IsLooped = IsLooped, Length = Length, LoopCount = LoopCount, Speed = Speed, Time = Time }; foreach (var track in Tracks) { if (!clone.Tracks.Contains(track.Name)) { continue; } foreach (var key in track.KeyFrames) { clone.Tracks[track.Name].KeyFrames.Add(key.Clone()); } } return(clone); }
/// <summary> /// Function to load an animation from a stream. /// </summary> /// <param name="stream">Stream to load from.</param> /// <returns>The animation in the stream.</returns> /// <exception cref="System.ArgumentNullException">Thrown when the <paramref name="stream"/> parameter is NULL (Nothing in VB.Net).</exception> /// <exception cref="System.ArgumentException">Thrown when the stream parameter does not contain a Gorgon animation file. /// <para>-or-</para> /// <para>Thrown when the name of the animation is already present in the controller animation collection.</para> /// <para>-or-</para> /// <para>Thrown when a track type cannot be associated with a property on the object type that the controller was declared with.</para> /// </exception> /// <exception cref="System.InvalidCastException">Thrown when the animation being loaded is for a different type than the controller was declared with.</exception> public GorgonAnimation <T> FromStream(Stream stream) { GorgonAnimation <T> animation; GorgonDebug.AssertNull(stream, "stream"); using (var chunk = new GorgonChunkReader(stream)) { // Get the header. chunk.Begin(AnimationVersion); chunk.Begin("ANIMDATA"); // Get the type data. string typeString = chunk.ReadString(); if (typeString != AnimatedObjectType.FullName) { throw new InvalidCastException(string.Format(Resources.GORANM_ANIMATION_TYPE_MISMATCH, typeString, AnimatedObjectType.FullName)); } // Get the name. string animationName = chunk.ReadString(); if (Contains(animationName)) { throw new ArgumentException(string.Format(Resources.GORANM_ANIMATION_ALREADY_EXISTS, animationName), "stream"); } animation = new GorgonAnimation <T>(this, animationName, chunk.ReadFloat()) { IsLooped = chunk.ReadBoolean() }; chunk.End(); // Get all the tracks. while (chunk.HasChunk("TRCKDATA")) { chunk.Begin("TRCKDATA"); string trackName = chunk.ReadString(); // Get the name of the track. if (!animation.Tracks.Contains(trackName)) { throw new ArgumentException( string.Format(Resources.GORANM_TRACK_TYPE_DOES_NOT_EXIST, trackName, AnimatedObjectType.FullName), "stream"); } animation.Tracks[trackName].FromChunk(chunk); chunk.End(); } } Add(animation); return(animation); }
/// <summary> /// Function to remove an item from the collection. /// </summary> /// <param name="item">Item to remove.</param> protected override void RemoveItem(GorgonAnimation <T> item) { if (CurrentAnimation == item) { Stop(); } item.AnimationController = null; base.RemoveItem(item); }
/// <summary> /// Function to stop the currently playing animation. /// </summary> public void Stop() { if (CurrentAnimation == null) { return; } AnimatedObject = null; CurrentAnimation = null; }
/// <summary> /// Function to remove an animation from the collection. /// </summary> /// <param name="animation">Animation to remove.</param> /// <exception cref="System.ArgumentNullException">Thrown when the <paramref name="animation"/> parameter is NULL (Nothing in VB.Net).</exception> /// <exception cref="System.Collections.Generic.KeyNotFoundException">Thrown when the animation was not found in the collection.</exception> public void Remove(GorgonAnimation <T> animation) { GorgonDebug.AssertNull(animation, "animation"); #if DEBUG if (!Contains(animation)) { throw new KeyNotFoundException(string.Format(Resources.GORANM_ANIMATION_DOES_NOT_EXIST, animation.Name)); } #endif RemoveItem(animation); }
/// <summary> /// Function to add an animation to the collection. /// </summary> /// <param name="animation">Animation to add to the collection.</param> /// <exception cref="System.ArgumentNullException">Thrown when the <paramref name="animation"/> parameter is NULL (Nothing in VB.Net).</exception> /// <exception cref="System.ArgumentException">Thrown when the animation already exists in this collection.</exception> public void Add(GorgonAnimation <T> animation) { GorgonDebug.AssertNull(animation, "animation"); #if DEBUG if (Contains(animation.Name)) { throw new ArgumentException(string.Format(Resources.GORANM_ANIMATION_ALREADY_EXISTS, animation.Name), "animation"); } #endif AddItem(animation); }
/// <summary> /// Function to add an animation to the collection. /// </summary> /// <param name="value">Animation to add.</param> protected override void AddItem(GorgonAnimation <T> value) { GorgonDebug.AssertNull(value, "value"); if ((value.AnimationController != null) && (value.AnimationController != this) && (value.AnimationController.Contains(value))) { value.AnimationController.Remove(value); value.AnimationController = this; } base.AddItem(value); }
/// <summary> /// Function to add an animation to the collection. /// </summary> /// <param name="name">Name of the animation to add.</param> /// <param name="length">Length of the animation, in seconds.</param> /// <returns>The newly created animation.</returns> /// <exception cref="System.ArgumentNullException">Thrown when the <paramref name="name"/> parameter is NULL (Nothing in VB.Net).</exception> /// <exception cref="System.ArgumentException">Thrown when the name parameter is an empty string. /// <para>-or-</para> /// <para>Thrown when the animation already exists in this collection.</para></exception> public GorgonAnimation <T> Add(string name, float length) { GorgonDebug.AssertParamString(name, "name"); #if DEBUG if (Contains(name)) { throw new ArgumentException(string.Format(Resources.GORANM_ANIMATION_ALREADY_EXISTS, name), "name"); } #endif var result = new GorgonAnimation <T>(this, name, length); AddItem(result); return(result); }
/// <summary> /// Function to set an item by its name. /// </summary> /// <param name="name">Name of the item to set.</param> /// <param name="value">Value to set.</param> protected override void SetItem(string name, GorgonAnimation <T> value) { if (CurrentAnimation == this[name]) { Stop(); } // Remove the clip from the other if ((value != null) && (value.AnimationController != null) && (value.AnimationController != this) && (value.AnimationController.Contains(value))) { value.AnimationController.Remove(value); } this[name].AnimationController = this; base.SetItem(name, value); }
/// <summary> /// Initializes a new instance of the <see cref="GorgonAnimationTrackCollection{T}" /> class. /// </summary> /// <param name="animation">The animation that owns this collection.</param> internal GorgonAnimationTrackCollection(GorgonAnimation <T> animation) : base(false) { _animation = animation; }