Esempio n. 1
0
        private void Paint()
        {
            if (!layers.HasLayerSelected)
            {
                return;
            }
            if (!tilesets.HasTilesetSelected)
            {
                return;
            }

            // Get brush bucket.
            BrushBucket brushBucket = brushBuckets[tilesets.SelectedTileset];

            // Return if no brush is selected or it cant be used for painting.
            if (!brushBucket.HasBrushSelected)
            {
                return;
            }
            if (!brushBucket.SelectedBrush.CanPaint())
            {
                return;
            }

            int fromX = Mouse.GetState().X;
            int fromY = Mouse.GetState().Y;

            // Check that mouse is inside editors view port.
            if (!SpriteBatch.GraphicsDevice.Viewport.Bounds.Contains(fromX, fromY))
            {
                return;
            }

            // Calculate index.
            fromX = fromX / tileEngine.TileSizeInPixels.X;
            fromY = fromY / tileEngine.TileSizeInPixels.Y;

            // Check that the index is in bounds.
            if (fromX < 0 || fromX >= layers.SelectedLayer.Width)
            {
                return;
            }
            if (fromY < 0 || fromY >= layers.SelectedLayer.Height)
            {
                return;
            }

            // Get brush and paint with it.
            TileBrush brush = brushBucket.SelectedBrush;

            // Transform position relative to the layer.
            fromX -= layers.SelectedLayer.X / tileEngine.TileSizeInPixels.X;
            fromY -= layers.SelectedLayer.Y / tileEngine.TileSizeInPixels.Y;

            int toX = fromX + brush.Width;

            toX = toX >= layers.SelectedLayer.Width ? layers.SelectedLayer.Width : toX;

            int toY = fromY + brush.Height;

            toY = toY >= layers.SelectedLayer.Height ? layers.SelectedLayer.Height : toY;

            // Begin paint.
            brush.BeginPainting();

            for (int i = fromY; i < toY; i++)
            {
                // Check if brush has finished painting.
                if (!brush.Painting())
                {
                    return;
                }

                for (int j = fromX; j < toX; j++)
                {
                    // Check if brush has finished painting.
                    if (!brush.Painting())
                    {
                        return;
                    }

                    PaintArgs paintArgs = brush.Paint();

                    layers.SelectedLayer.TileAtIndex(j, i).Paint(paintArgs);
                }
            }

            // End paint.
            brush.EndPainting();
        }