Exemplo n.º 1
0
        /// <summary>
        /// Draws the <see cref="ControlBorder"/> to the specified region.
        /// </summary>
        /// <param name="sb"><see cref="ISpriteBatch"/> to draw with.</param>
        /// <param name="region">Region to draw the <see cref="ControlBorder"/> to. These values represent
        /// the absolute screen position.</param>
        /// <param name="color">The <see cref="Color"/> to use to draw the <see cref="ControlBorder"/>.</param>
        public void Draw(ISpriteBatch sb, Rectangle region, Color color)
        {
            if (!_canDraw)
            {
                return;
            }

            var _bg = GetSprite(ControlBorderSpriteType.Background);
            var _t  = GetSprite(ControlBorderSpriteType.Top);
            var _tl = GetSprite(ControlBorderSpriteType.TopLeft);
            var _tr = GetSprite(ControlBorderSpriteType.TopRight);
            var _l  = GetSprite(ControlBorderSpriteType.Left);
            var _r  = GetSprite(ControlBorderSpriteType.Right);
            var _b  = GetSprite(ControlBorderSpriteType.Bottom);
            var _bl = GetSprite(ControlBorderSpriteType.BottomLeft);
            var _br = GetSprite(ControlBorderSpriteType.BottomRight);

            var bSrc  = _b.Source;
            var tSrc  = _t.Source;
            var tlSrc = _tl.Source;
            var trSrc = _tr.Source;
            var lSrc  = _l.Source;
            var rSrc  = _r.Source;
            var blSrc = _bl.Source;
            var brSrc = _br.Source;

            // Set some values we will be using extensively
            var cX = region.X;
            var cY = region.Y;
            var cW = region.Width;
            var cH = region.Height;

            lock (_drawSync)
            {
                // Background (draw first to ensure it stays behind the border in case something goes wrong)
                Rectangle r;
                if (_bg != null)
                {
                    r = new Rectangle(cX + lSrc.Width, cY + tSrc.Height, cW - lSrc.Width - rSrc.Width,
                                      cH - tSrc.Height - bSrc.Height);

                    switch (GetDrawStyle(ControlBorderSpriteType.Background))
                    {
                    case ControlBorderDrawStyle.Stretch:
                        _bg.Draw(sb, r, color);
                        break;

                    case ControlBorderDrawStyle.Tile:
                        AssertSaneDrawTilingAmount(r.X, r.Right, _bg.Size.X, ControlBorderSpriteType.Background);
                        AssertSaneDrawTilingAmount(r.Y, r.Bottom, _bg.Size.Y, ControlBorderSpriteType.Background);
                        sb.DrawTiledXY(r.X, r.Right, r.Y, r.Bottom, _bg, color);
                        break;
                    }
                }

                // Top side
                r = new Rectangle(cX + tlSrc.Width, cY, cW - tlSrc.Width - trSrc.Width, tSrc.Height);
                switch (GetDrawStyle(ControlBorderSpriteType.Top))
                {
                case ControlBorderDrawStyle.Stretch:
                    _t.Draw(sb, r, color);
                    break;

                case ControlBorderDrawStyle.Tile:
                    AssertSaneDrawTilingAmount(r.X, r.Right, _t.Size.X, ControlBorderSpriteType.Top);
                    sb.DrawTiledX(r.X, r.Right, r.Y, _t, color);
                    break;
                }

                // Left side
                r = new Rectangle(cX, cY + tlSrc.Height, lSrc.Width, cH - tlSrc.Height - blSrc.Height);
                switch (GetDrawStyle(ControlBorderSpriteType.Left))
                {
                case ControlBorderDrawStyle.Stretch:
                    _l.Draw(sb, r, color);
                    break;

                case ControlBorderDrawStyle.Tile:
                    AssertSaneDrawTilingAmount(r.Y, r.Bottom, _l.Size.Y, ControlBorderSpriteType.Left);
                    sb.DrawTiledY(r.Y, r.Bottom, r.X, _l, color);
                    break;
                }

                // Right side
                r = new Rectangle(cX + cW - rSrc.Width, cY + trSrc.Height, rSrc.Width, cH - trSrc.Height - brSrc.Height);
                switch (GetDrawStyle(ControlBorderSpriteType.Right))
                {
                case ControlBorderDrawStyle.Stretch:
                    _r.Draw(sb, r, color);
                    break;

                case ControlBorderDrawStyle.Tile:
                    AssertSaneDrawTilingAmount(r.Y, r.Bottom, _r.Size.Y, ControlBorderSpriteType.Right);
                    sb.DrawTiledY(r.Y, r.Bottom, r.X, _r, color);
                    break;
                }

                // Bottom side
                r = new Rectangle(cX + _bl.Source.Width, cY + cH - bSrc.Height, cW - blSrc.Width - brSrc.Width, bSrc.Height);
                switch (GetDrawStyle(ControlBorderSpriteType.Bottom))
                {
                case ControlBorderDrawStyle.Stretch:
                    _b.Draw(sb, r, color);
                    break;

                case ControlBorderDrawStyle.Tile:
                    AssertSaneDrawTilingAmount(r.X, r.Right, _b.Size.X, ControlBorderSpriteType.Bottom);
                    sb.DrawTiledX(r.X, r.Right, r.Y, _b, color);
                    break;
                }

                // Top-left corner
                r = new Rectangle(cX, cY, tlSrc.Width, tlSrc.Height);
                _tl.Draw(sb, r, color);

                // Top-right corner
                r = new Rectangle(cX + cW - trSrc.Width, cY, trSrc.Width, trSrc.Height);
                _tr.Draw(sb, r, color);

                // Bottom-left corner
                r = new Rectangle(cX, cY + cH - blSrc.Height, blSrc.Width, blSrc.Height);
                _bl.Draw(sb, r, color);

                // Bottom-right corner
                r = new Rectangle(cX + cW - brSrc.Width, cY + cH - brSrc.Height, brSrc.Width, brSrc.Height);
                _br.Draw(sb, r, color);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Handles drawing the <see cref="BackgroundImage"/>.
        /// </summary>
        /// <param name="sb">The <see cref="ISpriteBatch"/> to use to draw.</param>
        protected override void HandleDraw(ISpriteBatch sb)
        {
            var spriteSize = SpriteSourceSize;
            var pos = GetPosition(Map.Size, Camera, spriteSize);

            var vl = VerticalLayout;
            var hl = HorizontalLayout;

            if (hl == BackgroundLayerLayout.Stretched && vl == BackgroundLayerLayout.Stretched)
            {
                // Stretch both directions
                spriteSize.X = GetStretchedSize(Camera.Size.X * Camera.Scale, Map.Size.X, Depth);
                spriteSize.Y = GetStretchedSize(Camera.Size.Y * Camera.Scale, Map.Size.Y, Depth);

                var rect = new Rectangle((int)pos.X, (int)pos.Y, (int)spriteSize.X, (int)spriteSize.Y);
                Sprite.Draw(sb, rect, Color);
            }
            else if (vl == BackgroundLayerLayout.Tiled &&
                     (hl == BackgroundLayerLayout.None || hl == BackgroundLayerLayout.Stretched))
            {
                // Stretch/none horizontal, tile vertical
                int drawWidth;
                if (hl == BackgroundLayerLayout.None)
                    drawWidth = (int)spriteSize.X;
                else
                    drawWidth = (int)GetStretchedSize(Camera.Size.X * Camera.Scale, Map.Size.X, Depth);

                sb.DrawTiledY((int)pos.Y, (int)Camera.Max.Y + 64, (int)pos.X, Sprite, Color.White, drawWidth);
            }
            else if (hl == BackgroundLayerLayout.Tiled &&
                     (vl == BackgroundLayerLayout.None || vl == BackgroundLayerLayout.Stretched))
            {
                // Tile horizontal, stretch/none vertical
                int drawHeight;
                if (vl == BackgroundLayerLayout.None)
                    drawHeight = (int)spriteSize.Y;
                else
                    drawHeight = (int)GetStretchedSize(Camera.Size.X * Camera.Scale, Map.Size.X, Depth);

                sb.DrawTiledX((int)pos.X, (int)Camera.Max.X + 64, (int)pos.Y, Sprite, Color.White, drawHeight);
            }
            else if (hl == BackgroundLayerLayout.Tiled && vl == BackgroundLayerLayout.Tiled)
            {
                // Tile both directions
                sb.DrawTiledXY((int)pos.X, (int)Camera.Max.X + 64, (int)pos.Y, (int)Camera.Max.Y + 64, Sprite, Color);
            }
            else if (hl == BackgroundLayerLayout.None && vl == BackgroundLayerLayout.None)
            {
                // None in both directions
                Sprite.Draw(sb, pos);
            }
            else
            {
                // Unknown or unhandled permutation
                const string errmsg =
                    "Unknown or unhandled BackgroundLayerLayout provided." +
                    " Changing layouts to BackgroundLayerLayout.None. HorizontalLayout: `{0}` VerticalLayout: `{1}`";
                if (log.IsErrorEnabled)
                    log.ErrorFormat(errmsg, HorizontalLayout, VerticalLayout);

                HorizontalLayout = BackgroundLayerLayout.None;
                VerticalLayout = BackgroundLayerLayout.None;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Draws the <see cref="ControlBorder"/> to the specified region.
        /// </summary>
        /// <param name="sb"><see cref="ISpriteBatch"/> to draw with.</param>
        /// <param name="region">Region to draw the <see cref="ControlBorder"/> to. These values represent
        /// the absolute screen position.</param>
        /// <param name="color">The <see cref="Color"/> to use to draw the <see cref="ControlBorder"/>.</param>
        public void Draw(ISpriteBatch sb, Rectangle region, Color color)
        {
            if (!_canDraw)
                return;

            var _bg = GetSprite(ControlBorderSpriteType.Background);
            var _t = GetSprite(ControlBorderSpriteType.Top);
            var _tl = GetSprite(ControlBorderSpriteType.TopLeft);
            var _tr = GetSprite(ControlBorderSpriteType.TopRight);
            var _l = GetSprite(ControlBorderSpriteType.Left);
            var _r = GetSprite(ControlBorderSpriteType.Right);
            var _b = GetSprite(ControlBorderSpriteType.Bottom);
            var _bl = GetSprite(ControlBorderSpriteType.BottomLeft);
            var _br = GetSprite(ControlBorderSpriteType.BottomRight);

            var bSrc = _b.Source;
            var tSrc = _t.Source;
            var tlSrc = _tl.Source;
            var trSrc = _tr.Source;
            var lSrc = _l.Source;
            var rSrc = _r.Source;
            var blSrc = _bl.Source;
            var brSrc = _br.Source;

            // Set some values we will be using extensively
            var cX = region.X;
            var cY = region.Y;
            var cW = region.Width;
            var cH = region.Height;

            lock (_drawSync)
            {
                // Background (draw first to ensure it stays behind the border in case something goes wrong)
                Rectangle r;
                if (_bg != null)
                {
                    r = new Rectangle(cX + lSrc.Width, cY + tSrc.Height, cW - lSrc.Width - rSrc.Width,
                        cH - tSrc.Height - bSrc.Height);

                    switch (GetDrawStyle(ControlBorderSpriteType.Background))
                    {
                        case ControlBorderDrawStyle.Stretch:
                            _bg.Draw(sb, r, color);
                            break;

                        case ControlBorderDrawStyle.Tile:
                            AssertSaneDrawTilingAmount(r.X, r.Right, _bg.Size.X, ControlBorderSpriteType.Background);
                            AssertSaneDrawTilingAmount(r.Y, r.Bottom, _bg.Size.Y, ControlBorderSpriteType.Background);
                            sb.DrawTiledXY(r.X, r.Right, r.Y, r.Bottom, _bg, color);
                            break;
                    }
                }

                // Top side
                r = new Rectangle(cX + tlSrc.Width, cY, cW - tlSrc.Width - trSrc.Width, tSrc.Height);
                switch (GetDrawStyle(ControlBorderSpriteType.Top))
                {
                    case ControlBorderDrawStyle.Stretch:
                        _t.Draw(sb, r, color);
                        break;

                    case ControlBorderDrawStyle.Tile:
                        AssertSaneDrawTilingAmount(r.X, r.Right, _t.Size.X, ControlBorderSpriteType.Top);
                        sb.DrawTiledX(r.X, r.Right, r.Y, _t, color);
                        break;
                }

                // Left side
                r = new Rectangle(cX, cY + tlSrc.Height, lSrc.Width, cH - tlSrc.Height - blSrc.Height);
                switch (GetDrawStyle(ControlBorderSpriteType.Left))
                {
                    case ControlBorderDrawStyle.Stretch:
                        _l.Draw(sb, r, color);
                        break;

                    case ControlBorderDrawStyle.Tile:
                        AssertSaneDrawTilingAmount(r.Y, r.Bottom, _l.Size.Y, ControlBorderSpriteType.Left);
                        sb.DrawTiledY(r.Y, r.Bottom, r.X, _l, color);
                        break;
                }

                // Right side
                r = new Rectangle(cX + cW - rSrc.Width, cY + trSrc.Height, rSrc.Width, cH - trSrc.Height - brSrc.Height);
                switch (GetDrawStyle(ControlBorderSpriteType.Right))
                {
                    case ControlBorderDrawStyle.Stretch:
                        _r.Draw(sb, r, color);
                        break;

                    case ControlBorderDrawStyle.Tile:
                        AssertSaneDrawTilingAmount(r.Y, r.Bottom, _r.Size.Y, ControlBorderSpriteType.Right);
                        sb.DrawTiledY(r.Y, r.Bottom, r.X, _r, color);
                        break;
                }

                // Bottom side
                r = new Rectangle(cX + _bl.Source.Width, cY + cH - bSrc.Height, cW - blSrc.Width - brSrc.Width, bSrc.Height);
                switch (GetDrawStyle(ControlBorderSpriteType.Bottom))
                {
                    case ControlBorderDrawStyle.Stretch:
                        _b.Draw(sb, r, color);
                        break;

                    case ControlBorderDrawStyle.Tile:
                        AssertSaneDrawTilingAmount(r.X, r.Right, _b.Size.X, ControlBorderSpriteType.Bottom);
                        sb.DrawTiledX(r.X, r.Right, r.Y, _b, color);
                        break;
                }

                // Top-left corner
                r = new Rectangle(cX, cY, tlSrc.Width, tlSrc.Height);
                _tl.Draw(sb, r, color);

                // Top-right corner
                r = new Rectangle(cX + cW - trSrc.Width, cY, trSrc.Width, trSrc.Height);
                _tr.Draw(sb, r, color);

                // Bottom-left corner
                r = new Rectangle(cX, cY + cH - blSrc.Height, blSrc.Width, blSrc.Height);
                _bl.Draw(sb, r, color);

                // Bottom-right corner
                r = new Rectangle(cX + cW - brSrc.Width, cY + cH - brSrc.Height, brSrc.Width, brSrc.Height);
                _br.Draw(sb, r, color);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Handles drawing the <see cref="BackgroundImage"/>.
        /// </summary>
        /// <param name="sb">The <see cref="ISpriteBatch"/> to use to draw.</param>
        protected override void HandleDraw(ISpriteBatch sb)
        {
            var spriteSize = SpriteSourceSize;
            var pos        = GetPosition(Map.Size, Camera, spriteSize);

            var vl = VerticalLayout;
            var hl = HorizontalLayout;

            if (hl == BackgroundLayerLayout.Stretched && vl == BackgroundLayerLayout.Stretched)
            {
                // Stretch both directions
                spriteSize.X = GetStretchedSize(Camera.Size.X * Camera.Scale, Map.Size.X, Depth);
                spriteSize.Y = GetStretchedSize(Camera.Size.Y * Camera.Scale, Map.Size.Y, Depth);

                var rect = new Rectangle((int)pos.X, (int)pos.Y, (int)spriteSize.X, (int)spriteSize.Y);
                Sprite.Draw(sb, rect, Color);
            }
            else if (vl == BackgroundLayerLayout.Tiled &&
                     (hl == BackgroundLayerLayout.None || hl == BackgroundLayerLayout.Stretched))
            {
                // Stretch/none horizontal, tile vertical
                int drawWidth;
                if (hl == BackgroundLayerLayout.None)
                {
                    drawWidth = (int)spriteSize.X;
                }
                else
                {
                    drawWidth = (int)GetStretchedSize(Camera.Size.X * Camera.Scale, Map.Size.X, Depth);
                }

                sb.DrawTiledY((int)pos.Y, (int)Camera.Max.Y + 64, (int)pos.X, Sprite, Color.White, drawWidth);
            }
            else if (hl == BackgroundLayerLayout.Tiled &&
                     (vl == BackgroundLayerLayout.None || vl == BackgroundLayerLayout.Stretched))
            {
                // Tile horizontal, stretch/none vertical
                int drawHeight;
                if (vl == BackgroundLayerLayout.None)
                {
                    drawHeight = (int)spriteSize.Y;
                }
                else
                {
                    drawHeight = (int)GetStretchedSize(Camera.Size.X * Camera.Scale, Map.Size.X, Depth);
                }

                sb.DrawTiledX((int)pos.X, (int)Camera.Max.X + 64, (int)pos.Y, Sprite, Color.White, drawHeight);
            }
            else if (hl == BackgroundLayerLayout.Tiled && vl == BackgroundLayerLayout.Tiled)
            {
                // Tile both directions
                sb.DrawTiledXY((int)pos.X, (int)Camera.Max.X + 64, (int)pos.Y, (int)Camera.Max.Y + 64, Sprite, Color);
            }
            else if (hl == BackgroundLayerLayout.None && vl == BackgroundLayerLayout.None)
            {
                // None in both directions
                Sprite.Draw(sb, pos);
            }
            else
            {
                // Unknown or unhandled permutation
                const string errmsg =
                    "Unknown or unhandled BackgroundLayerLayout provided." +
                    " Changing layouts to BackgroundLayerLayout.None. HorizontalLayout: `{0}` VerticalLayout: `{1}`";
                if (log.IsErrorEnabled)
                {
                    log.ErrorFormat(errmsg, HorizontalLayout, VerticalLayout);
                }

                HorizontalLayout = BackgroundLayerLayout.None;
                VerticalLayout   = BackgroundLayerLayout.None;
            }
        }