From 8b2b0273b537e9ec966f7a5407c9d7afd85ac70f Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 4 Sep 2015 11:13:37 +0200 Subject: [PATCH] Implemented VisualBrush.DestinationRect. --- .../Perspex.Direct2D1/Media/DrawingContext.cs | 1 - .../Media/VisualBrushImpl.cs | 31 ++++++++++----- .../Media/VisualBrushTests.cs | 37 ++++++++++++++++++ ...rush_DestinationRect_Absolute.expected.png | Bin 0 -> 1426 bytes 4 files changed, 58 insertions(+), 11 deletions(-) create mode 100644 tests/TestFiles/Direct2D1/Media/VisualBrush/VisualBrush_DestinationRect_Absolute.expected.png diff --git a/src/Windows/Perspex.Direct2D1/Media/DrawingContext.cs b/src/Windows/Perspex.Direct2D1/Media/DrawingContext.cs index 1720591c57..22c79d0f72 100644 --- a/src/Windows/Perspex.Direct2D1/Media/DrawingContext.cs +++ b/src/Windows/Perspex.Direct2D1/Media/DrawingContext.cs @@ -8,7 +8,6 @@ namespace Perspex.Direct2D1.Media { using System; using System.Reactive.Disposables; - using Layout; using Perspex.Media; using SharpDX; using SharpDX.Direct2D1; diff --git a/src/Windows/Perspex.Direct2D1/Media/VisualBrushImpl.cs b/src/Windows/Perspex.Direct2D1/Media/VisualBrushImpl.cs index a17106b6b5..a1a7520fb2 100644 --- a/src/Windows/Perspex.Direct2D1/Media/VisualBrushImpl.cs +++ b/src/Windows/Perspex.Direct2D1/Media/VisualBrushImpl.cs @@ -16,8 +16,7 @@ namespace Perspex.Direct2D1.Media public VisualBrushImpl( Perspex.Media.VisualBrush brush, SharpDX.Direct2D1.RenderTarget target, - Size destinationSize) - : base(brush, target, destinationSize) + Size targetSize) { var visual = brush.Visual; var layoutable = visual as ILayoutable; @@ -29,21 +28,33 @@ namespace Perspex.Direct2D1.Media } var sourceRect = brush.SourceRect.ToPixels(layoutable.Bounds.Size); - var destinationRect = brush.DestinationRect.ToPixels(destinationSize); + var destinationRect = brush.DestinationRect.ToPixels(targetSize); + var bitmapSize = CalculateBitmapSize(brush.TileMode, destinationRect.Size, targetSize); var scale = brush.Stretch.CalculateScaling(destinationRect.Size, sourceRect.Size); var translate = CalculateTranslate(brush, sourceRect, destinationRect, scale); + var options = CompatibleRenderTargetOptions.None; - using (var brt = new BitmapRenderTarget( - target, - CompatibleRenderTargetOptions.None, - destinationRect.Size.ToSharpDX())) + using (var brt = new BitmapRenderTarget(target, options, bitmapSize.ToSharpDX())) { var renderer = new Renderer(brt); var transform = Matrix.CreateTranslation(-sourceRect.Position) * Matrix.CreateScale(scale) * Matrix.CreateTranslation(translate); renderer.Render(visual, null, transform); - this.PlatformBrush = new BitmapBrush(brt, brt.Bitmap); + + var result = new BitmapBrush(brt, brt.Bitmap); + this.PlatformBrush = result; + } + } + + private static Size CalculateBitmapSize(TileMode tileMode, Size size, Size targetSize) + { + switch (tileMode) + { + case TileMode.None: + return targetSize; + default: + throw new NotImplementedException(); } } @@ -53,8 +64,8 @@ namespace Perspex.Direct2D1.Media Rect destinationRect, Vector scale) { - var x = 0.0; - var y = 0.0; + var x = destinationRect.X; + var y = destinationRect.Y; var size = sourceRect.Size * scale; switch (brush.AlignmentX) diff --git a/tests/Perspex.RenderTests/Media/VisualBrushTests.cs b/tests/Perspex.RenderTests/Media/VisualBrushTests.cs index fc74160e74..962ecf01a6 100644 --- a/tests/Perspex.RenderTests/Media/VisualBrushTests.cs +++ b/tests/Perspex.RenderTests/Media/VisualBrushTests.cs @@ -283,5 +283,42 @@ namespace Perspex.Direct2D1.RenderTests.Media this.RenderToFile(target); this.CompareImages(); } + + [Fact] + public void VisualBrush_DestinationRect_Absolute() + { + Decorator target = new Decorator + { + Padding = new Thickness(8), + Width = 200, + Height = 200, + Child = new Rectangle + { + Fill = new VisualBrush + { + DestinationRect = new RelativeRect(92, 92, 92, 92, OriginUnit.Pixels), + Visual = new Border + { + Width = 180, + Height = 180, + Background = Brushes.Red, + BorderBrush = Brushes.Black, + BorderThickness = 2, + Child = new Ellipse + { + Width = 100, + Height = 100, + Fill = Brushes.Yellow, + VerticalAlignment = VerticalAlignment.Center, + HorizontalAlignment = HorizontalAlignment.Center, + } + } + } + } + }; + + this.RenderToFile(target); + this.CompareImages(); + } } } diff --git a/tests/TestFiles/Direct2D1/Media/VisualBrush/VisualBrush_DestinationRect_Absolute.expected.png b/tests/TestFiles/Direct2D1/Media/VisualBrush/VisualBrush_DestinationRect_Absolute.expected.png new file mode 100644 index 0000000000000000000000000000000000000000..9253eee16411dd2a474037f7f1b31ad03c64da65 GIT binary patch literal 1426 zcmb_cdr%Sv7^i|Hj3y^)nkgw!Qj^fqSfF7%H1kou$c(H|>uk0`={TGk&Mv}Cbc&vY z`O4cN?92y3rmb5Il%eUCDL6I3AcOhmO$jlRe4`b`j#QF)iNWj zNx(4;*plb??W>I(Tr-yC1iCn21*sL3tmP4X3wqVp63&>J-U+!Mz37UJJf0brx5u*E zdPIPKbg&!?!bVI4W5~%EYTxil{~Sza0JlA(2Zqe~F3(M|_WJaiMK5*mo$&6d5y8Hh zrt@2xJpFPI-J=g%o`8qMY)UEq&)0Ki;~#hrb=$RF>A_l_&!$HiRefB|bVaVWw zJf7a{H`ifxR|J4+|r#TgyGBQY;bo@ z^(Bd=V9F-ggwb{I$T^1Gd1JVON1IxXEtMWpPODsZgUfX%h-lLVJD__!^Q!=omSoR{ z!1ahhSJ0@k8}uqMe&sZrF=u()AdPiG$(vw|rxM4Qdc-Mt6E}V<)WzO{0Z+Zq^gTO5 ztRjbSQ|B$(*DhYQcM)rd+JV+=@8$j?T#cOIU-dTB?HE>~->;7Pwxh51>zB;pCd8Ug zt*Je6;jexOoN>nRIX=`xmmMwf3e-Pi>}8cMX>z8!+63KSrtpJXyvk~>e?m=HQoqN6 zf3i8ToW~+ z>@++W_b&}mv-4`q<&!aF8Od_7?SMh#8!U14!aY4U%Zx1Rs5@s0v ztQLC@F@w0md?_9P=3?&uc+t4(?oDDQW%)WYpHIEWW>$#!Dg4bpJKs~iQLU;5GcZ*t zBjf0dsExfEw8BYUufMU`RyT3f^1jPM{hsxz4gFuzj+K=!=Y~*2xRd7?rmH*Iu>mnb z8vdP-+xxS>U}`)`#Ia#I@!g)7D;i!dYT-&%8csQVHo7a2I&wP$oq{@4$!&?#fksQQ wpjJ0W2pss)n!?n1CF%Vm9sc=a1%!5;Z9_#@erLmzO)DRu2y!$@7M9BW8^BFwzyJUM literal 0 HcmV?d00001