public static Texture2D GetTextureFromImg(string name, out DFPosition offset, TextureFormat format = TextureFormat.ARGB32, bool readOnly = true) { offset = new DFPosition(); DaggerfallUnity dfUnity = DaggerfallUnity.Instance; if (!dfUnity.IsReady) { return(null); } ImgFile imgFile = new ImgFile(Path.Combine(dfUnity.Arena2Path, name), FileUsage.UseMemory, readOnly); Texture2D texture; if (!TextureReplacement.TryImportImage(name, out texture)) { imgFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, imgFile.PaletteName)); texture = GetTextureFromImg(imgFile, format, readOnly); } texture.filterMode = DaggerfallUI.Instance.GlobalFilterMode; offset = imgFile.ImageOffset; return(texture); }
void LoadAssets() { const string compassFilename = "COMPASS.IMG"; const string compassBoxFilename = "COMPBOX.IMG"; if (!dfUnity.IsReady) { return; } if (!compassTexture) { ImgFile file = new ImgFile(Path.Combine(dfUnity.Arena2Path, compassFilename), FileUsage.UseMemory, true); file.LoadPalette(Path.Combine(dfUnity.Arena2Path, file.PaletteName)); compassTexture = GetTextureFromImg(file); compassTexture.filterMode = dfUnity.MaterialReader.MainFilterMode; } if (!compassBoxTexture) { ImgFile file = new ImgFile(Path.Combine(dfUnity.Arena2Path, compassBoxFilename), FileUsage.UseMemory, true); file.LoadPalette(Path.Combine(dfUnity.Arena2Path, file.PaletteName)); compassBoxTexture = GetTextureFromImg(file); compassBoxTexture.filterMode = dfUnity.MaterialReader.MainFilterMode; } assetsLoaded = true; }
/// <summary> /// Loads IMG file to texture using a subrect of source image. /// Origin of source image (0,0) is bottom-left corner. /// </summary> public static Texture2D GetTextureFromImg(string name, Rect subRect, TextureFormat format = TextureFormat.ARGB32, bool readOnly = true) { ImgFile imgFile = new ImgFile(Path.Combine(DaggerfallUnity.Instance.Arena2Path, name), FileUsage.UseMemory, readOnly); imgFile.LoadPalette(Path.Combine(DaggerfallUnity.Instance.Arena2Path, imgFile.PaletteName)); DFBitmap bitmap = imgFile.GetDFBitmap(); Color32[] colors = imgFile.GetColor32(bitmap, 0); // Invert Y as Unity textures have origin 0,0 at bottom-left and UI expects top-left subRect.y = bitmap.Height - subRect.height; Color32[] newColors = new Color32[(int)subRect.width * (int)subRect.height]; ImageProcessing.CopyColors( ref colors, ref newColors, new DFSize(bitmap.Width, bitmap.Height), new DFSize((int)subRect.width, (int)subRect.height), new DFPosition((int)subRect.x, (int)subRect.y), new DFPosition(0, 0), new DFSize((int)subRect.width, (int)subRect.height)); Texture2D texture = new Texture2D((int)subRect.width, (int)subRect.height, format, false); texture.SetPixels32(newColors, 0); texture.Apply(false, true); texture.filterMode = DaggerfallUI.Instance.GlobalFilterMode; return(texture); }
public static Texture2D GetTextureFromImg(string name, out DFPosition offset, TextureFormat format = TextureFormat.ARGB32) { offset = new DFPosition(); DaggerfallUnity dfUnity = DaggerfallUnity.Instance; if (!dfUnity.IsReady) { return(null); } ImgFile imgFile = new ImgFile(Path.Combine(dfUnity.Arena2Path, name), FileUsage.UseMemory, true); Texture2D texture = null; // Custom texture if (TextureReplacement.CustomImageExist(name)) { texture = TextureReplacement.LoadCustomImage(name); } // Daggerfall texture else { imgFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, imgFile.PaletteName)); texture = GetTextureFromImg(imgFile, format); } texture.filterMode = DaggerfallUI.Instance.GlobalFilterMode; offset = imgFile.ImageOffset; return(texture); }
public LeftRightSpinner() { DaggerfallUnity dfUnity = DaggerfallUnity.Instance; if (!dfUnity.IsReady) { return; } // Load IMG texture ImgFile imgFile = new ImgFile(Path.Combine(dfUnity.Arena2Path, nativeImgName), FileUsage.UseMemory, true); imgFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, imgFile.PaletteName)); nativeTexture = TextureReader.CreateFromAPIImage(imgFile, 0, 0, 0); nativeTexture.filterMode = DaggerfallUI.Instance.GlobalFilterMode; // Setup spinner panel Size = TextureReplacement.GetSize(nativeTexture, nativeImgName); backgroundTexture = nativeTexture; // Add up/down buttons Components.Add(leftButton); Components.Add(rightButton); leftButton.Position = new Vector2(0, 0); leftButton.Size = new Vector2(11, 9); leftButton.OnMouseClick += LeftButton_OnMouseClick; leftButton.OnMouseDown += LeftButton_OnMouseDown; leftButton.OnMouseUp += LeftRightButtons_OnMouseUp; rightButton.Position = new Vector2(26, 0); rightButton.Size = new Vector2(11, 9); rightButton.OnMouseClick += RightButton_OnMouseClick; rightButton.OnMouseDown += RightButton_OnMouseDown; rightButton.OnMouseUp += LeftRightButtons_OnMouseUp; // Add value label Components.Add(valueLabel); valueLabel.Position = new Vector2(0, 2); valueLabel.Size = new Vector2(15, 9); valueLabel.HorizontalAlignment = HorizontalAlignment.Center; valueLabel.ShadowColor = DaggerfallUI.DaggerfallAlternateShadowColor1; SetValue(this.value); }
public UpDownSpinner() { DaggerfallUnity dfUnity = DaggerfallUnity.Instance; if (!dfUnity.IsReady) { return; } // Load IMG texture ImgFile imgFile = new ImgFile(Path.Combine(dfUnity.Arena2Path, nativeImgName), FileUsage.UseMemory, true); imgFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, imgFile.PaletteName)); nativeTexture = TextureReader.CreateFromAPIImage(imgFile, 0, 0, 0); nativeTexture.filterMode = DaggerfallUI.Instance.GlobalFilterMode; // Setup spinner panel Size = new Vector2(15, 20); backgroundTexture = nativeTexture; // Add up/down buttons Components.Add(upButton); Components.Add(downButton); upButton.Position = new Vector2(0, 0); upButton.Size = new Vector2(15, 7); upButton.OnMouseClick += UpButton_OnMouseClick; downButton.Position = new Vector2(0, 13); downButton.Size = new Vector2(15, 7); downButton.OnMouseClick += DownButton_OnMouseClick; // Add value label Components.Add(valueLabel); valueLabel.Position = new Vector2(0, 7); valueLabel.Size = new Vector2(15, 6); valueLabel.HorizontalAlignment = HorizontalAlignment.Center; valueLabel.ShadowColor = DaggerfallUI.DaggerfallAlternateShadowColor1; SetValue(this.value); }
void SetupControl() { dfUnity = DaggerfallUnity.Instance; if (!dfUnity.IsReady) { return; } // Load highlight texture ImgFile imgFile = new ImgFile(Path.Combine(dfUnity.Arena2Path, highlightImgName), FileUsage.UseMemory, true); imgFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, imgFile.PaletteName)); highlightTexture = TextureReader.CreateFromAPIImage(imgFile, 0, 0, 0); highlightTexture.filterMode = DaggerfallUI.Instance.GlobalFilterMode; // Add buttons Vector2 buttonPos = new Vector2(0, 0); Vector2 buttonSize = new Vector2(66, 9); for (int i = 0; i < 5; i++) { selectButtons[i] = new Button(); this.Components.Add(selectButtons[i]); selectButtons[i].Position = buttonPos; selectButtons[i].Size = buttonSize; selectButtons[i].Tag = i; selectButtons[i].OnMouseClick += ReflexButton_OnMouseClick; buttonPos.y += buttonSize.y; } // Set size of this panel Size = new Vector2(66, 45); // Set starting value PlayerReflexes = PlayerReflexes.Average; }
/// <summary> /// Reads any Daggerfall image file to ImageData package. /// </summary> /// <param name="filename">Name of standalone file as it appears in arena2 folder.</param> /// <param name="record">Which image record to read for multi-image files.</param> /// <param name="frame">Which frame to read for multi-frame images.</param> /// <param name="hasAlpha">Enable this for image cutouts.</param> /// <param name="createTexture">Create a Texture2D.</param> /// <param name="createAllFrameTextures">Creates a Texture2D for every frame in a TEXTURE file (if greater than 1 frames).</param> /// <param name="alphaIndex">Set palette index for alpha checks (default is 0).</param> /// <returns>ImageData. If result.type == ImageTypes.None then read failed.</returns> public static ImageData GetImageData(string filename, int record = 0, int frame = 0, bool hasAlpha = false, bool createTexture = true, bool createAllFrameTextures = false, int alphaIndex = 0) { // Check API ready DaggerfallUnity dfUnity = DaggerfallUnity.Instance; if (!dfUnity.IsReady) { return(new ImageData()); } // Parse image file type ImageTypes fileType; try { fileType = ParseFileType(filename); } catch { return(new ImageData()); } // Create base image data ImageData imageData = new ImageData(); imageData.type = fileType; imageData.filename = filename; imageData.record = record; imageData.frame = frame; imageData.hasAlpha = hasAlpha; imageData.alphaIndex = alphaIndex; // Read supported image files DFBitmap dfBitmap = null; DFBitmap[] dfBitmapAllFrames = null; switch (fileType) { case ImageTypes.TEXTURE: TextureFile textureFile = new TextureFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true); textureFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, textureFile.PaletteName)); dfBitmap = textureFile.GetDFBitmap(record, frame); int frameCount = textureFile.GetFrameCount(record); if (createAllFrameTextures && frameCount > 1) { dfBitmapAllFrames = new DFBitmap[frameCount]; for (int i = 0; i < frameCount; i++) { dfBitmapAllFrames[i] = textureFile.GetDFBitmap(record, i); } } imageData.offset = textureFile.GetOffset(record); imageData.scale = textureFile.GetScale(record); imageData.size = textureFile.GetSize(record); // Texture pack support int archive = AssetInjection.TextureReplacement.FileNameToArchive(filename); if (createTexture && AssetInjection.TextureReplacement.TryImportTexture(archive, record, frame, out imageData.texture)) { createTexture = false; } if (createAllFrameTextures && frameCount > 1 && AssetInjection.TextureReplacement.TryImportTexture(archive, record, out imageData.animatedTextures)) { createAllFrameTextures = false; } break; case ImageTypes.IMG: ImgFile imgFile = new ImgFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true); imgFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, imgFile.PaletteName)); dfBitmap = imgFile.GetDFBitmap(); imageData.offset = imgFile.ImageOffset; imageData.scale = new DFSize(); imageData.size = imgFile.GetSize(0); // Texture pack support if (createTexture && AssetInjection.TextureReplacement.TryImportImage(filename, false, out imageData.texture)) { createTexture = false; } break; case ImageTypes.CIF: case ImageTypes.RCI: CifRciFile cifFile = new CifRciFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true); cifFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, cifFile.PaletteName)); dfBitmap = cifFile.GetDFBitmap(record, frame); imageData.offset = cifFile.GetOffset(record); imageData.scale = new DFSize(); imageData.size = cifFile.GetSize(record); // Texture pack support if (createTexture && AssetInjection.TextureReplacement.TryImportCifRci(filename, record, frame, false, out imageData.texture)) { createTexture = false; } break; case ImageTypes.CFA: CfaFile cfaFile = new CfaFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true); cfaFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, cfaFile.PaletteName)); dfBitmap = cfaFile.GetDFBitmap(record, frame); imageData.offset = new DFPosition(0, 0); imageData.scale = new DFSize(); imageData.size = cfaFile.GetSize(record); // Texture pack support if (createTexture && AssetInjection.TextureReplacement.TryImportCifRci(filename, record, frame, false, out imageData.texture)) { createTexture = false; } break; case ImageTypes.BSS: BssFile bssFile = new BssFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true); bssFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, bssFile.PaletteName)); dfBitmap = bssFile.GetDFBitmap(record, frame); imageData.offset = new DFPosition(0, 0); imageData.scale = new DFSize(); imageData.size = bssFile.GetSize(record); // Texture pack support if (createTexture && AssetInjection.TextureReplacement.TryImportCifRci(filename, record, frame, false, out imageData.texture)) { createTexture = false; } break; case ImageTypes.GFX: GfxFile gfxFile = new GfxFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true); gfxFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, gfxFile.PaletteName)); dfBitmap = gfxFile.GetDFBitmap(record, frame); imageData.offset = new DFPosition(0, 0); imageData.scale = new DFSize(); imageData.size = gfxFile.GetSize(record); // Texture pack support if (createTexture && AssetInjection.TextureReplacement.TryImportCifRci(filename, record, frame, false, out imageData.texture)) { createTexture = false; } break; default: return(new ImageData()); } // Store bitmap imageData.dfBitmap = dfBitmap; imageData.width = dfBitmap.Width; imageData.height = dfBitmap.Height; // Create Texture2D if (createTexture) { // Get colors array Color32[] colors = GetColors(imageData); if (colors == null) { return(new ImageData()); } // Create new Texture2D imageData.texture = GetTexture(colors, imageData.width, imageData.height); } // Create animated Texture2D frames if (createAllFrameTextures && dfBitmapAllFrames != null) { imageData.animatedTextures = new Texture2D[dfBitmapAllFrames.Length]; for (int i = 0; i < dfBitmapAllFrames.Length; i++) { ImageData curFrame = imageData; curFrame.dfBitmap = dfBitmapAllFrames[i]; Color32[] colors = GetColors(curFrame); imageData.animatedTextures[i] = GetTexture(colors, imageData.width, imageData.height); } } return(imageData); }
/// <summary> /// Reads any Daggerfall image file to ImageData package. /// </summary> /// <param name="filename">Name of standalone file as it appears in arena2 folder.</param> /// <param name="record">Which image record to read for multi-image files.</param> /// <param name="frame">Which frame to read for multi-frame images.</param> /// <param name="hasAlpha">Enable this for image cutouts.</param> /// <param name="createTexture">Create a Texture2D.</param> /// <returns>ImageData. If result.type == ImageTypes.None then read failed.</returns> public static ImageData GetImageData(string filename, int record = 0, int frame = 0, bool hasAlpha = false, bool createTexture = true) { // Check API ready DaggerfallUnity dfUnity = DaggerfallUnity.Instance; if (!dfUnity.IsReady) { return(new ImageData()); } // Parse image file type ImageTypes fileType; try { fileType = ParseFileType(filename); } catch { return(new ImageData()); } // Create base image data ImageData imageData = new ImageData(); imageData.type = fileType; imageData.filename = filename; imageData.record = record; imageData.frame = frame; imageData.hasAlpha = hasAlpha; // Read supported image files DFBitmap dfBitmap = null; switch (fileType) { case ImageTypes.TEXTURE: TextureFile textureFile = new TextureFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true); textureFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, textureFile.PaletteName)); dfBitmap = textureFile.GetDFBitmap(record, frame); imageData.offset = textureFile.GetOffset(record); imageData.scale = textureFile.GetScale(record); imageData.size = textureFile.GetSize(record); break; case ImageTypes.IMG: ImgFile imgFile = new ImgFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true); imgFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, imgFile.PaletteName)); dfBitmap = imgFile.GetDFBitmap(); imageData.offset = imgFile.ImageOffset; imageData.scale = new DFSize(); imageData.size = imgFile.GetSize(0); break; case ImageTypes.CIF: case ImageTypes.RCI: CifRciFile cifFile = new CifRciFile(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true); cifFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, cifFile.PaletteName)); dfBitmap = cifFile.GetDFBitmap(record, frame); imageData.offset = cifFile.GetOffset(record); imageData.scale = new DFSize(); imageData.size = cifFile.GetSize(record); break; default: return(new ImageData()); } // Store bitmap imageData.dfBitmap = dfBitmap; imageData.width = dfBitmap.Width; imageData.height = dfBitmap.Height; // Create Texture2D if (createTexture) { // Get colors array Color32[] colors = GetColors(imageData); if (colors == null) { return(new ImageData()); } // Create new Texture2D imageData.texture = GetTexture(colors, imageData.width, imageData.height); } return(imageData); }