public MeshPart QuadPart(Vector2 bottomLeft, Vector2 topLeft, Vector2 bottomRight, Vector2 topRight) { var result = new MeshPart { VertexOffset = VertexCount, VertexCount = 4, Mesh = this }; var baseIndex = VertexCount; GrowVerticies(4); Verticies[baseIndex + 0] = new Vertex { Position = new Vector3(topLeft, 0.0f), TextureCoordinate = new Vector2(0.0f, 0.0f), Color = Vector4.One }; Verticies[baseIndex + 1] = new Vertex { Position = new Vector3(topRight, 0.0f), TextureCoordinate = new Vector2(1.0f, 0.0f), Color = Vector4.One }; Verticies[baseIndex + 2] = new Vertex { Position = new Vector3(bottomRight, 0.0f), TextureCoordinate = new Vector2(1.0f, 1.0f), Color = Vector4.One }; Verticies[baseIndex + 3] = new Vertex { Position = new Vector3(bottomLeft, 0.0f), TextureCoordinate = new Vector2(0.0f, 1.0f), Color = Vector4.One }; AddIndicies(baseIndex, 0, 1, 2, 3, 0, 2); return(result); }
/// <summary> /// Create a mesh for a scale9 background. This assumed the tilesheet is 3*3 and positions the /// corners without scalling, scales the edges on one axis only, and fills the middle with the /// center tile. /// </summary> /// <param name="Rect"></param> /// <param name="Tiles"></param> /// <param name="Corners"></param> /// <returns></returns> public MeshPart Scale9Part( Rectangle Rect, ITileSheet Tiles, Scale9Corners Corners = Scale9Corners.All) { var rects = new Rectangle[9]; var margin = new Margin(0, 0, 0, 0); if (Corners.HasFlag(Scale9Corners.Left)) { margin.Left = Tiles.TileWidth; } if (Corners.HasFlag(Scale9Corners.Right)) { margin.Right = Tiles.TileWidth; } if (Corners.HasFlag(Scale9Corners.Top)) { margin.Top = Tiles.TileHeight; } if (Corners.HasFlag(Scale9Corners.Bottom)) { margin.Bottom = Tiles.TileHeight; } rects[0] = new Rectangle(Rect.Left, Rect.Top, margin.Left, margin.Top); rects[1] = new Rectangle(Rect.Left + margin.Left, Rect.Top, Rect.Width - margin.Left - margin.Right, margin.Top); rects[2] = new Rectangle(Rect.Right - margin.Right, Rect.Top, margin.Right, margin.Top); rects[3] = new Rectangle(Rect.Left, Rect.Top + margin.Top, margin.Left, Rect.Height - margin.Top - margin.Bottom); rects[4] = new Rectangle(Rect.Left + margin.Left, Rect.Top + margin.Top, Rect.Width - margin.Left - margin.Right, Rect.Height - margin.Top - margin.Bottom); rects[5] = new Rectangle(Rect.Right - margin.Right, Rect.Top + margin.Top, margin.Right, Rect.Height - margin.Top - margin.Bottom); rects[6] = new Rectangle(Rect.Left, Rect.Bottom - margin.Bottom, margin.Left, margin.Bottom); rects[7] = new Rectangle(Rect.Left + margin.Left, Rect.Bottom - margin.Bottom, Rect.Width - margin.Left - margin.Right, margin.Bottom); rects[8] = new Rectangle(Rect.Right - margin.Right, Rect.Bottom - margin.Bottom, margin.Right, margin.Bottom); var result = new MeshPart { VertexOffset = this.VertexCount, Mesh = this }; for (var i = 0; i < 9; ++i) { if (rects[i].Width != 0 && rects[i].Height != 0) { if (Tiles.RepeatWhenUsedAsBorder) { TiledSpritePart(rects[i], Tiles, i); } else { FittedSpritePart(rects[i], Tiles, i); } } } result.VertexCount = this.VertexCount - result.VertexOffset; return(result); }
public MeshPart StringPart( String String, ITileSheet FontSheet, Vector2 GlyphScale, out Rectangle Bounds) { var pos = Vector2.Zero; var maxX = 0.0f; var r = new MeshPart { Mesh = this, VertexOffset = this.VertexCount }; foreach (var c in String) { if (c == '\n') { if (pos.X > maxX) { maxX = pos.X; } pos.X = 0; pos.Y += FontSheet.TileHeight * GlyphScale.Y; } else if (c < 32) { continue; } else { var x = c; if (!FontSheet.HasGlyph(c - ' ')) { x = '*'; } var glyphSize = FontSheet.GlyphSize(x - ' '); QuadPart() .Texture(FontSheet.TileMatrix(x - ' ')) .Scale(glyphSize.X * GlyphScale.X, glyphSize.Y * GlyphScale.Y) .Translate(pos.X, pos.Y); pos.X += glyphSize.X * GlyphScale.X; } } Bounds = new Rectangle(0, 0, (int)global::System.Math.Max(maxX, pos.X), (int)(pos.Y + ((FontSheet.TileHeight * GlyphScale.Y)))); r.VertexCount = this.VertexCount - r.VertexOffset; return(r); }
// Tiles a sprite across a rect. Expensive! public MeshPart TiledSpritePart(Rectangle Rect, ITileSheet Tiles, int Tile) { var pos = new Point(Rect.X, Rect.Y); var r = new MeshPart { VertexOffset = this.VertexCount, Mesh = this }; while (pos.X < Rect.Right) { while (pos.Y < Rect.Bottom) { var quad = QuadPart(); var size = new Point(Tiles.TileWidth, Tiles.TileHeight); // Adjust texture coordinates if needed. if (pos.Y + Tiles.TileHeight > Rect.Bottom) { size.Y = Rect.Bottom - pos.Y; var ratio = (float)(size.Y) / (float)Tiles.TileHeight; quad.MorphEx(v => { v.TextureCoordinate.Y *= ratio; return(v); }); } if (pos.X + Tiles.TileWidth > Rect.Right) { size.X = Rect.Right - pos.X; var ratio = (float)(size.X) / (float)Tiles.TileWidth; quad.MorphEx(v => { v.TextureCoordinate.X *= ratio; return(v); }); } quad.Scale(size.X, size.Y) .Translate(pos.X, pos.Y) .Texture(Tiles.TileMatrix(Tile)); pos.Y += Tiles.TileHeight; } pos.Y = Rect.Y; pos.X += Tiles.TileWidth; } r.VertexCount = this.VertexCount - r.VertexOffset; return(r); }
public MeshPart CreateTabPanelBackgroundPart(Rectangle Rect, ITileSheet Tiles, int TabX, int TabWidth) { var r = new MeshPart { Mesh = this, VertexOffset = this.VertexCount }; //Top-left corner if (TabX == Rect.X) { //Vertical edge segment QuadPart() .Texture(Tiles.TileMatrix(3)) .Scale(Tiles.TileWidth, Tiles.TileHeight) .Translate(Rect.X, Rect.Y); } else { //Corner QuadPart() .TileScaleAndTexture(Tiles, 0) .Translate(Rect.X, Rect.Y); //Top edge QuadPart() .Texture(Tiles.TileMatrix(1)) .Scale(TabX - Rect.X - Tiles.TileWidth, Tiles.TileHeight) .Translate(Rect.X + Tiles.TileWidth, Rect.Y); //Interior corner QuadPart() .Texture(Tiles.TileMatrix(9)) .Scale(Tiles.TileWidth, Tiles.TileHeight) .Translate(TabX, Rect.Y); } //Top-right corner if (Rect.X + Rect.Width == TabX + TabWidth) { //Vertical edge segment QuadPart() .Texture(Tiles.TileMatrix(5)) .Scale(Tiles.TileWidth, Tiles.TileHeight) .Translate(Rect.Right - Tiles.TileWidth, Rect.Y); } else { //Corner QuadPart() .TileScaleAndTexture(Tiles, 2) .Translate(Rect.Right - Tiles.TileWidth, Rect.Y); //Top edge QuadPart() .Texture(Tiles.TileMatrix(1)) .Scale(Rect.Right - TabX - TabWidth - Tiles.TileWidth, Tiles.TileHeight) .Translate(TabX + TabWidth, Rect.Y); //Interior corner QuadPart() .Texture(Tiles.TileMatrix(10)) .Scale(Tiles.TileWidth, Tiles.TileHeight) .Translate(TabX + TabWidth - Tiles.TileWidth, Rect.Y); } //Top edge - bottom of tab QuadPart() .Texture(Tiles.TileMatrix(4)) .Scale(TabWidth - (2 * Tiles.TileWidth), Tiles.TileHeight) .Translate(TabX + Tiles.TileWidth, Rect.Y); //Bottom-left corner QuadPart() .TileScaleAndTexture(Tiles, 6) .Translate(Rect.X, Rect.Bottom - Tiles.TileHeight); //Bottom-right corner QuadPart() .TileScaleAndTexture(Tiles, 8) .Translate(Rect.Right - Tiles.TileWidth, Rect.Bottom - Tiles.TileHeight); //Bottom edge QuadPart() .Texture(Tiles.TileMatrix(7)) .Scale(Rect.Width - (2 * Tiles.TileWidth), Tiles.TileHeight) .Translate(Rect.X + Tiles.TileWidth, Rect.Bottom - Tiles.TileHeight); //Left edge QuadPart() .Texture(Tiles.TileMatrix(3)) .Scale(Tiles.TileWidth, Rect.Height - (2 * Tiles.TileHeight)) .Translate(Rect.X, Rect.Y + Tiles.TileHeight); //Right edge QuadPart() .Texture(Tiles.TileMatrix(5)) .Scale(Tiles.TileWidth, Rect.Height - (2 * Tiles.TileHeight)) .Translate(Rect.Right - Tiles.TileWidth, Rect.Y + Tiles.TileHeight); //Center QuadPart() .Texture(Tiles.TileMatrix(4)) .Scale(Rect.Width - (2 * Tiles.TileWidth), Rect.Height - (2 * Tiles.TileHeight)) .Translate(Rect.X + Tiles.TileWidth, Rect.Y + Tiles.TileHeight); r.VertexCount = this.VertexCount - r.VertexOffset; return(r); }