PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/ContentSystem/Rendering/ImageData.cs

#
C# | 220 lines | 119 code | 16 blank | 85 comment | 0 complexity | 8a900bd3400c6b8264075fd6641093d8 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System.Collections.Generic;
  2. using Delta.Utilities.Datatypes;
  3. using Delta.Utilities.Graphics;
  4. using Delta.Utilities.Helpers;
  5. namespace Delta.ContentSystem.Rendering
  6. {
  7. /// <summary>
  8. /// Helper class for accessing image content, which is needed in the Texture
  9. /// class (but it can also be used in other places when loading images).
  10. /// </summary>
  11. public class ImageData : Content
  12. {
  13. #region Get (Static)
  14. /// <summary>
  15. /// Get and load content based on the content name. This method makes sure
  16. /// we do not load the same content twice (the constructor is protected).
  17. /// </summary>
  18. /// <param name="contentName">Content name we want to load, this is
  19. /// passed onto the Content System, which will do the actual loading with
  20. /// help of the Load method in this class.</param>
  21. /// <returns>The loaded Content object, always unique for the same
  22. /// name, this helps comparing data.</returns>
  23. public static ImageData Get(string contentName)
  24. {
  25. return Get<ImageData>(contentName, ContentType.Image);
  26. }
  27. #endregion
  28. #region GetAllContentNames (Static)
  29. /// <summary>
  30. /// Get list of all image content names available in the current project.
  31. /// </summary>
  32. /// <returns>List of all image content names</returns>
  33. public static string[] GetAllContentNames()
  34. {
  35. Dictionary<string, ContentMetaData> meshContents =
  36. ContentManager.GetAllContentMetaData(ContentType.Image);
  37. return ArrayHelper.ToArray(meshContents.Keys);
  38. }
  39. #endregion
  40. #region PixelSize (Public)
  41. /// <summary>
  42. /// The size of the image in pixels (NOT in quadratic space and also NOT
  43. /// necessarily the size of the real bitmap from disk, which might be
  44. /// an atlas texture that is much bigger than this image size).
  45. /// </summary>
  46. public Size PixelSize
  47. {
  48. get
  49. {
  50. return data.PixelSize;
  51. }
  52. }
  53. #endregion
  54. #region BlendMode (Public)
  55. /// <summary>
  56. /// Blend mode used for this image, set in constructor and can't be
  57. /// changed. This is important for the MaterialManager sorting logic!
  58. /// </summary>
  59. public BlendMode BlendMode
  60. {
  61. get
  62. {
  63. return data.BlendMode;
  64. }
  65. }
  66. #endregion
  67. #region UseLinearFiltering (Public)
  68. /// <summary>
  69. /// Helper property to determinate if we need to enable filtering for
  70. /// rendering this image (usually in a shader). True is the default and
  71. /// means we are going to use Trilinear filtering, false means no
  72. /// filtering, which is often called Nearest or Point filtering.
  73. /// Note: For fonts and the default image (4x4 pixels) this is
  74. /// automatically set to false for more accurate and sharp rendering.
  75. /// </summary>
  76. public bool UseLinearFiltering
  77. {
  78. get
  79. {
  80. return data.UseFiltering;
  81. }
  82. }
  83. #endregion
  84. #region AllowTiling (Public)
  85. /// <summary>
  86. /// Allow tiling for this texture? By default this is off and using this
  87. /// prevents us from using atlas textures and many optimizations, so it
  88. /// should be avoided as much as possible.
  89. /// </summary>
  90. public bool AllowTiling
  91. {
  92. get
  93. {
  94. return data.AllowTiling;
  95. }
  96. }
  97. #endregion
  98. #region UV (Public)
  99. /// <summary>
  100. /// The texture coordinates of this image. Normally (0, 0, 1, 1)
  101. /// But necessary when using atlas textures (what we are mostly doing).
  102. /// </summary>
  103. public Rectangle UV
  104. {
  105. get
  106. {
  107. return data.UV;
  108. }
  109. }
  110. #endregion
  111. #region InnerDrawArea (Public)
  112. /// <summary>
  113. /// Helper for the inner rectangle for rendering, which will reduce any
  114. /// draw rectangle by this to make it fit in case there were empty pixels
  115. /// around this image.
  116. /// </summary>
  117. public Rectangle InnerDrawArea
  118. {
  119. get
  120. {
  121. return data.InnerDrawArea;
  122. }
  123. }
  124. #endregion
  125. #region AtlasFileId (Public)
  126. /// <summary>
  127. /// Atlas file id, needed to compare for atlas textures. Each content file
  128. /// with the same PlatformFileId will use the same native texture object
  129. /// (sharing it with all the other content entries using the same atlas).
  130. /// By comparing this we can quickly group all rendering by atlas textures
  131. /// to make best use of draw call batching and speeding up the rendering.
  132. /// </summary>
  133. public int AtlasFileId
  134. {
  135. get
  136. {
  137. return data.PlatformFileId;
  138. }
  139. }
  140. #endregion
  141. #region IsCubeMap (Public)
  142. /// <summary>
  143. /// Is this image data for a cube map texture? Usually off.
  144. /// </summary>
  145. public bool IsCubeMap
  146. {
  147. get
  148. {
  149. return data.IsCubeMap;
  150. }
  151. }
  152. #endregion
  153. #region Constructors
  154. /// <summary>
  155. /// Create a base texture, just makes sure we use the image content type.
  156. /// Use the static Get method to call this.
  157. /// </summary>
  158. /// <param name="setImageName">Set image name</param>
  159. protected ImageData(string setImageName)
  160. : base(setImageName, ContentType.Image)
  161. {
  162. // Nothing to be done here, same for Load, we do not actually load
  163. // any data here, the magic happens in classes that use us.
  164. }
  165. #endregion
  166. #region ToString (Public)
  167. /// <summary>
  168. /// To string, will display the image name and some extra data.
  169. /// </summary>
  170. /// <returns>
  171. /// A <see cref="System.String"/> that represents this ImageData instance
  172. /// with the content name, pixel size, blend mode and uv meta data.
  173. /// </returns>
  174. public override string ToString()
  175. {
  176. return GetType().GetClassName() +
  177. " Name=" + Name +
  178. ", PixelSize=" + data.PixelSize +
  179. ", BlendMode=" + data.BlendMode +
  180. ", UV=" + data.UV;
  181. }
  182. #endregion
  183. #region Methods (Private)
  184. #region Load
  185. /// <summary>
  186. /// Load texture content data. This method does not load the content yet,
  187. /// it only provides the common functionality for all derived classes that
  188. /// actually contain the image data. No loading happens here (it all
  189. /// happens in Texture), we can obviously clone all ImageDatas with the
  190. /// same name as they are not loaded here.
  191. /// </summary>
  192. /// <param name="alreadyLoadedNativeData">If we already have native data
  193. /// loaded, clone it from this instance. Only used if not null. Please
  194. /// note that this is only used for other objects that had the same
  195. /// RawFileId and are already loaded and returned true for this method.
  196. /// </param>
  197. protected override void Load(Content alreadyLoadedNativeData)
  198. {
  199. // Nothing to do here, it all happens in the Texture class
  200. }
  201. #endregion
  202. #endregion
  203. }
  204. }