using System; using System.Collections.Generic; using System.Text; using gr2lib.core.helpers; using gr2lib.core.interfaces; using System.Diagnostics; using gr2lib.core.exceptions; using gr2lib.core.ui.helpers; using System.ComponentModel; using gr2lib.core.typedefs; using gr2lib.core.ui.typeeditors; using System.Drawing.Design; namespace gr2lib.core.coretypes.implementation { /// /// Represents a Granny_Texture /// [Editor(typeof(UITextureEditor), typeof(UITypeEditor))] public class Texture : ITexture { private GrannyRigResource _rigResource; private FilePath _FromFileName; private NameValuePair _TextureType; private int _Width; private int _Height; private NameValuePair _Encoding; private NameValuePair _SubFormat; private Layout _Layout; private List _Images; //private bool _IsTexture; /// /// Gets the string representation of this instance /// /// public override string ToString() { return this.FromFileName.Name; } //private string _ParentResourceName; /// /// Get's the parent resource name /// protected internal string ParentResourceName { get { return "Textures"; } } /// /// default constructor /// public Texture()// : base("Textures") { this.FromFileName = ""; this.TextureType = new NameValuePair(coreapi.EnumToStringSupport.GetGrannyEnumTypeString(granny_texture_type.GrannyColorMapTextureType), granny_texture_type.GrannyColorMapTextureType); this.Width = 0; this.Height = 0; this.Encoding = new NameValuePair(coreapi.EnumToStringSupport.GetGrannyEnumTypeString(granny_texture_encoding.GrannyBinkTextureEncoding), granny_texture_encoding.GrannyBinkTextureEncoding); this.SubFormat = new NameValuePair(coreapi.EnumToStringSupport.GetGrannyEnumTypeString(granny_pixel_layout.GrannyBGRA8888PixelFormat), granny_pixel_layout.GrannyBGRA8888PixelFormat); this.Layout = new Layout(); this.Images = new List(); //this.IsTexture = true; //this.ExtendedData = new IntPtr(); //this.NativePointer = IntPtr.Zero; this._rigResource = new GrannyRigResource(); } /// /// default constructor (specifying index) /// /// the texture's index public Texture(int index) { this.FromFileName = ""; this.TextureType = new NameValuePair(coreapi.EnumToStringSupport.GetGrannyEnumTypeString(granny_texture_type.GrannyColorMapTextureType), granny_texture_type.GrannyColorMapTextureType); this.Width = 0; this.Height = 0; this.Encoding = new NameValuePair(coreapi.EnumToStringSupport.GetGrannyEnumTypeString(granny_texture_encoding.GrannyBinkTextureEncoding), granny_texture_encoding.GrannyBinkTextureEncoding); this.SubFormat = new NameValuePair(coreapi.EnumToStringSupport.GetGrannyEnumTypeString(granny_pixel_layout.GrannyBGRA8888PixelFormat), granny_pixel_layout.GrannyBGRA8888PixelFormat); this.Layout = new Layout(); this.Images = new List(); //this.IsTexture = true; //this.ExtendedData = new IntPtr(); //this.NativePointer = IntPtr.Zero; this._rigResource = new GrannyRigResource(); this.Index = index; } internal static native.Texture AssignToStructure(Texture texture) { native.Texture _texture_struct = new gr2lib.core.coretypes.native.Texture(); //_texture_struct.Encoding = texture.Encoding; //_texture_struct.ExtendedData = texture.ExtendedData; //_texture_struct.FromFileName = texture.FromFileName.FullPath; //_texture_struct.Height = texture.Height; ////_texture_struct.Images = texture.Images; //_texture_struct.Layout = Layout.AssignToStructure(texture.Layout); //_texture_struct.SubFormat = texture.SubFormat; //_texture_struct.TextureType = texture.TextureType; //_texture_struct.Width = texture.Width; return _texture_struct; } internal static Texture ReadFromMemory(IntPtr pointer) { try { if (pointer == IntPtr.Zero) return null; native.Texture native = Helpers.ReadFromMemory(pointer); Texture managed = ReadFromNative(native); managed.NativePointer = pointer; return managed; } catch (Exception ex) { StackTrace st = new StackTrace(true); #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE Granny2ExceptionWriter.WriteToConsole(ex,st); #endif return default(Texture); } } internal static Texture ReadFromNative(native.Texture native) { try { Texture managed = new Texture(); managed.FromFileName = native.FromFileName; managed.TextureType = new NameValuePair(coreapi.EnumToStringSupport.GetGrannyEnumTypeString((granny_texture_type)native.TextureType), (granny_texture_type)native.TextureType); managed.Width = native.Width; managed.Height = native.Height; managed.Encoding = new NameValuePair(coreapi.EnumToStringSupport.GetGrannyEnumTypeString((granny_texture_encoding)native.Encoding), (granny_texture_encoding)native.Encoding); managed.SubFormat = new NameValuePair(coreapi.EnumToStringSupport.GetGrannyEnumTypeString((granny_pixel_layout)native.SubFormat), (granny_pixel_layout)native.SubFormat); managed.Layout = Layout.ReadFromNative(native.Layout); IntPtr[] images = Helpers.ReadPtrArrayFromMemory(native.Images); int length = images.Length; managed.Images = new List(length); foreach (IntPtr ptr in images) { Image _tmp = new Image(); _tmp.NativePointer = ptr; managed.Images.Add(_tmp); } for (int i = 0; i < length; i++) { managed.Images[i] = Image.ReadFromMemory(images[i]); } for (int i = 0; i < managed.Images.Count; i++) { for (int j = 0; j < managed.Images[i].MIPLevels.Count; j++) { for (int k = 0; k < managed.Images[i].MIPLevels[j].Pixels.Count; k++) { managed.Images[i].MIPLevels[j].Pixels[k].ReferenceTexture = managed; } } } managed.ExtendedData = native.ExtendedData; return managed; } catch (Exception ex) { StackTrace st = new StackTrace(true); #if ENABLE_EXCEPTION_OUTPUT_TO_CONSOLE Granny2ExceptionWriter.WriteToConsole(ex,st); #endif return default(Texture); } } #region ITexture Members /// /// Get's the texture's name /// [RefreshPropertiesAttribute(RefreshProperties.All)] public FilePath FromFileName { get { return _FromFileName; } set { _FromFileName = value; } } /// /// Get's the texture's type /// [RefreshPropertiesAttribute(RefreshProperties.All)] [Editor(typeof(UIComboValueSelector), typeof(UITypeEditor))] public NameValuePair TextureType { get { return _TextureType; } set { _TextureType = value; } } /// /// Get's the texture's widh /// [RefreshPropertiesAttribute(RefreshProperties.All)] public int Width { get { return _Width; } set { _Width = value; } } /// /// Get's the texture's height /// [RefreshPropertiesAttribute(RefreshProperties.All)] public int Height { get { return _Height; } set { _Height = value; } } /// /// Get's the texture's encoding /// [RefreshPropertiesAttribute(RefreshProperties.All)] [Editor(typeof(UIComboValueSelector), typeof(UITypeEditor))] public NameValuePair Encoding { get { return _Encoding; } set { _Encoding = value; } } /// /// Get's the texture's subformat /// [RefreshPropertiesAttribute(RefreshProperties.All)] [Editor(typeof(UIComboValueSelector), typeof(UITypeEditor))] public NameValuePair SubFormat { get { return _SubFormat; } set { _SubFormat = value; } } /// /// Get's the texture's Pixel Layout /// [RefreshPropertiesAttribute(RefreshProperties.All)] [Browsable(true)] public Layout Layout { get { return _Layout; } set { _Layout = value; } } /// /// Get a List of Images associated with this texture /// [RefreshPropertiesAttribute(RefreshProperties.All)] public List Images { get { return _Images; } set { _Images = value; } } ///// ///// Indicates if this instance is a texture or not ///// //public bool IsTexture { get { return _IsTexture; } set { _IsTexture = value; } } #endregion #region INativePointer Members private IntPtr _NativePointer; /// /// When used in a derived class, gets the native pointer for this instance /// public IntPtr NativePointer { get { return _NativePointer; } set { _NativePointer = value; } } #endregion #region IExtendedData Members private IntPtr _ExtendedData; /// /// When used in a derived class, gets the ExtendedData pointer for this instance /// public IntPtr ExtendedData { get { return _ExtendedData; } set { _ExtendedData = value; } } #endregion #region IResource Members private int _index; [RefreshPropertiesAttribute(RefreshProperties.All)] internal int Index { get { return _index; } set { _index = value; } } #endregion #region IEmbeddedResource Members [RefreshPropertiesAttribute(RefreshProperties.All)] internal string IndexName { get { return new GrannyRigResource("Texture", this.Index, this.Name).ResourceBlockIndexFormat; } } [RefreshPropertiesAttribute(RefreshProperties.All)] internal string FullName { get { return new GrannyRigResource("Texture", this.Index, this.Name).ResourceBlockFormat; } } [RefreshPropertiesAttribute(RefreshProperties.All)] internal string Name { get { return this.FromFileName; } set { this.FromFileName = value; } } #endregion } }