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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 160 - (hide annotations) (download)
Thu Jul 22 10:20:55 2010 UTC (9 years, 4 months ago) by william
File size: 10821 byte(s)
Add UIEditor to edit EnumType Values via a dropdown combobox

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

  ViewVC Help
Powered by ViewVC 1.1.22