/[gr2lib]/trunk/gr2lib/core/coretypes/implementation/Texture.cs
ViewVC logotype

Contents of /trunk/gr2lib/core/coretypes/implementation/Texture.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 196 - (show annotations) (download)
Thu Jul 29 07:47:26 2010 UTC (9 years, 4 months ago) by william
File size: 12446 byte(s)
bulk commit

1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using gr2lib.core.helpers;
5
6 using gr2lib.core.interfaces;
7 using System.Diagnostics;
8 using gr2lib.core.exceptions;
9 using gr2lib.core.ui.helpers;
10 using System.ComponentModel;
11 using gr2lib.core.typedefs;
12 using gr2lib.core.ui.typeeditors;
13 using System.Drawing.Design;
14
15 namespace gr2lib.core.coretypes.implementation
16 {
17 /// <summary>
18 /// Represents a Granny_Texture
19 /// </summary>
20 [Editor(typeof(UITextureEditor), typeof(UITypeEditor))]
21 public class Texture : ITexture
22 {
23 private GrannyRigResource _rigResource;
24
25 private FilePath _FromFileName;
26 private NameValuePair<string, granny_texture_type> _TextureType;
27 private int _Width;
28 private int _Height;
29 private NameValuePair<string, granny_texture_encoding> _Encoding;
30 private NameValuePair<string, granny_pixel_layout> _SubFormat;
31 private Layout _Layout;
32 private List<Image> _Images;
33 //private bool _IsTexture;
34 /// <summary>
35 /// Gets the string representation of this instance
36 /// </summary>
37 /// <returns></returns>
38 public override string ToString()
39 {
40 return this.FromFileName.Name;
41 }
42
43 //private string _ParentResourceName;
44 /// <summary>
45 /// Get's the parent resource name
46 /// </summary>
47 protected internal string ParentResourceName { get { return "Textures"; } }
48
49 /// <summary>
50 /// default constructor
51 /// </summary>
52 public Texture()// : base("Textures")
53 {
54 this.FromFileName = "";
55 this.TextureType = new NameValuePair<string, granny_texture_type>(coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_texture_type>(granny_texture_type.GrannyColorMapTextureType), granny_texture_type.GrannyColorMapTextureType);
56 this.Width = 0;
57 this.Height = 0;
58 this.Encoding = new NameValuePair<string, granny_texture_encoding>(coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_texture_encoding>(granny_texture_encoding.GrannyBinkTextureEncoding), granny_texture_encoding.GrannyBinkTextureEncoding);
59 this.SubFormat = new NameValuePair<string, granny_pixel_layout>(coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_pixel_layout>(granny_pixel_layout.GrannyBGRA8888PixelFormat), granny_pixel_layout.GrannyBGRA8888PixelFormat);
60 this.Layout = new Layout();
61 this.Images = new List<Image>();
62 //this.IsTexture = true;
63 //this.ExtendedData = new IntPtr();
64 //this.NativePointer = IntPtr.Zero;
65 this._rigResource = new GrannyRigResource();
66 }
67 /// <summary>
68 /// default constructor (specifying index)
69 /// </summary>
70 /// <param name="index">the texture's index</param>
71 public Texture(int index)
72 {
73 this.FromFileName = "";
74 this.TextureType = new NameValuePair<string, granny_texture_type>(coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_texture_type>(granny_texture_type.GrannyColorMapTextureType), granny_texture_type.GrannyColorMapTextureType);
75 this.Width = 0;
76 this.Height = 0;
77 this.Encoding = new NameValuePair<string, granny_texture_encoding>(coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_texture_encoding>(granny_texture_encoding.GrannyBinkTextureEncoding), granny_texture_encoding.GrannyBinkTextureEncoding);
78 this.SubFormat = new NameValuePair<string, granny_pixel_layout>(coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_pixel_layout>(granny_pixel_layout.GrannyBGRA8888PixelFormat), granny_pixel_layout.GrannyBGRA8888PixelFormat);
79 this.Layout = new Layout();
80 this.Images = new List<Image>();
81 //this.IsTexture = true;
82 //this.ExtendedData = new IntPtr();
83 //this.NativePointer = IntPtr.Zero;
84 this._rigResource = new GrannyRigResource();
85 this.Index = index;
86 }
87
88 internal static native.Texture AssignToStructure(Texture texture)
89 {
90 native.Texture _texture_struct = new gr2lib.core.coretypes.native.Texture();
91
92 //_texture_struct.Encoding = texture.Encoding;
93 //_texture_struct.ExtendedData = texture.ExtendedData;
94 //_texture_struct.FromFileName = texture.FromFileName.FullPath;
95 //_texture_struct.Height = texture.Height;
96 ////_texture_struct.Images = texture.Images;
97 //_texture_struct.Layout = Layout.AssignToStructure(texture.Layout);
98 //_texture_struct.SubFormat = texture.SubFormat;
99 //_texture_struct.TextureType = texture.TextureType;
100 //_texture_struct.Width = texture.Width;
101
102 return _texture_struct;
103 }
104
105 internal static Texture ReadFromMemory(IntPtr pointer)
106 {
107 try
108 {
109 if (pointer == IntPtr.Zero) return null;
110 native.Texture native = Helpers.ReadFromMemory<native.Texture>(pointer);
111 Texture managed = ReadFromNative(native);
112 managed.NativePointer = pointer;
113 return managed;
114 }
115 catch (Exception ex)
116 {
117 StackTrace st = new StackTrace(true);
118 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
119 Granny2ExceptionWriter.WriteToConsole(ex,st);
120 #endif
121 return default(Texture);
122 }
123 }
124
125 internal static Texture ReadFromNative(native.Texture native)
126 {
127 try
128 {
129 Texture managed = new Texture();
130 managed.FromFileName = native.FromFileName;
131 managed.TextureType = new NameValuePair<string, granny_texture_type>(coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_texture_type>((granny_texture_type)native.TextureType), (granny_texture_type)native.TextureType);
132 managed.Width = native.Width;
133 managed.Height = native.Height;
134 managed.Encoding = new NameValuePair<string, granny_texture_encoding>(coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_texture_encoding>((granny_texture_encoding)native.Encoding), (granny_texture_encoding)native.Encoding);
135 managed.SubFormat = new NameValuePair<string, granny_pixel_layout>(coreapi.EnumToStringSupport.GetGrannyEnumTypeString<granny_pixel_layout>((granny_pixel_layout)native.SubFormat), (granny_pixel_layout)native.SubFormat);
136 managed.Layout = Layout.ReadFromNative(native.Layout);
137 IntPtr[] images = Helpers.ReadPtrArrayFromMemory<native.Image>(native.Images);
138 int length = images.Length;
139 managed.Images = new List<Image>(length);
140 foreach (IntPtr ptr in images)
141 {
142 Image _tmp = new Image();
143 _tmp.NativePointer = ptr;
144 managed.Images.Add(_tmp);
145 }
146 for (int i = 0; i < length; i++)
147 {
148 managed.Images[i] = Image.ReadFromMemory(images[i]);
149 }
150
151 for (int i = 0; i < managed.Images.Count; i++)
152 {
153 for (int j = 0; j < managed.Images[i].MIPLevels.Count; j++)
154 {
155 for (int k = 0; k < managed.Images[i].MIPLevels[j].Pixels.Count; k++)
156 {
157 managed.Images[i].MIPLevels[j].Pixels[k].ReferenceTexture = managed;
158 }
159 }
160 }
161
162
163 managed.ExtendedData = native.ExtendedData;
164 return managed;
165 }
166 catch (Exception ex)
167 {
168 StackTrace st = new StackTrace(true);
169 #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE
170 Granny2ExceptionWriter.WriteToConsole(ex,st);
171 #endif
172 return default(Texture);
173 }
174 }
175
176 #region ITexture Members
177 /// <summary>
178 /// Get's the texture's name
179 /// </summary>
180 [RefreshPropertiesAttribute(RefreshProperties.All)]
181 public FilePath FromFileName { get { return _FromFileName; } set { _FromFileName = value; } }
182 /// <summary>
183 /// Get's the texture's type
184 /// </summary>
185 [RefreshPropertiesAttribute(RefreshProperties.All)]
186 [Editor(typeof(UIComboValueSelector<granny_texture_type, int>), typeof(UITypeEditor))]
187 public NameValuePair<string, granny_texture_type> TextureType { get { return _TextureType; } set { _TextureType = value; } }
188 /// <summary>
189 /// Get's the texture's widh
190 /// </summary>
191 [RefreshPropertiesAttribute(RefreshProperties.All)]
192 public int Width { get { return _Width; } set { _Width = value; } }
193 /// <summary>
194 /// Get's the texture's height
195 /// </summary>
196 [RefreshPropertiesAttribute(RefreshProperties.All)]
197 public int Height { get { return _Height; } set { _Height = value; } }
198 /// <summary>
199 /// Get's the texture's encoding
200 /// </summary>
201 [RefreshPropertiesAttribute(RefreshProperties.All)]
202 [Editor(typeof(UIComboValueSelector<granny_texture_encoding, int>), typeof(UITypeEditor))]
203 public NameValuePair<string, granny_texture_encoding> Encoding { get { return _Encoding; } set { _Encoding = value; } }
204 /// <summary>
205 /// Get's the texture's subformat
206 /// </summary>
207 [RefreshPropertiesAttribute(RefreshProperties.All)]
208 [Editor(typeof(UIComboValueSelector<granny_pixel_layout, int>), typeof(UITypeEditor))]
209 public NameValuePair<string, granny_pixel_layout> SubFormat { get { return _SubFormat; } set { _SubFormat = value; } }
210 /// <summary>
211 /// Get's the texture's Pixel Layout
212 /// </summary>
213 [RefreshPropertiesAttribute(RefreshProperties.All)]
214 [Browsable(true)]
215 public Layout Layout { get { return _Layout; } set { _Layout = value; } }
216 /// <summary>
217 /// Get a List of Images associated with this texture
218 /// </summary>
219 [RefreshPropertiesAttribute(RefreshProperties.All)]
220 public List<Image> Images { get { return _Images; } set { _Images = value; } }
221
222
223 ///// <summary>
224 ///// Indicates if this instance is a texture or not
225 ///// </summary>
226 //public bool IsTexture { get { return _IsTexture; } set { _IsTexture = value; } }
227 #endregion
228
229 #region INativePointer Members
230 private IntPtr _NativePointer;
231 /// <summary>
232 /// When used in a derived class, gets the native pointer for this instance
233 /// </summary>
234 public IntPtr NativePointer { get { return _NativePointer; } set { _NativePointer = value; } }
235 #endregion
236
237 #region IExtendedData Members
238 private IntPtr _ExtendedData;
239 /// <summary>
240 /// When used in a derived class, gets the ExtendedData pointer for this instance
241 /// </summary>
242 public IntPtr ExtendedData { get { return _ExtendedData; } set { _ExtendedData = value; } }
243 #endregion
244
245 #region IResource Members
246 private int _index;
247 [RefreshPropertiesAttribute(RefreshProperties.All)]
248 internal int Index { get { return _index; } set { _index = value; } }
249 #endregion
250 #region IEmbeddedResource Members
251 [RefreshPropertiesAttribute(RefreshProperties.All)]
252 internal string IndexName { get { return new GrannyRigResource("Texture", this.Index, this.Name).ResourceBlockIndexFormat; } }
253 [RefreshPropertiesAttribute(RefreshProperties.All)]
254 internal string FullName { get { return new GrannyRigResource("Texture", this.Index, this.Name).ResourceBlockFormat; } }
255 [RefreshPropertiesAttribute(RefreshProperties.All)]
256 internal string Name { get { return this.FromFileName; } set { this.FromFileName = value; } }
257 #endregion
258 }
259 }

  ViewVC Help
Powered by ViewVC 1.1.22