Allow fonts to disable certain unsupported styles

Also clean up some stuff
This commit is contained in:
2020-07-25 01:36:19 -07:00
parent 30870b263a
commit 1c776c0969
7 changed files with 130 additions and 50 deletions

View File

@ -1,27 +1,95 @@
using System;
using System.Collections.Generic;
using ConformalDecals.Util;
using JetBrains.Annotations;
using TMPro;
using UniLinq;
namespace ConformalDecals.Text {
public class DecalFont {
public readonly string title;
public readonly TMP_FontAsset fontAsset;
public readonly FontStyles fontStyle;
public class DecalFont : IEquatable<DecalFont> {
public string Title { get; }
public DecalFont(string title, TMP_FontAsset fontAsset, FontStyles fontStyle) {
if (fontAsset == null) throw new ArgumentNullException(nameof(fontAsset));
this.title = title;
this.fontAsset = fontAsset;
this.fontStyle = fontStyle;
public TMP_FontAsset FontAsset { get; }
public string Name => FontAsset.name;
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;
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) {
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;
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);
}
}
}

View File

@ -1,6 +1,7 @@
using System;
using TMPro;
using UnityEngine;
// ReSharper disable NonReadonlyMemberInGetHashCode
namespace ConformalDecals.Text {
public struct DecalTextStyle : IEquatable<DecalTextStyle> {

View File

@ -63,8 +63,8 @@ namespace ConformalDecals.Text {
public void RenderText(DecalText text, out Texture2D texture, out Rect window) {
// SETUP TMP OBJECT FOR RENDERING
_tmp.text = text.FormattedText;
_tmp.font = text.Font.fontAsset;
_tmp.fontStyle = text.Style.FontStyle | text.Font.fontStyle;
_tmp.font = text.Font.FontAsset;
_tmp.fontStyle = text.Style.FontStyle | text.Font.FontStyle;
_tmp.lineSpacing = text.Style.LineSpacing;
_tmp.characterSpacing = text.Style.CharacterSpacing;
@ -76,7 +76,7 @@ namespace ConformalDecals.Text {
_tmp.fontSize = FontSize;
// SETUP BLIT MATERIAL
_blitMaterial.SetTexture(PropertyIDs._MainTex, text.Font.fontAsset.atlas);
_blitMaterial.SetTexture(PropertyIDs._MainTex, text.Font.FontAsset.atlas);
// GENERATE MESH
_tmp.ForceMeshUpdate();