/// <summary> /// Calculates a translate based on a <see cref="TileBrush"/>, 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> protected static Vector CalculateTranslate( TileBrush 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 static SurfacePattern CreateTileBrush(TileBrush 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; } }
private static BitmapBrushProperties GetBitmapBrushProperties(TileBrush brush) { var tileMode = brush.TileMode; return new BitmapBrushProperties { ExtendModeX = GetExtendModeX(tileMode), ExtendModeY = GetExtendModeY(tileMode), }; }
private static BrushProperties GetBrushProperties(TileBrush brush, Rect destinationRect) { return new BrushProperties { Opacity = (float)brush.Opacity, Transform = brush.TileMode != TileMode.None ? SharpDX.Matrix3x2.Translation( (float)destinationRect.X, (float)destinationRect.Y) : SharpDX.Matrix3x2.Identity, }; }
private static BrushProperties GetBrushProperties(TileBrush 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(), }; }
public TileBrushImpl( TileBrush 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()) helper.DrawIntermediate(ctx); PlatformBrush = new BitmapBrush( target, intermediate.Bitmap, GetBitmapBrushProperties(brush), GetBrushProperties(brush, helper.DestinationRect)); } }