Browse Source

Added support for dashed pens.

pull/39/head
Steven Kirk 11 years ago
parent
commit
e0cbfc3a2d
  1. 22
      Perspex.Controls/Shapes/Shape.cs
  2. 19
      Perspex.SceneGraph/Media/Pen.cs
  3. 15
      Windows/Perspex.Direct2D1/Media/DrawingContext.cs
  4. 18
      Windows/Perspex.Direct2D1/PrimitiveExtensions.cs

22
Perspex.Controls/Shapes/Shape.cs

@ -7,6 +7,7 @@
namespace Perspex.Controls.Shapes
{
using System;
using Perspex.Collections;
using Perspex.Controls;
using Perspex.Media;
@ -21,6 +22,9 @@ namespace Perspex.Controls.Shapes
public static readonly PerspexProperty<Brush> StrokeProperty =
PerspexProperty.Register<Shape, Brush>("Stroke");
public static readonly PerspexProperty<PerspexList<double>> StrokeDashArrayProperty =
PerspexProperty.Register<Shape, PerspexList<double>>("StrokeDashArray");
public static readonly PerspexProperty<double> StrokeThicknessProperty =
PerspexProperty.Register<Shape, double>("StrokeThickness");
@ -28,6 +32,15 @@ namespace Perspex.Controls.Shapes
private Geometry renderedGeometry;
static Shape()
{
Control.AffectsRender(FillProperty);
Control.AffectsMeasure(StretchProperty);
Control.AffectsRender(StrokeProperty);
Control.AffectsRender(StrokeDashArrayProperty);
Control.AffectsMeasure(StrokeThicknessProperty);
}
public abstract Geometry DefiningGeometry
{
get;
@ -68,6 +81,12 @@ namespace Perspex.Controls.Shapes
set { this.SetValue(StrokeProperty, value); }
}
public PerspexList<double> StrokeDashArray
{
get { return this.GetValue(StrokeDashArrayProperty); }
set { this.SetValue(StrokeDashArrayProperty, value); }
}
public double StrokeThickness
{
get { return this.GetValue(StrokeThicknessProperty); }
@ -80,7 +99,8 @@ namespace Perspex.Controls.Shapes
if (geometry != null)
{
context.DrawGeometry(this.Fill, new Pen(this.Stroke, this.StrokeThickness), geometry);
var pen = new Pen(this.Stroke, this.StrokeThickness, this.StrokeDashArray);
context.DrawGeometry(this.Fill, pen, geometry);
}
}

19
Perspex.SceneGraph/Media/Pen.cs

@ -6,6 +6,8 @@
namespace Perspex.Media
{
using System.Collections.Generic;
/// <summary>
/// Describes how a stroke is drawn.
/// </summary>
@ -16,10 +18,12 @@ namespace Perspex.Media
/// </summary>
/// <param name="brush">The brush used to draw.</param>
/// <param name="thickness">The stroke thickness.</param>
public Pen(Brush brush, double thickness)
/// <param name="dashArray">The length of alternating dashes and gaps.</param>
public Pen(Brush brush, double thickness, IReadOnlyList<double> dashArray = null)
{
this.Brush = brush;
this.Thickness = thickness;
this.DashArray = dashArray;
}
/// <summary>
@ -27,20 +31,27 @@ namespace Perspex.Media
/// </summary>
/// <param name="color">The stroke color.</param>
/// <param name="thickness">The stroke thickness.</param>
public Pen(uint color, double thickness)
/// <param name="dashArray">The length of alternating dashes and gaps.</param>
public Pen(uint color, double thickness, IReadOnlyList<double> dashArray = null)
{
this.Brush = new SolidColorBrush(color);
this.Thickness = thickness;
this.DashArray = dashArray;
}
/// <summary>
/// Gets the brush used to draw the stroke.
/// </summary>
public Brush Brush { get; private set; }
public Brush Brush { get; }
/// <summary>
/// Gets the length of alternating dashes and gaps.
/// </summary>
public IReadOnlyList<double> DashArray { get; }
/// <summary>
/// Gets the stroke thickness.
/// </summary>
public double Thickness { get; private set; }
public double Thickness { get; }
}
}

15
Windows/Perspex.Direct2D1/Media/DrawingContext.cs

@ -80,8 +80,14 @@ namespace Perspex.Direct2D1.Media
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);
this.renderTarget.DrawLine(
p1.ToSharpDX(),
p2.ToSharpDX(),
d2dBrush,
(float)pen.Thickness,
d2dStroke);
}
}
}
@ -106,9 +112,10 @@ namespace Perspex.Direct2D1.Media
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);
this.renderTarget.DrawGeometry(impl.Geometry, d2dBrush, (float)pen.Thickness, d2dStroke);
}
}
}
@ -121,11 +128,13 @@ namespace Perspex.Direct2D1.Media
public void DrawRectange(Pen pen, Rect rect)
{
using (var brush = pen.Brush.ToDirect2D(this.renderTarget))
using (var d2dStroke = pen.ToDirect2DStrokeStyle(this.renderTarget))
{
this.renderTarget.DrawRectangle(
rect.ToDirect2D(),
brush,
(float)pen.Thickness);
(float)pen.Thickness,
d2dStroke);
}
}

18
Windows/Perspex.Direct2D1/PrimitiveExtensions.cs

@ -6,6 +6,7 @@
namespace Perspex.Direct2D1
{
using System.Linq;
using SharpDX;
using SharpDX.Direct2D1;
@ -51,6 +52,23 @@ namespace Perspex.Direct2D1
}
}
public static StrokeStyle ToDirect2DStrokeStyle(this Perspex.Media.Pen pen, RenderTarget target)
{
if (pen.DashArray != null && pen.DashArray.Count > 0)
{
var properties = new StrokeStyleProperties
{
DashStyle = DashStyle.Custom,
};
return new StrokeStyle(target.Factory, properties, pen.DashArray.Select(x => (float)x).ToArray());
}
else
{
return null;
}
}
/// <summary>
/// Converts a Perspex <see cref="Perspex.Media.Color"/> to Direct2D.
/// </summary>

Loading…
Cancel
Save