1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 04:53:29 +01:00

Implemented IEquatable on a lot of classes that were missing it

This commit is contained in:
Ell 2021-12-21 11:39:29 +01:00
parent 3541b8d3e1
commit c0ce5a07ad
5 changed files with 125 additions and 27 deletions

View file

@ -1,3 +1,4 @@
using System;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using MonoGame.Extended.Tiled; using MonoGame.Extended.Tiled;
@ -6,7 +7,7 @@ namespace MLEM.Extended.Tiled {
/// A struct that represents a position on a <see cref="TiledMap"/> with multiple layers. /// A struct that represents a position on a <see cref="TiledMap"/> with multiple layers.
/// </summary> /// </summary>
[DataContract] [DataContract]
public struct LayerPosition { public struct LayerPosition : IEquatable<LayerPosition> {
/// <summary> /// <summary>
/// The name of the layer that this position is on /// The name of the layer that this position is on
@ -36,16 +37,6 @@ namespace MLEM.Extended.Tiled {
this.Y = y; this.Y = y;
} }
/// <inheritdoc cref="Equals(LayerPosition)"/>
public static bool operator ==(LayerPosition left, LayerPosition right) {
return left.Equals(right);
}
/// <inheritdoc cref="Equals(LayerPosition)"/>
public static bool operator !=(LayerPosition left, LayerPosition right) {
return !left.Equals(right);
}
/// <inheritdoc cref="Equals(object)"/> /// <inheritdoc cref="Equals(object)"/>
public bool Equals(LayerPosition other) { public bool Equals(LayerPosition other) {
return this.Layer == other.Layer && this.X == other.X && this.Y == other.Y; return this.Layer == other.Layer && this.X == other.X && this.Y == other.Y;
@ -73,5 +64,58 @@ namespace MLEM.Extended.Tiled {
return $"{nameof(this.Layer)}: {this.Layer}, {nameof(this.X)}: {this.X}, {nameof(this.Y)}: {this.Y}"; return $"{nameof(this.Layer)}: {this.Layer}, {nameof(this.X)}: {this.X}, {nameof(this.Y)}: {this.Y}";
} }
/// <summary>
/// Adds the given layer positions together, returning a new layer position with the sum of their coordinates.
/// If the two layer positions' <see cref="Layer"/> differ, an <see cref="ArgumentException"/> is thrown.
/// </summary>
/// <param name="left">The left position.</param>
/// <param name="right">The right position.</param>
/// <returns>The sum of the positions.</returns>
/// <exception cref="ArgumentException">Thrown if the two positions' <see cref="Layer"/> are not the same.</exception>
public static LayerPosition Add(LayerPosition left, LayerPosition right) {
if (left.Layer != right.Layer)
throw new ArgumentException("Cannot add layer positions on different layers");
return new LayerPosition(left.Layer, left.X + right.X, left.Y + right.Y);
}
/// <inheritdoc cref="Equals(LayerPosition)"/>
public static bool operator ==(LayerPosition left, LayerPosition right) {
return left.Equals(right);
}
/// <inheritdoc cref="Equals(LayerPosition)"/>
public static bool operator !=(LayerPosition left, LayerPosition right) {
return !left.Equals(right);
}
/// <summary>
/// Returns the negative of the given layer position.
/// </summary>
/// <param name="position">The position to negate.</param>
/// <returns>The negative position.</returns>
public static LayerPosition operator -(LayerPosition position) {
return new LayerPosition(position.Layer, -position.X, -position.Y);
}
/// <summary>
/// Returns the sum of the two layer positions using <see cref="Add"/>.
/// </summary>
/// <param name="left">The left position.</param>
/// <param name="right">The right position.</param>
/// <returns>The sum of the positions.</returns>
public static LayerPosition operator +(LayerPosition left, LayerPosition right) {
return Add(left, right);
}
/// <summary>
/// Subtracts the second from the first position using <see cref="Add"/>.
/// </summary>
/// <param name="left">The left position.</param>
/// <param name="right">The right position.</param>
/// <returns>The difference of the positions.</returns>
public static LayerPosition operator -(LayerPosition left, LayerPosition right) {
return Add(left, -right);
}
} }
} }

View file

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using MLEM.Ui.Elements; using MLEM.Ui.Elements;
@ -8,20 +9,20 @@ namespace MLEM.Ui.Style {
/// Note that <c>T</c> implicitly converts to <c>StyleProp{T}</c> and vice versa. /// Note that <c>T</c> implicitly converts to <c>StyleProp{T}</c> and vice versa.
/// </summary> /// </summary>
/// <typeparam name="T">The type of style setting that this property stores</typeparam> /// <typeparam name="T">The type of style setting that this property stores</typeparam>
public struct StyleProp<T> { public struct StyleProp<T> : IEquatable<StyleProp<T>> {
/// <summary> /// <summary>
/// The currently applied style /// The currently applied style
/// </summary> /// </summary>
public T Value { get; private set; } public T Value { get; private set; }
private byte lastSetPriority; private byte priority;
/// <summary> /// <summary>
/// Creates a new style property with the given custom style. /// Creates a new style property with the given custom style and a priority of <see cref="byte.MaxValue"/>.
/// </summary> /// </summary>
/// <param name="value">The custom style to apply</param> /// <param name="value">The custom style to apply</param>
public StyleProp(T value) { public StyleProp(T value) {
this.lastSetPriority = byte.MaxValue; this.priority = byte.MaxValue;
this.Value = value; this.Value = value;
} }
@ -33,9 +34,9 @@ namespace MLEM.Ui.Style {
/// <param name="priority">The priority that this style value has. Higher priority style values will override lower priority style values.</param> /// <param name="priority">The priority that this style value has. Higher priority style values will override lower priority style values.</param>
///<seealso cref="CopyFromStyle"/> ///<seealso cref="CopyFromStyle"/>
public void SetFromStyle(T value, byte priority = 0) { public void SetFromStyle(T value, byte priority = 0) {
if (priority >= this.lastSetPriority) { if (priority >= this.priority) {
this.Value = value; this.Value = value;
this.lastSetPriority = priority; this.priority = priority;
} }
} }
@ -70,6 +71,26 @@ namespace MLEM.Ui.Style {
return !EqualityComparer<T>.Default.Equals(this.Value, default); return !EqualityComparer<T>.Default.Equals(this.Value, default);
} }
/// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>true if the current object is equal to the <paramref name="other">other</paramref> parameter; otherwise, false.</returns>
public bool Equals(StyleProp<T> other) {
return EqualityComparer<T>.Default.Equals(this.Value, other.Value);
}
/// <summary>Indicates whether this instance and a specified object are equal.</summary>
/// <param name="obj">The object to compare with the current instance.</param>
/// <returns>true if <paramref name="obj">obj</paramref> and this instance are the same type and represent the same value; otherwise, false.</returns>
public override bool Equals(object obj) {
return obj is StyleProp<T> other && this.Equals(other);
}
/// <summary>Returns the hash code for this instance.</summary>
/// <returns>A 32-bit signed integer that is the hash code for this instance.</returns>
public override int GetHashCode() {
return EqualityComparer<T>.Default.GetHashCode(this.Value);
}
/// <summary>Returns the fully qualified type name of this instance.</summary> /// <summary>Returns the fully qualified type name of this instance.</summary>
/// <returns>The fully qualified type name.</returns> /// <returns>The fully qualified type name.</returns>
public override string ToString() { public override string ToString() {
@ -94,5 +115,25 @@ namespace MLEM.Ui.Style {
return new StyleProp<T>(prop); return new StyleProp<T>(prop);
} }
/// <summary>
/// Compares the two style properties and returns whether they are equal using <see cref="Equals(MLEM.Ui.Style.StyleProp{T})"/>.
/// </summary>
/// <param name="left">The left style property.</param>
/// <param name="right">The right style property.</param>
/// <returns>Whether the two style properties are equal.</returns>
public static bool operator ==(StyleProp<T> left, StyleProp<T> right) {
return left.Equals(right);
}
/// <summary>
/// Compares the two style properties and returns whether they are not equal using <see cref="Equals(MLEM.Ui.Style.StyleProp{T})"/>.
/// </summary>
/// <param name="left">The left style property.</param>
/// <param name="right">The right style property.</param>
/// <returns>Whether the two style properties are not equal.</returns>
public static bool operator !=(StyleProp<T> left, StyleProp<T> right) {
return !left.Equals(right);
}
} }
} }

View file

@ -10,7 +10,7 @@ namespace MLEM.Input {
/// Note that this type is serializable using <see cref="DataContractAttribute"/>. /// Note that this type is serializable using <see cref="DataContractAttribute"/>.
/// </summary> /// </summary>
[DataContract] [DataContract]
public readonly struct GenericInput { public readonly struct GenericInput : IEquatable<GenericInput> {
/// <summary> /// <summary>
/// The <see cref="InputType"/> of this generic input's current <see cref="value"/>. /// The <see cref="InputType"/> of this generic input's current <see cref="value"/>.
@ -40,11 +40,18 @@ namespace MLEM.Input {
} }
} }
/// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>true if the current object is equal to the <paramref name="other">other</paramref> parameter; otherwise, false.</returns>
public bool Equals(GenericInput other) {
return this.Type == other.Type && this.value == other.value;
}
/// <summary>Indicates whether this instance and a specified object are equal.</summary> /// <summary>Indicates whether this instance and a specified object are equal.</summary>
/// <param name="obj">The object to compare with the current instance.</param> /// <param name="obj">The object to compare with the current instance.</param>
/// <returns><see langword="true" /> if <paramref name="obj" /> and this instance are the same type and represent the same value; otherwise, <see langword="false" />.</returns> /// <returns>true if <paramref name="obj">obj</paramref> and this instance are the same type and represent the same value; otherwise, false.</returns>
public override bool Equals(object obj) { public override bool Equals(object obj) {
return obj is GenericInput o && this.Type == o.Type && this.value == o.value; return obj is GenericInput other && this.Equals(other);
} }
/// <summary>Returns the hash code for this instance.</summary> /// <summary>Returns the hash code for this instance.</summary>
@ -54,7 +61,7 @@ namespace MLEM.Input {
} }
/// <summary> /// <summary>
/// Compares the two generic input instances for equality using <see cref="Equals"/> /// Compares the two generic input instances for equality using <see cref="Equals(GenericInput)"/>
/// </summary> /// </summary>
/// <param name="left">The left input</param> /// <param name="left">The left input</param>
/// <param name="right">The right input</param> /// <param name="right">The right input</param>
@ -64,7 +71,7 @@ namespace MLEM.Input {
} }
/// <summary> /// <summary>
/// Compares the two generic input instances for inequality using <see cref="Equals"/> /// Compares the two generic input instances for inequality using <see cref="Equals(GenericInput)"/>
/// </summary> /// </summary>
/// <param name="left">The left input</param> /// <param name="left">The left input</param>
/// <param name="right">The right input</param> /// <param name="right">The right input</param>

View file

@ -1,3 +1,4 @@
using System;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
@ -7,7 +8,7 @@ namespace MLEM.Misc {
/// A padding is an object of data that stores an offset from each side of a rectangle or square. /// A padding is an object of data that stores an offset from each side of a rectangle or square.
/// </summary> /// </summary>
[DataContract] [DataContract]
public struct Padding { public struct Padding : IEquatable<Padding> {
/// <summary> /// <summary>
/// The empty padding, with all borders set to 0 /// The empty padding, with all borders set to 0

View file

@ -179,7 +179,7 @@ namespace MLEM.Pathfinding {
/// A point in a <see cref="AStar{T}"/> path /// A point in a <see cref="AStar{T}"/> path
/// </summary> /// </summary>
/// <typeparam name="T">The type of point used for this path</typeparam> /// <typeparam name="T">The type of point used for this path</typeparam>
public class PathPoint<T> { public class PathPoint<T> : IEquatable<PathPoint<T>> {
/// <summary> /// <summary>
/// The path point that this point originated from /// The path point that this point originated from
@ -214,13 +214,18 @@ namespace MLEM.Pathfinding {
this.F = this.G + distance * defaultCost; this.F = this.G + distance * defaultCost;
} }
/// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>true if the current object is equal to the <paramref name="other">other</paramref> parameter; otherwise, false.</returns>
public bool Equals(PathPoint<T> other) {
return ReferenceEquals(this, other) || EqualityComparer<T>.Default.Equals(this.Pos, other.Pos);
}
/// <summary>Indicates whether this instance and a specified object are equal.</summary> /// <summary>Indicates whether this instance and a specified object are equal.</summary>
/// <param name="obj">The object to compare with the current instance.</param> /// <param name="obj">The object to compare with the current instance.</param>
/// <returns><see langword="true" /> if <paramref name="obj" /> and this instance are the same type and represent the same value; otherwise, <see langword="false" />.</returns> /// <returns><see langword="true" /> if <paramref name="obj" /> and this instance are the same type and represent the same value; otherwise, <see langword="false" />.</returns>
public override bool Equals(object obj) { public override bool Equals(object obj) {
if (obj == this) return obj is PathPoint<T> other && this.Equals(other);
return true;
return obj is PathPoint<T> point && point.Pos.Equals(this.Pos);
} }
/// <summary>Returns the hash code for this instance.</summary> /// <summary>Returns the hash code for this instance.</summary>