mirror of
https://github.com/drewcassidy/KSP-Conformal-Decals.git
synced 2024-09-01 18:23:54 +00:00
Allow fonts to disable certain unsupported styles
Also clean up some stuff
This commit is contained in:
parent
30870b263a
commit
1c776c0969
@ -39,6 +39,7 @@ CONFORMALDECALS {
|
|||||||
FONT {
|
FONT {
|
||||||
name = Dited SDF
|
name = Dited SDF
|
||||||
title = Dited
|
title = Dited
|
||||||
|
styleMask = 4
|
||||||
}
|
}
|
||||||
|
|
||||||
FONT {
|
FONT {
|
||||||
@ -59,6 +60,7 @@ CONFORMALDECALS {
|
|||||||
FONT {
|
FONT {
|
||||||
name = amarurgt SDF
|
name = amarurgt SDF
|
||||||
title = Amarillo USAF
|
title = Amarillo USAF
|
||||||
style = 16
|
style = 32
|
||||||
|
styleMask = 4
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,3 +1,4 @@
|
|||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using ConformalDecals.Text;
|
using ConformalDecals.Text;
|
||||||
using ConformalDecals.Util;
|
using ConformalDecals.Util;
|
||||||
@ -95,17 +96,13 @@ namespace ConformalDecals {
|
|||||||
var allFonts = Resources.FindObjectsOfTypeAll<TMP_FontAsset>();
|
var allFonts = Resources.FindObjectsOfTypeAll<TMP_FontAsset>();
|
||||||
|
|
||||||
foreach (var fontNode in node.GetNodes("FONT")) {
|
foreach (var fontNode in node.GetNodes("FONT")) {
|
||||||
var name = ParseUtil.ParseString(fontNode, "name");
|
try {
|
||||||
var title = ParseUtil.ParseString(fontNode, "title", true, name);
|
var font = new DecalFont(node, allFonts);
|
||||||
var style = ParseUtil.ParseInt(fontNode, "style", true);
|
_fontList.Add(font.Name, font);
|
||||||
|
}
|
||||||
var font = allFonts.First(o => o.name == name);
|
catch (Exception e) {
|
||||||
if (font == null) {
|
Debug.LogException(e);
|
||||||
Debug.LogWarning($"[ConformalDecals] Could not found named {name}");
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Debug.Log($"Adding font named {name}");
|
|
||||||
_fontList.Add(name, new DecalFont(title, font, (FontStyles) style));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,27 +1,95 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using ConformalDecals.Util;
|
||||||
|
using JetBrains.Annotations;
|
||||||
using TMPro;
|
using TMPro;
|
||||||
|
using UniLinq;
|
||||||
|
|
||||||
namespace ConformalDecals.Text {
|
namespace ConformalDecals.Text {
|
||||||
public class DecalFont {
|
public class DecalFont : IEquatable<DecalFont> {
|
||||||
public readonly string title;
|
public string Title { get; }
|
||||||
public readonly TMP_FontAsset fontAsset;
|
|
||||||
public readonly FontStyles fontStyle;
|
|
||||||
|
|
||||||
public DecalFont(string title, TMP_FontAsset fontAsset, FontStyles fontStyle) {
|
public TMP_FontAsset FontAsset { get; }
|
||||||
if (fontAsset == null) throw new ArgumentNullException(nameof(fontAsset));
|
|
||||||
|
|
||||||
this.title = title;
|
public string Name => FontAsset.name;
|
||||||
this.fontAsset = fontAsset;
|
|
||||||
this.fontStyle = fontStyle;
|
|
||||||
|
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) {
|
public void SetupSample(TMP_Text tmp) {
|
||||||
if (tmp == null) throw new ArgumentNullException(nameof(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.text = Title;
|
||||||
tmp.font = fontAsset;
|
tmp.font = FontAsset;
|
||||||
tmp.fontStyle = fontStyle;
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using TMPro;
|
using TMPro;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
// ReSharper disable NonReadonlyMemberInGetHashCode
|
||||||
|
|
||||||
namespace ConformalDecals.Text {
|
namespace ConformalDecals.Text {
|
||||||
public struct DecalTextStyle : IEquatable<DecalTextStyle> {
|
public struct DecalTextStyle : IEquatable<DecalTextStyle> {
|
||||||
|
@ -63,8 +63,8 @@ namespace ConformalDecals.Text {
|
|||||||
public void RenderText(DecalText text, out Texture2D texture, out Rect window) {
|
public void RenderText(DecalText text, out Texture2D texture, out Rect window) {
|
||||||
// SETUP TMP OBJECT FOR RENDERING
|
// SETUP TMP OBJECT FOR RENDERING
|
||||||
_tmp.text = text.FormattedText;
|
_tmp.text = text.FormattedText;
|
||||||
_tmp.font = text.Font.fontAsset;
|
_tmp.font = text.Font.FontAsset;
|
||||||
_tmp.fontStyle = text.Style.FontStyle | text.Font.fontStyle;
|
_tmp.fontStyle = text.Style.FontStyle | text.Font.FontStyle;
|
||||||
_tmp.lineSpacing = text.Style.LineSpacing;
|
_tmp.lineSpacing = text.Style.LineSpacing;
|
||||||
_tmp.characterSpacing = text.Style.CharacterSpacing;
|
_tmp.characterSpacing = text.Style.CharacterSpacing;
|
||||||
|
|
||||||
@ -76,7 +76,7 @@ namespace ConformalDecals.Text {
|
|||||||
_tmp.fontSize = FontSize;
|
_tmp.fontSize = FontSize;
|
||||||
|
|
||||||
// SETUP BLIT MATERIAL
|
// SETUP BLIT MATERIAL
|
||||||
_blitMaterial.SetTexture(PropertyIDs._MainTex, text.Font.fontAsset.atlas);
|
_blitMaterial.SetTexture(PropertyIDs._MainTex, text.Font.FontAsset.atlas);
|
||||||
|
|
||||||
// GENERATE MESH
|
// GENERATE MESH
|
||||||
_tmp.ForceMeshUpdate();
|
_tmp.ForceMeshUpdate();
|
||||||
|
@ -45,10 +45,10 @@ namespace ConformalDecals.UI {
|
|||||||
|
|
||||||
Toggle active = null;
|
Toggle active = null;
|
||||||
|
|
||||||
foreach (var font in fonts.OrderBy(x => x.title)) {
|
foreach (var font in fonts.OrderBy(x => x.Title)) {
|
||||||
Debug.Log(font.title);
|
Debug.Log(font.Title);
|
||||||
var listItem = GameObject.Instantiate(_menuItem, _menuList.transform);
|
var listItem = GameObject.Instantiate(_menuItem, _menuList.transform);
|
||||||
listItem.name = font.title;
|
listItem.name = font.Title;
|
||||||
listItem.SetActive(true);
|
listItem.SetActive(true);
|
||||||
|
|
||||||
var fontItem = listItem.AddComponent<FontMenuItem>();
|
var fontItem = listItem.AddComponent<FontMenuItem>();
|
||||||
|
@ -35,33 +35,19 @@ namespace ConformalDecals.UI {
|
|||||||
|
|
||||||
var controller = window.GetComponent<TextEntryController>();
|
var controller = window.GetComponent<TextEntryController>();
|
||||||
controller._text = text;
|
controller._text = text;
|
||||||
|
controller._font = font;
|
||||||
|
controller._style = style;
|
||||||
controller.onValueChanged.AddListener(textUpdateCallback);
|
controller.onValueChanged.AddListener(textUpdateCallback);
|
||||||
|
|
||||||
return controller;
|
return controller;
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnClose() {
|
public void OnClose() {
|
||||||
if (_fontMenu != null) _fontMenu.OnClose();
|
if (_fontMenu != null) _fontMenu.OnClose();
|
||||||
Destroy(gameObject);
|
Destroy(gameObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnValueChanged() {
|
|
||||||
onValueChanged.Invoke(_text, _font, _style);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnTextUpdate(string newText) {
|
public void OnTextUpdate(string newText) {
|
||||||
this._text = newText;
|
this._text = newText;
|
||||||
|
|
||||||
@ -77,9 +63,10 @@ namespace ConformalDecals.UI {
|
|||||||
font.SetupSample(_fontButton.GetComponentInChildren<TextMeshProUGUI>());
|
font.SetupSample(_fontButton.GetComponentInChildren<TextMeshProUGUI>());
|
||||||
|
|
||||||
var textBox = ((TMP_InputField) _textBox);
|
var textBox = ((TMP_InputField) _textBox);
|
||||||
textBox.textComponent.fontStyle = _style.FontStyle | _font.fontStyle;
|
textBox.textComponent.fontStyle = _style.FontStyle | _font.FontStyle;
|
||||||
textBox.fontAsset = _font.fontAsset;
|
textBox.fontAsset = _font.FontAsset;
|
||||||
|
|
||||||
|
UpdateStyleButtons();
|
||||||
OnValueChanged();
|
OnValueChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,5 +98,30 @@ namespace ConformalDecals.UI {
|
|||||||
_style.Vertical = state;
|
_style.Vertical = state;
|
||||||
OnValueChanged();
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user