From 200f683d36d4763defb9b459b24aef7551193296 Mon Sep 17 00:00:00 2001 From: Nelson Carrillo Date: Thu, 3 Sep 2015 15:21:03 -0400 Subject: [PATCH 1/2] Implemented LinearGradientBrush for Cairo --- src/Gtk/Perspex.Cairo/Media/DrawingContext.cs | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/Gtk/Perspex.Cairo/Media/DrawingContext.cs b/src/Gtk/Perspex.Cairo/Media/DrawingContext.cs index ac7294fd80..39477d2817 100644 --- a/src/Gtk/Perspex.Cairo/Media/DrawingContext.cs +++ b/src/Gtk/Perspex.Cairo/Media/DrawingContext.cs @@ -101,7 +101,9 @@ namespace Perspex.Cairo.Media /// The second point of the line. public void DrawLine(Pen pen, Perspex.Point p1, Perspex.Point p2) { - this.SetBrush(pen.Brush); + var size = new Rect(p1, p2).Size; + + this.SetBrush(pen.Brush, size); this.context.LineWidth = pen.Thickness; this.context.MoveTo(p1.ToCairo()); this.context.LineTo(p2.ToCairo()); @@ -149,7 +151,7 @@ namespace Perspex.Cairo.Media if (brush != null) { - this.SetBrush(brush); + this.SetBrush(brush, geometry.Bounds.Size); if (pen != null) this.context.FillPreserve(); @@ -160,7 +162,7 @@ namespace Perspex.Cairo.Media if (pen != null) { - this.SetPen(pen); + this.SetPen(pen, geometry.Bounds.Size); this.context.Stroke(); } } @@ -173,7 +175,7 @@ namespace Perspex.Cairo.Media /// The rectangle bounds. public void DrawRectange(Pen pen, Rect rect, float cornerRadius) { - this.SetPen(pen); + this.SetPen(pen, rect.Size); this.context.Rectangle(rect.ToCairo()); this.context.Stroke(); } @@ -187,7 +189,7 @@ namespace Perspex.Cairo.Media public void DrawText(Brush foreground, Point origin, FormattedText text) { var layout = ((FormattedTextImpl)text.PlatformImpl).Layout; - this.SetBrush(foreground); + this.SetBrush(foreground, new Size(0, 0)); this.context.MoveTo(origin.X, origin.Y); Pango.CairoHelper.ShowLayout(this.context, layout); @@ -200,7 +202,7 @@ namespace Perspex.Cairo.Media /// The rectangle bounds. public void FillRectange(Perspex.Media.Brush brush, Rect rect, float cornerRadius) { - this.SetBrush(brush); + this.SetBrush(brush, rect.Size); this.context.Rectangle(rect.ToCairo()); this.context.Fill(); } @@ -244,9 +246,10 @@ namespace Perspex.Cairo.Media }); } - private void SetBrush(Brush brush) + private void SetBrush(Brush brush, Size destinationSize) { var solid = brush as SolidColorBrush; + var linearGradientBrush = brush as LinearGradientBrush; if (solid != null) { @@ -256,11 +259,22 @@ namespace Perspex.Cairo.Media solid.Color.B / 255.0, solid.Color.A / 255.0); } + else if (linearGradientBrush != null) + { + Cairo.LinearGradient g = new Cairo.LinearGradient(linearGradientBrush.StartPoint.X * destinationSize.Width, linearGradientBrush.StartPoint.Y * destinationSize.Height, linearGradientBrush.EndPoint.X * destinationSize.Width, linearGradientBrush.EndPoint.Y * destinationSize.Height); + + foreach (var s in linearGradientBrush.GradientStops) + g.AddColorStopRgb(s.Offset, new Cairo.Color(s.Color.R, s.Color.G, s.Color.B, s.Color.A)); + + g.Extend = Cairo.Extend.Pad; + + this.context.SetSource(g); + } } - private void SetPen(Pen pen) + private void SetPen(Pen pen, Size destinationSize) { - this.SetBrush(pen.Brush); + this.SetBrush(pen.Brush, destinationSize); this.context.LineWidth = pen.Thickness; } } From 7bee5a79ecec71571bc7f72b8bb10cba5021c5d5 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 4 Sep 2015 10:53:20 +0200 Subject: [PATCH 2/2] Removed redundant params. And added file headers etc. --- .../Perspex.Direct2D1/Media/BrushImpl.cs | 14 +++++++++----- .../Perspex.Direct2D1/Media/DrawingContext.cs | 4 ++-- .../Media/LinearGradientBrushImpl.cs | 6 ++++-- .../Media/SolidColorBrushImpl.cs | 17 ++++++++--------- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/Windows/Perspex.Direct2D1/Media/BrushImpl.cs b/src/Windows/Perspex.Direct2D1/Media/BrushImpl.cs index e6114b3764..2f90c92e05 100644 --- a/src/Windows/Perspex.Direct2D1/Media/BrushImpl.cs +++ b/src/Windows/Perspex.Direct2D1/Media/BrushImpl.cs @@ -1,19 +1,23 @@ -using System; +// ----------------------------------------------------------------------- +// +// Copyright 2015 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- namespace Perspex.Direct2D1.Media { + using System; + public abstract class BrushImpl : IDisposable { public SharpDX.Direct2D1.Brush PlatformBrush { get; set; } - public BrushImpl(Perspex.Media.Brush brush, SharpDX.Direct2D1.RenderTarget target, Size destinationSize) - { - } - public virtual void Dispose() { if (this.PlatformBrush != null) + { this.PlatformBrush.Dispose(); + } } } } diff --git a/src/Windows/Perspex.Direct2D1/Media/DrawingContext.cs b/src/Windows/Perspex.Direct2D1/Media/DrawingContext.cs index c7ae3950dd..99d9772ae8 100644 --- a/src/Windows/Perspex.Direct2D1/Media/DrawingContext.cs +++ b/src/Windows/Perspex.Direct2D1/Media/DrawingContext.cs @@ -267,7 +267,7 @@ namespace Perspex.Direct2D1.Media if (solidColorBrush != null) { - return new SolidColorBrushImpl(solidColorBrush, this.renderTarget, destinationSize); + return new SolidColorBrushImpl(solidColorBrush, this.renderTarget); } else if (linearGradientBrush != null) { @@ -275,7 +275,7 @@ namespace Perspex.Direct2D1.Media } else { - return new SolidColorBrushImpl(null, this.renderTarget, destinationSize); + return new SolidColorBrushImpl(null, this.renderTarget); } } } diff --git a/src/Windows/Perspex.Direct2D1/Media/LinearGradientBrushImpl.cs b/src/Windows/Perspex.Direct2D1/Media/LinearGradientBrushImpl.cs index 81242c9e0b..de6521446e 100644 --- a/src/Windows/Perspex.Direct2D1/Media/LinearGradientBrushImpl.cs +++ b/src/Windows/Perspex.Direct2D1/Media/LinearGradientBrushImpl.cs @@ -8,8 +8,10 @@ public class LinearGradientBrushImpl : BrushImpl { - public LinearGradientBrushImpl(Perspex.Media.LinearGradientBrush brush, SharpDX.Direct2D1.RenderTarget target, Size destinationSize) - : base(brush, target, destinationSize) + public LinearGradientBrushImpl( + Perspex.Media.LinearGradientBrush brush, + SharpDX.Direct2D1.RenderTarget target, + Size destinationSize) { if (brush != null) { diff --git a/src/Windows/Perspex.Direct2D1/Media/SolidColorBrushImpl.cs b/src/Windows/Perspex.Direct2D1/Media/SolidColorBrushImpl.cs index 949b8e72be..73093b2e0a 100644 --- a/src/Windows/Perspex.Direct2D1/Media/SolidColorBrushImpl.cs +++ b/src/Windows/Perspex.Direct2D1/Media/SolidColorBrushImpl.cs @@ -1,15 +1,14 @@ -namespace Perspex.Direct2D1.Media -{ - using System; - using System.Collections.Generic; - using System.Linq; - using System.Text; - using System.Threading.Tasks; +// ----------------------------------------------------------------------- +// +// Copyright 2015 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- +namespace Perspex.Direct2D1.Media +{ public class SolidColorBrushImpl : BrushImpl { - public SolidColorBrushImpl(Perspex.Media.SolidColorBrush brush, SharpDX.Direct2D1.RenderTarget target, Size destinationSize) - : base(brush, target, destinationSize) + public SolidColorBrushImpl(Perspex.Media.SolidColorBrush brush, SharpDX.Direct2D1.RenderTarget target) { this.PlatformBrush = new SharpDX.Direct2D1.SolidColorBrush(target, brush?.Color.ToDirect2D() ?? new SharpDX.Color4()); }