diff --git a/Perspex.Controls/Primitives/TemplatedControl.cs b/Perspex.Controls/Primitives/TemplatedControl.cs index a6bf3bcc4e..bce7b5e4ab 100644 --- a/Perspex.Controls/Primitives/TemplatedControl.cs +++ b/Perspex.Controls/Primitives/TemplatedControl.cs @@ -7,7 +7,6 @@ namespace Perspex.Controls.Primitives { using System; - using System.Collections.Generic; using System.Linq; using Perspex.Media; using Perspex.Styling; @@ -65,7 +64,6 @@ namespace Perspex.Controls.Primitives i.ApplyTemplate(); } - this.OnTemplateApplied(); } diff --git a/Windows/Perspex.Direct2D1/Media/DrawingContext.cs b/Windows/Perspex.Direct2D1/Media/DrawingContext.cs index e9c88f1b6d..9d2db473dc 100644 --- a/Windows/Perspex.Direct2D1/Media/DrawingContext.cs +++ b/Windows/Perspex.Direct2D1/Media/DrawingContext.cs @@ -46,8 +46,8 @@ namespace Perspex.Direct2D1.Media public Matrix CurrentTransform { - get { return this.Convert(this.renderTarget.Transform); } - set { this.renderTarget.Transform = this.Convert(value); } + get { return this.renderTarget.Transform.ToPerspex(); } + set { this.renderTarget.Transform = value.ToDirect2D(); } } /// @@ -80,7 +80,7 @@ namespace Perspex.Direct2D1.Media { if (pen != null) { - using (SharpDX.Direct2D1.SolidColorBrush d2dBrush = this.Convert(pen.Brush)) + using (var d2dBrush = pen.Brush.ToDirect2D(this.renderTarget)) { this.renderTarget.DrawLine(p1.ToSharpDX(), p2.ToSharpDX(), d2dBrush); } @@ -97,7 +97,7 @@ namespace Perspex.Direct2D1.Media { if (brush != null) { - using (SharpDX.Direct2D1.SolidColorBrush d2dBrush = this.Convert(brush)) + using (var d2dBrush = brush.ToDirect2D(this.renderTarget)) { GeometryImpl impl = (GeometryImpl)geometry.PlatformImpl; this.renderTarget.FillGeometry(impl.Geometry, d2dBrush); @@ -106,7 +106,7 @@ namespace Perspex.Direct2D1.Media if (pen != null) { - using (SharpDX.Direct2D1.SolidColorBrush d2dBrush = this.Convert(pen.Brush)) + using (var d2dBrush = pen.Brush.ToDirect2D(this.renderTarget)) { GeometryImpl impl = (GeometryImpl)geometry.PlatformImpl; this.renderTarget.DrawGeometry(impl.Geometry, d2dBrush, (float)pen.Thickness); @@ -121,10 +121,10 @@ namespace Perspex.Direct2D1.Media /// The rectangle bounds. public void DrawRectange(Pen pen, Rect rect) { - using (SharpDX.Direct2D1.SolidColorBrush brush = this.Convert(pen.Brush)) + using (var brush = pen.Brush.ToDirect2D(this.renderTarget)) { this.renderTarget.DrawRectangle( - this.Convert(rect), + rect.ToDirect2D(), brush, (float)pen.Thickness); } @@ -142,7 +142,7 @@ namespace Perspex.Direct2D1.Media { var impl = (FormattedTextImpl)text.PlatformImpl; - using (SharpDX.Direct2D1.SolidColorBrush brush = this.Convert(foreground)) + using (var brush = foreground.ToDirect2D(this.renderTarget)) { this.renderTarget.DrawTextLayout( origin.ToSharpDX(), @@ -159,7 +159,7 @@ namespace Perspex.Direct2D1.Media /// The rectangle bounds. public void FillRectange(Perspex.Media.Brush brush, Rect rect) { - using (SharpDX.Direct2D1.SolidColorBrush b = this.Convert(brush)) + using (var b = brush.ToDirect2D(this.renderTarget)) { this.renderTarget.FillRectangle( new RectangleF( @@ -193,7 +193,7 @@ namespace Perspex.Direct2D1.Media /// A disposable used to undo the transformation. public IDisposable PushTransform(Matrix matrix) { - Matrix3x2 m3x2 = this.Convert(matrix); + Matrix3x2 m3x2 = matrix.ToDirect2D(); Matrix3x2 transform = this.renderTarget.Transform * m3x2; this.renderTarget.Transform = transform; @@ -203,83 +203,5 @@ namespace Perspex.Direct2D1.Media this.renderTarget.Transform = transform * m3x2; }); } - - /// - /// Converts a brush to Direct2D. - /// - /// The brush to convert. - /// The Direct2D brush. - private SharpDX.Direct2D1.SolidColorBrush Convert(Perspex.Media.Brush brush) - { - Perspex.Media.SolidColorBrush solidColorBrush = brush as Perspex.Media.SolidColorBrush; - - if (solidColorBrush != null) - { - return new SharpDX.Direct2D1.SolidColorBrush( - this.renderTarget, - this.Convert(solidColorBrush.Color)); - } - else - { - return new SharpDX.Direct2D1.SolidColorBrush( - this.renderTarget, - new Color4()); - } - } - - /// - /// Converts a color to Direct2D. - /// - /// The color to convert. - /// The Direct2D color. - private Color4 Convert(Perspex.Media.Color color) - { - return new Color4( - (float)(color.R / 255.0), - (float)(color.G / 255.0), - (float)(color.B / 255.0), - (float)(color.A / 255.0)); - } - - /// - /// Converts a to a Direct2D - /// - /// The . - /// The . - private Matrix3x2 Convert(Matrix matrix) - { - return new Matrix3x2( - (float)matrix.M11, - (float)matrix.M12, - (float)matrix.M21, - (float)matrix.M22, - (float)matrix.OffsetX, - (float)matrix.OffsetY); - } - - private Matrix Convert(Matrix3x2 matrix) - { - return new Matrix( - matrix.M11, - matrix.M12, - matrix.M21, - matrix.M22, - matrix.M31, - matrix.M32); - } - - /// - /// Converts a to a - /// - /// The . - /// The . - private RectangleF Convert(Rect rect) - { - return new RectangleF( - (float)rect.X, - (float)rect.Y, - (float)rect.Width, - (float)rect.Height); - } } } diff --git a/Windows/Perspex.Direct2D1/PrimitiveExtensions.cs b/Windows/Perspex.Direct2D1/PrimitiveExtensions.cs index 48704bd6c0..d18f8cb1e7 100644 --- a/Windows/Perspex.Direct2D1/PrimitiveExtensions.cs +++ b/Windows/Perspex.Direct2D1/PrimitiveExtensions.cs @@ -7,6 +7,7 @@ namespace Perspex.Direct2D1 { using SharpDX; + using SharpDX.Direct2D1; public static class PrimitiveExtensions { @@ -24,5 +25,86 @@ namespace Perspex.Direct2D1 { return new Vector2((float)p.X, (float)p.Y); } + + /// + /// Converts a brush to Direct2D. + /// + /// The brush to convert. + /// The Direct2D brush. + public static SharpDX.Direct2D1.Brush ToDirect2D(this Perspex.Media.Brush brush, RenderTarget target) + { + Perspex.Media.SolidColorBrush solidColorBrush = brush as Perspex.Media.SolidColorBrush; + + if (solidColorBrush != null) + { + return new SharpDX.Direct2D1.SolidColorBrush(target, solidColorBrush.Color.ToDirect2D()); + } + else + { + // TODO: Implement other brushes. + return new SharpDX.Direct2D1.SolidColorBrush(target, new Color4()); + } + } + + /// + /// Converts a Perspex to Direct2D. + /// + /// The color to convert. + /// The Direct2D color. + public static Color4 ToDirect2D(this Perspex.Media.Color color) + { + return new Color4( + (float)(color.R / 255.0), + (float)(color.G / 255.0), + (float)(color.B / 255.0), + (float)(color.A / 255.0)); + } + + /// + /// Converts a Perspex to a Direct2D + /// + /// The . + /// The . + public static Matrix3x2 ToDirect2D(this Perspex.Matrix matrix) + { + return new Matrix3x2( + (float)matrix.M11, + (float)matrix.M12, + (float)matrix.M21, + (float)matrix.M22, + (float)matrix.OffsetX, + (float)matrix.OffsetY); + } + + /// + /// Converts a Direct2D to a Perspex . + /// + /// + /// a . + public static Perspex.Matrix ToPerspex(this Matrix3x2 matrix) + { + return new Perspex.Matrix( + matrix.M11, + matrix.M12, + matrix.M21, + matrix.M22, + matrix.M31, + matrix.M32); + } + + /// + /// Converts a Perspex to a Direct2D + /// + /// The . + /// The . + public static RectangleF ToDirect2D(this Rect rect) + { + return new RectangleF( + (float)rect.X, + (float)rect.Y, + (float)rect.Width, + (float)rect.Height); + } + } }