diff --git a/Source/ConformalDecals/Text/DecalTextStyle.cs b/Source/ConformalDecals/Text/DecalTextStyle.cs index 147642f..2611292 100644 --- a/Source/ConformalDecals/Text/DecalTextStyle.cs +++ b/Source/ConformalDecals/Text/DecalTextStyle.cs @@ -1,11 +1,21 @@ using System; +using ConformalDecals.Util; using TMPro; using UnityEngine; + // ReSharper disable NonReadonlyMemberInGetHashCode namespace ConformalDecals.Text { - public struct DecalTextStyle : IEquatable { - public FontStyles FontStyle { get; set; } + public class DecalTextStyle : ScriptableObject, IEquatable { + private FontStyles _fontStyle; + private bool _vertical; + private float _lineSpacing; + private float _characterSpacing; + + public FontStyles FontStyle { + get => _fontStyle; + set => _fontStyle = value; + } public bool Bold { get => (FontStyle & FontStyles.Bold) != 0; @@ -39,38 +49,69 @@ namespace ConformalDecals.Text { } } - public bool Vertical { get; set; } + public bool Vertical { + get => _vertical; + set => _vertical = value; + } + + public float LineSpacing { + get => _lineSpacing; + set => _lineSpacing = value; + } - public float LineSpacing { get; set; } + public float CharacterSpacing { + get => _characterSpacing; + set => _characterSpacing = value; + } - public float CharacterSpacing { get; set; } + public static DecalTextStyle Load(ConfigNode node) { + var style = CreateInstance(); + style._fontStyle = (FontStyles) ParseUtil.ParseInt(node, "fontStyle", true); + style._vertical = ParseUtil.ParseBool(node, "vertical", true); + style._lineSpacing = ParseUtil.ParseFloat(node, "lineSpacing", true); + style._characterSpacing = ParseUtil.ParseFloat(node, "characterSpacing", true); + return style; + } + + public ConfigNode Save() { + var node = new ConfigNode("STYLE"); + node.AddValue("fontStyle", (int) _fontStyle); + node.AddValue("vertical", _vertical); + node.AddValue("lineSpacing", _lineSpacing); + node.AddValue("characterSpacing", _characterSpacing); + return node; + } public bool Equals(DecalTextStyle other) { - return FontStyle == other.FontStyle && Vertical == other.Vertical && - Mathf.Approximately(LineSpacing, other.LineSpacing) && - Mathf.Approximately(CharacterSpacing, other.CharacterSpacing); + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return base.Equals(other) && _fontStyle == other._fontStyle && _vertical == other._vertical && _lineSpacing.Equals(other._lineSpacing) && _characterSpacing.Equals(other._characterSpacing); } public override bool Equals(object obj) { - return obj is DecalTextStyle other && Equals(other); + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) return false; + return Equals((DecalTextStyle) obj); } public override int GetHashCode() { unchecked { - var hashCode = (int) FontStyle; - hashCode = (hashCode * 397) ^ Vertical.GetHashCode(); - hashCode = (hashCode * 397) ^ LineSpacing.GetHashCode(); - hashCode = (hashCode * 397) ^ CharacterSpacing.GetHashCode(); + int hashCode = base.GetHashCode(); + hashCode = (hashCode * 397) ^ (int) _fontStyle; + hashCode = (hashCode * 397) ^ _vertical.GetHashCode(); + hashCode = (hashCode * 397) ^ _lineSpacing.GetHashCode(); + hashCode = (hashCode * 397) ^ _characterSpacing.GetHashCode(); return hashCode; } } public static bool operator ==(DecalTextStyle left, DecalTextStyle right) { - return left.Equals(right); + return Equals(left, right); } public static bool operator !=(DecalTextStyle left, DecalTextStyle right) { - return !left.Equals(right); + return !Equals(left, right); } } } \ No newline at end of file