Преглед изворни кода

First two VisualBrush tests passing.

Steven Kirk пре 10 година
родитељ
комит
0d938acbed

+ 13 - 2
src/Windows/Perspex.Direct2D1/Media/DrawingContext.cs

@@ -279,10 +279,17 @@ namespace Perspex.Direct2D1.Media
             });
         }
 
+        /// <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)
         {
-            Perspex.Media.SolidColorBrush solidColorBrush = brush as Perspex.Media.SolidColorBrush;
-            Perspex.Media.LinearGradientBrush linearGradientBrush = brush as Perspex.Media.LinearGradientBrush;
+            var solidColorBrush = brush as Perspex.Media.SolidColorBrush;
+            var linearGradientBrush = brush as Perspex.Media.LinearGradientBrush;
+            var visualBrush = brush as Perspex.Media.VisualBrush;
 
             if (solidColorBrush != null)
             {
@@ -292,6 +299,10 @@ namespace Perspex.Direct2D1.Media
             {
                 return new LinearGradientBrushImpl(linearGradientBrush, this.renderTarget, destinationSize);
             }
+            else if (visualBrush != null)
+            {
+                return new VisualBrushImpl(visualBrush, this.renderTarget, destinationSize);
+            }
             else
             {
                 return new SolidColorBrushImpl(null, this.renderTarget, destinationSize);

+ 51 - 0
src/Windows/Perspex.Direct2D1/Media/VisualBrushImpl.cs

@@ -0,0 +1,51 @@
+// -----------------------------------------------------------------------
+// <copyright file="VisualBrushImpl.cs" company="Steven Kirk">
+// Copyright 2015 MIT Licence. See licence.md for more information.
+// </copyright>
+// -----------------------------------------------------------------------
+
+namespace Perspex.Direct2D1.Media
+{
+    using Perspex.Layout;
+    using Perspex.Media;
+    using SharpDX.Direct2D1;
+
+    public class VisualBrushImpl : BrushImpl
+    {
+        public VisualBrushImpl(
+            Perspex.Media.VisualBrush brush,
+            SharpDX.Direct2D1.RenderTarget target,
+            Size destinationSize)
+            : base(brush, target, destinationSize)
+        {
+            var visual = brush.Visual;
+            var layoutable = visual as ILayoutable;
+
+            if (layoutable?.IsArrangeValid == false)
+            {
+                layoutable.Measure(Size.Infinity);
+                layoutable.Arrange(new Rect(layoutable.DesiredSize));
+            }
+
+            var sourceSize = layoutable.Bounds.Size;
+            var destinationRect = brush.DestinationRect.ToPixels(destinationSize);
+            var scale = brush.Stretch.CalculateScaling(destinationRect.Size, sourceSize);
+
+            using (var brt = new BitmapRenderTarget(
+                target,
+                CompatibleRenderTargetOptions.None,
+                destinationRect.Size.ToSharpDX()))
+            {
+                var renderer = new Renderer(brt);
+                renderer.Render(visual, null, Matrix.Identity, Matrix.CreateScale(scale));
+                this.PlatformBrush = new BitmapBrush(brt, brt.Bitmap);
+            }
+        }
+
+        public override void Dispose()
+        {
+            ((BitmapBrush)this.PlatformBrush).Bitmap.Dispose();
+            base.Dispose();
+        }
+    }
+}

+ 1 - 0
src/Windows/Perspex.Direct2D1/Perspex.Direct2D1.csproj

@@ -83,6 +83,7 @@
     <Compile Include="Media\Imaging\BitmapImpl.cs" />
     <Compile Include="Media\LinearGradientBrushImpl.cs" />
     <Compile Include="Media\PerspexTextRenderer.cs" />
+    <Compile Include="Media\VisualBrushImpl.cs" />
     <Compile Include="Media\SolidColorBrushImpl.cs" />
     <Compile Include="Media\StreamGeometryContextImpl.cs" />
     <Compile Include="Media\GeometryImpl.cs" />

+ 3 - 2
tests/Perspex.RenderTests/Brushes/VisualBrushTests.cs → tests/Perspex.RenderTests/Media/VisualBrushTests.cs

@@ -4,7 +4,7 @@
 // </copyright>
 // -----------------------------------------------------------------------
 
-namespace Perspex.Direct2D1.RenderTests.Controls
+namespace Perspex.Direct2D1.RenderTests.Media
 {
     using Perspex.Controls;
     using Perspex.Controls.Shapes;
@@ -15,7 +15,7 @@ namespace Perspex.Direct2D1.RenderTests.Controls
     public class VisualBrushTests : TestBase
     {
         public VisualBrushTests()
-            : base(@"Brushes\VisualBrush")
+            : base(@"Media\VisualBrush")
         {
         }
 
@@ -31,6 +31,7 @@ namespace Perspex.Direct2D1.RenderTests.Controls
                 {
                     Fill = new VisualBrush
                     {
+                        Stretch = Stretch.None,
                         Visual = new Border
                         {
                             Width = 92,

+ 1 - 1
tests/Perspex.RenderTests/Perspex.Direct2D1.RenderTests.csproj

@@ -77,7 +77,7 @@
     <Otherwise />
   </Choose>
   <ItemGroup>
-    <Compile Include="Brushes\VisualBrushTests.cs" />
+    <Compile Include="Media\VisualBrushTests.cs" />
     <Compile Include="Controls\ImageTests.cs" />
     <Compile Include="Controls\BorderTests.cs" />
     <Compile Include="GlobalSuppressions.cs" />

BIN
tests/TestFiles/Direct2D1/Media/VisualBrush/VisualBrush_Stretch_Fill_Large.expected.png


BIN
tests/TestFiles/Direct2D1/Media/VisualBrush/VisualBrush_Stretch_None.expected.png