// Reverse the buffer along the x- or y- axis
        public void Flip(ClipboardBuffer buffer, bool flipX)
        {
            ClipboardBuffer flippedBuffer = new ClipboardBuffer(buffer.Size);
            //var sprites = new Dictionary<Vector2Int32, Sprite>();
            var spriteSizes = new Dictionary <Vector2Int32, Vector2Short>();
            int maxX        = buffer.Size.X - 1;
            int maxY        = buffer.Size.Y - 1;

            for (int x = 0; x <= maxX; x++)
            {
                for (int y = 0; y <= maxY; y++)
                {
                    int bufferX;
                    int bufferY;

                    if (flipX)
                    {
                        bufferX = maxX - x;
                        bufferY = y;
                    }
                    else
                    {
                        bufferX = x;
                        bufferY = maxY - y;
                    }

                    Tile tile           = (Tile)buffer.Tiles[x, y].Clone();
                    var  tileProperties = World.TileProperties[tile.Type];


                    // locate all the sprites and make a list
                    if (tileProperties.IsFramed)
                    {
                        var loc = new Vector2Int32(x, y);
                        if (tileProperties.IsOrigin(tile.GetUV()))
                        {
                            Vector2Short tileSize = tileProperties.GetFrameSize(tile.V);
                            spriteSizes[loc] = tileSize;
                        }
                    }
                    else
                    {
                        if (flipX)
                        {
                            //  Ignore multi-width objects when flipping on x-axis

                            // Flip brick-style
                            switch (tile.BrickStyle)
                            {
                            case BrickStyle.SlopeTopRight:
                                tile.BrickStyle = BrickStyle.SlopeTopLeft;
                                break;

                            case BrickStyle.SlopeTopLeft:
                                tile.BrickStyle = BrickStyle.SlopeTopRight;
                                break;

                            case BrickStyle.SlopeBottomRight:
                                tile.BrickStyle = BrickStyle.SlopeBottomLeft;
                                break;

                            case BrickStyle.SlopeBottomLeft:
                                tile.BrickStyle = BrickStyle.SlopeBottomRight;
                                break;
                            }
                        }

                        else
                        {
                            //  Ignore multi-height tiles when flipping on y-axis

                            // Flip brick-style
                            switch (tile.BrickStyle)
                            {
                            case BrickStyle.SlopeTopRight:
                                tile.BrickStyle = BrickStyle.SlopeBottomRight;
                                break;

                            case BrickStyle.SlopeTopLeft:
                                tile.BrickStyle = BrickStyle.SlopeBottomLeft;
                                break;

                            case BrickStyle.SlopeBottomRight:
                                tile.BrickStyle = BrickStyle.SlopeTopRight;
                                break;

                            case BrickStyle.SlopeBottomLeft:
                                tile.BrickStyle = BrickStyle.SlopeTopLeft;
                                break;
                            }
                        }

                        flippedBuffer.Tiles[bufferX, bufferY] = (Tile)tile;
                    }
                }
            }

            foreach (var item in spriteSizes)
            {
                var flipOrigin = FlipFramed(buffer.Size, item.Key, item.Value, flipX);

                for (int y = 0; y < item.Value.Y; y++)
                {
                    int sourceY = y + item.Key.Y;
                    int targetY = y + flipOrigin.Y;

                    for (int x = 0; x < item.Value.X; x++)
                    {
                        try
                        {
                            int sourceX = x + item.Key.X;
                            int targetX = x + flipOrigin.X;

                            Tile tile = (Tile)buffer.Tiles[sourceX, sourceY].Clone();
                            flippedBuffer.Tiles[targetX, targetY] = (Tile)tile;
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
            }

            foreach (var chest in buffer.Chests)
            {
                var flipOrigin = FlipFramed(buffer.Size, new Vector2Int32(chest.X, chest.Y), new Vector2Short(2, 2), flipX);
                chest.X = flipOrigin.X;
                chest.Y = flipOrigin.Y;
                flippedBuffer.Chests.Add(chest);
            }

            foreach (var sign in buffer.Signs)
            {
                var flipOrigin = FlipFramed(buffer.Size, new Vector2Int32(sign.X, sign.Y), new Vector2Short(2, 2), flipX);
                sign.X = flipOrigin.X;
                sign.Y = flipOrigin.Y;
                flippedBuffer.Signs.Add(sign);
            }

            foreach (var te in buffer.TileEntities)
            {
                var          tileProperties = World.TileProperties[(int)te.TileType];
                Vector2Short tileSize       = tileProperties.FrameSize[0];

                var flipOrigin = FlipFramed(buffer.Size, new Vector2Int32(te.PosX, te.PosY), tileSize, flipX);
                te.PosX = (short)flipOrigin.X;
                te.PosY = (short)flipOrigin.Y;
                flippedBuffer.TileEntities.Add(te);
            }

            // Replace the existing buffer with the new one
            int bufferIndex = LoadedBuffers.IndexOf(buffer);

            if (bufferIndex > -1)
            {
                LoadedBuffers.Insert(bufferIndex, flippedBuffer);
                LoadedBuffers.RemoveAt(bufferIndex + 1);
            }

            flippedBuffer.RenderBuffer();

            if (Buffer == buffer)
            {
                Buffer = flippedBuffer;
                _wvm.PreviewChange();
            }
        }
Ejemplo n.º 2
0
        // Reverse the buffer along the x- or y- axis
        public void Flip(ClipboardBuffer buffer, bool flipX)
        {
            ClipboardBuffer flippedBuffer = new ClipboardBuffer(buffer.Size);

            for (int x = 0, maxX = buffer.Size.X - 1; x <= maxX; x++)
            {
                for (int y = 0, maxY = buffer.Size.Y - 1; y <= maxY; y++)
                {
                    int bufferX;
                    int bufferY;

                    if (flipX)
                    {
                        bufferX = maxX - x;
                        bufferY = y;
                    }
                    else
                    {
                        bufferX = x;
                        bufferY = maxY - y;
                    }

                    Tile tile = (Tile)buffer.Tiles[x, y].Clone();

                    Vector2Short tileSize = World.TileProperties[tile.Type].FrameSize;

                    if (flipX)
                    {
                        //  Ignore multi-width objects when flipping on x-axis
                        if (tileSize.X > 1)
                        {
                            ClearTile(tile);
                        }
                        // Flip brick-style
                        switch (tile.BrickStyle)
                        {
                        case BrickStyle.SlopeTopRight:
                            tile.BrickStyle = BrickStyle.SlopeTopLeft;
                            break;

                        case BrickStyle.SlopeTopLeft:
                            tile.BrickStyle = BrickStyle.SlopeTopRight;
                            break;

                        case BrickStyle.SlopeBottomRight:
                            tile.BrickStyle = BrickStyle.SlopeBottomLeft;
                            break;

                        case BrickStyle.SlopeBottomLeft:
                            tile.BrickStyle = BrickStyle.SlopeBottomRight;
                            break;
                        }
                    }

                    else
                    {
                        //  Ignore multi-height tiles when flipping on y-axis
                        if (tileSize.Y > 1)
                        {
                            ClearTile(tile);
                        }
                        // Flip brick-style
                        switch (tile.BrickStyle)
                        {
                        case BrickStyle.SlopeTopRight:
                            tile.BrickStyle = BrickStyle.SlopeBottomRight;
                            break;

                        case BrickStyle.SlopeTopLeft:
                            tile.BrickStyle = BrickStyle.SlopeBottomLeft;
                            break;

                        case BrickStyle.SlopeBottomRight:
                            tile.BrickStyle = BrickStyle.SlopeTopRight;
                            break;

                        case BrickStyle.SlopeBottomLeft:
                            tile.BrickStyle = BrickStyle.SlopeTopLeft;
                            break;
                        }
                    }

                    flippedBuffer.Tiles[bufferX, bufferY] = (Tile)tile;
                }
            }

            // Replace the existing buffer with the new one
            int bufferIndex = LoadedBuffers.IndexOf(buffer);

            if (bufferIndex > -1)
            {
                LoadedBuffers.Insert(bufferIndex, flippedBuffer);
                LoadedBuffers.RemoveAt(bufferIndex + 1);
            }

            flippedBuffer.RenderBuffer();

            if (Buffer == buffer)
            {
                Buffer = flippedBuffer;
                _wvm.PreviewChange();
            }
        }
        // Reverse the buffer along the x- or y- axis
        public void Flip(ClipboardBuffer buffer, bool flipX)
        {
            ClipboardBuffer flippedBuffer = new ClipboardBuffer(buffer.Size);
            var             sprites       = new Dictionary <Vector2Int32, Sprite>();

            for (int x = 0, maxX = buffer.Size.X - 1; x <= maxX; x++)
            {
                for (int y = 0, maxY = buffer.Size.Y - 1; y <= maxY; y++)
                {
                    int bufferX;
                    int bufferY;

                    if (flipX)
                    {
                        bufferX = maxX - x;
                        bufferY = y;
                    }
                    else
                    {
                        bufferX = x;
                        bufferY = maxY - y;
                    }

                    Tile         tile           = (Tile)buffer.Tiles[x, y].Clone();
                    var          tileProperties = World.TileProperties[tile.Type];
                    Vector2Short tileSize       = tileProperties.FrameSize;

                    // locate all the sprites and make a list
                    if (tileProperties.IsFramed)
                    {
                        var loc = new Vector2Int32(x, y);
                        var uv  = tile.GetUV();
                        if (tileProperties.IsOrigin(uv, out var frame))
                        {
                            var sprite = World.Sprites.FirstOrDefault(s => s.Tile == tile.Type && s.Origin == uv);
                            sprites[loc] = sprite;
                        }
                    }

                    if (flipX)
                    {
                        //  Ignore multi-width objects when flipping on x-axis
                        if (tileSize.X > 1)
                        {
                            ClearTile(tile);
                        }
                        // Flip brick-style
                        switch (tile.BrickStyle)
                        {
                        case BrickStyle.SlopeTopRight:
                            tile.BrickStyle = BrickStyle.SlopeTopLeft;
                            break;

                        case BrickStyle.SlopeTopLeft:
                            tile.BrickStyle = BrickStyle.SlopeTopRight;
                            break;

                        case BrickStyle.SlopeBottomRight:
                            tile.BrickStyle = BrickStyle.SlopeBottomLeft;
                            break;

                        case BrickStyle.SlopeBottomLeft:
                            tile.BrickStyle = BrickStyle.SlopeBottomRight;
                            break;
                        }
                    }

                    else
                    {
                        //  Ignore multi-height tiles when flipping on y-axis
                        if (tileSize.Y > 1)
                        {
                            ClearTile(tile);
                        }
                        // Flip brick-style
                        switch (tile.BrickStyle)
                        {
                        case BrickStyle.SlopeTopRight:
                            tile.BrickStyle = BrickStyle.SlopeBottomRight;
                            break;

                        case BrickStyle.SlopeTopLeft:
                            tile.BrickStyle = BrickStyle.SlopeBottomLeft;
                            break;

                        case BrickStyle.SlopeBottomRight:
                            tile.BrickStyle = BrickStyle.SlopeTopRight;
                            break;

                        case BrickStyle.SlopeBottomLeft:
                            tile.BrickStyle = BrickStyle.SlopeTopLeft;
                            break;
                        }
                    }

                    flippedBuffer.Tiles[bufferX, bufferY] = (Tile)tile;
                }
            }

            foreach (var sprite in sprites)
            {
                var flipOrigin = FlipSprite(buffer.Size, sprite.Key, sprite.Value.Size, flipX);
                Sprite.PlaceSprite(flipOrigin.X, flipOrigin.Y, sprite.Value, flippedBuffer);
            }

            foreach (var chest in buffer.Chests)
            {
                var flipOrigin = FlipSprite(buffer.Size, new Vector2Int32(chest.X, chest.Y), new Vector2Short(2, 2), flipX);
                chest.X = flipOrigin.X;
                chest.Y = flipOrigin.Y;
                flippedBuffer.Chests.Add(chest);
            }

            foreach (var sign in buffer.Signs)
            {
                var flipOrigin = FlipSprite(buffer.Size, new Vector2Int32(sign.X, sign.Y), new Vector2Short(2, 2), flipX);
                sign.X = flipOrigin.X;
                sign.Y = flipOrigin.Y;
                flippedBuffer.Signs.Add(sign);
            }

            foreach (var te in buffer.TileEntities)
            {
                var          tileProperties = World.TileProperties[(int)te.TileType];
                Vector2Short tileSize       = tileProperties.FrameSize;

                var flipOrigin = FlipSprite(buffer.Size, new Vector2Int32(te.PosX, te.PosY), tileSize, flipX);
                te.PosX = (short)flipOrigin.X;
                te.PosY = (short)flipOrigin.Y;
                flippedBuffer.TileEntities.Add(te);
            }

            // Replace the existing buffer with the new one
            int bufferIndex = LoadedBuffers.IndexOf(buffer);

            if (bufferIndex > -1)
            {
                LoadedBuffers.Insert(bufferIndex, flippedBuffer);
                LoadedBuffers.RemoveAt(bufferIndex + 1);
            }

            flippedBuffer.RenderBuffer();

            if (Buffer == buffer)
            {
                Buffer = flippedBuffer;
                _wvm.PreviewChange();
            }
        }