Allow fonts to disable certain unsupported styles
Also clean up some stufffeature-better-tweakables
parent
30870b263a
commit
1c776c0969
@ -1,27 +1,95 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using ConformalDecals.Util;
|
||||||
|
using JetBrains.Annotations;
|
||||||
using TMPro;
|
using TMPro;
|
||||||
|
using UniLinq;
|
||||||
|
|
||||||
namespace ConformalDecals.Text {
|
namespace ConformalDecals.Text {
|
||||||
public class DecalFont {
|
public class DecalFont : IEquatable<DecalFont> {
|
||||||
public readonly string title;
|
public string Title { get; }
|
||||||
public readonly TMP_FontAsset fontAsset;
|
|
||||||
public readonly FontStyles fontStyle;
|
public TMP_FontAsset FontAsset { get; }
|
||||||
|
|
||||||
public DecalFont(string title, TMP_FontAsset fontAsset, FontStyles fontStyle) {
|
public string Name => FontAsset.name;
|
||||||
if (fontAsset == null) throw new ArgumentNullException(nameof(fontAsset));
|
|
||||||
|
|
||||||
this.title = title;
|
public FontStyles FontStyle { get; }
|
||||||
this.fontAsset = fontAsset;
|
|
||||||
this.fontStyle = fontStyle;
|
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;
|
||||||
|
|
||||||
|
|
||||||
|
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([NotNull] ConfigNode node, [NotNull] IEnumerable<TMP_FontAsset> fontAssets) {
|
||||||
|
if (node == null) throw new ArgumentNullException(nameof(node));
|
||||||
|
if (fontAssets == null) throw new ArgumentNullException(nameof(fontAssets));
|
||||||
|
|
||||||
|
var name = ParseUtil.ParseString(node, "name");
|
||||||
|
FontAsset = fontAssets.First(o => o.name == name);
|
||||||
|
if (FontAsset == null) {
|
||||||
|
throw new FormatException($"Could not find font asset named {name}");
|
||||||
|
}
|
||||||
|
|
||||||
|
Title = ParseUtil.ParseString(node, "title", true, name);
|
||||||
|
FontStyle = (FontStyles) ParseUtil.ParseInt(node, "style", true);
|
||||||
|
FontStyleMask = (FontStyles) ParseUtil.ParseInt(node, "styleMask", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void SetupSample(TMP_Text tmp) {
|
public void SetupSample(TMP_Text tmp) {
|
||||||
if (tmp == null) throw new ArgumentNullException(nameof(tmp));
|
if (tmp == null) throw new ArgumentNullException(nameof(tmp));
|
||||||
if (fontAsset == null) throw new InvalidOperationException("DecalFont has not been initialized and Font is null.");
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
tmp.text = title;
|
public static bool operator !=(DecalFont left, DecalFont right) {
|
||||||
tmp.font = fontAsset;
|
return !Equals(left, right);
|
||||||
tmp.fontStyle = fontStyle;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue