Browse Source

Implemented equality for Pens.

pull/2744/head
Steven Kirk 7 years ago
parent
commit
afa594cd48
  1. 13
      src/Avalonia.Visuals/Media/Immutable/ImmutablePen.cs
  2. 43
      src/Avalonia.Visuals/Media/Pen.cs
  3. 2
      src/Avalonia.Visuals/Rendering/SceneGraph/LineNode.cs

13
src/Avalonia.Visuals/Media/Immutable/ImmutablePen.cs

@ -1,12 +1,14 @@
// Copyright (c) The Avalonia Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System;
namespace Avalonia.Media.Immutable
{
/// <summary>
/// Describes how a stroke is drawn.
/// </summary>
public class ImmutablePen : IPen
public class ImmutablePen : IPen, IEquatable<IPen>
{
/// <summary>
/// Initializes a new instance of the <see cref="Pen"/> class.
@ -81,5 +83,14 @@ namespace Avalonia.Media.Immutable
/// The limit on the ratio of the miter length to half this pen's Thickness.
/// </summary>
public double MiterLimit { get; }
/// <inheritdoc/>
public override bool Equals(object obj) => Pen.PenEquals(this, obj as IPen);
/// <inheritdoc/>
public bool Equals(IPen other) => Pen.PenEquals(this, other);
/// <inheritdoc/>
public override int GetHashCode() => Pen.GetHashCode(this);
}
}

43
src/Avalonia.Visuals/Media/Pen.cs

@ -2,6 +2,7 @@
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System;
using System.Collections.Generic;
using Avalonia.Media.Immutable;
using Avalonia.Utilities;
@ -10,7 +11,7 @@ namespace Avalonia.Media
/// <summary>
/// Describes how a stroke is drawn.
/// </summary>
public class Pen : AvaloniaObject, IMutablePen
public class Pen : AvaloniaObject, IMutablePen, IEquatable<IPen>
{
/// <summary>
/// Defines the <see cref="Brush"/> property.
@ -170,6 +171,15 @@ namespace Avalonia.Media
/// </summary>
public event EventHandler Invalidated;
/// <inheritdoc/>
public override bool Equals(object obj) => PenEquals(this, obj as IPen);
/// <inheritdoc/>
public bool Equals(IPen other) => PenEquals(this, other);
/// <inheritdoc/>
public override int GetHashCode() => GetHashCode(this);
/// <summary>
/// Creates an immutable clone of the brush.
/// </summary>
@ -232,6 +242,37 @@ namespace Avalonia.Media
/// <param name="e">The event args.</param>
protected void RaiseInvalidated(EventArgs e) => Invalidated?.Invoke(this, e);
internal static int GetHashCode(IPen pen)
{
var hashCode = 1181807663;
hashCode = hashCode * -1521134295 + EqualityComparer<IBrush>.Default.GetHashCode(pen.Brush);
hashCode = hashCode * -1521134295 + pen.Thickness.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<DashStyle>.Default.GetHashCode(pen.DashStyle);
hashCode = hashCode * -1521134295 + pen.LineCap.GetHashCode();
hashCode = hashCode * -1521134295 + pen.LineJoin.GetHashCode();
hashCode = hashCode * -1521134295 + pen.MiterLimit.GetHashCode();
return hashCode;
}
internal static bool PenEquals(IPen a, IPen b)
{
if (ReferenceEquals(a, b))
{
return true;
}
else if (a is null && !(b is null) || (b is null && !(a is null)))
{
return false;
}
return EqualityComparer<IBrush>.Default.Equals(a.Brush, b.Brush) &&
a.Thickness == b.Thickness &&
EqualityComparer<DashStyle>.Default.Equals(a.DashStyle, b.DashStyle) &&
a.LineCap == b.LineCap &&
a.LineJoin == b.LineJoin &&
a.MiterLimit == b.MiterLimit;
}
private void AffectsRenderInvalidated(object sender, EventArgs e) => RaiseInvalidated(EventArgs.Empty);
}
}

2
src/Avalonia.Visuals/Rendering/SceneGraph/LineNode.cs

@ -74,7 +74,7 @@ namespace Avalonia.Rendering.SceneGraph
/// </remarks>
public bool Equals(Matrix transform, IPen pen, Point p1, Point p2)
{
return transform == Transform && pen == Pen && p1 == P1 && p2 == P2;
return transform == Transform && Equals(pen, Pen) && p1 == P1 && p2 == P2;
}
public override void Render(IDrawingContextImpl context)

Loading…
Cancel
Save