2020-07-20 04:12:48 +00:00
|
|
|
using System;
|
2020-07-25 08:36:19 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using ConformalDecals.Util;
|
2020-07-20 04:12:48 +00:00
|
|
|
using TMPro;
|
2020-07-25 08:36:19 +00:00
|
|
|
using UniLinq;
|
2020-11-15 05:11:16 +00:00
|
|
|
using UnityEngine;
|
2020-07-20 04:12:48 +00:00
|
|
|
|
|
|
|
namespace ConformalDecals.Text {
|
2020-11-15 05:11:16 +00:00
|
|
|
public class DecalFont : ScriptableObject, ISerializationCallbackReceiver, IEquatable<DecalFont> {
|
|
|
|
[SerializeField] private string _title;
|
|
|
|
[SerializeField] private TMP_FontAsset _fontAsset;
|
|
|
|
[SerializeField] private FontStyles _fontStyle;
|
|
|
|
[SerializeField] private FontStyles _fontStyleMask;
|
|
|
|
|
2020-09-28 01:26:55 +00:00
|
|
|
/// Human-readable name for the font
|
2020-11-15 05:11:16 +00:00
|
|
|
public string Title => _title;
|
2020-07-25 08:36:19 +00:00
|
|
|
|
2020-09-28 01:26:55 +00:00
|
|
|
/// Internal name for the font
|
2020-11-15 05:11:16 +00:00
|
|
|
public string Name => _fontAsset.name;
|
2020-07-25 08:36:19 +00:00
|
|
|
|
2020-09-28 01:26:55 +00:00
|
|
|
/// The font asset itself
|
2020-11-15 05:11:16 +00:00
|
|
|
public TMP_FontAsset FontAsset => _fontAsset;
|
2020-07-25 08:36:19 +00:00
|
|
|
|
2020-09-28 01:26:55 +00:00
|
|
|
/// Styles that are forced on for this font,
|
|
|
|
/// e.g. smallcaps for a font without lower case characters
|
2020-11-15 05:11:16 +00:00
|
|
|
public FontStyles FontStyle => _fontStyle;
|
2020-07-25 08:36:19 +00:00
|
|
|
|
2020-11-15 05:11:16 +00:00
|
|
|
public bool Bold => (_fontStyle & FontStyles.Bold) != 0;
|
2020-07-25 08:36:19 +00:00
|
|
|
|
2020-11-15 05:11:16 +00:00
|
|
|
public bool Italic => (_fontStyle & FontStyles.Italic) != 0;
|
2020-07-25 08:36:19 +00:00
|
|
|
|
2020-11-15 05:11:16 +00:00
|
|
|
public bool Underline => (_fontStyle & FontStyles.Underline) != 0;
|
2020-07-25 08:36:19 +00:00
|
|
|
|
2020-11-15 05:11:16 +00:00
|
|
|
public bool SmallCaps => (_fontStyle & FontStyles.SmallCaps) != 0;
|
2020-07-25 08:36:19 +00:00
|
|
|
|
2020-09-28 01:26:55 +00:00
|
|
|
/// Styles that are forced off for this font,
|
|
|
|
/// e.g. underline for a font with no underscore character
|
2020-11-15 05:11:16 +00:00
|
|
|
public FontStyles FontStyleMask => _fontStyleMask;
|
2020-07-25 08:36:19 +00:00
|
|
|
|
2020-11-15 05:11:16 +00:00
|
|
|
public bool BoldMask => (_fontStyleMask & FontStyles.Bold) != 0;
|
2020-07-25 08:36:19 +00:00
|
|
|
|
2020-11-15 05:11:16 +00:00
|
|
|
public bool ItalicMask => (_fontStyleMask & FontStyles.Italic) != 0;
|
2020-07-25 08:36:19 +00:00
|
|
|
|
2020-11-15 05:11:16 +00:00
|
|
|
public bool UnderlineMask => (_fontStyleMask & FontStyles.Underline) != 0;
|
2020-07-25 08:36:19 +00:00
|
|
|
|
2020-11-15 05:11:16 +00:00
|
|
|
public bool SmallCapsMask => (_fontStyleMask & FontStyles.SmallCaps) != 0;
|
2020-07-25 08:36:19 +00:00
|
|
|
|
|
|
|
|
2020-12-05 01:05:37 +00:00
|
|
|
public static DecalFont Parse(ConfigNode node, IEnumerable<TMP_FontAsset> fontAssets) {
|
2020-07-25 08:36:19 +00:00
|
|
|
if (node == null) throw new ArgumentNullException(nameof(node));
|
2020-11-13 09:55:42 +00:00
|
|
|
if (fontAssets == null) throw new ArgumentNullException(nameof(fontAssets));
|
|
|
|
|
2020-12-05 01:05:37 +00:00
|
|
|
var font = ScriptableObject.CreateInstance<DecalFont>();
|
|
|
|
|
2020-11-13 09:55:42 +00:00
|
|
|
var name = ParseUtil.ParseString(node, "name");
|
2020-12-05 01:05:37 +00:00
|
|
|
var fontAsset = fontAssets.First(o => o.name == name);
|
|
|
|
if (fontAsset == null) {
|
2020-11-13 09:55:42 +00:00
|
|
|
throw new FormatException($"Could not find font asset named {name}");
|
|
|
|
}
|
2020-07-25 08:36:19 +00:00
|
|
|
|
2020-12-05 01:05:37 +00:00
|
|
|
font._fontAsset = fontAsset;
|
|
|
|
font._title = ParseUtil.ParseString(node, "title", true, name);
|
|
|
|
font._fontStyle = (FontStyles) ParseUtil.ParseInt(node, "style", true);
|
|
|
|
font._fontStyleMask = (FontStyles) ParseUtil.ParseInt(node, "styleMask", true);
|
2020-07-20 04:12:48 +00:00
|
|
|
|
2020-12-05 01:05:37 +00:00
|
|
|
return font;
|
|
|
|
}
|
2020-07-25 08:36:19 +00:00
|
|
|
|
2020-07-20 04:12:48 +00:00
|
|
|
public void SetupSample(TMP_Text tmp) {
|
|
|
|
if (tmp == null) throw new ArgumentNullException(nameof(tmp));
|
2020-07-25 08:36:19 +00:00
|
|
|
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);
|
|
|
|
}
|
2020-07-20 04:12:48 +00:00
|
|
|
|
2020-07-25 08:36:19 +00:00
|
|
|
public static bool operator !=(DecalFont left, DecalFont right) {
|
|
|
|
return !Equals(left, right);
|
2020-07-20 04:12:48 +00:00
|
|
|
}
|
2020-11-15 05:11:16 +00:00
|
|
|
|
|
|
|
public void OnBeforeSerialize() { }
|
|
|
|
|
|
|
|
public void OnAfterDeserialize() { }
|
2020-12-18 00:14:33 +00:00
|
|
|
|
|
|
|
public override string ToString() {
|
|
|
|
return _title;
|
|
|
|
}
|
2020-07-20 04:12:48 +00:00
|
|
|
}
|
|
|
|
}
|