From e10113025025e09efa93bfa22096f5e980f9afaa Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 16 Sep 2015 02:54:52 +0200 Subject: [PATCH] Renamed RelativeUnit values. Percent -> Relative, Pixels -> Absolute. --- .../Adapters/PerspexAdapter.cs | 4 +- src/Perspex.SceneGraph/RelativePoint.cs | 59 ++++++++++++++++--- src/Perspex.SceneGraph/RelativeRect.cs | 4 +- .../Media/LinearGradientBrushTests.cs | 8 +-- .../Media/VisualBrushTests.cs | 20 +++---- 5 files changed, 69 insertions(+), 26 deletions(-) diff --git a/src/Perspex.HtmlRenderer/Adapters/PerspexAdapter.cs b/src/Perspex.HtmlRenderer/Adapters/PerspexAdapter.cs index 1a3c190751..2dc9f2ea08 100644 --- a/src/Perspex.HtmlRenderer/Adapters/PerspexAdapter.cs +++ b/src/Perspex.HtmlRenderer/Adapters/PerspexAdapter.cs @@ -77,8 +77,8 @@ namespace TheArtOfDev.HtmlRenderer.Perspex.Adapters double y = angle <= 45 ? Math.Max(0.5 - angle / 90, 0) : angle > 135 ? Math.Abs(1.5 - angle / 90) : 0; return new BrushAdapter(new LinearGradientBrush { - StartPoint = new RelativePoint(x, y, RelativeUnit.Percent), - EndPoint = new RelativePoint(1 - x, 1 - y, RelativeUnit.Percent), + StartPoint = new RelativePoint(x, y, RelativeUnit.Relative), + EndPoint = new RelativePoint(1 - x, 1 - y, RelativeUnit.Relative), GradientStops = { new GradientStop(startColor, 0), diff --git a/src/Perspex.SceneGraph/RelativePoint.cs b/src/Perspex.SceneGraph/RelativePoint.cs index a87ea874f1..da2f652b7a 100644 --- a/src/Perspex.SceneGraph/RelativePoint.cs +++ b/src/Perspex.SceneGraph/RelativePoint.cs @@ -1,6 +1,10 @@ // Copyright (c) The Perspex Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. +using System; +using System.Globalization; +using System.Linq; + namespace Perspex { /// @@ -10,14 +14,14 @@ namespace Perspex public enum RelativeUnit { /// - /// The point is expressed as a percentage of the containing element's size. + /// The point is expressed as a fraction of the containing element's size. /// - Percent, + Relative, /// - /// The origin's point is in pixels. + /// The point is absolute (i.e. in pixels). /// - Pixels, + Absolute, } /// @@ -28,17 +32,17 @@ namespace Perspex /// /// A point at the top left of the containing element. /// - public static readonly RelativePoint TopLeft = new RelativePoint(0, 0, RelativeUnit.Percent); + public static readonly RelativePoint TopLeft = new RelativePoint(0, 0, RelativeUnit.Relative); /// /// A point at the center of the containing element. /// - public static readonly RelativePoint Center = new RelativePoint(0.5, 0.5, RelativeUnit.Percent); + public static readonly RelativePoint Center = new RelativePoint(0.5, 0.5, RelativeUnit.Relative); /// /// A point at the bottom right of the containing element. /// - public static readonly RelativePoint BottomRight = new RelativePoint(1, 1, RelativeUnit.Percent); + public static readonly RelativePoint BottomRight = new RelativePoint(1, 1, RelativeUnit.Relative); private Point _point; @@ -83,9 +87,48 @@ namespace Perspex /// The origin point in pixels. public Point ToPixels(Size size) { - return _unit == RelativeUnit.Pixels ? + return _unit == RelativeUnit.Absolute ? _point : new Point(_point.X * size.Width, _point.Y * size.Height); } + + /// + /// Parses a string. + /// + /// The string. + /// The current culture. + /// The parsed . + public static RelativePoint Parse(string s, CultureInfo culture) + { + var parts = s.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries) + .Select(x => x.Trim()) + .ToList(); + + if (parts.Count == 2) + { + var unit = RelativeUnit.Absolute; + + if (parts[0].EndsWith("%")) + { + if (!parts[1].EndsWith("%")) + { + throw new FormatException("If one coordinate is relative, both must be."); + } + + parts[0] = parts[0].TrimEnd('%'); + parts[1] = parts[1].TrimEnd('%'); + unit = RelativeUnit.Relative; + } + + return new RelativePoint( + double.Parse(parts[0], culture), + double.Parse(parts[1], culture), + unit); + } + else + { + throw new FormatException("Invalid Point."); + } + } } } diff --git a/src/Perspex.SceneGraph/RelativeRect.cs b/src/Perspex.SceneGraph/RelativeRect.cs index 02b5d63143..5dedbdf00d 100644 --- a/src/Perspex.SceneGraph/RelativeRect.cs +++ b/src/Perspex.SceneGraph/RelativeRect.cs @@ -11,7 +11,7 @@ namespace Perspex /// /// A rectangle that represents 100% of an area. /// - public static readonly RelativeRect Fill = new RelativeRect(0, 0, 1, 1, RelativeUnit.Percent); + public static readonly RelativeRect Fill = new RelativeRect(0, 0, 1, 1, RelativeUnit.Relative); /// /// Initializes a new instance of the structure. @@ -90,7 +90,7 @@ namespace Perspex /// The origin point in pixels. public Rect ToPixels(Size size) { - return Unit == RelativeUnit.Pixels ? + return Unit == RelativeUnit.Absolute ? Rect : new Rect( Rect.X * size.Width, diff --git a/tests/Perspex.RenderTests/Media/LinearGradientBrushTests.cs b/tests/Perspex.RenderTests/Media/LinearGradientBrushTests.cs index 3eb4671da9..8d5185096d 100644 --- a/tests/Perspex.RenderTests/Media/LinearGradientBrushTests.cs +++ b/tests/Perspex.RenderTests/Media/LinearGradientBrushTests.cs @@ -34,8 +34,8 @@ namespace Perspex.Direct2D1.RenderTests.Media { Background = new LinearGradientBrush { - StartPoint = new RelativePoint(0, 0.5, RelativeUnit.Percent), - EndPoint = new RelativePoint(1, 0.5, RelativeUnit.Percent), + StartPoint = new RelativePoint(0, 0.5, RelativeUnit.Relative), + EndPoint = new RelativePoint(1, 0.5, RelativeUnit.Relative), GradientStops = { new GradientStop { Color = Colors.Red, Offset = 0 }, @@ -61,8 +61,8 @@ namespace Perspex.Direct2D1.RenderTests.Media { Background = new LinearGradientBrush { - StartPoint = new RelativePoint(0.5, 0, RelativeUnit.Percent), - EndPoint = new RelativePoint(0.5, 1, RelativeUnit.Percent), + StartPoint = new RelativePoint(0.5, 0, RelativeUnit.Relative), + EndPoint = new RelativePoint(0.5, 1, RelativeUnit.Relative), GradientStops = { new GradientStop { Color = Colors.Red, Offset = 0 }, diff --git a/tests/Perspex.RenderTests/Media/VisualBrushTests.cs b/tests/Perspex.RenderTests/Media/VisualBrushTests.cs index d05b3cf13e..10fbef3e79 100644 --- a/tests/Perspex.RenderTests/Media/VisualBrushTests.cs +++ b/tests/Perspex.RenderTests/Media/VisualBrushTests.cs @@ -288,7 +288,7 @@ namespace Perspex.Direct2D1.RenderTests.Media { Fill = new VisualBrush { - SourceRect = new RelativeRect(40, 40, 100, 100, RelativeUnit.Pixels), + SourceRect = new RelativeRect(40, 40, 100, 100, RelativeUnit.Absolute), Visual = new Border { Width = 180, @@ -329,7 +329,7 @@ namespace Perspex.Direct2D1.RenderTests.Media { Fill = new VisualBrush { - DestinationRect = new RelativeRect(92, 92, 92, 92, RelativeUnit.Pixels), + DestinationRect = new RelativeRect(92, 92, 92, 92, RelativeUnit.Absolute), Visual = new Border { Width = 180, @@ -370,8 +370,8 @@ namespace Perspex.Direct2D1.RenderTests.Media { Fill = new VisualBrush { - SourceRect = new RelativeRect(40, 40, 100, 100, RelativeUnit.Pixels), - DestinationRect = new RelativeRect(92, 92, 92, 92, RelativeUnit.Pixels), + SourceRect = new RelativeRect(40, 40, 100, 100, RelativeUnit.Absolute), + DestinationRect = new RelativeRect(92, 92, 92, 92, RelativeUnit.Absolute), Visual = new Border { Width = 180, @@ -412,8 +412,8 @@ namespace Perspex.Direct2D1.RenderTests.Media { Fill = new VisualBrush { - SourceRect = new RelativeRect(0.22, 0.22, 0.56, 0.56, RelativeUnit.Percent), - DestinationRect = new RelativeRect(0.5, 0.5, 0.5, 0.5, RelativeUnit.Percent), + SourceRect = new RelativeRect(0.22, 0.22, 0.56, 0.56, RelativeUnit.Relative), + DestinationRect = new RelativeRect(0.5, 0.5, 0.5, 0.5, RelativeUnit.Relative), Visual = new Border { Width = 180, @@ -456,7 +456,7 @@ namespace Perspex.Direct2D1.RenderTests.Media { Stretch = Stretch.None, TileMode = TileMode.Tile, - DestinationRect = new RelativeRect(0.25, 0.25, 0.5, 0.5, RelativeUnit.Percent), + DestinationRect = new RelativeRect(0.25, 0.25, 0.5, 0.5, RelativeUnit.Relative), Visual = new Border { Width = 92, @@ -543,7 +543,7 @@ namespace Perspex.Direct2D1.RenderTests.Media { Stretch = Stretch.None, TileMode = TileMode.FlipX, - DestinationRect = new RelativeRect(0, 0, 0.5, 0.5, RelativeUnit.Percent), + DestinationRect = new RelativeRect(0, 0, 0.5, 0.5, RelativeUnit.Relative), Visual = new Border { Width = 92, @@ -586,7 +586,7 @@ namespace Perspex.Direct2D1.RenderTests.Media { Stretch = Stretch.None, TileMode = TileMode.FlipY, - DestinationRect = new RelativeRect(0, 0, 0.5, 0.5, RelativeUnit.Percent), + DestinationRect = new RelativeRect(0, 0, 0.5, 0.5, RelativeUnit.Relative), Visual = new Border { Width = 92, @@ -629,7 +629,7 @@ namespace Perspex.Direct2D1.RenderTests.Media { Stretch = Stretch.None, TileMode = TileMode.FlipXY, - DestinationRect = new RelativeRect(0, 0, 0.5, 0.5, RelativeUnit.Percent), + DestinationRect = new RelativeRect(0, 0, 0.5, 0.5, RelativeUnit.Relative), Visual = new Border { Width = 92,