PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/ContentSystem/Rendering/ImageAnimationData.cs

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