Esempio n. 1
0
        public static Mesh CreateStringMesh(
            String String,
            ITileSheet FontSheet,
            Vector2 GlyphScale,
            out Rectangle Bounds)
        {
            var glyphMeshes = new List <Mesh>();
            var pos         = Vector2.Zero;

            foreach (var c in String)
            {
                if (c == '\n')
                {
                    pos.X  = 0;
                    pos.Y += FontSheet.TileHeight * GlyphScale.Y;
                }
                else if (c < 32)
                {
                    continue;
                }
                else
                {
                    var glyphSize = FontSheet.GlyphSize(c - ' ');
                    glyphMeshes.Add(Mesh.Quad()
                                    .Texture(FontSheet.TileMatrix(c - ' '))
                                    .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)pos.X, (int)(FontSheet.TileHeight * GlyphScale.Y));

            return(Merge(glyphMeshes.ToArray()));
        }
 public static Mesh FittedSprite(Rectangle Rect, ITileSheet Tiles, int Tile)
 {
     return(Quad()
            .Scale(Rect.Width, Rect.Height)
            .Translate(Rect.X, Rect.Y)
            .Texture(Tiles.TileMatrix(Tile)));
 }
Esempio n. 3
0
        /// <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);
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
        public static Mesh CreateStringMesh(
            String String,
            ITileSheet FontSheet,
            Vector2 GlyphScale,
            out Rectangle Bounds)
        {
            var glyphMeshes = new List <Mesh>();
            var pos         = Vector2.Zero;
            var maxX        = 0.0f;

            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 - ' ');
                    glyphMeshes.Add(Mesh.Quad()
                                    .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))));

            return(Merge(glyphMeshes.ToArray()));
        }
Esempio n. 6
0
        // 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);
        }
        // Tiles a sprite across a rect. Expensive!
        public static Mesh TiledSprite(Rectangle Rect, ITileSheet Tiles, int Tile)
        {
            var meshes = new List <Mesh>();
            var pos    = new Point(Rect.X, Rect.Y);

            while (pos.X < Rect.Right)
            {
                while (pos.Y < Rect.Bottom)
                {
                    var quad = Mesh.Quad();
                    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));

                    meshes.Add(quad);

                    pos.Y += Tiles.TileHeight;
                }
                pos.Y  = Rect.Y;
                pos.X += Tiles.TileWidth;
            }

            return(Mesh.Merge(meshes.ToArray()));
        }
Esempio n. 8
0
        public static Rectangle MeasureStringMesh(
            String String,
            ITileSheet FontSheet,
            Vector2 GlyphScale)
        {
            var pos  = Vector2.Zero;
            var maxX = 0.0f;

            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 - ' ');
                    pos.X += glyphSize.X * GlyphScale.X;
                }
            }

            return(new Rectangle(0, 0, (int)global::System.Math.Max(maxX, pos.X), (int)(pos.Y + ((FontSheet.TileHeight * GlyphScale.Y)))));
        }
Esempio n. 9
0
 public Mesh TileScaleAndTexture(ITileSheet Sheet, int T)
 {
     return(this.Scale(Sheet.TileWidth, Sheet.TileHeight)
            .Texture(Sheet.TileMatrix(T)));
 }
        /// <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 static Mesh CreateScale9Background(
            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 List <Mesh>();

            for (var i = 0; i < 9; ++i)
            {
                if (rects[i].Width != 0 && rects[i].Height != 0)
                {
                    if (Tiles.RepeatWhenUsedAsBorder)
                    {
                        result.Add(TiledSprite(rects[i], Tiles, i));
                    }
                    else
                    {
                        result.Add(FittedSprite(rects[i], Tiles, i));
                    }
                }
            }

            return(Merge(result.ToArray()));
        }
Esempio n. 11
0
        public static Mesh CreateTabPanelBackground(Rectangle Rect, ITileSheet Tiles, int TabX, int TabWidth)
        {
            var result = new List <Mesh>();

            //Top-left corner
            if (TabX == Rect.X)
            {
                //Vertical edge segment
                result.Add(Quad()
                           .Texture(Tiles.TileMatrix(3))
                           .Scale(Tiles.TileWidth, Tiles.TileHeight)
                           .Translate(Rect.X, Rect.Y));
            }
            else
            {
                //Corner
                result.Add(Quad()
                           .TileScaleAndTexture(Tiles, 0)
                           .Translate(Rect.X, Rect.Y));

                //Top edge
                result.Add(Quad()
                           .Texture(Tiles.TileMatrix(1))
                           .Scale(TabX - Rect.X - Tiles.TileWidth, Tiles.TileHeight)
                           .Translate(Rect.X + Tiles.TileWidth, Rect.Y));

                //Interior corner
                result.Add(Quad()
                           .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
                result.Add(Quad()
                           .Texture(Tiles.TileMatrix(5))
                           .Scale(Tiles.TileWidth, Tiles.TileHeight)
                           .Translate(Rect.Right - Tiles.TileWidth, Rect.Y));
            }
            else
            {
                //Corner
                result.Add(Quad()
                           .TileScaleAndTexture(Tiles, 2)
                           .Translate(Rect.Right - Tiles.TileWidth, Rect.Y));

                //Top edge
                result.Add(Quad()
                           .Texture(Tiles.TileMatrix(1))
                           .Scale(Rect.Right - TabX - TabWidth - Tiles.TileWidth, Tiles.TileHeight)
                           .Translate(TabX + TabWidth, Rect.Y));

                //Interior corner
                result.Add(Quad()
                           .Texture(Tiles.TileMatrix(10))
                           .Scale(Tiles.TileWidth, Tiles.TileHeight)
                           .Translate(TabX + TabWidth - Tiles.TileWidth, Rect.Y));
            }

            //Top edge - bottom of tab
            result.Add(Quad()
                       .Texture(Tiles.TileMatrix(4))
                       .Scale(TabWidth - (2 * Tiles.TileWidth), Tiles.TileHeight)
                       .Translate(TabX + Tiles.TileWidth, Rect.Y));


            //Bottom-left corner
            result.Add(Quad()
                       .TileScaleAndTexture(Tiles, 6)
                       .Translate(Rect.X, Rect.Bottom - Tiles.TileHeight));

            //Bottom-right corner
            result.Add(Quad()
                       .TileScaleAndTexture(Tiles, 8)
                       .Translate(Rect.Right - Tiles.TileWidth, Rect.Bottom - Tiles.TileHeight));

            //Bottom edge
            result.Add(Quad()
                       .Texture(Tiles.TileMatrix(7))
                       .Scale(Rect.Width - (2 * Tiles.TileWidth), Tiles.TileHeight)
                       .Translate(Rect.X + Tiles.TileWidth, Rect.Bottom - Tiles.TileHeight));

            //Left edge
            result.Add(Quad()
                       .Texture(Tiles.TileMatrix(3))
                       .Scale(Tiles.TileWidth, Rect.Height - (2 * Tiles.TileHeight))
                       .Translate(Rect.X, Rect.Y + Tiles.TileHeight));

            //Right edge
            result.Add(Quad()
                       .Texture(Tiles.TileMatrix(5))
                       .Scale(Tiles.TileWidth, Rect.Height - (2 * Tiles.TileHeight))
                       .Translate(Rect.Right - Tiles.TileWidth, Rect.Y + Tiles.TileHeight));

            //Center
            result.Add(Quad()
                       .Texture(Tiles.TileMatrix(4))
                       .Scale(Rect.Width - (2 * Tiles.TileWidth), Rect.Height - (2 * Tiles.TileHeight))
                       .Translate(Rect.X + Tiles.TileWidth, Rect.Y + Tiles.TileHeight));

            return(Merge(result.ToArray()));
        }
Esempio n. 12
0
        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);
        }