/// <summary> /// Unload texture /// </summary> private void UnloadTexture(bool innerTextureOnly = false) { if (this.ninePatchTexture != null) { //WaveServices.GraphicsDevice.Textures.DestroyTexture(this.ninePatchTexture.InnerTexture); this.ninePatchTexture = null; if (!innerTextureOnly) { this.InternalUnloadTexture(); } } }
/// <summary> /// Updates the strechable rectangles and inner texture. /// </summary> /// <param name="textureId">The nine patch texture id</param> /// <param name="sourceTexture">The source texture.</param> /// <param name="sourceRectangle">The source rectangle.</param> private void UpdateNinePatchData(string textureId, Texture sourceTexture, Rectangle sourceRectangle) { NinePatchTextureInfo textureInfo; if (innerTextureInfoCache.TryGetValue(textureId, out textureInfo)) { this.ninePatchTexture = textureInfo; } else { var stretchableRectangle = RectangleF.Empty; var stretchableRectangleCoords = RectangleF.Empty; var innerTexture = new Texture2D() { Width = sourceRectangle.Width - 2, Height = sourceRectangle.Height - 2, Format = PixelFormat.R8G8B8A8, Levels = 1, Data = new byte[1][][] }; var textureData = sourceTexture.GetData(); // Horizontal stretchable area search int index = 0; bool startFound = false; var rectangleOffset = (sourceTexture.Width * sourceRectangle.Y) + sourceRectangle.X; for (; index < sourceRectangle.Width; index++) { var pixelColor = textureData[rectangleOffset + index]; if (startFound ^ (pixelColor == Color.Black)) { if (!startFound) { stretchableRectangle.X = index - 1; stretchableRectangleCoords.X = stretchableRectangle.X / innerTexture.Width; startFound = true; } else { stretchableRectangle.Width = index - stretchableRectangle.X - 1; stretchableRectangleCoords.Width = stretchableRectangle.Width / innerTexture.Width; break; } } } // Vertical stretchable area search index = 0; startFound = false; for (; index < sourceRectangle.Height; index++) { var pixelColor = textureData[rectangleOffset + (index * sourceTexture.Width)]; if (startFound ^ (pixelColor == Color.Black)) { if (!startFound) { stretchableRectangle.Y = index - 1; stretchableRectangleCoords.Y = stretchableRectangle.Y / innerTexture.Height; startFound = true; } else { stretchableRectangle.Height = index - stretchableRectangle.Y - 1; stretchableRectangleCoords.Height = stretchableRectangle.Height / innerTexture.Height; break; } } } // Fill innerTexture data innerTexture.Data[0] = new byte[1][]; innerTexture.Data[0][0] = new byte[4 * innerTexture.Width * innerTexture.Height]; int destinationIndex = 0; for (int i = 1; i < sourceRectangle.Height - 1; i++) { for (int j = 1; j < sourceRectangle.Width - 1; j++) { var pixelColor = textureData[rectangleOffset + (i * (sourceTexture.Width)) + j]; innerTexture.Data[0][0][destinationIndex++] = pixelColor.R; innerTexture.Data[0][0][destinationIndex++] = pixelColor.G; innerTexture.Data[0][0][destinationIndex++] = pixelColor.B; innerTexture.Data[0][0][destinationIndex++] = pixelColor.A; } } WaveServices.GraphicsDevice.Textures.UploadTexture(innerTexture); this.ninePatchTexture = new NinePatchTextureInfo() { InnerTexture = innerTexture, StretchableRectangle = stretchableRectangle, StretchableRectangleCoords = stretchableRectangleCoords }; innerTextureInfoCache.Add(textureId, this.ninePatchTexture); } (this.material as StandardMaterial).Diffuse = this.ninePatchTexture.InnerTexture; if (this.size == Vector2.Zero) { this.size = new Vector2(this.ninePatchTexture.InnerTexture.Width, this.ninePatchTexture.InnerTexture.Height); } }