From da75a6bfdfa53871637c6cb8b4dd2a2e9762e8ab Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 25 Jun 2014 21:25:49 +0200 Subject: [PATCH] Rectangle stuff. --- .../Perspex.Direct2D1.RenderTests.csproj | 1 + .../Shapes/RectangleTests.cs | 80 ++++++++++++++++++ Perspex/Controls/Control.cs | 12 ++- Perspex/Shapes/Rectangle.cs | 20 ++++- Perspex/Size.cs | 22 +++++ .../Rectangle_1px_Stroke.expected.png | Bin 0 -> 663 bytes .../Rectangle_2px_Stroke.expected.png | Bin 0 -> 619 bytes .../Rectangle_Stroke_Fill.expected.png | Bin 0 -> 620 bytes 8 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 Perspex.Direct2D1.RenderTests/Shapes/RectangleTests.cs create mode 100644 TestFiles/Direct2D1/Shapes/Rectangle/Rectangle_1px_Stroke.expected.png create mode 100644 TestFiles/Direct2D1/Shapes/Rectangle/Rectangle_2px_Stroke.expected.png create mode 100644 TestFiles/Direct2D1/Shapes/Rectangle/Rectangle_Stroke_Fill.expected.png diff --git a/Perspex.Direct2D1.RenderTests/Perspex.Direct2D1.RenderTests.csproj b/Perspex.Direct2D1.RenderTests/Perspex.Direct2D1.RenderTests.csproj index 889e35f54b..4dac4719fa 100644 --- a/Perspex.Direct2D1.RenderTests/Perspex.Direct2D1.RenderTests.csproj +++ b/Perspex.Direct2D1.RenderTests/Perspex.Direct2D1.RenderTests.csproj @@ -59,6 +59,7 @@ + diff --git a/Perspex.Direct2D1.RenderTests/Shapes/RectangleTests.cs b/Perspex.Direct2D1.RenderTests/Shapes/RectangleTests.cs new file mode 100644 index 0000000000..b8af94466f --- /dev/null +++ b/Perspex.Direct2D1.RenderTests/Shapes/RectangleTests.cs @@ -0,0 +1,80 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2014 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.Direct2D1.RenderTests.Shapes +{ + using Microsoft.VisualStudio.TestTools.UnitTesting; + using Perspex.Controls; + using Perspex.Media; + using Perspex.Shapes; + + [TestClass] + public class RectangleTests : TestBase + { + public RectangleTests() + : base(@"Shapes\Rectangle") + { + } + + [TestMethod] + public void Rectangle_1px_Stroke() + { + Decorator target = new Decorator + { + Padding = new Thickness(8), + Width = 200, + Height = 200, + Content = new Rectangle + { + Stroke = Brushes.Black, + StrokeThickness = 1, + } + }; + + this.RenderToFile(target); + this.CompareImages(); + } + + [TestMethod] + public void Rectangle_2px_Stroke() + { + Decorator target = new Decorator + { + Padding = new Thickness(8), + Width = 200, + Height = 200, + Content = new Rectangle + { + Stroke = Brushes.Black, + StrokeThickness = 2, + } + }; + + this.RenderToFile(target); + this.CompareImages(); + } + + [TestMethod] + public void Rectangle_Stroke_Fill() + { + Decorator target = new Decorator + { + Padding = new Thickness(8), + Width = 200, + Height = 200, + Content = new Rectangle + { + Stroke = Brushes.Black, + StrokeThickness = 2, + Fill = Brushes.Red, + } + }; + + this.RenderToFile(target); + this.CompareImages(); + } + } +} diff --git a/Perspex/Controls/Control.cs b/Perspex/Controls/Control.cs index 3ab8b3449b..6c54d9a55a 100644 --- a/Perspex/Controls/Control.cs +++ b/Perspex/Controls/Control.cs @@ -425,8 +425,16 @@ namespace Perspex.Controls protected virtual Size MeasureCore(Size availableSize) { - availableSize = availableSize.Deflate(this.Margin); - return this.MeasureOverride(availableSize); + Size measuredSize = this.MeasureOverride(availableSize.Deflate(this.Margin)); + double width = (this.Width > 0) ? this.Width : measuredSize.Width; + double height = (this.Height > 0) ? this.Height : measuredSize.Height; + + width = Math.Min(width, this.MaxWidth); + width = Math.Max(width, this.MinWidth); + height = Math.Min(height, this.MaxHeight); + height = Math.Max(height, this.MinHeight); + + return new Size(width, height); } protected virtual Size MeasureOverride(Size availableSize) diff --git a/Perspex/Shapes/Rectangle.cs b/Perspex/Shapes/Rectangle.cs index 9f769055a9..6f325331fa 100644 --- a/Perspex/Shapes/Rectangle.cs +++ b/Perspex/Shapes/Rectangle.cs @@ -10,9 +10,27 @@ namespace Perspex.Shapes public class Rectangle : Shape { + private Geometry geometry; + + private Size geometrySize; + public override Geometry DefiningGeometry { - get { return new RectangleGeometry(new Rect(0, 0, this.Width, this.Height)); } + get + { + if (this.geometry == null || this.geometrySize != this.ActualSize) + { + this.geometry = new RectangleGeometry(new Rect(0, 0, this.ActualSize.Width, this.ActualSize.Height)); + this.geometrySize = this.ActualSize; + } + + return this.geometry; + } + } + + protected override Size MeasureOverride(Size availableSize) + { + return new Size(this.StrokeThickness, this.StrokeThickness); } } } diff --git a/Perspex/Size.cs b/Perspex/Size.cs index 6eee721332..e32fe843f8 100644 --- a/Perspex/Size.cs +++ b/Perspex/Size.cs @@ -51,6 +51,28 @@ namespace Perspex get { return this.height; } } + /// + /// Checks for equality between two s. + /// + /// The first size. + /// The second size. + /// True if the sizes are equal; otherwise false. + public static bool operator ==(Size left, Size right) + { + return left.width == right.width && left.height == right.height; + } + + /// + /// Checks for unequality between two s. + /// + /// The first size. + /// The second size. + /// True if the sizes are unequal; otherwise false. + public static bool operator !=(Size left, Size right) + { + return !(left == right); + } + /// /// Constrains the size. /// diff --git a/TestFiles/Direct2D1/Shapes/Rectangle/Rectangle_1px_Stroke.expected.png b/TestFiles/Direct2D1/Shapes/Rectangle/Rectangle_1px_Stroke.expected.png new file mode 100644 index 0000000000000000000000000000000000000000..ac360079e7ad53c3d70681030eb7a17fbc856dcc GIT binary patch literal 663 zcmeAS@N?(olHy`uVBq!ia0vp^CqS5k4M?tyST_$yF%}28J29*~C-V}>VM%xNb!1@J z*w6hZk(Ggg$c`QYyR)*F{`b4f z1k}uMz}mWZ@AG1wGq22^E$!dwOgjSeP6~ ziJ=;M1& literal 0 HcmV?d00001 diff --git a/TestFiles/Direct2D1/Shapes/Rectangle/Rectangle_2px_Stroke.expected.png b/TestFiles/Direct2D1/Shapes/Rectangle/Rectangle_2px_Stroke.expected.png new file mode 100644 index 0000000000000000000000000000000000000000..c3ee9548b0b56d55f906f6dab4810e516c287333 GIT binary patch literal 619 zcmeAS@N?(olHy`uVBq!ia0vp^CqS5k4M?tyST_$yF%}28J29*~C-V}>VM%xNb!1@J z*w6hZk(GggNz~KDF{C2y?Nvjr!wv#02PgmkpDuZL*>$E=yH%5Z9x&St)Wg8=BmMfD zbGF>-x63}vWR3e#D-hojY{`Z6 W#q7DC*bgd#1Uy~+T-G@yGywo`EMoxx literal 0 HcmV?d00001 diff --git a/TestFiles/Direct2D1/Shapes/Rectangle/Rectangle_Stroke_Fill.expected.png b/TestFiles/Direct2D1/Shapes/Rectangle/Rectangle_Stroke_Fill.expected.png new file mode 100644 index 0000000000000000000000000000000000000000..2eb5c946cee0c765bd2948e8172a1f4c3062a8a4 GIT binary patch literal 620 zcmeAS@N?(olHy`uVBq!ia0vp^CqS5k4M?tyST_$yF%}28J29*~C-V}>VM%xNb!1@J z*w6hZk(GggNzBv5F{C2y?Nvj*!v;Joj+X!bZ@#?2BT=`hL@;gEZwB!gpe6=}Kby15 zp3iHYoNHZimWhAgFNJ#?>c1@N