public LinearGradientBrushImpl( Perspex.Media.LinearGradientBrush brush, SharpDX.Direct2D1.RenderTarget target, Size destinationSize) { if (brush != null) { var gradientStops = brush.GradientStops.Select(s => new SharpDX.Direct2D1.GradientStop { Color = s.Color.ToDirect2D(), Position = (float)s.Offset }).ToArray(); Point startPoint = new Point(0, 0); Point endPoint = new Point(0, 0); switch (brush.MappingMode) { case Perspex.Media.BrushMappingMode.Absolute: // TODO: break; case Perspex.Media.BrushMappingMode.RelativeToBoundingBox: startPoint = new Point(brush.StartPoint.X * destinationSize.Width, brush.StartPoint.Y * destinationSize.Height); endPoint = new Point(brush.EndPoint.X * destinationSize.Width, brush.EndPoint.Y * destinationSize.Height); break; } PlatformBrush = new SharpDX.Direct2D1.LinearGradientBrush( target, new SharpDX.Direct2D1.LinearGradientBrushProperties { StartPoint = startPoint.ToSharpDX(), EndPoint = endPoint.ToSharpDX() }, new SharpDX.Direct2D1.BrushProperties { Opacity = (float)brush.Opacity, Transform = target.Transform }, new SharpDX.Direct2D1.GradientStopCollection(target, gradientStops, brush.SpreadMethod.ToDirect2D()) ); } }
public IFormattedTextImpl CreateFormattedText( string text, string fontFamily, double fontSize, FontStyle fontStyle, TextAlignment textAlignment, Perspex.Media.FontWeight fontWeight) { return new FormattedTextImpl(s_pangoContext, text, fontFamily, fontSize, fontStyle, textAlignment, fontWeight); }
public SolidColorBrushImpl(Perspex.Media.SolidColorBrush brush, double opacityOverride = 1.0f) { var color = brush?.Color.ToCairo() ?? new Color(); if (brush != null) color.A = Math.Min(brush.Opacity, color.A); if (opacityOverride < 1.0f) color.A = Math.Min(opacityOverride, color.A); this.PlatformBrush = new SolidPattern(color); }
public SolidColorBrushImpl(Perspex.Media.SolidColorBrush brush, SharpDX.Direct2D1.RenderTarget target) { PlatformBrush = new SharpDX.Direct2D1.SolidColorBrush( target, brush?.Color.ToDirect2D() ?? new SharpDX.Color4(), new SharpDX.Direct2D1.BrushProperties { Opacity = brush != null ? (float)brush.Opacity : 1.0f, Transform = target.Transform } ); }
public LinearGradientBrushImpl(Perspex.Media.LinearGradientBrush brush, Size destinationSize) { var start = brush.StartPoint.ToPixels(destinationSize); var end = brush.EndPoint.ToPixels(destinationSize); this.PlatformBrush = new LinearGradient(start.X, start.Y, end.X, end.Y); foreach (var stop in brush.GradientStops) ((LinearGradient)this.PlatformBrush).AddColorStop(stop.Offset, stop.Color.ToCairo()); ((LinearGradient)this.PlatformBrush).Extend = Extend.Pad; }
public RadialGradientBrushImpl(Perspex.Media.RadialGradientBrush brush, Size destinationSize) { var center = brush.Center.ToPixels(destinationSize); var gradientOrigin = brush.GradientOrigin.ToPixels(destinationSize); var radius = brush.Radius; this.PlatformBrush = new RadialGradient(center.X, center.Y, radius, gradientOrigin.X, gradientOrigin.Y, radius); foreach (var stop in brush.GradientStops) { ((LinearGradient)this.PlatformBrush).AddColorStop(stop.Offset, stop.Color.ToCairo()); } ((LinearGradient)this.PlatformBrush).Extend = Extend.Pad; }
public void ArcTo( Point point, Size size, double rotationAngle, bool isLargeArc, Perspex.Media.SweepDirection sweepDirection) { _sink.AddArc(new ArcSegment { Point = point.ToSharpDX(), Size = size.ToSharpDX(), RotationAngle = (float)rotationAngle, ArcSize = isLargeArc ? ArcSize.Large : ArcSize.Small, SweepDirection = (SweepDirection)sweepDirection, }); }
public LinearGradientBrushImpl( Perspex.Media.LinearGradientBrush brush, SharpDX.Direct2D1.RenderTarget target, Size destinationSize) { var gradientStops = brush.GradientStops.Select(s => new SharpDX.Direct2D1.GradientStop { Color = s.Color.ToDirect2D(), Position = (float)s.Offset }).ToArray(); Point startPoint = brush.StartPoint.ToPixels(destinationSize); Point endPoint = brush.EndPoint.ToPixels(destinationSize); PlatformBrush = new SharpDX.Direct2D1.LinearGradientBrush( target, new SharpDX.Direct2D1.LinearGradientBrushProperties { StartPoint = startPoint.ToSharpDX(), EndPoint = endPoint.ToSharpDX() }, new SharpDX.Direct2D1.BrushProperties { Opacity = (float)brush.Opacity, Transform = target.Transform }, new SharpDX.Direct2D1.GradientStopCollection(target, gradientStops, brush.SpreadMethod.ToDirect2D()) ); }
public RadialGradientBrushImpl( Perspex.Media.RadialGradientBrush brush, SharpDX.Direct2D1.RenderTarget target, Size destinationSize) { if (brush.GradientStops.Count == 0) { return; } var gradientStops = brush.GradientStops.Select(s => new SharpDX.Direct2D1.GradientStop { Color = s.Color.ToDirect2D(), Position = (float)s.Offset }).ToArray(); var centerPoint = brush.Center.ToPixels(destinationSize); var GradientOriginOffset = brush.GradientOrigin.ToPixels(destinationSize); // Note: Direct2D supports RadiusX and RadiusY but Cairo backend supports only Radius property var radiusX = brush.Radius; var radiusY = brush.Radius; using (var stops = new SharpDX.Direct2D1.GradientStopCollection( target, gradientStops, brush.SpreadMethod.ToDirect2D())) { PlatformBrush = new SharpDX.Direct2D1.RadialGradientBrush( target, new SharpDX.Direct2D1.RadialGradientBrushProperties { Center = centerPoint.ToSharpDX(), GradientOriginOffset = GradientOriginOffset.ToSharpDX(), RadiusX = (float)radiusX, RadiusY = (float)radiusY }, new SharpDX.Direct2D1.BrushProperties { Opacity = (float)brush.Opacity, Transform = PrimitiveExtensions.Matrix3x2Identity, }, stops); } }
public RadialGradientBrushImpl( Perspex.Media.RadialGradientBrush brush, SharpDX.Direct2D1.RenderTarget target, Size destinationSize) { var gradientStops = brush.GradientStops.Select(s => new SharpDX.Direct2D1.GradientStop { Color = s.Color.ToDirect2D(), Position = (float)s.Offset }).ToArray(); Point centerPoint = brush.Center.ToPixels(destinationSize); Point GradientOriginOffset = brush.GradientOrigin.ToPixels(destinationSize); // Note: Direct2D supports RadiusX and RadiusY but Cairo backend supports only Radius property double radiusX = brush.Radius; double radiusY = brush.Radius; PlatformBrush = new SharpDX.Direct2D1.RadialGradientBrush( target, new SharpDX.Direct2D1.RadialGradientBrushProperties { Center = centerPoint.ToSharpDX(), GradientOriginOffset = GradientOriginOffset.ToSharpDX(), RadiusX = (float)radiusX, RadiusY = (float)radiusY }, new SharpDX.Direct2D1.BrushProperties { Opacity = (float)brush.Opacity, Transform = target.Transform }, new SharpDX.Direct2D1.GradientStopCollection(target, gradientStops, brush.SpreadMethod.ToDirect2D()) ); }
/// <summary> /// Draws a geometry. /// </summary> /// <param name="brush">The fill brush.</param> /// <param name="pen">The stroke pen.</param> /// <param name="geometry">The geometry.</param> public void DrawGeometry(Perspex.Media.Brush brush, Perspex.Media.Pen pen, Perspex.Media.Geometry geometry) { if (brush != null) { using (var d2dBrush = brush.ToDirect2D(this.renderTarget)) { GeometryImpl impl = (GeometryImpl)geometry.PlatformImpl; this.renderTarget.FillGeometry(impl.Geometry, d2dBrush); } } if (pen != null) { using (var d2dBrush = pen.Brush.ToDirect2D(this.renderTarget)) using (var d2dStroke = pen.ToDirect2DStrokeStyle(this.renderTarget)) { GeometryImpl impl = (GeometryImpl)geometry.PlatformImpl; this.renderTarget.DrawGeometry(impl.Geometry, d2dBrush, (float)pen.Thickness, d2dStroke); } } }
public LinearGradientBrushImpl( Perspex.Media.LinearGradientBrush brush, SharpDX.Direct2D1.RenderTarget target, Size destinationSize) { if (brush.GradientStops.Count == 0) { return; } var gradientStops = brush.GradientStops.Select(s => new SharpDX.Direct2D1.GradientStop { Color = s.Color.ToDirect2D(), Position = (float)s.Offset }).ToArray(); var startPoint = brush.StartPoint.ToPixels(destinationSize); var endPoint = brush.EndPoint.ToPixels(destinationSize); using (var stops = new SharpDX.Direct2D1.GradientStopCollection( target, gradientStops, brush.SpreadMethod.ToDirect2D())) { PlatformBrush = new SharpDX.Direct2D1.LinearGradientBrush( target, new SharpDX.Direct2D1.LinearGradientBrushProperties { StartPoint = startPoint.ToSharpDX(), EndPoint = endPoint.ToSharpDX() }, new SharpDX.Direct2D1.BrushProperties { Opacity = (float)brush.Opacity, Transform = PrimitiveExtensions.Matrix3x2Identity, }, stops); } }
/// <summary> /// Draws a geometry. /// </summary> /// <param name="brush">The fill brush.</param> /// <param name="pen">The stroke pen.</param> /// <param name="geometry">The geometry.</param> public void DrawGeometry(Perspex.Media.Brush brush, Perspex.Media.Pen pen, Perspex.Media.Geometry geometry) { var impl = geometry.PlatformImpl as StreamGeometryImpl; var clone = new Queue<GeometryOp>(impl.Operations); using (var pop = this.PushTransform(impl.Transform)) { while (clone.Count > 0) { var current = clone.Dequeue(); if (current is BeginOp) { var bo = current as BeginOp; this.context.MoveTo(bo.Point.ToCairo()); } else if (current is LineToOp) { var lto = current as LineToOp; this.context.LineTo(lto.Point.ToCairo()); } else if (current is EndOp) { if (((EndOp)current).IsClosed) this.context.ClosePath(); } else if (current is CurveToOp) { var cto = current as CurveToOp; this.context.CurveTo(cto.Point.ToCairo(), cto.Point2.ToCairo(), cto.Point3.ToCairo()); } } if (brush != null) { this.SetBrush(brush); if (pen != null) this.context.FillPreserve(); else this.context.Fill(); } if (pen != null) { this.SetPen(pen); this.context.Stroke(); } } }
/// <summary> /// Draws a filled rectangle. /// </summary> /// <param name="brush">The brush.</param> /// <param name="rect">The rectangle bounds.</param> /// <param name="cornerRadius">The corner radius.</param> public void FillRectange(Perspex.Media.Brush brush, Rect rect, float cornerRadius) { using (var b = brush.ToDirect2D(this.renderTarget)) { this.renderTarget.FillRoundedRectangle( new RoundedRectangle { Rect = new RectangleF( (float)rect.X, (float)rect.Y, (float)rect.Width, (float)rect.Height), RadiusX = cornerRadius, RadiusY = cornerRadius }, b); } }
/// <summary> /// Draws text. /// </summary> /// <param name="foreground">The foreground brush.</param> /// <param name="origin">The upper-left corner of the text.</param> /// <param name="text">The text.</param> public void DrawText(Perspex.Media.Brush foreground, Perspex.Point origin, FormattedText text) { if (!string.IsNullOrEmpty(text.Text)) { var impl = (FormattedTextImpl)text.PlatformImpl; using (var renderer = new PerspexTextRenderer(this.renderTarget, foreground.ToDirect2D(this.renderTarget))) { impl.TextLayout.Draw(renderer, (float)origin.X, (float)origin.Y); } } }
/// <summary> /// Draws a line. /// </summary> /// <param name="pen">The stroke pen.</param> /// <param name="p1">The first point of the line.</param> /// <param name="p2">The second point of the line.</param> public void DrawLine(Pen pen, Perspex.Point p1, Perspex.Point p2) { if (pen != null) { using (var d2dBrush = pen.Brush.ToDirect2D(this.renderTarget)) using (var d2dStroke = pen.ToDirect2DStrokeStyle(this.renderTarget)) { this.renderTarget.DrawLine( p1.ToSharpDX(), p2.ToSharpDX(), d2dBrush, (float)pen.Thickness, d2dStroke); } } }
/// <summary> /// Creates a Direct2D brush wrapper for a Perspex brush. /// </summary> /// <param name="brush">The perspex brush.</param> /// <param name="destinationSize">The size of the brush's target area.</param> /// <returns>The Direct2D brush wrapper.</returns> public BrushImpl CreateBrush(Perspex.Media.Brush brush, Size destinationSize) { var solidColorBrush = brush as Perspex.Media.SolidColorBrush; var linearGradientBrush = brush as Perspex.Media.LinearGradientBrush; var radialGradientBrush = brush as Perspex.Media.RadialGradientBrush; var imageBrush = brush as ImageBrush; var visualBrush = brush as VisualBrush; if (solidColorBrush != null) { return new SolidColorBrushImpl(solidColorBrush, _renderTarget); } else if (linearGradientBrush != null) { return new LinearGradientBrushImpl(linearGradientBrush, _renderTarget, destinationSize); } else if (radialGradientBrush != null) { return new RadialGradientBrushImpl(radialGradientBrush, _renderTarget, destinationSize); } else if (imageBrush != null) { return new ImageBrushImpl(imageBrush, _renderTarget, destinationSize); } else if (visualBrush != null) { return new VisualBrushImpl(visualBrush, _renderTarget, destinationSize); } else { return new SolidColorBrushImpl(null, _renderTarget); } }
/// <summary> /// Draws a filled rectangle. /// </summary> /// <param name="brush">The brush.</param> /// <param name="rect">The rectangle bounds.</param> public void FillRectange(Perspex.Media.Brush brush, Rect rect, float cornerRadius) { this.SetBrush(brush); this.context.Rectangle(rect.ToCairo()); this.context.Fill(); }
/// <summary> /// Draws text. /// </summary> /// <param name="foreground">The foreground brush.</param> /// <param name="origin">The upper-left corner of the text.</param> /// <param name="text">The text.</param> public void DrawText(Perspex.Media.Brush foreground, Point origin, FormattedText text) { if (!string.IsNullOrEmpty(text.Text)) { var impl = (FormattedTextImpl)text.PlatformImpl; using (var brush = CreateBrush(foreground, impl.Measure())) using (var renderer = new PerspexTextRenderer(this, _renderTarget, brush.PlatformBrush)) { if (brush.PlatformBrush != null) { impl.TextLayout.Draw(renderer, (float)origin.X, (float)origin.Y); } } } }
/// <summary> /// Draws a filled rectangle. /// </summary> /// <param name="brush">The brush.</param> /// <param name="rect">The rectangle bounds.</param> /// <param name="cornerRadius">The corner radius.</param> public void FillRectangle(Perspex.Media.Brush brush, Rect rect, float cornerRadius) { using (var b = CreateBrush(brush, rect.Size)) { if (b.PlatformBrush != null) { if (cornerRadius == 0) { _renderTarget.FillRectangle(rect.ToDirect2D(), b.PlatformBrush); } else { _renderTarget.FillRoundedRectangle( new RoundedRectangle { Rect = new RectangleF( (float)rect.X, (float)rect.Y, (float)rect.Width, (float)rect.Height), RadiusX = cornerRadius, RadiusY = cornerRadius }, b.PlatformBrush); } } } }
/// <summary> /// Draws a geometry. /// </summary> /// <param name="brush">The fill brush.</param> /// <param name="pen">The stroke pen.</param> /// <param name="geometry">The geometry.</param> public void DrawGeometry(Perspex.Media.Brush brush, Pen pen, Perspex.Media.Geometry geometry) { if (brush != null) { using (var d2dBrush = CreateBrush(brush, geometry.Bounds.Size)) { if (d2dBrush.PlatformBrush != null) { var impl = (GeometryImpl)geometry.PlatformImpl; _renderTarget.FillGeometry(impl.Geometry, d2dBrush.PlatformBrush); } } } if (pen != null) { using (var d2dBrush = CreateBrush(pen.Brush, geometry.GetRenderBounds(pen.Thickness).Size)) using (var d2dStroke = pen.ToDirect2DStrokeStyle(_renderTarget)) { if (d2dBrush.PlatformBrush != null) { var impl = (GeometryImpl)geometry.PlatformImpl; _renderTarget.DrawGeometry(impl.Geometry, d2dBrush.PlatformBrush, (float)pen.Thickness, d2dStroke); } } } }
public ImageBrushImpl(Perspex.Media.ImageBrush brush, Size destinationSize) { this.PlatformBrush = TileBrushes.CreateTileBrush(brush, destinationSize); }
public NameScopeWrapper(Perspex.INameScope inner) { _inner = inner; }
/// <summary> /// Draws a line. /// </summary> /// <param name="pen">The stroke pen.</param> /// <param name="p1">The first point of the line.</param> /// <param name="p1">The second point of the line.</param> public void DrawLine(Pen pen, Perspex.Point p1, Perspex.Point p2) { this.SetBrush(pen.Brush); this.context.LineWidth = pen.Thickness; this.context.MoveTo(p1.ToCairo()); this.context.LineTo(p2.ToCairo()); this.context.Stroke(); }
public VisualBrushImpl(Perspex.Media.VisualBrush brush, Size destinationSize) { this.PlatformBrush = TileBrushes.CreateVisualBrush(brush, destinationSize); }