diff --git a/GameData/ConformalDecals/Resources/ConformalDecals.cfg b/GameData/ConformalDecals/Resources/ConformalDecals.cfg index 6076358..afc1281 100644 --- a/GameData/ConformalDecals/Resources/ConformalDecals.cfg +++ b/GameData/ConformalDecals/Resources/ConformalDecals.cfg @@ -39,6 +39,7 @@ CONFORMALDECALS { FONT { name = Dited SDF title = Dited + styleMask = 4 } FONT { @@ -59,6 +60,7 @@ CONFORMALDECALS { FONT { name = amarurgt SDF title = Amarillo USAF - style = 16 + style = 32 + styleMask = 4 } } \ No newline at end of file diff --git a/Source/ConformalDecals/DecalConfig.cs b/Source/ConformalDecals/DecalConfig.cs index d019383..13ff10d 100644 --- a/Source/ConformalDecals/DecalConfig.cs +++ b/Source/ConformalDecals/DecalConfig.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using ConformalDecals.Text; using ConformalDecals.Util; @@ -95,17 +96,13 @@ namespace ConformalDecals { var allFonts = Resources.FindObjectsOfTypeAll(); foreach (var fontNode in node.GetNodes("FONT")) { - var name = ParseUtil.ParseString(fontNode, "name"); - var title = ParseUtil.ParseString(fontNode, "title", true, name); - var style = ParseUtil.ParseInt(fontNode, "style", true); - - var font = allFonts.First(o => o.name == name); - if (font == null) { - Debug.LogWarning($"[ConformalDecals] Could not found named {name}"); - } - - Debug.Log($"Adding font named {name}"); - _fontList.Add(name, new DecalFont(title, font, (FontStyles) style)); + try { + var font = new DecalFont(node, allFonts); + _fontList.Add(font.Name, font); + } + catch (Exception e) { + Debug.LogException(e); + } } } diff --git a/Source/ConformalDecals/Text/DecalFont.cs b/Source/ConformalDecals/Text/DecalFont.cs index 936a44b..e2d4281 100644 --- a/Source/ConformalDecals/Text/DecalFont.cs +++ b/Source/ConformalDecals/Text/DecalFont.cs @@ -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 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 class DecalFont : IEquatable { + public string Title { get; } + + 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 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; + } + + 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; - tmp.font = fontAsset; - tmp.fontStyle = fontStyle; + public static bool operator !=(DecalFont left, DecalFont right) { + return !Equals(left, right); } } } \ No newline at end of file diff --git a/Source/ConformalDecals/Text/DecalTextStyle.cs b/Source/ConformalDecals/Text/DecalTextStyle.cs index 459fed2..147642f 100644 --- a/Source/ConformalDecals/Text/DecalTextStyle.cs +++ b/Source/ConformalDecals/Text/DecalTextStyle.cs @@ -1,6 +1,7 @@ using System; using TMPro; using UnityEngine; +// ReSharper disable NonReadonlyMemberInGetHashCode namespace ConformalDecals.Text { public struct DecalTextStyle : IEquatable { diff --git a/Source/ConformalDecals/Text/TextRenderer.cs b/Source/ConformalDecals/Text/TextRenderer.cs index 5c46bbb..aee2b59 100644 --- a/Source/ConformalDecals/Text/TextRenderer.cs +++ b/Source/ConformalDecals/Text/TextRenderer.cs @@ -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(); diff --git a/Source/ConformalDecals/UI/FontMenuController.cs b/Source/ConformalDecals/UI/FontMenuController.cs index c01003b..8a9a0f3 100644 --- a/Source/ConformalDecals/UI/FontMenuController.cs +++ b/Source/ConformalDecals/UI/FontMenuController.cs @@ -45,10 +45,10 @@ namespace ConformalDecals.UI { Toggle active = null; - foreach (var font in fonts.OrderBy(x => x.title)) { - Debug.Log(font.title); + foreach (var font in fonts.OrderBy(x => x.Title)) { + Debug.Log(font.Title); var listItem = GameObject.Instantiate(_menuItem, _menuList.transform); - listItem.name = font.title; + listItem.name = font.Title; listItem.SetActive(true); var fontItem = listItem.AddComponent(); diff --git a/Source/ConformalDecals/UI/TextEntryController.cs b/Source/ConformalDecals/UI/TextEntryController.cs index 0f06d18..c8c33c3 100644 --- a/Source/ConformalDecals/UI/TextEntryController.cs +++ b/Source/ConformalDecals/UI/TextEntryController.cs @@ -35,33 +35,19 @@ namespace ConformalDecals.UI { var controller = window.GetComponent(); controller._text = text; + controller._font = font; + controller._style = style; controller.onValueChanged.AddListener(textUpdateCallback); return controller; } - private void Start() { - ((TMP_InputField) _textBox).text = _text; - - _font.SetupSample(_fontButton.GetComponentInChildren()); - - _boldButton.isOn = _style.Bold; - _italicButton.isOn = _style.Italic; - _underlineButton.isOn = _style.Underline; - _smallCapsButton.isOn = _style.SmallCaps; - _verticalButton.isOn = _style.Vertical; - - } public void OnClose() { if (_fontMenu != null) _fontMenu.OnClose(); Destroy(gameObject); } - public void OnValueChanged() { - onValueChanged.Invoke(_text, _font, _style); - } - public void OnTextUpdate(string newText) { this._text = newText; @@ -77,9 +63,10 @@ namespace ConformalDecals.UI { font.SetupSample(_fontButton.GetComponentInChildren()); var textBox = ((TMP_InputField) _textBox); - textBox.textComponent.fontStyle = _style.FontStyle | _font.fontStyle; - textBox.fontAsset = _font.fontAsset; + textBox.textComponent.fontStyle = _style.FontStyle | _font.FontStyle; + textBox.fontAsset = _font.FontAsset; + UpdateStyleButtons(); OnValueChanged(); } @@ -111,5 +98,30 @@ namespace ConformalDecals.UI { _style.Vertical = state; OnValueChanged(); } + + + private void Start() { + ((TMP_InputField) _textBox).text = _text; + + _font.SetupSample(_fontButton.GetComponentInChildren()); + + _boldButton.isOn = _style.Bold; + _italicButton.isOn = _style.Italic; + _underlineButton.isOn = _style.Underline; + _smallCapsButton.isOn = _style.SmallCaps; + _verticalButton.isOn = _style.Vertical; + UpdateStyleButtons(); + } + + private void OnValueChanged() { + onValueChanged.Invoke(_text, _font, _style); + } + + private void UpdateStyleButtons() { + _boldButton.interactable = !_font.Bold && !_font.BoldMask; + _italicButton.interactable = !_font.Italic && !_font.ItalicMask; + _underlineButton.interactable = !_font.Underline && !_font.UnderlineMask; + _smallCapsButton.interactable = !_font.SmallCaps && !_font.SmallCapsMask; + } } } \ No newline at end of file