mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-12-24 17:29:23 +01:00
Implemented IEquatable on a lot of classes that were missing it
This commit is contained in:
parent
3541b8d3e1
commit
c0ce5a07ad
5 changed files with 125 additions and 27 deletions
|
@ -1,3 +1,4 @@
|
|||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
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.
|
||||
/// </summary>
|
||||
[DataContract]
|
||||
public struct LayerPosition {
|
||||
public struct LayerPosition : IEquatable<LayerPosition> {
|
||||
|
||||
/// <summary>
|
||||
/// The name of the layer that this position is on
|
||||
|
@ -36,16 +37,6 @@ namespace MLEM.Extended.Tiled {
|
|||
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)"/>
|
||||
public bool Equals(LayerPosition other) {
|
||||
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}";
|
||||
}
|
||||
|
||||
/// <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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
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.
|
||||
/// </summary>
|
||||
/// <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>
|
||||
/// The currently applied style
|
||||
/// </summary>
|
||||
public T Value { get; private set; }
|
||||
private byte lastSetPriority;
|
||||
private byte priority;
|
||||
|
||||
/// <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>
|
||||
/// <param name="value">The custom style to apply</param>
|
||||
public StyleProp(T value) {
|
||||
this.lastSetPriority = byte.MaxValue;
|
||||
this.priority = byte.MaxValue;
|
||||
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>
|
||||
///<seealso cref="CopyFromStyle"/>
|
||||
public void SetFromStyle(T value, byte priority = 0) {
|
||||
if (priority >= this.lastSetPriority) {
|
||||
if (priority >= this.priority) {
|
||||
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);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
/// <returns>The fully qualified type name.</returns>
|
||||
public override string ToString() {
|
||||
|
@ -94,5 +115,25 @@ namespace MLEM.Ui.Style {
|
|||
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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -10,7 +10,7 @@ namespace MLEM.Input {
|
|||
/// Note that this type is serializable using <see cref="DataContractAttribute"/>.
|
||||
/// </summary>
|
||||
[DataContract]
|
||||
public readonly struct GenericInput {
|
||||
public readonly struct GenericInput : IEquatable<GenericInput> {
|
||||
|
||||
/// <summary>
|
||||
/// 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>
|
||||
/// <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) {
|
||||
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>
|
||||
|
@ -54,7 +61,7 @@ namespace MLEM.Input {
|
|||
}
|
||||
|
||||
/// <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>
|
||||
/// <param name="left">The left input</param>
|
||||
/// <param name="right">The right input</param>
|
||||
|
@ -64,7 +71,7 @@ namespace MLEM.Input {
|
|||
}
|
||||
|
||||
/// <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>
|
||||
/// <param name="left">The left input</param>
|
||||
/// <param name="right">The right input</param>
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
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.
|
||||
/// </summary>
|
||||
[DataContract]
|
||||
public struct Padding {
|
||||
public struct Padding : IEquatable<Padding> {
|
||||
|
||||
/// <summary>
|
||||
/// The empty padding, with all borders set to 0
|
||||
|
|
|
@ -179,7 +179,7 @@ namespace MLEM.Pathfinding {
|
|||
/// A point in a <see cref="AStar{T}"/> path
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of point used for this path</typeparam>
|
||||
public class PathPoint<T> {
|
||||
public class PathPoint<T> : IEquatable<PathPoint<T>> {
|
||||
|
||||
/// <summary>
|
||||
/// The path point that this point originated from
|
||||
|
@ -214,13 +214,18 @@ namespace MLEM.Pathfinding {
|
|||
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>
|
||||
/// <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>
|
||||
public override bool Equals(object obj) {
|
||||
if (obj == this)
|
||||
return true;
|
||||
return obj is PathPoint<T> point && point.Pos.Equals(this.Pos);
|
||||
return obj is PathPoint<T> other && this.Equals(other);
|
||||
}
|
||||
|
||||
/// <summary>Returns the hash code for this instance.</summary>
|
||||
|
|
Loading…
Reference in a new issue