Browse Source

Add RadialGradientBrush support for Direct2D and Cairo backend.

pull/162/head
Amer Koleci 11 years ago
parent
commit
3e2efc7af9
  1. 9
      src/Gtk/Perspex.Cairo/Media/DrawingContext.cs
  2. 25
      src/Gtk/Perspex.Cairo/Media/RadialGradientBrushImpl.cs
  3. 3
      src/Gtk/Perspex.Cairo/Perspex.Cairo.csproj
  4. 2
      src/Perspex.SceneGraph/Media/LinearGradientBrush.cs
  5. 63
      src/Perspex.SceneGraph/Media/RadialGradientBrush.cs
  6. 1
      src/Perspex.SceneGraph/Perspex.SceneGraph.csproj
  7. 5
      src/Windows/Perspex.Direct2D1/Media/DrawingContext.cs
  8. 38
      src/Windows/Perspex.Direct2D1/Media/RadialGradientBrushImpl.cs
  9. 1
      src/Windows/Perspex.Direct2D1/Perspex.Direct2D1.csproj

9
src/Gtk/Perspex.Cairo/Media/DrawingContext.cs

@ -254,6 +254,7 @@ namespace Perspex.Cairo.Media
var solid = brush as SolidColorBrush;
var linearGradientBrush = brush as LinearGradientBrush;
var radialGradientBrush = brush as RadialGradientBrush;
var imageBrush = brush as ImageBrush;
var visualBrush = brush as VisualBrush;
BrushImpl impl = null;
@ -265,8 +266,12 @@ namespace Perspex.Cairo.Media
else if (linearGradientBrush != null)
{
impl = new LinearGradientBrushImpl(linearGradientBrush, destinationSize);
}
else if (imageBrush != null)
}
else if (radialGradientBrush != null)
{
impl = new RadialGradientBrushImpl(radialGradientBrush, destinationSize);
}
else if (imageBrush != null)
{
impl = new ImageBrushImpl(imageBrush, destinationSize);
}

25
src/Gtk/Perspex.Cairo/Media/RadialGradientBrushImpl.cs

@ -0,0 +1,25 @@
using System;
using global::Cairo;
namespace Perspex.Cairo
{
public class RadialGradientBrushImpl : BrushImpl
{
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;
}
}
}

3
src/Gtk/Perspex.Cairo/Perspex.Cairo.csproj

@ -61,6 +61,7 @@
<Compile Include="Media\FormattedTextImpl.cs" />
<Compile Include="Media\Imaging\BitmapImpl.cs" />
<Compile Include="Media\Imaging\RenderTargetBitmapImpl.cs" />
<Compile Include="Media\RadialGradientBrushImpl.cs" />
<Compile Include="Media\TileBrushes.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Renderer.cs" />
@ -103,4 +104,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>

2
src/Perspex.SceneGraph/Media/LinearGradientBrush.cs

@ -6,7 +6,7 @@ namespace Perspex.Media
/// <summary>
/// A brush that draws with a linear gradient.
/// </summary>
public class LinearGradientBrush : GradientBrush
public sealed class LinearGradientBrush : GradientBrush
{
/// <summary>
/// Defines the <see cref="StartPoint"/> property.

63
src/Perspex.SceneGraph/Media/RadialGradientBrush.cs

@ -0,0 +1,63 @@
// Copyright (c) The Perspex Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
namespace Perspex.Media
{
/// <summary>
/// Paints an area with a radial gradient. A focal point defines the beginning of the gradient,
/// and a circle defines the end point of the gradient.
/// </summary>
public sealed class RadialGradientBrush : GradientBrush
{
/// <summary>
/// Defines the <see cref="Center"/> property.
/// </summary>
public static readonly PerspexProperty<RelativePoint> CenterProperty =
PerspexProperty.Register<RadialGradientBrush, RelativePoint>(
nameof(Center),
RelativePoint.Center);
/// <summary>
/// Defines the <see cref="GradientOrigin"/> property.
/// </summary>
public static readonly PerspexProperty<RelativePoint> GradientOriginProperty =
PerspexProperty.Register<RadialGradientBrush, RelativePoint>(
nameof(GradientOrigin),
RelativePoint.Center);
/// <summary>
/// Defines the <see cref="Radius"/> property.
/// </summary>
public static readonly PerspexProperty<double> RadiusProperty =
PerspexProperty.Register<RadialGradientBrush, double>(
nameof(Radius),
0.5);
/// <summary>
/// Gets or sets the start point for the gradient.
/// </summary>
public RelativePoint Center
{
get { return GetValue(CenterProperty); }
set { SetValue(CenterProperty, value); }
}
/// <summary>
/// Gets or sets the location of the two-dimensional focal point that defines the beginning of the gradient.
/// </summary>
public RelativePoint GradientOrigin
{
get { return GetValue(GradientOriginProperty); }
set { SetValue(GradientOriginProperty, value); }
}
/// <summary>
/// ets or sets the horizontal and vertical radius of the outermost circle of the radial gradient.
/// </summary>
public double Radius
{
get { return GetValue(RadiusProperty); }
set { SetValue(RadiusProperty, value); }
}
}
}

1
src/Perspex.SceneGraph/Perspex.SceneGraph.csproj

@ -67,6 +67,7 @@
<Compile Include="Media\GradientBrush.cs" />
<Compile Include="Media\GradientSpreadMethod.cs" />
<Compile Include="Media\GradientStop.cs" />
<Compile Include="Media\RadialGradientBrush.cs" />
<Compile Include="Media\LinearGradientBrush.cs" />
<Compile Include="Media\MediaExtensions.cs" />
<Compile Include="Media\PenLineCap.cs" />

5
src/Windows/Perspex.Direct2D1/Media/DrawingContext.cs

@ -303,6 +303,7 @@ namespace Perspex.Direct2D1.Media
{
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;
@ -314,6 +315,10 @@ namespace Perspex.Direct2D1.Media
{
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);

38
src/Windows/Perspex.Direct2D1/Media/RadialGradientBrushImpl.cs

@ -0,0 +1,38 @@
// Copyright (c) The Perspex Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Perspex.Direct2D1.Media
{
public class RadialGradientBrushImpl : BrushImpl
{
public RadialGradientBrushImpl(
Perspex.Media.RadialGradientBrush 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 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())
);
}
}
}
}

1
src/Windows/Perspex.Direct2D1/Perspex.Direct2D1.csproj

@ -79,6 +79,7 @@
<Compile Include="Media\DrawingContext.cs" />
<Compile Include="Media\Imaging\RenderTargetBitmapImpl.cs" />
<Compile Include="Media\Imaging\BitmapImpl.cs" />
<Compile Include="Media\RadialGradientBrushImpl.cs" />
<Compile Include="Media\LinearGradientBrushImpl.cs" />
<Compile Include="Media\PerspexTextRenderer.cs" />
<Compile Include="Media\ImageBrushImpl.cs" />

Loading…
Cancel
Save