You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
KSP-Conformal-Decals/Source/ConformalDecals/Text/DecalFont.cs

95 lines
3.5 KiB
C#

using System;
using System.Collections.Generic;
using ConformalDecals.Util;
using TMPro;
using UniLinq;
namespace ConformalDecals.Text {
public class DecalFont : IEquatable<DecalFont> {
/// Human-readable name for the font
public string Title { get; }
/// Internal name for the font
public string Name => FontAsset.name;
/// The font asset itself
public TMP_FontAsset FontAsset { get; }
/// Styles that are forced on for this font,
/// e.g. smallcaps for a font without lower case characters
public FontStyles FontStyle { get; }
public bool Bold => (FontStyle & FontStyles.Bold) != 0;
public bool Italic => (FontStyle & FontStyles.Italic) != 0;
public bool Underline => (FontStyle & FontStyles.Underline) != 0;
public bool SmallCaps => (FontStyle & FontStyles.SmallCaps) != 0;
/// Styles that are forced off for this font,
/// e.g. underline for a font with no underscore character
public FontStyles FontStyleMask { get; }
public bool BoldMask => (FontStyleMask & FontStyles.Bold) != 0;
public bool ItalicMask => (FontStyleMask & FontStyles.Italic) != 0;
public bool UnderlineMask => (FontStyleMask & FontStyles.Underline) != 0;
public bool SmallCapsMask => (FontStyleMask & FontStyles.SmallCaps) != 0;
public DecalFont(string name, ConfigNode node, TMP_FontAsset font) {
if (name == null) throw new ArgumentNullException(nameof(name));
if (node == null) throw new ArgumentNullException(nameof(node));
if (font == null) throw new ArgumentNullException(nameof(font));
Title = ParseUtil.ParseString(node, "title", true, name);
FontStyle = (FontStyles) ParseUtil.ParseInt(node, "style", true);
FontStyleMask = (FontStyles) ParseUtil.ParseInt(node, "styleMask", true);
FontAsset = font;
}
public void SetupSample(TMP_Text tmp) {
if (tmp == null) throw new ArgumentNullException(nameof(tmp));
if (FontAsset == null) throw new InvalidOperationException("DecalFont has not been initialized and Font is null.");
tmp.text = Title;
tmp.font = FontAsset;
tmp.fontStyle = FontStyle;
}
public bool Equals(DecalFont other) {
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Title == other.Title && Equals(FontAsset, other.FontAsset) && FontStyle == other.FontStyle && FontStyleMask == other.FontStyleMask;
}
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((DecalFont) obj);
}
public override int GetHashCode() {
unchecked {
var hashCode = (Title != null ? Title.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (FontAsset != null ? FontAsset.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (int) FontStyle;
hashCode = (hashCode * 397) ^ (int) FontStyleMask;
return hashCode;
}
}
public static bool operator ==(DecalFont left, DecalFont right) {
return Equals(left, right);
}
public static bool operator !=(DecalFont left, DecalFont right) {
return !Equals(left, right);
}
}
}