PageRenderTime 22ms CodeModel.GetById 14ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/ContentSystem/Rendering/ImageAnimationData.cs

#
C# | 209 lines | 108 code | 15 blank | 86 comment | 4 complexity | f074e561105a859ce7e4eec68cc11cc8 MD5 | raw file
  1using System.Collections.Generic;
  2using Delta.Utilities;
  3using Delta.Utilities.Datatypes;
  4using Delta.Utilities.Graphics;
  5using Delta.Utilities.Helpers;
  6
  7namespace Delta.ContentSystem.Rendering
  8{
  9	/// <summary>
 10	/// Helper class for accessing image animations, which is just a collection
 11	/// of images, but with all important meta data for them. All images are
 12	/// just children and can be accessed easily with this class.
 13	/// <para />
 14	/// Please note that Image Animations just contain images and have no data
 15	/// own their own (just meta data).
 16	/// </summary>
 17	public class ImageAnimationData : Content
 18	{
 19		#region Get (Static)
 20		/// <summary>
 21		/// Get and load content based on the content name. This method makes sure
 22		/// we do not load the same content twice (the constructor is protected).
 23		/// </summary>
 24		/// <param name="contentName">Content name we want to load, this is
 25		/// passed onto the Content System, which will do the actual loading with
 26		/// help of the Load method in this class.</param>
 27		/// <returns>The loaded Content object, always unique for the same
 28		/// name, this helps comparing data.</returns>
 29		public static ImageAnimationData Get(string contentName)
 30		{
 31			return Get<ImageAnimationData>(contentName,
 32				ContentType.ImageAnimation);
 33		}
 34		#endregion
 35
 36		#region Images (Public)
 37		/// <summary>
 38		/// List of images that are children to this ImageAnimation content node.
 39		/// </summary>
 40		public string[] Images
 41		{
 42			get;
 43			protected set;
 44		}
 45		#endregion
 46
 47		#region AnimationSpeed (Public)
 48		/// <summary>
 49		/// Animation speed in FPS. 30 means we got 30 animations per second, 12
 50		/// means we only have 12 animations that are played per second (default).
 51		/// Used for 2D animated image sequences, but also for 3D Models using
 52		/// animations. The default value is 30 and usually used for all animated
 53		/// content unless it is optimized for 15 fps or less to save memory.
 54		/// Note: Not used if each of the images has its own AnimationLengthInMs!
 55		/// </summary>
 56		public float AnimationSpeed
 57		{
 58			get
 59			{
 60				return data.AnimationSpeed;
 61			}
 62		}
 63		#endregion
 64
 65		#region AnimationIndicesAndMs (Public)
 66		/// <summary>
 67		/// Animation frame indices and their lengths in milliseconds for whatever
 68		/// crazy animation logic you want to build. Usually unused (0), but if
 69		/// this is used for all animated images in a sequence, you can control
 70		/// how quickly each part of the animation is played back. You are not
 71		/// forced to play the animation in order and you can repeat frames as
 72		/// many times as you like and make the animation as long as you want (see
 73		/// TotalAnimationLengthMs). AnimationSpeed (see above) is also ignored,
 74		/// you need to set each of these frame length times yourself.
 75		/// </summary>
 76		public int[] AnimationIndicesAndMs
 77		{
 78			get
 79			{
 80				return data.AnimationIndicesAndMs;
 81			}
 82		}
 83		#endregion
 84
 85		#region PixelSize (Public)
 86		/// <summary>
 87		/// Size of the images in this animation in pixels. Each animation image
 88		/// can have its own PixelSize, but this is the important size used for
 89		/// displaying this image animation (children can be bigger or smaller).
 90		/// </summary>
 91		public Size PixelSize
 92		{
 93			get
 94			{
 95				return data.PixelSize;
 96			}
 97		}
 98		#endregion
 99
100		#region BlendMode (Public)
101		/// <summary>
102		/// Blend mode used for all images, set in data and can't be changed.
103		/// This is important for the MaterialManager sorting logic!
104		/// </summary>
105		public BlendMode BlendMode
106		{
107			get
108			{
109				return data.BlendMode;
110			}
111		}
112		#endregion
113
114		#region UseLinearFiltering (Public)
115		/// <summary>
116		/// Helper property to determinate if we need to enable filtering for
117		/// rendering this images (usually in a shader). True is the default and
118		/// means we are going to use Trilinear filtering, false means no
119		/// filtering, which is often called Nearest or Point filtering.
120		/// Note: For fonts and the default image (4x4 pixels) this is
121		/// automatically set to false for more accurate and sharp rendering.
122		/// </summary>
123		public bool UseLinearFiltering
124		{
125			get
126			{
127				return data.UseFiltering;
128			}
129		}
130		#endregion
131
132		#region Constructors
133		/// <summary>
134		/// Create image animation instance, which just holds some meta data and
135		/// the list of images for this animation (see Images property).
136		/// Use the static Get method to call this.
137		/// </summary>
138		/// <param name="setImageAnimationName">
139		/// Name for this content object, should not contain any path, project,
140		/// scene or any special character! If this is empty or starts with an
141		/// &gt; character, we assume this is code generated content
142		/// (e.g. "&gt;IntroScene&lt;" or "") and no loading will happen!
143		/// </param>
144		protected ImageAnimationData(string setImageAnimationName)
145			: base(setImageAnimationName, ContentType.ImageAnimation)
146		{
147			// In Load Images are set (this way it works with content updates)!
148		}
149		#endregion
150
151		#region ToString (Public)
152		/// <summary>
153		/// To string, will display the image animation images list.
154		/// </summary>
155		/// <returns>
156		/// A <see cref="System.String"/> that represents this ImageAnimationData
157		/// instance with the list of image names.
158		/// </returns>
159		public override string ToString()
160		{
161			return GetType().GetClassName() + " " + Name + ": " +
162			       "Images=" + Images.Write();
163		}
164		#endregion
165
166		#region Methods (Private)
167
168		#region Load
169		/// <summary>
170		/// Load animated image content data, will just set the Images property.
171		/// </summary>
172		/// <param name="alreadyLoadedNativeData">
173		/// Ignored here, can't be cloned.
174		/// </param>
175		protected override void Load(Content alreadyLoadedNativeData)
176		{
177			// Initialize a new list for all the children and grab all their names
178			List<string> ret = new List<string>();
179			// Note: Only direct image children nodes are allowed, nothing else!
180			foreach (ContentMetaData childrenData in data.Children)
181			{
182				if (childrenData.Type != ContentType.Image ||
183				    childrenData.Children.Count > 0)
184				{
185					Log.Warning("Invalid children node for " + this + " found, only " +
186					            "Image nodes and no extra children are allowed: " +
187					            childrenData);
188					// Skip this, maybe other data works
189					continue;
190				}
191
192				// Note: We just add names, we don't want to load the images right away,
193				// only when they need to be displayed they will be loaded (in the
194				// Material class).
195				ret.Add(childrenData.Name);
196			}
197
198			if (ret.Count == 0)
199			{
200				Log.Warning("No images found for " + this);
201			}
202
203			Images = ret.ToArray();
204		}
205		#endregion
206
207		#endregion
208	}
209}