//////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// private static Rectangle GetSourceArea(Size imageSize, Size partSize, Margins margins, Alignment alignment, int index) { Rectangle rect = new Rectangle(); int xc = (int)((float)imageSize.Width / partSize.Width); int yc = (int)((float)imageSize.Height / partSize.Height); int xm = (index) % xc; int ym = (index) / xc; int adj = 1; margins.Left += margins.Left > 0 ? adj : 0; margins.Top += margins.Top > 0 ? adj : 0; margins.Right += margins.Right > 0 ? adj : 0; margins.Bottom += margins.Bottom > 0 ? adj : 0; margins = new Margins(margins.Left, margins.Top, margins.Right, margins.Bottom); switch (alignment) { case Alignment.TopLeft: { rect = new Rectangle((0 + (xm * partSize.Width)), (0 + (ym * partSize.Height)), margins.Left, margins.Top); break; } case Alignment.TopCenter: { rect = new Rectangle((0 + (xm * partSize.Width)) + margins.Left, (0 + (ym * partSize.Height)), partSize.Width - margins.Left - margins.Right, margins.Top); break; } case Alignment.TopRight: { rect = new Rectangle((partSize.Width + (xm * partSize.Width)) - margins.Right, (0 + (ym * partSize.Height)), margins.Right, margins.Top); break; } case Alignment.MiddleLeft: { rect = new Rectangle((0 + (xm * partSize.Width)), (0 + (ym * partSize.Height)) + margins.Top, margins.Left, partSize.Height - margins.Top - margins.Bottom); break; } case Alignment.MiddleCenter: { rect = new Rectangle((0 + (xm * partSize.Width)) + margins.Left, (0 + (ym * partSize.Height)) + margins.Top, partSize.Width - margins.Left - margins.Right, partSize.Height - margins.Top - margins.Bottom); break; } case Alignment.MiddleRight: { rect = new Rectangle((partSize.Width + (xm * partSize.Width)) - margins.Right, (0 + (ym * partSize.Height)) + margins.Top, margins.Right, partSize.Height - margins.Top - margins.Bottom); break; } case Alignment.BottomLeft: { rect = new Rectangle((0 + (xm * partSize.Width)), (partSize.Height + (ym * partSize.Height)) - margins.Bottom, margins.Left, margins.Bottom); break; } case Alignment.BottomCenter: { rect = new Rectangle((0 + (xm * partSize.Width)) + margins.Left, (partSize.Height + (ym * partSize.Height)) - margins.Bottom, partSize.Width - margins.Left - margins.Right, margins.Bottom); break; } case Alignment.BottomRight: { rect = new Rectangle((partSize.Width + (xm * partSize.Width)) - margins.Right, (partSize.Height + (ym * partSize.Height)) - margins.Bottom, margins.Right, margins.Bottom); break; } } return rect; }
//////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// public virtual void DrawLayer(SkinLayer layer, Rectangle rect, Color color, int index) { Size imageSize = new Size(layer.Image.Resource.Width, layer.Image.Resource.Height); Size partSize = new Size(layer.Width, layer.Height); Draw(layer.Image.Resource, GetDestinationArea(rect, layer.SizingMargins, Alignment.TopLeft), GetSourceArea(imageSize, partSize, layer.SizingMargins, Alignment.TopLeft, index), color); Draw(layer.Image.Resource, GetDestinationArea(rect, layer.SizingMargins, Alignment.TopCenter), GetSourceArea(imageSize, partSize, layer.SizingMargins, Alignment.TopCenter, index), color); Draw(layer.Image.Resource, GetDestinationArea(rect, layer.SizingMargins, Alignment.TopRight), GetSourceArea(imageSize, partSize, layer.SizingMargins, Alignment.TopRight, index), color); Draw(layer.Image.Resource, GetDestinationArea(rect, layer.SizingMargins, Alignment.MiddleLeft), GetSourceArea(imageSize, partSize, layer.SizingMargins, Alignment.MiddleLeft, index), color); Draw(layer.Image.Resource, GetDestinationArea(rect, layer.SizingMargins, Alignment.MiddleCenter), GetSourceArea(imageSize, partSize, layer.SizingMargins, Alignment.MiddleCenter, index), color); Draw(layer.Image.Resource, GetDestinationArea(rect, layer.SizingMargins, Alignment.MiddleRight), GetSourceArea(imageSize, partSize, layer.SizingMargins, Alignment.MiddleRight, index), color); Draw(layer.Image.Resource, GetDestinationArea(rect, layer.SizingMargins, Alignment.BottomLeft), GetSourceArea(imageSize, partSize, layer.SizingMargins, Alignment.BottomLeft, index), color); Draw(layer.Image.Resource, GetDestinationArea(rect, layer.SizingMargins, Alignment.BottomCenter), GetSourceArea(imageSize, partSize, layer.SizingMargins, Alignment.BottomCenter, index), color); Draw(layer.Image.Resource, GetDestinationArea(rect, layer.SizingMargins, Alignment.BottomRight), GetSourceArea(imageSize, partSize, layer.SizingMargins, Alignment.BottomRight, index), color); }
/// <summary> /// Used to grab a piece of the source region of a skin resource /// when multiple assets are packed into a single image file. /// </summary> /// <param name="imageSize">Size of the entire image.</param> /// <param name="partSize">Size of the asset piece to retrieve the source region for.</param> /// <param name="margins">???</param> /// <param name="alignment">???</param> /// <param name="index">Index specifying where on the source image, the asset we want is located.</param> /// <returns>Returns a rectangle that specifies the location and size of the asset that was requested.</returns> private static Rectangle GetSourceArea(Size imageSize, Size partSize, Margins margins, Alignment alignment, int index) { Rectangle rect = new Rectangle(); // Break the image down into rows and columns. int xc = (int)((float)imageSize.Width / partSize.Width); int yc = (int)((float)imageSize.Height / partSize.Height); // Figure out which row and column the asset is located at. int xm = (index) % xc; int ym = (index) / xc; int adj = 1; margins.Left += margins.Left > 0 ? adj : 0; margins.Top += margins.Top > 0 ? adj : 0; margins.Right += margins.Right > 0 ? adj : 0; margins.Bottom += margins.Bottom > 0 ? adj : 0; margins = new Margins(margins.Left, margins.Top, margins.Right, margins.Bottom); // NOTE: Baffled... switch (alignment) { case Alignment.TopLeft: { rect = new Rectangle((0 + (xm * partSize.Width)), (0 + (ym * partSize.Height)), margins.Left, margins.Top); break; } case Alignment.TopCenter: { rect = new Rectangle((0 + (xm * partSize.Width)) + margins.Left, (0 + (ym * partSize.Height)), partSize.Width - margins.Left - margins.Right, margins.Top); break; } case Alignment.TopRight: { rect = new Rectangle((partSize.Width + (xm * partSize.Width)) - margins.Right, (0 + (ym * partSize.Height)), margins.Right, margins.Top); break; } case Alignment.MiddleLeft: { rect = new Rectangle((0 + (xm * partSize.Width)), (0 + (ym * partSize.Height)) + margins.Top, margins.Left, partSize.Height - margins.Top - margins.Bottom); break; } case Alignment.MiddleCenter: { rect = new Rectangle((0 + (xm * partSize.Width)) + margins.Left, (0 + (ym * partSize.Height)) + margins.Top, partSize.Width - margins.Left - margins.Right, partSize.Height - margins.Top - margins.Bottom); break; } case Alignment.MiddleRight: { rect = new Rectangle((partSize.Width + (xm * partSize.Width)) - margins.Right, (0 + (ym * partSize.Height)) + margins.Top, margins.Right, partSize.Height - margins.Top - margins.Bottom); break; } case Alignment.BottomLeft: { rect = new Rectangle((0 + (xm * partSize.Width)), (partSize.Height + (ym * partSize.Height)) - margins.Bottom, margins.Left, margins.Bottom); break; } case Alignment.BottomCenter: { rect = new Rectangle((0 + (xm * partSize.Width)) + margins.Left, (partSize.Height + (ym * partSize.Height)) - margins.Bottom, partSize.Width - margins.Left - margins.Right, margins.Bottom); break; } case Alignment.BottomRight: { rect = new Rectangle((partSize.Width + (xm * partSize.Width)) - margins.Right, (partSize.Height + (ym * partSize.Height)) - margins.Bottom, margins.Right, margins.Bottom); break; } } return rect; }
//////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// public void DrawGlyph(Glyph glyph, Rectangle rect) { Size imageSize = new Size(glyph.Image.Width, glyph.Image.Height); if (!glyph.SourceRect.IsEmpty) { imageSize = new Size(glyph.SourceRect.Width, glyph.SourceRect.Height); } if (glyph.SizeMode == SizeMode.Centered) { rect = new Rectangle((rect.X + (rect.Width - imageSize.Width) / 2) + glyph.Offset.X, (rect.Y + (rect.Height - imageSize.Height) / 2) + glyph.Offset.Y, imageSize.Width, imageSize.Height); } else if (glyph.SizeMode == SizeMode.Normal) { rect = new Rectangle(rect.X + glyph.Offset.X, rect.Y + glyph.Offset.Y, imageSize.Width, imageSize.Height); } else if (glyph.SizeMode == SizeMode.Auto) { rect = new Rectangle(rect.X + glyph.Offset.X, rect.Y + glyph.Offset.Y, imageSize.Width, imageSize.Height); } if (glyph.SourceRect.IsEmpty) { Draw(glyph.Image, rect, glyph.Color); } else { Draw(glyph.Image, rect, glyph.SourceRect, glyph.Color); } }
/// <summary> /// Draws a glyph. (An image on a control.) /// </summary> /// <param name="glyph">Glyph to draw.</param> /// <param name="rect">Destination region where the glyph will be drawn.</param> public void DrawGlyph(Glyph glyph, Rectangle rect) { // Get the dimensions of the glyph image asset. Size imageSize = new Size(glyph.Image.Width, glyph.Image.Height); // Use the source region if one is specified. if (!glyph.SourceRect.IsEmpty) { imageSize = new Size(glyph.SourceRect.Width, glyph.SourceRect.Height); } // Glyph is centered? if (glyph.SizeMode == SizeMode.Centered) { // Update destination and apply offsets. rect = new Rectangle((rect.X + (rect.Width - imageSize.Width) / 2) + glyph.Offset.X, (rect.Y + (rect.Height - imageSize.Height) / 2) + glyph.Offset.Y, imageSize.Width, imageSize.Height); } // Glyph is left-aligned? else if (glyph.SizeMode == SizeMode.Normal) { rect = new Rectangle(rect.X + glyph.Offset.X, rect.Y + glyph.Offset.Y, imageSize.Width, imageSize.Height); } // Glyph is auto-scaled when drawn? else if (glyph.SizeMode == SizeMode.Auto) { rect = new Rectangle(rect.X + glyph.Offset.X, rect.Y + glyph.Offset.Y, imageSize.Width, imageSize.Height); } // Draw with or without source region argument? if (glyph.SourceRect.IsEmpty) { Draw(glyph.Image, rect, glyph.Color); } else { Draw(glyph.Image, rect, glyph.SourceRect, glyph.Color); } }