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