Browse Source

Fix/test VisualBrush alignment.

Steven Kirk 10 years ago
parent
commit
5dca21e6b9

+ 29 - 1
src/Windows/Perspex.Direct2D1/Media/VisualBrushImpl.cs

@@ -31,7 +31,7 @@ namespace Perspex.Direct2D1.Media
             var sourceSize = layoutable.Bounds.Size;
             var destinationRect = brush.DestinationRect.ToPixels(destinationSize);
             var scale = brush.Stretch.CalculateScaling(destinationRect.Size, sourceSize);
-            var translate = new Rect(destinationSize).CenterIn(new Rect(sourceSize * scale)).Position;
+            var translate = CalculateTranslate(brush, destinationRect.Size, sourceSize * scale);
 
             using (var brt = new BitmapRenderTarget(
                 target,
@@ -44,6 +44,34 @@ namespace Perspex.Direct2D1.Media
             }
         }
 
+        private static Vector CalculateTranslate(VisualBrush brush, Size destinationSize, Size sourceSize)
+        {
+            double x = 0;
+            double y = 0;
+
+            switch (brush.AlignmentX)
+            {
+                case AlignmentX.Center:
+                    x = (destinationSize.Width - sourceSize.Width) / 2;
+                    break;
+                case AlignmentX.Right:
+                    x = destinationSize.Width - sourceSize.Width;
+                    break;
+            }
+
+            switch (brush.AlignmentY)
+            {
+                case AlignmentY.Center:
+                    y = (destinationSize.Height - sourceSize.Height) / 2;
+                    break;
+                case AlignmentY.Bottom:
+                    y = destinationSize.Height - sourceSize.Height;
+                    break;
+            }
+
+            return new Vector(x, y);
+        }
+
         public override void Dispose()
         {
             ((BitmapBrush)this.PlatformBrush).Bitmap.Dispose();

+ 42 - 1
tests/Perspex.RenderTests/Media/VisualBrushTests.cs

@@ -20,7 +20,7 @@ namespace Perspex.Direct2D1.RenderTests.Media
         }
 
         [Fact]
-        public void VisualBrush_Stretch_None()
+        public void VisualBrush_Align_TopLeft()
         {
             Decorator target = new Decorator
             {
@@ -31,6 +31,8 @@ namespace Perspex.Direct2D1.RenderTests.Media
                 {
                     Fill = new VisualBrush
                     {
+                        AlignmentX = AlignmentX.Left,
+                        AlignmentY = AlignmentY.Top,
                         Stretch = Stretch.None,
                         Visual = new Border
                         {
@@ -95,6 +97,45 @@ namespace Perspex.Direct2D1.RenderTests.Media
             this.CompareImages();
         }
 
+        [Fact]
+        public void VisualBrush_Align_BottomRight()
+        {
+            Decorator target = new Decorator
+            {
+                Padding = new Thickness(8),
+                Width = 200,
+                Height = 200,
+                Child = new Rectangle
+                {
+                    Fill = new VisualBrush
+                    {
+                        AlignmentX = AlignmentX.Right,
+                        AlignmentY = AlignmentY.Bottom,
+                        Stretch = Stretch.None,
+                        Visual = new Border
+                        {
+                            Width = 92,
+                            Height = 92,
+                            Background = Brushes.Red,
+                            BorderBrush = Brushes.Black,
+                            BorderThickness = 2,
+                            Child = new TextBlock
+                            {
+                                Text = "Perspex",
+                                FontSize = 12,
+                                FontFamily = "Arial",
+                                HorizontalAlignment = HorizontalAlignment.Center,
+                                VerticalAlignment = VerticalAlignment.Center,
+                            }
+                        }
+                    }
+                }
+            };
+
+            this.RenderToFile(target);
+            this.CompareImages();
+        }
+
         [Fact]
         public void VisualBrush_Stretch_Fill_Large()
         {

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


+ 0 - 0
tests/TestFiles/Direct2D1/Media/VisualBrush/VisualBrush_Stretch_None.expected.png → tests/TestFiles/Direct2D1/Media/VisualBrush/VisualBrush_Align_TopLeft.expected.png