KSP-Conformal-Decals/Source/ConformalDecals/Text/DecalText.cs

64 lines
2.1 KiB
C#
Raw Normal View History

2020-07-20 04:12:48 +00:00
using System;
2020-07-24 21:39:35 +00:00
using System.Text.RegularExpressions;
2020-07-20 04:12:48 +00:00
namespace ConformalDecals.Text {
2020-07-24 21:39:35 +00:00
public class DecalText : IEquatable<DecalText> {
2020-09-28 01:26:55 +00:00
/// Raw text contents
2020-07-24 21:39:35 +00:00
public string Text { get; }
2020-09-28 01:26:55 +00:00
/// Font asset used by this text snippet
2020-07-24 21:39:35 +00:00
public DecalFont Font { get; }
2020-09-28 01:26:55 +00:00
/// Style used by this text snippet
2020-07-24 21:39:35 +00:00
public DecalTextStyle Style { get; }
2020-09-28 01:26:55 +00:00
/// The text formatted with newlines for vertical text
2020-07-24 21:39:35 +00:00
public string FormattedText {
get {
if (Style.Vertical) {
return Regex.Replace(Text, @"(.)", "$1\n");
}
else {
return Text;
}
}
}
public DecalText(string text, DecalFont font, DecalTextStyle style) {
if (font == null) throw new ArgumentNullException(nameof(font));
2020-07-24 21:39:35 +00:00
Text = text;
Font = font;
Style = style;
}
public bool Equals(DecalText other) {
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Text == other.Text && Equals(Font, other.Font) && Style.Equals(other.Style);
2020-07-24 21:39:35 +00:00
}
public override bool Equals(object obj) {
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((DecalText) obj);
2020-07-24 21:39:35 +00:00
}
public override int GetHashCode() {
unchecked {
var hashCode = (Text != null ? Text.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (Font != null ? Font.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ Style.GetHashCode();
return hashCode;
}
}
public static bool operator ==(DecalText left, DecalText right) {
return Equals(left, right);
2020-07-24 21:39:35 +00:00
}
public static bool operator !=(DecalText left, DecalText right) {
return !Equals(left, right);
2020-07-24 21:39:35 +00:00
}
2020-07-20 04:12:48 +00:00
}
}