Browse Source

Accept RelativePoint in XAML.

pull/145/head
Steven Kirk 11 years ago
parent
commit
3aa9b7e60c
  1. 27
      Tests/Perspex.SceneGraph.UnitTests/RelativePointTests.cs
  2. 2
      samples/XamlTestApplication/Views/MainWindow.xaml
  3. 1
      src/Markup/Perspex.Markup.Xaml/Context/PerspexWiringContext.cs
  4. 32
      src/Markup/Perspex.Markup.Xaml/Converters/RelativePointTypeConverter.cs
  5. 1
      src/Markup/Perspex.Markup.Xaml/Perspex.Markup.Xaml.csproj
  6. 65
      src/Perspex.SceneGraph/RelativePoint.cs
  7. 1
      tests/Perspex.SceneGraph.UnitTests/Perspex.SceneGraph.UnitTests.csproj

27
Tests/Perspex.SceneGraph.UnitTests/RelativePointTests.cs

@ -0,0 +1,27 @@
// 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.Globalization;
using Xunit;
namespace Perspex.SceneGraph.UnitTests
{
public class RelativePointTests
{
[Fact]
public void Parse_Should_Accept_Absolute_Value()
{
var result = RelativePoint.Parse("4,5", CultureInfo.InvariantCulture);
Assert.Equal(new RelativePoint(4, 5, RelativeUnit.Absolute), result);
}
[Fact]
public void Parse_Should_Accept_Relative_Value()
{
var result = RelativePoint.Parse("25%, 50%", CultureInfo.InvariantCulture);
Assert.Equal(new RelativePoint(0.25, 0.5, RelativeUnit.Relative), result);
}
}
}

2
samples/XamlTestApplication/Views/MainWindow.xaml

@ -104,7 +104,7 @@
</Grid.RowDefinitions>
<Border Width="100" Height="100">
<Border.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<LinearGradientBrush StartPoint="0%,0%" EndPoint="100%,100%">
<GradientStop Color="Red" Offset="0"/>
<GradientStop Color="Green" Offset="1"/>
</LinearGradientBrush>

1
src/Markup/Perspex.Markup.Xaml/Context/PerspexWiringContext.cs

@ -83,6 +83,7 @@ namespace Perspex.Markup.Xaml.Context
new TypeConverterRegistration(typeof(GridLength), new GridLengthTypeConverter()),
new TypeConverterRegistration(typeof(Point), new PointTypeConverter()),
new TypeConverterRegistration(typeof(PerspexProperty), new PerspexPropertyTypeConverter()),
new TypeConverterRegistration(typeof(RelativePoint), new RelativePointTypeConverter()),
new TypeConverterRegistration(typeof(RowDefinitions), new RowDefinitionsTypeConverter()),
new TypeConverterRegistration(typeof(Thickness), new ThicknessTypeConverter()),
new TypeConverterRegistration(typeof(Selector), new SelectorTypeConverter()),

32
src/Markup/Perspex.Markup.Xaml/Converters/RelativePointTypeConverter.cs

@ -0,0 +1,32 @@
// 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 OmniXaml.TypeConversion;
namespace Perspex.Markup.Xaml.Converters
{
public class RelativePointTypeConverter : ITypeConverter
{
public bool CanConvertFrom(IXamlTypeConverterContext context, Type sourceType)
{
return sourceType == typeof(string);
}
public bool CanConvertTo(IXamlTypeConverterContext context, Type destinationType)
{
return false;
}
public object ConvertFrom(IXamlTypeConverterContext context, CultureInfo culture, object value)
{
return RelativePoint.Parse((string)value, culture);
}
public object ConvertTo(IXamlTypeConverterContext context, CultureInfo culture, object value, Type destinationType)
{
throw new NotImplementedException();
}
}
}

1
src/Markup/Perspex.Markup.Xaml/Perspex.Markup.Xaml.csproj

@ -39,6 +39,7 @@
<Link>Properties\SharedAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Converters\ColorTypeConverter.cs" />
<Compile Include="Converters\RelativePointTypeConverter.cs" />
<Compile Include="Converters\PointTypeConverter.cs" />
<Compile Include="Converters\ClassesTypeConverter.cs" />
<Compile Include="Converters\RowDefinitionsTypeConverter.cs" />

65
src/Perspex.SceneGraph/RelativePoint.cs

@ -27,7 +27,7 @@ namespace Perspex
/// <summary>
/// Defines a point that may be defined relative to a containing element.
/// </summary>
public struct RelativePoint
public struct RelativePoint : IEquatable<RelativePoint>
{
/// <summary>
/// A point at the top left of the containing element.
@ -80,6 +80,63 @@ namespace Perspex
/// </summary>
public RelativeUnit Unit => _unit;
/// <summary>
/// Checks for equality between two <see cref="RelativePoint"/>s.
/// </summary>
/// <param name="left">The first point.</param>
/// <param name="right">The second point.</param>
/// <returns>True if the points are equal; otherwise false.</returns>
public static bool operator ==(RelativePoint left, RelativePoint right)
{
return left.Equals(right);
}
/// <summary>
/// Checks for unequality between two <see cref="RelativePoint"/>s.
/// </summary>
/// <param name="left">The first point.</param>
/// <param name="right">The second point.</param>
/// <returns>True if the points are unequal; otherwise false.</returns>
public static bool operator !=(RelativePoint left, RelativePoint right)
{
return !left.Equals(right);
}
/// <summary>
/// Checks if the <see cref="RelativePoint"/> equals another object.
/// </summary>
/// <param name="obj">The other object.</param>
/// <returns>True if the objects are equal, otherwise false.</returns>
public override bool Equals(object obj)
{
return (obj is RelativePoint) ? Equals((RelativePoint)obj) : false;
}
/// <summary>
/// Checks if the <see cref="RelativePoint"/> equals another point.
/// </summary>
/// <param name="p">The other point.</param>
/// <returns>True if the objects are equal, otherwise false.</returns>
public bool Equals(RelativePoint p)
{
return Unit == p.Unit && Point == p.Point;
}
/// <summary>
/// Gets a hashcode for a <see cref="RelativePoint"/>.
/// </summary>
/// <returns>A hash code.</returns>
public override int GetHashCode()
{
unchecked
{
int hash = 17;
hash = (hash * 23) + Unit.GetHashCode();
hash = (hash * 23) + Point.GetHashCode();
return hash;
}
}
/// <summary>
/// Converts a <see cref="RelativePoint"/> into pixels.
/// </summary>
@ -107,6 +164,7 @@ namespace Perspex
if (parts.Count == 2)
{
var unit = RelativeUnit.Absolute;
var scale = 1.0;
if (parts[0].EndsWith("%"))
{
@ -118,11 +176,12 @@ namespace Perspex
parts[0] = parts[0].TrimEnd('%');
parts[1] = parts[1].TrimEnd('%');
unit = RelativeUnit.Relative;
scale = 0.01;
}
return new RelativePoint(
double.Parse(parts[0], culture),
double.Parse(parts[1], culture),
double.Parse(parts[0], culture) * scale,
double.Parse(parts[1], culture) * scale,
unit);
}
else

1
tests/Perspex.SceneGraph.UnitTests/Perspex.SceneGraph.UnitTests.csproj

@ -85,6 +85,7 @@
<Compile Include="Media\ColorTests.cs" />
<Compile Include="TestRoot.cs" />
<Compile Include="TestVisual.cs" />
<Compile Include="RelativePointTests.cs" />
<Compile Include="VisualTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="VisualTree\BoundsTrackerTests.cs" />

Loading…
Cancel
Save