コード例 #1
0
ファイル: Widget.cs プロジェクト: polytronicgr/dwarfcorp
        /// <summary>
        /// Create the rendering mesh for this widget.
        /// </summary>
        /// <returns></returns>
        protected virtual Mesh Redraw()
        {
            if (Hidden)
            {
                throw new InvalidOperationException();
            }

            if (Transparent || Root == null)
            {
                return(Mesh.EmptyMesh());
            }

            var result = new List <Mesh>();

            if (Background != null)
            {
                if (Rotation != 0.0)
                {
                    result.Add(Mesh.Quad()
                               .Scale(Rect.Width, Rect.Height)
                               .Transform(Rect.X, Rect.Y, Rect.Width, Rect.Height, Rotation)
                               .Colorize(BackgroundColor)
                               .Texture(Root.GetTileSheet(Background.Sheet).TileMatrix(Background.Tile)));
                }
                else
                {
                    result.Add(Mesh.Quad()
                               .Scale(Rect.Width, Rect.Height)
                               .Translate(Rect.X, Rect.Y)
                               .Colorize(BackgroundColor)
                               .Texture(Root.GetTileSheet(Background.Sheet).TileMatrix(Background.Tile)));
                }
            }

            if (!String.IsNullOrEmpty(Border))
            {
                //Create a 'scale 9' background
                result.Add(
                    Mesh.CreateScale9Background(Rect, Root.GetTileSheet(Border))
                    .Colorize(BackgroundColor));
            }

            // Add text label
            if (!String.IsNullOrEmpty(Text))
            {
                GetTextMesh(result);
            }

            return(Mesh.Merge(result.ToArray()));
        }
コード例 #2
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()));
        }
コード例 #3
0
        // 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()));
        }