diff --git a/MLEM.Extended/Tiled/LayerPosition.cs b/MLEM.Extended/Tiled/LayerPosition.cs index c7138ff..c47cfc0 100644 --- a/MLEM.Extended/Tiled/LayerPosition.cs +++ b/MLEM.Extended/Tiled/LayerPosition.cs @@ -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 with multiple layers. /// [DataContract] - public struct LayerPosition { + public struct LayerPosition : IEquatable { /// /// The name of the layer that this position is on @@ -36,16 +37,6 @@ namespace MLEM.Extended.Tiled { this.Y = y; } - /// - public static bool operator ==(LayerPosition left, LayerPosition right) { - return left.Equals(right); - } - - /// - public static bool operator !=(LayerPosition left, LayerPosition right) { - return !left.Equals(right); - } - /// 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}"; } + /// + /// Adds the given layer positions together, returning a new layer position with the sum of their coordinates. + /// If the two layer positions' differ, an is thrown. + /// + /// The left position. + /// The right position. + /// The sum of the positions. + /// Thrown if the two positions' are not the same. + 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); + } + + /// + public static bool operator ==(LayerPosition left, LayerPosition right) { + return left.Equals(right); + } + + /// + public static bool operator !=(LayerPosition left, LayerPosition right) { + return !left.Equals(right); + } + + /// + /// Returns the negative of the given layer position. + /// + /// The position to negate. + /// The negative position. + public static LayerPosition operator -(LayerPosition position) { + return new LayerPosition(position.Layer, -position.X, -position.Y); + } + + /// + /// Returns the sum of the two layer positions using . + /// + /// The left position. + /// The right position. + /// The sum of the positions. + public static LayerPosition operator +(LayerPosition left, LayerPosition right) { + return Add(left, right); + } + + /// + /// Subtracts the second from the first position using . + /// + /// The left position. + /// The right position. + /// The difference of the positions. + public static LayerPosition operator -(LayerPosition left, LayerPosition right) { + return Add(left, -right); + } + } } \ No newline at end of file diff --git a/MLEM.Ui/Style/StyleProp.cs b/MLEM.Ui/Style/StyleProp.cs index 946ef6d..87163a9 100644 --- a/MLEM.Ui/Style/StyleProp.cs +++ b/MLEM.Ui/Style/StyleProp.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using MLEM.Ui.Elements; @@ -8,20 +9,20 @@ namespace MLEM.Ui.Style { /// Note that T implicitly converts to StyleProp{T} and vice versa. /// /// The type of style setting that this property stores - public struct StyleProp { + public struct StyleProp : IEquatable> { /// /// The currently applied style /// public T Value { get; private set; } - private byte lastSetPriority; + private byte priority; /// - /// Creates a new style property with the given custom style. + /// Creates a new style property with the given custom style and a priority of . /// /// The custom style to apply public StyleProp(T value) { - this.lastSetPriority = byte.MaxValue; + this.priority = byte.MaxValue; this.Value = value; } @@ -33,9 +34,9 @@ namespace MLEM.Ui.Style { /// The priority that this style value has. Higher priority style values will override lower priority style values. /// 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.Default.Equals(this.Value, default); } + /// Indicates whether the current object is equal to another object of the same type. + /// An object to compare with this object. + /// true if the current object is equal to the other parameter; otherwise, false. + public bool Equals(StyleProp other) { + return EqualityComparer.Default.Equals(this.Value, other.Value); + } + + /// Indicates whether this instance and a specified object are equal. + /// The object to compare with the current instance. + /// true if obj and this instance are the same type and represent the same value; otherwise, false. + public override bool Equals(object obj) { + return obj is StyleProp other && this.Equals(other); + } + + /// Returns the hash code for this instance. + /// A 32-bit signed integer that is the hash code for this instance. + public override int GetHashCode() { + return EqualityComparer.Default.GetHashCode(this.Value); + } + /// Returns the fully qualified type name of this instance. /// The fully qualified type name. public override string ToString() { @@ -94,5 +115,25 @@ namespace MLEM.Ui.Style { return new StyleProp(prop); } + /// + /// Compares the two style properties and returns whether they are equal using . + /// + /// The left style property. + /// The right style property. + /// Whether the two style properties are equal. + public static bool operator ==(StyleProp left, StyleProp right) { + return left.Equals(right); + } + + /// + /// Compares the two style properties and returns whether they are not equal using . + /// + /// The left style property. + /// The right style property. + /// Whether the two style properties are not equal. + public static bool operator !=(StyleProp left, StyleProp right) { + return !left.Equals(right); + } + } } \ No newline at end of file diff --git a/MLEM/Input/GenericInput.cs b/MLEM/Input/GenericInput.cs index 3d3584e..871b651 100644 --- a/MLEM/Input/GenericInput.cs +++ b/MLEM/Input/GenericInput.cs @@ -10,7 +10,7 @@ namespace MLEM.Input { /// Note that this type is serializable using . /// [DataContract] - public readonly struct GenericInput { + public readonly struct GenericInput : IEquatable { /// /// The of this generic input's current . @@ -40,11 +40,18 @@ namespace MLEM.Input { } } + /// Indicates whether the current object is equal to another object of the same type. + /// An object to compare with this object. + /// true if the current object is equal to the other parameter; otherwise, false. + public bool Equals(GenericInput other) { + return this.Type == other.Type && this.value == other.value; + } + /// Indicates whether this instance and a specified object are equal. /// The object to compare with the current instance. - /// if and this instance are the same type and represent the same value; otherwise, . + /// true if obj and this instance are the same type and represent the same value; otherwise, false. 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); } /// Returns the hash code for this instance. @@ -54,7 +61,7 @@ namespace MLEM.Input { } /// - /// Compares the two generic input instances for equality using + /// Compares the two generic input instances for equality using /// /// The left input /// The right input @@ -64,7 +71,7 @@ namespace MLEM.Input { } /// - /// Compares the two generic input instances for inequality using + /// Compares the two generic input instances for inequality using /// /// The left input /// The right input diff --git a/MLEM/Misc/Padding.cs b/MLEM/Misc/Padding.cs index e8185e0..44692c9 100644 --- a/MLEM/Misc/Padding.cs +++ b/MLEM/Misc/Padding.cs @@ -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. /// [DataContract] - public struct Padding { + public struct Padding : IEquatable { /// /// The empty padding, with all borders set to 0 diff --git a/MLEM/Pathfinding/AStar.cs b/MLEM/Pathfinding/AStar.cs index eed1717..bc719c3 100644 --- a/MLEM/Pathfinding/AStar.cs +++ b/MLEM/Pathfinding/AStar.cs @@ -179,7 +179,7 @@ namespace MLEM.Pathfinding { /// A point in a path /// /// The type of point used for this path - public class PathPoint { + public class PathPoint : IEquatable> { /// /// The path point that this point originated from @@ -214,13 +214,18 @@ namespace MLEM.Pathfinding { this.F = this.G + distance * defaultCost; } + /// Indicates whether the current object is equal to another object of the same type. + /// An object to compare with this object. + /// true if the current object is equal to the other parameter; otherwise, false. + public bool Equals(PathPoint other) { + return ReferenceEquals(this, other) || EqualityComparer.Default.Equals(this.Pos, other.Pos); + } + /// Indicates whether this instance and a specified object are equal. /// The object to compare with the current instance. /// if and this instance are the same type and represent the same value; otherwise, . public override bool Equals(object obj) { - if (obj == this) - return true; - return obj is PathPoint point && point.Pos.Equals(this.Pos); + return obj is PathPoint other && this.Equals(other); } /// Returns the hash code for this instance.