示例#1
0
        public TileBrushImpl(
            ITileBrush brush,
            SharpDX.Direct2D1.RenderTarget target,
            Size targetSize)
        {
            var helper = new TileBrushImplHelper(brush, targetSize);

            if (!helper.IsValid)
            {
                return;
            }

            using (var intermediate = new BitmapRenderTarget(target, CompatibleRenderTargetOptions.None, helper.IntermediateSize.ToSharpDX()))
            {
                using (var ctx = new RenderTarget(intermediate).CreateDrawingContext())
                {
                    intermediate.Clear(null);
                    helper.DrawIntermediate(ctx);
                }

                PlatformBrush = new BitmapBrush(
                    target,
                    intermediate.Bitmap,
                    GetBitmapBrushProperties(brush),
                    GetBrushProperties(brush, helper.DestinationRect));
            }
        }
示例#2
0
        public ImageBrushImpl(
            ITileBrush brush,
            SharpDX.Direct2D1.RenderTarget target,
            BitmapImpl bitmap,
            Size targetSize)
        {
            var calc = new TileBrushCalculator(brush, new Size(bitmap.PixelWidth, bitmap.PixelHeight), targetSize);

            if (!calc.NeedsIntermediate)
            {
                PlatformBrush = new BitmapBrush(
                    target,
                    bitmap.GetDirect2DBitmap(target),
                    GetBitmapBrushProperties(brush),
                    GetBrushProperties(brush, calc.DestinationRect));
            }
            else
            {
                using (var intermediate = RenderIntermediate(target, bitmap, calc))
                {
                    PlatformBrush = new BitmapBrush(
                        target,
                        intermediate.Bitmap,
                        GetBitmapBrushProperties(brush),
                        GetBrushProperties(brush, calc.DestinationRect));
                }
            }
        }
示例#3
0
        /// <summary>
        /// Calculates a translate based on an <see cref="ITileBrush"/>, a source and destination
        /// rectangle and a scale.
        /// </summary>
        /// <param name="brush">The brush.</param>
        /// <param name="sourceRect">The source rectangle.</param>
        /// <param name="destinationRect">The destination rectangle.</param>
        /// <param name="scale">The _scale factor.</param>
        /// <returns>A vector with the X and Y _translate.</returns>

        public static Vector CalculateTranslate(
            ITileBrush brush,
            Rect sourceRect,
            Rect destinationRect,
            Vector scale)
        {
            var x    = 0.0;
            var y    = 0.0;
            var size = sourceRect.Size * scale;

            switch (brush.AlignmentX)
            {
            case AlignmentX.Center:
                x += (destinationRect.Width - size.Width) / 2;
                break;

            case AlignmentX.Right:
                x += destinationRect.Width - size.Width;
                break;
            }

            switch (brush.AlignmentY)
            {
            case AlignmentY.Center:
                y += (destinationRect.Height - size.Height) / 2;
                break;

            case AlignmentY.Bottom:
                y += destinationRect.Height - size.Height;
                break;
            }

            return(new Vector(x, y));
        }
        public ImageBrushImpl(
            ITileBrush brush,
            SharpDX.Direct2D1.RenderTarget target,
            BitmapImpl bitmap,
            Size targetSize)
        {
            var dpi  = new Vector(target.DotsPerInch.Width, target.DotsPerInch.Height);
            var calc = new TileBrushCalculator(brush, bitmap.PixelSize.ToSizeWithDpi(dpi), targetSize);

            if (!calc.NeedsIntermediate)
            {
                _bitmap       = bitmap.GetDirect2DBitmap(target);
                PlatformBrush = new BitmapBrush(
                    target,
                    _bitmap.Value,
                    GetBitmapBrushProperties(brush),
                    GetBrushProperties(brush, calc.DestinationRect));
            }
            else
            {
                using (var intermediate = RenderIntermediate(target, bitmap, calc))
                {
                    PlatformBrush = new BitmapBrush(
                        target,
                        intermediate.Bitmap,
                        GetBitmapBrushProperties(brush),
                        GetBrushProperties(brush, calc.DestinationRect));
                }
            }

            _bitmapInterpolationMode = brush.BitmapInterpolationMode;
        }
 public BucketToolBehavior(ITileBrush brush)
 {
     _brush = brush;
     changes = new List<TileChange>();
     width = brush.Cells.Length;
     height = brush.Cells[0].Length;
 }
 public BucketToolBehavior(ITileBrush brush)
 {
     _brush  = brush;
     changes = new List <TileChange>();
     width   = brush.Cells.Length;
     height  = brush.Cells[0].Length;
 }
示例#7
0
        public void AddBrush(ITileBrush brush)
        {
            if (brush == null) return;

            brushes.Add(brush);
            AddBrushPanel(brush);
            SaveBrushes();
        }
示例#8
0
 public BrushTool(ITileBrush brush)
 {
     this.brush = brush;
     held       = false;
     Icon       = new Bitmap(brush.Width * brush.CellSize, brush.Height * brush.CellSize);
     using (Graphics g = Graphics.FromImage(Icon))
     {
         brush.DrawOn(g, 0, 0);
     }
 }
        private static BitmapBrushProperties GetBitmapBrushProperties(ITileBrush brush)
        {
            var tileMode = brush.TileMode;

            return(new BitmapBrushProperties
            {
                ExtendModeX = GetExtendModeX(tileMode),
                ExtendModeY = GetExtendModeY(tileMode),
            });
        }
示例#10
0
 public BrushTool(ITileBrush brush)
 {
     this.brush = brush;
     held = false;
     Icon = new Bitmap(brush.Width * brush.CellSize, brush.Height * brush.CellSize);
     using (Graphics g = Graphics.FromImage(Icon))
     {
         brush.DrawOn(g, 0, 0);
     }
 }
示例#11
0
        private void ChangeBrush(ITileBrush brush)
        {
            currentBrush = brush;
            BrushChangedEventArgs args = new BrushChangedEventArgs(brush);

            if (BrushChanged != null)
            {
                BrushChanged(args);
            }
        }
示例#12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ImageBrush"/> class.
 /// </summary>
 /// <param name="source">The brush from which this brush's properties should be copied.</param>
 protected ImmutableTileBrush(ITileBrush source)
     : this(
         source.AlignmentX,
         source.AlignmentY,
         source.DestinationRect,
         source.Opacity,
         source.SourceRect,
         source.Stretch,
         source.TileMode)
 {
 }
示例#13
0
        public void AddBrush(ITileBrush brush)
        {
            if (brush == null)
            {
                return;
            }

            brushes.Add(brush);
            AddBrushPanel(brush);
            SaveBrushes();
        }
示例#14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TileBrushCalculator"/> class.
 /// </summary>
 /// <param name="brush">The brush to be rendered.</param>
 /// <param name="contentSize">The size of the content of the tile brush.</param>
 /// <param name="targetSize">The size of the control to which the brush is being rendered.</param>
 public TileBrushCalculator(ITileBrush brush, Size contentSize, Size targetSize)
     : this(
         brush.TileMode,
         brush.Stretch,
         brush.AlignmentX,
         brush.AlignmentY,
         brush.SourceRect,
         brush.DestinationRect,
         contentSize,
         targetSize)
 {
 }
        private static BrushProperties GetBrushProperties(ITileBrush brush, Rect destinationRect)
        {
            var tileTransform =
                brush.TileMode != TileMode.None ?
                Matrix.CreateTranslation(destinationRect.X, destinationRect.Y) :
                Matrix.Identity;

            return(new BrushProperties
            {
                Opacity = (float)brush.Opacity,
                Transform = tileTransform.ToDirect2D(),
            });
        }
示例#16
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ImageBrush"/> class.
 /// </summary>
 /// <param name="source">The brush from which this brush's properties should be copied.</param>
 protected ImmutableTileBrush(ITileBrush source)
     : this(
         source.AlignmentX,
         source.AlignmentY,
         source.DestinationRect,
         source.Opacity,
         source.Transform?.ToImmutable(),
         source.SourceRect,
         source.Stretch,
         source.TileMode,
         source.BitmapInterpolationMode)
 {
 }
示例#17
0
        public TileBrushImplHelper(ITileBrush brush, Size targetSize)
        {
            _imageBrush  = brush as IImageBrush;
            _visualBrush = brush as IVisualBrush;
            if (_imageBrush != null)
            {
                if (_imageBrush.Source == null)
                {
                    return;
                }
                _imageSize = new Size(_imageBrush.Source.PixelWidth, _imageBrush.Source.PixelHeight);
                IsValid    = true;
            }
            else if (_visualBrush != null)
            {
                var control = _visualBrush.Visual as IControl;

                if (control != null)
                {
                    EnsureInitialized(control);

                    if (control.IsArrangeValid == false)
                    {
                        control.Measure(Size.Infinity);
                        control.Arrange(new Rect(control.DesiredSize));
                    }

                    _imageSize = control.Bounds.Size;
                    IsValid    = true;
                }
            }
            else
            {
                return;
            }

            _tileMode        = brush.TileMode;
            _sourceRect      = brush.SourceRect.ToPixels(_imageSize);
            DestinationRect  = brush.DestinationRect.ToPixels(targetSize);
            _scale           = brush.Stretch.CalculateScaling(DestinationRect.Size, _sourceRect.Size);
            _translate       = CalculateTranslate(brush, _sourceRect, DestinationRect, _scale);
            IntermediateSize = CalculateIntermediateSize(_tileMode, targetSize, DestinationRect.Size);
            _transform       = CalculateIntermediateTransform(
                _tileMode,
                _sourceRect,
                DestinationRect,
                _scale,
                _translate,
                out _drawRect);
        }
示例#18
0
 public Bucket(ITileBrush brush)
 {
     width = brush.Width;
     height = brush.Height;
     cells = new Tile[width, height];
     foreach (TileBrushCell cell in brush.Cells())
     {
         cells[cell.x, cell.y] = cell.tile;
     }
     Icon = new Bitmap(brush.Width * brush.CellSize, brush.Height * brush.CellSize);
     using (Graphics g = Graphics.FromImage(Icon))
     {
         brush.DrawOn(g, 0, 0);
     }
     changes = new List<TileChange>();
 }
示例#19
0
 public Bucket(ITileBrush brush)
 {
     width  = brush.Width;
     height = brush.Height;
     cells  = new Tile[width, height];
     foreach (TileBrushCell cell in brush.Cells())
     {
         cells[cell.x, cell.y] = cell.tile;
     }
     Icon = new Bitmap(brush.Width * brush.CellSize, brush.Height * brush.CellSize);
     using (Graphics g = Graphics.FromImage(Icon))
     {
         brush.DrawOn(g, 0, 0);
     }
     changes = new List <TileChange>();
 }
示例#20
0
        public ImageBrushImpl(
            ITileBrush brush,
            IBitmapImpl bitmap,
            Size targetSize)
        {
            var calc = new TileBrushCalculator(brush, new Size(bitmap.PixelWidth, bitmap.PixelHeight), targetSize);

            using (var intermediate = new ImageSurface(Format.ARGB32, (int)calc.IntermediateSize.Width, (int)calc.IntermediateSize.Height))
            {
                using (var context = new RenderTarget(intermediate).CreateDrawingContext(null))
                {
                    var rect = new Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight);

                    context.Clear(Colors.Transparent);
                    context.PushClip(calc.IntermediateClip);
                    context.Transform = calc.IntermediateTransform;
                    context.DrawImage(bitmap, 1, rect, rect);
                    context.PopClip();
                }

                var result = new SurfacePattern(intermediate);

                if ((brush.TileMode & TileMode.FlipXY) != 0)
                {
                    // TODO: Currently always FlipXY as that's all cairo supports natively.
                    // Support separate FlipX and FlipY by drawing flipped images to intermediate
                    // surface.
                    result.Extend = Extend.Reflect;
                }
                else
                {
                    result.Extend = Extend.Repeat;
                }

                if (brush.TileMode != TileMode.None)
                {
                    var matrix = result.Matrix;
                    matrix.InitTranslate(-calc.DestinationRect.X, -calc.DestinationRect.Y);
                    result.Matrix = matrix;
                }

                PlatformBrush = result;
            }
        }
示例#21
0
        public static SurfacePattern CreateTileBrush(ITileBrush brush, Size targetSize)
        {
            var helper = new TileBrushImplHelper(brush, targetSize);

            if (!helper.IsValid)
            {
                return(null);
            }

            using (var intermediate = new ImageSurface(Format.ARGB32, (int)helper.IntermediateSize.Width, (int)helper.IntermediateSize.Height))
                using (var ctx = new RenderTarget(intermediate).CreateDrawingContext())
                {
                    helper.DrawIntermediate(ctx);

                    var result = new SurfacePattern(intermediate);

                    if ((brush.TileMode & TileMode.FlipXY) != 0)
                    {
                        // TODO: Currently always FlipXY as that's all cairo supports natively.
                        // Support separate FlipX and FlipY by drawing flipped images to intermediate
                        // surface.
                        result.Extend = Extend.Reflect;
                    }
                    else
                    {
                        result.Extend = Extend.Repeat;
                    }

                    if (brush.TileMode != TileMode.None)
                    {
                        var matrix = result.Matrix;
                        matrix.InitTranslate(-helper.DestinationRect.X, -helper.DestinationRect.Y);
                        result.Matrix = matrix;
                    }

                    return(result);
                }
        }
示例#22
0
        private void AddBrushPanel(ITileBrush brush)
        {
            PictureBox brushPict = new PictureBox();

            if (Tileset != null)
            {
                brushPict.Image = new Bitmap(brush.Width * Tileset.TileSize, brush.Height * Tileset.TileSize);
                brushPict.Size  = brushPict.Image.Size;
                using (Graphics g = Graphics.FromImage(brushPict.Image))
                {
                    brush.DrawOn(g, 0, 0);
                }
            }

            Panel border = new Panel
            {
                BackColor = brushPanel.BackColor,
                Width     = brushPict.Width + 8,
                Height    = brushPict.Height + 8
            };

            border.Controls.Add(brushPict);
            brushPict.Top  = 4;
            brushPict.Left = 4;

            brushPict.Click += (snd, args) =>
            {
                ChangeBrush(brush);
                foreach (Control c in brushPanel.Controls)
                {
                    c.BackColor = brushPanel.BackColor;
                }
                border.BackColor = Color.Orange;
            };

            brushPanels.Add(brush, border);
            brushPanel.Controls.Add(border);
        }
示例#23
0
 public BrushChangedEventArgs(ITileBrush brush)
 {
     Brush = brush;
 }
示例#24
0
 public RectangleTool(ITileBrush brush)
 {
     this.brush = brush;
     held = false;
 }
示例#25
0
        public TileBrushCursor(ITileBrush brush)
        {
            _brush = brush;

            var image = new WriteableBitmap((int)Width, (int)Height, 96, 96, PixelFormats.Pbgra32, null);
        }
示例#26
0
 public RectangleToolBehavior(ITileBrush brush)
 {
     this.brush = brush;
     held       = false;
 }
示例#27
0
 public TileBrushToolBehavior(ITileBrush brush)
 {
     _brush = brush;
 }
        public TileBrushCursor(ITileBrush brush)
        {
            _brush = brush;

            var image = new WriteableBitmap((int)Width, (int)Height, 96, 96, PixelFormats.Pbgra32, null);
        }
示例#29
0
 private void ChangeBrush(ITileBrush brush)
 {
     _currentBrush = brush;
     ConstructTool();
 }
 public TileBrushToolBehavior(ITileBrush brush)
 {
     _brush = brush;
 }
示例#31
0
        /// <summary>
        /// Configure paint wrapper for using tile brush.
        /// </summary>
        /// <param name="paintWrapper">Paint wrapper.</param>
        /// <param name="targetSize">Target size.</param>
        /// <param name="tileBrush">Tile brush to use.</param>
        /// <param name="tileBrushImage">Tile brush image.</param>
        /// <param name="interpolationMode">The bitmap interpolation mode.</param>
        private void ConfigureTileBrush(ref PaintWrapper paintWrapper, Size targetSize, ITileBrush tileBrush, IDrawableBitmapImpl tileBrushImage)
        {
            var calc = new TileBrushCalculator(tileBrush,
                                               new Size(tileBrushImage.PixelWidth, tileBrushImage.PixelHeight), targetSize);

            var intermediate = CreateRenderTarget(
                (int)calc.IntermediateSize.Width,
                (int)calc.IntermediateSize.Height, _dpi);

            paintWrapper.AddDisposable(intermediate);

            using (var context = intermediate.CreateDrawingContext(null))
            {
                var rect = new Rect(0, 0, tileBrushImage.PixelWidth, tileBrushImage.PixelHeight);

                context.Clear(Colors.Transparent);
                context.PushClip(calc.IntermediateClip);
                context.Transform = calc.IntermediateTransform;
                context.DrawImage(RefCountable.CreateUnownedNotClonable(tileBrushImage), 1, rect, rect, tileBrush.BitmapInterpolationMode);
                context.PopClip();
            }

            var tileTransform =
                tileBrush.TileMode != TileMode.None
                    ? SKMatrix.MakeTranslation(-(float)calc.DestinationRect.X, -(float)calc.DestinationRect.Y)
                    : SKMatrix.MakeIdentity();

            SKShaderTileMode tileX =
                tileBrush.TileMode == TileMode.None
                    ? SKShaderTileMode.Clamp
                    : tileBrush.TileMode == TileMode.FlipX || tileBrush.TileMode == TileMode.FlipXY
                        ? SKShaderTileMode.Mirror
                        : SKShaderTileMode.Repeat;

            SKShaderTileMode tileY =
                tileBrush.TileMode == TileMode.None
                    ? SKShaderTileMode.Clamp
                    : tileBrush.TileMode == TileMode.FlipY || tileBrush.TileMode == TileMode.FlipXY
                        ? SKShaderTileMode.Mirror
                        : SKShaderTileMode.Repeat;


            var image = intermediate.SnapshotImage();

            paintWrapper.AddDisposable(image);

            using (var shader = image.ToShader(tileX, tileY, tileTransform))
            {
                paintWrapper.Paint.Shader = shader;
            }
        }
示例#32
0
 public BrushChangedEventArgs(ITileBrush brush)
 {
     Brush = brush;
 }
示例#33
0
 private void ChangeBrush(ITileBrush brush)
 {
     currentBrush = brush;
     BrushChangedEventArgs args = new BrushChangedEventArgs(brush);
     if (BrushChanged != null) BrushChanged(args);
 }
示例#34
0
        private void AddBrushPanel(ITileBrush brush)
        {
            PictureBox brushPict = new PictureBox();

            if (Tileset != null)
            {
                brushPict.Image = new Bitmap(brush.Width * Tileset.TileSize, brush.Height * Tileset.TileSize);
                brushPict.Size = brushPict.Image.Size;
                using (Graphics g = Graphics.FromImage(brushPict.Image))
                {
                    brush.DrawOn(g, 0, 0);
                }
            }

            Panel border = new Panel
            {
                BackColor = brushPanel.BackColor,
                Width = brushPict.Width + 8,
                Height = brushPict.Height + 8
            };

            border.Controls.Add(brushPict);
            brushPict.Top = 4;
            brushPict.Left = 4;

            brushPict.Click += (snd, args) =>
            {
                ChangeBrush(brush);
                foreach (Control c in brushPanel.Controls) c.BackColor = brushPanel.BackColor;
                border.BackColor = Color.Orange;
            };

            brushPanels.Add(brush, border);
            brushPanel.Controls.Add(border);
        }
示例#35
0
        /// <summary>
        /// Configure paint wrapper for using tile brush.
        /// </summary>
        /// <param name="paintWrapper">Paint wrapper.</param>
        /// <param name="targetSize">Target size.</param>
        /// <param name="tileBrush">Tile brush to use.</param>
        /// <param name="tileBrushImage">Tile brush image.</param>
        private void ConfigureTileBrush(ref PaintWrapper paintWrapper, Size targetSize, ITileBrush tileBrush, IDrawableBitmapImpl tileBrushImage)
        {
            var calc         = new TileBrushCalculator(tileBrush, tileBrushImage.PixelSize.ToSizeWithDpi(_dpi), targetSize);
            var intermediate = CreateRenderTarget(calc.IntermediateSize);

            paintWrapper.AddDisposable(intermediate);

            using (var context = intermediate.CreateDrawingContext(null))
            {
                var sourceRect = new Rect(tileBrushImage.PixelSize.ToSizeWithDpi(96));
                var targetRect = new Rect(tileBrushImage.PixelSize.ToSizeWithDpi(_dpi));

                context.Clear(Colors.Transparent);
                context.PushClip(calc.IntermediateClip);
                context.Transform = calc.IntermediateTransform;
                context.DrawBitmap(
                    RefCountable.CreateUnownedNotClonable(tileBrushImage),
                    1,
                    sourceRect,
                    targetRect,
                    tileBrush.BitmapInterpolationMode);
                context.PopClip();
            }

            var tileTransform =
                tileBrush.TileMode != TileMode.None
                    ? SKMatrix.CreateTranslation(-(float)calc.DestinationRect.X, -(float)calc.DestinationRect.Y)
                    : SKMatrix.CreateIdentity();

            SKShaderTileMode tileX =
                tileBrush.TileMode == TileMode.None
                    ? SKShaderTileMode.Clamp
                    : tileBrush.TileMode == TileMode.FlipX || tileBrush.TileMode == TileMode.FlipXY
                        ? SKShaderTileMode.Mirror
                        : SKShaderTileMode.Repeat;

            SKShaderTileMode tileY =
                tileBrush.TileMode == TileMode.None
                    ? SKShaderTileMode.Clamp
                    : tileBrush.TileMode == TileMode.FlipY || tileBrush.TileMode == TileMode.FlipXY
                        ? SKShaderTileMode.Mirror
                        : SKShaderTileMode.Repeat;


            var image = intermediate.SnapshotImage();

            paintWrapper.AddDisposable(image);

            var paintTransform = default(SKMatrix);

            SKMatrix.Concat(
                ref paintTransform,
                tileTransform,
                SKMatrix.CreateScale((float)(96.0 / _dpi.X), (float)(96.0 / _dpi.Y)));

            using (var shader = image.ToShader(tileX, tileY, paintTransform))
            {
                paintWrapper.Paint.Shader = shader;
            }
        }
 private void ChangeBrush(ITileBrush brush)
 {
     _currentBrush = brush;
     ConstructTool();
 }