public void UpdateCanvas()
    {
        List <tk2dSparseTile> newTiles = new List <tk2dSparseTile>();

        for (int iLayer = 0; iLayer < layers.Length; ++iLayer)
        {
            tk2dScratchpadLayer layer = layers[iLayer];

            for (int y = 0; y < layer.height; ++y)
            {
                for (int x = 0; x < layer.width; ++x)
                {
                    int k = y * layer.width + x;

                    if (layer.tiles[k] != -1)
                    {
                        newTiles.Add(new tk2dSparseTile(x, y, iLayer, layer.tiles[k]));
                    }
                }
            }
        }

        canvas.tiles = newTiles.ToArray();

        canvas.SortTiles(tileSortLeftToRight, tileSortBottomToTop);

        canvas.UpdateBrushHash();
    }
示例#2
0
    public void UpdateWorkingBrush()
    {
        tk2dTileMapEditorBrush workBrush   = WorkingBrush;
        tk2dTileMapEditorBrush activeBrush = editorData.activeBrush;

        int rectX1 = Mathf.Min(cursorX, cursorX0);
        int rectX2 = Mathf.Max(cursorX, cursorX0);
        int rectY1 = Mathf.Min(cursorY, cursorY0);
        int rectY2 = Mathf.Max(cursorY, cursorY0);

        int xoffset = 0;

        if (tileMap.data.tileType == tk2dTileMapData.TileType.Isometric && (cursorY & 1) == 1)
        {
            xoffset = 1;
        }

        workBrush.tiles = new tk2dSparseTile[0];

        tk2dSparseTile[] srcTiles;
        if (activeBrush.type != tk2dTileMapEditorBrush.Type.MultiSelect)
        {
            srcTiles = activeBrush.tiles;
        }
        else
        {
            int n = activeBrush.multiSelectTiles.Length;
            srcTiles = new tk2dSparseTile[n];
            for (int i = 0; i < n; ++i)
            {
                srcTiles[i] = new tk2dSparseTile(i, 0, editorData.layer, activeBrush.multiSelectTiles[i]);
            }
        }

        if (srcTiles.Length == 0)
        {
            workBrush.UpdateBrushHash();
            return;
        }

        bool flipH = tk2dTileMapToolbar.workBrushFlipX;
        bool flipV = tk2dTileMapToolbar.workBrushFlipY;

        if (tk2dTileMapToolbar.mainMode == tk2dTileMapToolbar.MainMode.Brush)
        {
            if (rectX1 == rectX2 && rectY1 == rectY2)
            {
                int nTiles = srcTiles.Length;
                workBrush.tiles = new tk2dSparseTile[nTiles];

                for (int i = 0; i < nTiles; ++i)
                {
                    int spriteId = srcTiles[i].spriteId;
                    int tx       = srcTiles[i].x;
                    int ty       = srcTiles[i].y;

                    if (flipH)
                    {
                        tx = -tx;
                        tk2dRuntime.TileMap.BuilderUtil.InvertRawTileFlag(ref spriteId, tk2dTileFlags.FlipX);
                    }
                    if (flipV)
                    {
                        ty = -ty;
                        tk2dRuntime.TileMap.BuilderUtil.InvertRawTileFlag(ref spriteId, tk2dTileFlags.FlipY);
                    }

                    int thisRowXOffset = ((ty & 1) == 1) ? xoffset : 0;

                    workBrush.tiles[i] = new tk2dSparseTile(
                        cursorX + tx + thisRowXOffset,
                        cursorY + ty,
                        editorData.layer,
                        spriteId);
                }
            }
            else
            {
                int gridWidth  = 1 + rectX2 - rectX1;
                int gridHeight = 1 + rectY2 - rectY1;
                workBrush.tiles = new tk2dSparseTile[gridWidth * gridHeight];

                // fill with tiles repeated pattern...
                int patternX1 = 0;
                int patternY1 = 0;
                int patternX2 = 0;
                int patternY2 = 0;
                foreach (var tile in srcTiles)
                {
                    patternX1 = Mathf.Min(patternX1, tile.x);
                    patternY1 = Mathf.Min(patternY1, tile.y);
                    patternX2 = Mathf.Max(patternX2, tile.x);
                    patternY2 = Mathf.Max(patternY2, tile.y);
                }
                int patternW = 1 + patternX2 - patternX1;
                int patternH = 1 + patternY2 - patternY1;

                int idx = 0;
                for (int y = 0; y < gridHeight; ++y)
                {
                    int thisRowXOffset = ((y & 1) == 1) ? xoffset : 0;
                    for (int x = 0; x < gridWidth; ++x)
                    {
                        int spriteId = srcTiles[0].spriteId;
                        foreach (var tile in srcTiles)
                        {
                            if ((x % patternW) == (tile.x - patternX1) &&
                                (y % patternH) == (tile.y - patternY1))
                            {
                                spriteId = tile.spriteId;
                                break;
                            }
                        }
                        if (flipH)
                        {
                            tk2dRuntime.TileMap.BuilderUtil.InvertRawTileFlag(ref spriteId, tk2dTileFlags.FlipX);
                        }
                        if (flipV)
                        {
                            tk2dRuntime.TileMap.BuilderUtil.InvertRawTileFlag(ref spriteId, tk2dTileFlags.FlipY);
                        }
                        workBrush.tiles[idx++] = new tk2dSparseTile(
                            rectX1 + x + thisRowXOffset, rectY1 + y, editorData.layer, spriteId);
                    }
                }
            }
        }
        if (tk2dTileMapToolbar.mainMode == tk2dTileMapToolbar.MainMode.BrushRandom)
        {
            int gridWidth  = 1 + rectX2 - rectX1;
            int gridHeight = 1 + rectY2 - rectY1;
            workBrush.tiles = new tk2dSparseTile[gridWidth * gridHeight];

            var rng = new System.Random(randomSeed + cursorY * tileMap.width + cursorX);

            int idx = 0;
            for (int y = 0; y < gridHeight; ++y)
            {
                int thisRowXOffset = ((y & 1) == 1) ? xoffset : 0;
                for (int x = 0; x < gridWidth; ++x)
                {
                    int spriteId = srcTiles[rng.Next(srcTiles.Length)].spriteId;
                    workBrush.tiles[idx++] = new tk2dSparseTile(
                        rectX1 + x + thisRowXOffset, rectY1 + y, editorData.layer, spriteId);
                }
            }
        }

        if (scratchpadGUI.workingHere)
        {
            int scratchW, scratchH;
            scratchpadGUI.GetScratchpadSize(out scratchW, out scratchH);
            workBrush.ClipTiles(0, 0, scratchW - 1, scratchH - 1);
        }
        else
        {
            workBrush.ClipTiles(0, 0, tileMap.width - 1, tileMap.height - 1);
        }

        workBrush.SortTiles(tileMapData.sortMethod == tk2dTileMapData.SortMethod.BottomLeft || tileMapData.sortMethod == tk2dTileMapData.SortMethod.TopLeft,
                            tileMapData.sortMethod == tk2dTileMapData.SortMethod.BottomLeft || tileMapData.sortMethod == tk2dTileMapData.SortMethod.BottomRight);

        workBrush.UpdateBrushHash();
    }