From da3bcf78194a68d807b448ac25d2f0d0b3317f86 Mon Sep 17 00:00:00 2001 From: drewcassidy Date: Sun, 26 Jul 2020 19:30:19 -0700 Subject: [PATCH] text input changes --- Source/ConformalDecals/DecalConfig.cs | 2 +- Source/ConformalDecals/Text/DecalFont.cs | 3 +- Source/ConformalDecals/Text/DecalTextStyle.cs | 59 +++++-------- .../ConformalDecals/UI/TextEntryController.cs | 86 ++++++++++++++++--- 4 files changed, 97 insertions(+), 53 deletions(-) diff --git a/Source/ConformalDecals/DecalConfig.cs b/Source/ConformalDecals/DecalConfig.cs index 13ff10d..c5bbcae 100644 --- a/Source/ConformalDecals/DecalConfig.cs +++ b/Source/ConformalDecals/DecalConfig.cs @@ -97,7 +97,7 @@ namespace ConformalDecals { foreach (var fontNode in node.GetNodes("FONT")) { try { - var font = new DecalFont(node, allFonts); + var font = new DecalFont(fontNode, allFonts); _fontList.Add(font.Name, font); } catch (Exception e) { diff --git a/Source/ConformalDecals/Text/DecalFont.cs b/Source/ConformalDecals/Text/DecalFont.cs index e2d4281..45ec8f7 100644 --- a/Source/ConformalDecals/Text/DecalFont.cs +++ b/Source/ConformalDecals/Text/DecalFont.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using ConformalDecals.Util; -using JetBrains.Annotations; using TMPro; using UniLinq; @@ -36,7 +35,7 @@ namespace ConformalDecals.Text { public bool SmallCapsMask => (FontStyleMask & FontStyles.SmallCaps) != 0; - public DecalFont([NotNull] ConfigNode node, [NotNull] IEnumerable fontAssets) { + public DecalFont(ConfigNode node, IEnumerable fontAssets) { if (node == null) throw new ArgumentNullException(nameof(node)); if (fontAssets == null) throw new ArgumentNullException(nameof(fontAssets)); diff --git a/Source/ConformalDecals/Text/DecalTextStyle.cs b/Source/ConformalDecals/Text/DecalTextStyle.cs index 2611292..6d30103 100644 --- a/Source/ConformalDecals/Text/DecalTextStyle.cs +++ b/Source/ConformalDecals/Text/DecalTextStyle.cs @@ -1,16 +1,14 @@ using System; -using ConformalDecals.Util; using TMPro; using UnityEngine; - // ReSharper disable NonReadonlyMemberInGetHashCode namespace ConformalDecals.Text { - public class DecalTextStyle : ScriptableObject, IEquatable { + public struct DecalTextStyle : IEquatable { private FontStyles _fontStyle; - private bool _vertical; - private float _lineSpacing; - private float _characterSpacing; + private bool _vertical; + private float _lineSpacing; + private float _characterSpacing; public FontStyles FontStyle { get => _fontStyle; @@ -63,55 +61,42 @@ namespace ConformalDecals.Text { get => _characterSpacing; set => _characterSpacing = value; } - - public static DecalTextStyle Load(ConfigNode node) { - var style = CreateInstance(); - style._fontStyle = (FontStyles) ParseUtil.ParseInt(node, "fontStyle", true); - style._vertical = ParseUtil.ParseBool(node, "vertical", true); - style._lineSpacing = ParseUtil.ParseFloat(node, "lineSpacing", true); - style._characterSpacing = ParseUtil.ParseFloat(node, "characterSpacing", true); - return style; - } - - public ConfigNode Save() { - var node = new ConfigNode("STYLE"); - node.AddValue("fontStyle", (int) _fontStyle); - node.AddValue("vertical", _vertical); - node.AddValue("lineSpacing", _lineSpacing); - node.AddValue("characterSpacing", _characterSpacing); - return node; + + + public DecalTextStyle(FontStyles fontStyle, bool vertical, float lineSpacing, float characterSpacing) { + _fontStyle = fontStyle; + _vertical = vertical; + _lineSpacing = lineSpacing; + _characterSpacing = characterSpacing; } + public bool Equals(DecalTextStyle other) { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return base.Equals(other) && _fontStyle == other._fontStyle && _vertical == other._vertical && _lineSpacing.Equals(other._lineSpacing) && _characterSpacing.Equals(other._characterSpacing); + return FontStyle == other.FontStyle && Vertical == other.Vertical && + Mathf.Approximately(LineSpacing, other.LineSpacing) && + Mathf.Approximately(CharacterSpacing, other.CharacterSpacing); } 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((DecalTextStyle) obj); + return obj is DecalTextStyle other && Equals(other); } public override int GetHashCode() { unchecked { - int hashCode = base.GetHashCode(); - hashCode = (hashCode * 397) ^ (int) _fontStyle; - hashCode = (hashCode * 397) ^ _vertical.GetHashCode(); - hashCode = (hashCode * 397) ^ _lineSpacing.GetHashCode(); - hashCode = (hashCode * 397) ^ _characterSpacing.GetHashCode(); + var hashCode = (int) FontStyle; + hashCode = (hashCode * 397) ^ Vertical.GetHashCode(); + hashCode = (hashCode * 397) ^ LineSpacing.GetHashCode(); + hashCode = (hashCode * 397) ^ CharacterSpacing.GetHashCode(); return hashCode; } } public static bool operator ==(DecalTextStyle left, DecalTextStyle right) { - return Equals(left, right); + return left.Equals(right); } public static bool operator !=(DecalTextStyle left, DecalTextStyle right) { - return !Equals(left, right); + return !left.Equals(right); } } } \ No newline at end of file diff --git a/Source/ConformalDecals/UI/TextEntryController.cs b/Source/ConformalDecals/UI/TextEntryController.cs index c8c33c3..ffffc73 100644 --- a/Source/ConformalDecals/UI/TextEntryController.cs +++ b/Source/ConformalDecals/UI/TextEntryController.cs @@ -27,6 +27,8 @@ namespace ConformalDecals.UI { private FontMenuController _fontMenu; + private bool _ignoreUpdates; + public static TextEntryController Create(string text, DecalFont font, DecalTextStyle style, UnityAction textUpdateCallback) { var window = Instantiate(UILoader.TextEntryPrefab, MainCanvasUtil.MainCanvas.transform, true); @@ -42,7 +44,6 @@ namespace ConformalDecals.UI { return controller; } - public void OnClose() { if (_fontMenu != null) _fontMenu.OnClose(); Destroy(gameObject); @@ -59,6 +60,8 @@ namespace ConformalDecals.UI { } public void OnFontUpdate(DecalFont font) { + if (_ignoreUpdates) return; + _font = font; font.SetupSample(_fontButton.GetComponentInChildren()); @@ -71,45 +74,49 @@ namespace ConformalDecals.UI { } public void OnBoldUpdate(bool state) { - _style.Bold = state; + if (_ignoreUpdates) return; + _style.Bold = state; OnValueChanged(); } public void OnItalicUpdate(bool state) { + if (_ignoreUpdates) return; + _style.Italic = state; OnValueChanged(); } public void OnUnderlineUpdate(bool state) { + if (_ignoreUpdates) return; + _style.Underline = state; OnValueChanged(); } public void OnSmallCapsUpdate(bool state) { + if (_ignoreUpdates) return; + _style.SmallCaps = state; OnValueChanged(); } public void OnVerticalUpdate(bool state) { + if (_ignoreUpdates) return; + _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(); } @@ -118,10 +125,63 @@ namespace ConformalDecals.UI { } 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; + _ignoreUpdates = true; + + if (_font.Bold) { + _boldButton.interactable = false; + _boldButton.isOn = true; + } + else if (_font.BoldMask) { + _boldButton.interactable = false; + _boldButton.isOn = false; + } + else { + _boldButton.interactable = true; + _boldButton.isOn = _style.Bold; + } + + if (_font.Italic) { + _italicButton.interactable = false; + _italicButton.isOn = true; + } + else if (_font.ItalicMask) { + _italicButton.interactable = false; + _italicButton.isOn = false; + } + else { + _italicButton.interactable = true; + _italicButton.isOn = _style.Italic; + } + + if (_font.Underline) { + _underlineButton.interactable = false; + _underlineButton.isOn = true; + } + else if (_font.UnderlineMask) { + _underlineButton.interactable = false; + _underlineButton.isOn = false; + } + else { + _underlineButton.interactable = true; + _underlineButton.isOn = _style.Underline; + } + + if (_font.SmallCaps) { + _smallCapsButton.interactable = false; + _smallCapsButton.isOn = true; + } + else if (_font.SmallCapsMask) { + _smallCapsButton.interactable = false; + _smallCapsButton.isOn = false; + } + else { + _smallCapsButton.interactable = true; + _smallCapsButton.isOn = _style.SmallCaps; + } + + _verticalButton.isOn = _style.Vertical; + + _ignoreUpdates = false; } } } \ No newline at end of file