text input changes

feature-better-tweakables
Andrew Cassidy 4 years ago
parent f367a30dec
commit da3bcf7819

@ -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) {

@ -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<TMP_FontAsset> fontAssets) {
public DecalFont(ConfigNode node, IEnumerable<TMP_FontAsset> fontAssets) {
if (node == null) throw new ArgumentNullException(nameof(node));
if (fontAssets == null) throw new ArgumentNullException(nameof(fontAssets));

@ -1,16 +1,14 @@
using System;
using ConformalDecals.Util;
using TMPro;
using UnityEngine;
// ReSharper disable NonReadonlyMemberInGetHashCode
namespace ConformalDecals.Text {
public class DecalTextStyle : ScriptableObject, IEquatable<DecalTextStyle> {
public struct DecalTextStyle : IEquatable<DecalTextStyle> {
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<DecalTextStyle>();
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);
}
}
}

@ -27,6 +27,8 @@ namespace ConformalDecals.UI {
private FontMenuController _fontMenu;
private bool _ignoreUpdates;
public static TextEntryController Create(string text, DecalFont font, DecalTextStyle style, UnityAction<string, DecalFont, DecalTextStyle> 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<TextMeshProUGUI>());
@ -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<TextMeshProUGUI>());
_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;
}
}
}
Loading…
Cancel
Save