mirror of
https://github.com/drewcassidy/KSP-Conformal-Decals.git
synced 2024-09-01 18:23:54 +00:00
Merge branch 'main' into feature-multiProject
# Conflicts: # GameData/ConformalDecals/Plugins/ConformalDecals.dll # Source/ConformalDecals/ModuleConformalDecal.cs # Source/ConformalDecals/ModuleConformalText.cs
This commit is contained in:
@ -3,41 +3,47 @@ using System.Collections.Generic;
|
||||
using ConformalDecals.Util;
|
||||
using TMPro;
|
||||
using UniLinq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace ConformalDecals.Text {
|
||||
public class DecalFont : IEquatable<DecalFont> {
|
||||
public class DecalFont : ScriptableObject, ISerializationCallbackReceiver, IEquatable<DecalFont> {
|
||||
[SerializeField] private string _title;
|
||||
[SerializeField] private TMP_FontAsset _fontAsset;
|
||||
[SerializeField] private FontStyles _fontStyle;
|
||||
[SerializeField] private FontStyles _fontStyleMask;
|
||||
|
||||
/// Human-readable name for the font
|
||||
public string Title { get; }
|
||||
public string Title => _title;
|
||||
|
||||
/// Internal name for the font
|
||||
public string Name => FontAsset.name;
|
||||
public string Name => _fontAsset.name;
|
||||
|
||||
/// The font asset itself
|
||||
public TMP_FontAsset FontAsset { get; }
|
||||
public TMP_FontAsset FontAsset => _fontAsset;
|
||||
|
||||
/// Styles that are forced on for this font,
|
||||
/// e.g. smallcaps for a font without lower case characters
|
||||
public FontStyles FontStyle { get; }
|
||||
public FontStyles FontStyle => _fontStyle;
|
||||
|
||||
public bool Bold => (FontStyle & FontStyles.Bold) != 0;
|
||||
public bool Bold => (_fontStyle & FontStyles.Bold) != 0;
|
||||
|
||||
public bool Italic => (FontStyle & FontStyles.Italic) != 0;
|
||||
public bool Italic => (_fontStyle & FontStyles.Italic) != 0;
|
||||
|
||||
public bool Underline => (FontStyle & FontStyles.Underline) != 0;
|
||||
public bool Underline => (_fontStyle & FontStyles.Underline) != 0;
|
||||
|
||||
public bool SmallCaps => (FontStyle & FontStyles.SmallCaps) != 0;
|
||||
public bool SmallCaps => (_fontStyle & FontStyles.SmallCaps) != 0;
|
||||
|
||||
/// Styles that are forced off for this font,
|
||||
/// e.g. underline for a font with no underscore character
|
||||
public FontStyles FontStyleMask { get; }
|
||||
public FontStyles FontStyleMask => _fontStyleMask;
|
||||
|
||||
public bool BoldMask => (FontStyleMask & FontStyles.Bold) != 0;
|
||||
public bool BoldMask => (_fontStyleMask & FontStyles.Bold) != 0;
|
||||
|
||||
public bool ItalicMask => (FontStyleMask & FontStyles.Italic) != 0;
|
||||
public bool ItalicMask => (_fontStyleMask & FontStyles.Italic) != 0;
|
||||
|
||||
public bool UnderlineMask => (FontStyleMask & FontStyles.Underline) != 0;
|
||||
public bool UnderlineMask => (_fontStyleMask & FontStyles.Underline) != 0;
|
||||
|
||||
public bool SmallCapsMask => (FontStyleMask & FontStyles.SmallCaps) != 0;
|
||||
public bool SmallCapsMask => (_fontStyleMask & FontStyles.SmallCaps) != 0;
|
||||
|
||||
|
||||
public DecalFont(ConfigNode node, IEnumerable<TMP_FontAsset> fontAssets) {
|
||||
@ -45,14 +51,14 @@ namespace ConformalDecals.Text {
|
||||
if (fontAssets == null) throw new ArgumentNullException(nameof(fontAssets));
|
||||
|
||||
var name = ParseUtil.ParseString(node, "name");
|
||||
FontAsset = fontAssets.First(o => o.name == 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);
|
||||
_title = ParseUtil.ParseString(node, "title", true, name);
|
||||
_fontStyle = (FontStyles) ParseUtil.ParseInt(node, "style", true);
|
||||
_fontStyleMask = (FontStyles) ParseUtil.ParseInt(node, "styleMask", true);
|
||||
}
|
||||
|
||||
|
||||
@ -95,5 +101,9 @@ namespace ConformalDecals.Text {
|
||||
public static bool operator !=(DecalFont left, DecalFont right) {
|
||||
return !Equals(left, right);
|
||||
}
|
||||
|
||||
public void OnBeforeSerialize() { }
|
||||
|
||||
public void OnAfterDeserialize() { }
|
||||
}
|
||||
}
|
||||
@ -1,21 +1,39 @@
|
||||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
using TMPro;
|
||||
|
||||
namespace ConformalDecals.Text {
|
||||
public class DecalText : IEquatable<DecalText> {
|
||||
private readonly string _text;
|
||||
private readonly DecalFont _font;
|
||||
private readonly FontStyles _style;
|
||||
private readonly bool _vertical;
|
||||
private readonly float _lineSpacing;
|
||||
private readonly float _charSpacing;
|
||||
|
||||
/// Raw text contents
|
||||
public string Text { get; }
|
||||
public string Text => _text;
|
||||
|
||||
/// Font asset used by this text snippet
|
||||
public DecalFont Font { get; }
|
||||
public DecalFont Font => _font;
|
||||
|
||||
/// Style used by this text snippet
|
||||
public DecalTextStyle Style { get; }
|
||||
public FontStyles Style => _style;
|
||||
|
||||
/// If this text snippet is vertical
|
||||
public bool Vertical => _vertical;
|
||||
|
||||
/// The text snippet's line spacing
|
||||
public float LineSpacing => _lineSpacing;
|
||||
|
||||
/// The text snippet's character spacing
|
||||
public float CharSpacing => _charSpacing;
|
||||
|
||||
/// The text formatted with newlines for vertical text
|
||||
public string FormattedText {
|
||||
get {
|
||||
if (Style.Vertical) {
|
||||
if (string.IsNullOrWhiteSpace(Text)) return "•";
|
||||
if (Vertical) {
|
||||
return Regex.Replace(Text, @"(.)", "$1\n");
|
||||
}
|
||||
else {
|
||||
@ -24,17 +42,22 @@ namespace ConformalDecals.Text {
|
||||
}
|
||||
}
|
||||
|
||||
public DecalText(string text, DecalFont font, DecalTextStyle style) {
|
||||
|
||||
public DecalText(string text, DecalFont font, FontStyles style, bool vertical, float linespacing, float charspacing) {
|
||||
if (font == null) throw new ArgumentNullException(nameof(font));
|
||||
Text = text;
|
||||
Font = font;
|
||||
Style = style;
|
||||
_text = text;
|
||||
_font = font;
|
||||
_style = style;
|
||||
_vertical = vertical;
|
||||
_lineSpacing = linespacing;
|
||||
_charSpacing = charspacing;
|
||||
}
|
||||
|
||||
public bool Equals(DecalText other) {
|
||||
if (ReferenceEquals(null, other)) return false;
|
||||
if (ReferenceEquals(this, other)) return true;
|
||||
return Text == other.Text && Equals(Font, other.Font) && Style.Equals(other.Style);
|
||||
return _text == other._text && Equals(_font, other._font) && _style == other._style && _vertical == other._vertical && _lineSpacing.Equals(other._lineSpacing) &&
|
||||
_charSpacing.Equals(other._charSpacing);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj) {
|
||||
@ -46,9 +69,12 @@ namespace ConformalDecals.Text {
|
||||
|
||||
public override int GetHashCode() {
|
||||
unchecked {
|
||||
var hashCode = (Text != null ? Text.GetHashCode() : 0);
|
||||
hashCode = (hashCode * 397) ^ (Font != null ? Font.GetHashCode() : 0);
|
||||
hashCode = (hashCode * 397) ^ Style.GetHashCode();
|
||||
var hashCode = (_text != null ? _text.GetHashCode() : 0);
|
||||
hashCode = (hashCode * 397) ^ (_font != null ? _font.GetHashCode() : 0);
|
||||
hashCode = (hashCode * 397) ^ (int) _style;
|
||||
hashCode = (hashCode * 397) ^ _vertical.GetHashCode();
|
||||
hashCode = (hashCode * 397) ^ _lineSpacing.GetHashCode();
|
||||
hashCode = (hashCode * 397) ^ _charSpacing.GetHashCode();
|
||||
return hashCode;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,101 +0,0 @@
|
||||
using System;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
// ReSharper disable NonReadonlyMemberInGetHashCode
|
||||
|
||||
namespace ConformalDecals.Text {
|
||||
public struct DecalTextStyle : IEquatable<DecalTextStyle> {
|
||||
private FontStyles _fontStyle;
|
||||
private bool _vertical;
|
||||
private float _lineSpacing;
|
||||
private float _charSpacing;
|
||||
|
||||
public FontStyles FontStyle {
|
||||
get => _fontStyle;
|
||||
set => _fontStyle = value;
|
||||
}
|
||||
|
||||
public bool Bold {
|
||||
get => (FontStyle & FontStyles.Bold) != 0;
|
||||
set {
|
||||
if (value) FontStyle |= FontStyles.Bold;
|
||||
else FontStyle &= ~FontStyles.Bold;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Italic {
|
||||
get => (FontStyle & FontStyles.Italic) != 0;
|
||||
set {
|
||||
if (value) FontStyle |= FontStyles.Italic;
|
||||
else FontStyle &= ~FontStyles.Italic;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Underline {
|
||||
get => (FontStyle & FontStyles.Underline) != 0;
|
||||
set {
|
||||
if (value) FontStyle |= FontStyles.Underline;
|
||||
else FontStyle &= ~FontStyles.Underline;
|
||||
}
|
||||
}
|
||||
|
||||
public bool SmallCaps {
|
||||
get => (FontStyle & FontStyles.SmallCaps) != 0;
|
||||
set {
|
||||
if (value) FontStyle |= FontStyles.SmallCaps;
|
||||
else FontStyle &= ~FontStyles.SmallCaps;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Vertical {
|
||||
get => _vertical;
|
||||
set => _vertical = value;
|
||||
}
|
||||
|
||||
public float LineSpacing {
|
||||
get => _lineSpacing;
|
||||
set => _lineSpacing = value;
|
||||
}
|
||||
|
||||
public float CharSpacing {
|
||||
get => _charSpacing;
|
||||
set => _charSpacing = value;
|
||||
}
|
||||
|
||||
public DecalTextStyle(FontStyles fontStyle, bool vertical, float lineSpacing, float charSpacing) {
|
||||
_fontStyle = fontStyle;
|
||||
_vertical = vertical;
|
||||
_lineSpacing = lineSpacing;
|
||||
_charSpacing = charSpacing;
|
||||
}
|
||||
|
||||
public bool Equals(DecalTextStyle other) {
|
||||
return FontStyle == other.FontStyle && Vertical == other.Vertical &&
|
||||
Mathf.Approximately(LineSpacing, other.LineSpacing) &&
|
||||
Mathf.Approximately(CharSpacing, other.CharSpacing);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj) {
|
||||
return obj is DecalTextStyle other && Equals(other);
|
||||
}
|
||||
|
||||
public override int GetHashCode() {
|
||||
unchecked {
|
||||
var hashCode = (int) FontStyle;
|
||||
hashCode = (hashCode * 397) ^ Vertical.GetHashCode();
|
||||
hashCode = (hashCode * 397) ^ LineSpacing.GetHashCode();
|
||||
hashCode = (hashCode * 397) ^ CharSpacing.GetHashCode();
|
||||
return hashCode;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool operator ==(DecalTextStyle left, DecalTextStyle right) {
|
||||
return left.Equals(right);
|
||||
}
|
||||
|
||||
public static bool operator !=(DecalTextStyle left, DecalTextStyle right) {
|
||||
return !left.Equals(right);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -89,9 +89,11 @@ namespace ConformalDecals.Text {
|
||||
if (SystemInfo.graphicsDeviceType == GraphicsDeviceType.Direct3D11 || SystemInfo.graphicsDeviceType == GraphicsDeviceType.Direct3D12) {
|
||||
textRenderTextureFormat = RenderTextureFormat.ARGB32; // DirectX is dumb
|
||||
}
|
||||
|
||||
if (!SystemInfo.SupportsTextureFormat(textTextureFormat)) {
|
||||
Logging.LogError($"Text texture format {textTextureFormat} not supported on this platform.");
|
||||
}
|
||||
|
||||
if (!SystemInfo.SupportsRenderTextureFormat(textRenderTextureFormat)) {
|
||||
Logging.LogError($"Text texture format {textRenderTextureFormat} not supported on this platform.");
|
||||
}
|
||||
@ -161,9 +163,9 @@ namespace ConformalDecals.Text {
|
||||
// SETUP TMP OBJECT FOR RENDERING
|
||||
_tmp.text = text.FormattedText;
|
||||
_tmp.font = text.Font.FontAsset;
|
||||
_tmp.fontStyle = text.Style.FontStyle | text.Font.FontStyle;
|
||||
_tmp.lineSpacing = text.Style.LineSpacing;
|
||||
_tmp.characterSpacing = text.Style.CharSpacing;
|
||||
_tmp.fontStyle = text.Style | text.Font.FontStyle;
|
||||
_tmp.lineSpacing = text.LineSpacing;
|
||||
_tmp.characterSpacing = text.CharSpacing;
|
||||
|
||||
_tmp.extraPadding = true;
|
||||
_tmp.enableKerning = true;
|
||||
@ -205,6 +207,9 @@ namespace ConformalDecals.Text {
|
||||
|
||||
// CALCULATE SIZES
|
||||
var size = bounds.size * PixelDensity;
|
||||
size.x = Mathf.Max(size.x, 0.1f);
|
||||
size.y = Mathf.Max(size.y, 0.1f);
|
||||
|
||||
var textureSize = new Vector2Int {
|
||||
x = Mathf.NextPowerOfTwo((int) size.x),
|
||||
y = Mathf.NextPowerOfTwo((int) size.y)
|
||||
@ -250,13 +255,13 @@ namespace ConformalDecals.Text {
|
||||
bounds.center.y - halfSize.y, bounds.center.y + halfSize.y, -1, 1);
|
||||
|
||||
// GET RENDERTEX
|
||||
var renderTex = RenderTexture.GetTemporary(textureSize.x, textureSize.y, 0, textRenderTextureFormat, RenderTextureReadWrite.Linear, 1);
|
||||
renderTex.autoGenerateMips = false;
|
||||
var renderTex = new RenderTexture(textureSize.x, textureSize.y, 0, textRenderTextureFormat, RenderTextureReadWrite.Linear) {autoGenerateMips = false};
|
||||
|
||||
// RENDER
|
||||
Graphics.SetRenderTarget(renderTex);
|
||||
GL.PushMatrix();
|
||||
GL.LoadProjectionMatrix(matrix);
|
||||
GL.LoadIdentity();
|
||||
GL.Clear(false, true, Color.black);
|
||||
|
||||
for (var i = 0; i < meshes.Length; i++) {
|
||||
@ -266,19 +271,25 @@ namespace ConformalDecals.Text {
|
||||
}
|
||||
}
|
||||
|
||||
GL.PopMatrix();
|
||||
|
||||
// COPY TEXTURE BACK INTO RAM
|
||||
var prevRT = RenderTexture.active;
|
||||
RenderTexture.active = renderTex;
|
||||
texture.ReadPixels(new Rect(0, 0, textureSize.x, textureSize.y), 0, 0, true);
|
||||
texture.Apply();
|
||||
RenderTexture.active = prevRT;
|
||||
|
||||
GL.PopMatrix();
|
||||
|
||||
// RELEASE RENDERTEX
|
||||
RenderTexture.ReleaseTemporary(renderTex);
|
||||
renderTex.Release();
|
||||
RenderTexture.Destroy(renderTex);
|
||||
|
||||
// CLEAR SUBMESHES
|
||||
_tmp.text = "";
|
||||
|
||||
for (int i = 0; i < transform.childCount; i++) {
|
||||
Destroy(transform.GetChild(i).gameObject);
|
||||
var child = transform.GetChild(i);
|
||||
Destroy(child.gameObject);
|
||||
}
|
||||
|
||||
return new TextRenderOutput(texture, window);
|
||||
|
||||
Reference in New Issue
Block a user