Browse Source

Renamed RelativeUnit values.

Percent -> Relative, Pixels -> Absolute.
pull/145/head
Steven Kirk 11 years ago
parent
commit
e101130250
  1. 4
      src/Perspex.HtmlRenderer/Adapters/PerspexAdapter.cs
  2. 59
      src/Perspex.SceneGraph/RelativePoint.cs
  3. 4
      src/Perspex.SceneGraph/RelativeRect.cs
  4. 8
      tests/Perspex.RenderTests/Media/LinearGradientBrushTests.cs
  5. 20
      tests/Perspex.RenderTests/Media/VisualBrushTests.cs

4
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),

59
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
{
/// <summary>
@ -10,14 +14,14 @@ namespace Perspex
public enum RelativeUnit
{
/// <summary>
/// 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.
/// </summary>
Percent,
Relative,
/// <summary>
/// The origin's point is in pixels.
/// The point is absolute (i.e. in pixels).
/// </summary>
Pixels,
Absolute,
}
/// <summary>
@ -28,17 +32,17 @@ namespace Perspex
/// <summary>
/// A point at the top left of the containing element.
/// </summary>
public static readonly RelativePoint TopLeft = new RelativePoint(0, 0, RelativeUnit.Percent);
public static readonly RelativePoint TopLeft = new RelativePoint(0, 0, RelativeUnit.Relative);
/// <summary>
/// A point at the center of the containing element.
/// </summary>
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);
/// <summary>
/// A point at the bottom right of the containing element.
/// </summary>
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
/// <returns>The origin point in pixels.</returns>
public Point ToPixels(Size size)
{
return _unit == RelativeUnit.Pixels ?
return _unit == RelativeUnit.Absolute ?
_point :
new Point(_point.X * size.Width, _point.Y * size.Height);
}
/// <summary>
/// Parses a <see cref="RelativePoint"/> string.
/// </summary>
/// <param name="s">The string.</param>
/// <param name="culture">The current culture.</param>
/// <returns>The parsed <see cref="RelativePoint"/>.</returns>
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.");
}
}
}
}

4
src/Perspex.SceneGraph/RelativeRect.cs

@ -11,7 +11,7 @@ namespace Perspex
/// <summary>
/// A rectangle that represents 100% of an area.
/// </summary>
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);
/// <summary>
/// Initializes a new instance of the <see cref="RelativeRect"/> structure.
@ -90,7 +90,7 @@ namespace Perspex
/// <returns>The origin point in pixels.</returns>
public Rect ToPixels(Size size)
{
return Unit == RelativeUnit.Pixels ?
return Unit == RelativeUnit.Absolute ?
Rect :
new Rect(
Rect.X * size.Width,

8
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 },

20
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,

Loading…
Cancel
Save