2022-06-14 12:39:13 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
|
namespace Coffee.NanoMonitor
|
|
|
|
|
{
|
2022-06-14 12:39:13 +08:00
|
|
|
|
public class FixedFont
|
|
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
|
private const string k_Characters =
|
|
|
|
|
"_!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
|
|
|
|
|
|
|
|
|
|
private static readonly Dictionary<Font, FixedFont> s_Fonts = new Dictionary<Font, FixedFont>();
|
|
|
|
|
private static readonly TextGenerator s_TextGenerator = new TextGenerator(100);
|
2022-06-14 12:39:13 +08:00
|
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
|
private static TextGenerationSettings s_Settings = new TextGenerationSettings
|
2022-06-14 12:39:13 +08:00
|
|
|
|
{
|
|
|
|
|
scaleFactor = 1,
|
|
|
|
|
horizontalOverflow = HorizontalWrapMode.Overflow,
|
|
|
|
|
verticalOverflow = VerticalWrapMode.Overflow,
|
|
|
|
|
alignByGeometry = true,
|
|
|
|
|
textAnchor = TextAnchor.MiddleCenter,
|
|
|
|
|
color = Color.white
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private readonly Font _font;
|
2023-08-17 08:43:02 +08:00
|
|
|
|
private readonly UIVertex[] _tmpVerts = new UIVertex[4];
|
2022-06-14 12:39:13 +08:00
|
|
|
|
private UICharInfo[] _charInfos;
|
|
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
|
private int _resolution;
|
|
|
|
|
private UIVertex[] _verts;
|
|
|
|
|
|
2022-06-14 12:39:13 +08:00
|
|
|
|
private FixedFont(Font font)
|
|
|
|
|
{
|
|
|
|
|
_font = font;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
|
public int fontSize
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _font.dynamic ? 32 : _font.fontSize;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-14 12:39:13 +08:00
|
|
|
|
|
|
|
|
|
public static FixedFont GetOrCreate(Font font)
|
|
|
|
|
{
|
|
|
|
|
if (font == null) return null;
|
2023-08-17 08:43:02 +08:00
|
|
|
|
if (s_Fonts.TryGetValue(font, out var data)) return data;
|
2022-06-14 12:39:13 +08:00
|
|
|
|
|
|
|
|
|
data = new FixedFont(font);
|
2023-08-17 08:43:02 +08:00
|
|
|
|
s_Fonts.Add(font, data);
|
2022-06-14 12:39:13 +08:00
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Invalidate()
|
|
|
|
|
{
|
|
|
|
|
_resolution = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateFont()
|
|
|
|
|
{
|
|
|
|
|
if (!_font) return;
|
|
|
|
|
|
|
|
|
|
var mat = _font.material;
|
|
|
|
|
if (!mat) return;
|
|
|
|
|
|
|
|
|
|
var tex = mat.mainTexture;
|
|
|
|
|
if (!tex) return;
|
|
|
|
|
|
|
|
|
|
var currentResolution = tex.width * tex.height;
|
|
|
|
|
|
|
|
|
|
if (_resolution == currentResolution) return;
|
|
|
|
|
_resolution = currentResolution;
|
|
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
|
s_Settings.font = _font;
|
|
|
|
|
s_TextGenerator.Invalidate();
|
|
|
|
|
s_TextGenerator.Populate(k_Characters, s_Settings);
|
2022-06-14 12:39:13 +08:00
|
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
|
_verts = s_TextGenerator.GetVerticesArray();
|
|
|
|
|
_charInfos = s_TextGenerator.GetCharactersArray();
|
2022-06-14 12:39:13 +08:00
|
|
|
|
|
|
|
|
|
float offsetX = 0;
|
|
|
|
|
for (var i = 0; i < _verts.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if ((i & 3) == 0)
|
2023-08-17 08:43:02 +08:00
|
|
|
|
{
|
2022-06-14 12:39:13 +08:00
|
|
|
|
offsetX = _verts[i].position.x;
|
2023-08-17 08:43:02 +08:00
|
|
|
|
}
|
2022-06-14 12:39:13 +08:00
|
|
|
|
|
|
|
|
|
var v = _verts[i];
|
|
|
|
|
v.position -= new Vector3(offsetX, 0);
|
|
|
|
|
_verts[i] = v;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float Layout(char c, float offset, float scale)
|
|
|
|
|
{
|
|
|
|
|
if (_charInfos == null) return offset;
|
|
|
|
|
if (c < 0x20 || 0x7e < c) return offset;
|
|
|
|
|
var ci = c - 0x20;
|
|
|
|
|
|
|
|
|
|
return offset + _charInfos[ci].charWidth * scale;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float Append(VertexHelper toFill, char c, float offset, float scale, Color color)
|
|
|
|
|
{
|
|
|
|
|
if (_verts == null || _charInfos == null) return offset;
|
|
|
|
|
if (c < 0x20 || 0x7e < c) return offset;
|
|
|
|
|
var ci = c - 0x20;
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < 4; i++)
|
|
|
|
|
{
|
|
|
|
|
_tmpVerts[i] = _verts[ci * 4 + i];
|
|
|
|
|
_tmpVerts[i].position = _tmpVerts[i].position * scale + new Vector3(offset, 0);
|
|
|
|
|
_tmpVerts[i].color = ci == 0 ? Color.clear : color;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toFill.AddUIVertexQuad(_tmpVerts);
|
|
|
|
|
return offset + _charInfos[ci].charWidth * scale;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Fill(VertexHelper toFill, Color color, RectTransform tr)
|
|
|
|
|
{
|
|
|
|
|
if (_verts == null || _charInfos == null) return;
|
|
|
|
|
const int ci = '*' - 0x20;
|
|
|
|
|
var uv = (_verts[ci * 4].uv0 + _verts[ci * 4 + 2].uv0) / 2;
|
|
|
|
|
|
|
|
|
|
var rect = tr.rect;
|
|
|
|
|
var size = rect.size / 2;
|
|
|
|
|
var offset = (new Vector2(0.5f, 0.5f) - tr.pivot) * rect.size;
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < 4; i++)
|
|
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
|
_tmpVerts[i] = new UIVertex
|
|
|
|
|
{
|
|
|
|
|
uv0 = uv,
|
|
|
|
|
color = color
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
switch (i)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
_tmpVerts[i].position = new Vector2(-size.x, -size.y) + offset;
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
_tmpVerts[i].position = new Vector2(-size.x, size.y) + offset;
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
_tmpVerts[i].position = new Vector2(size.x, size.y) + offset;
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
_tmpVerts[i].position = new Vector2(size.x, -size.y) + offset;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-06-14 12:39:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toFill.AddUIVertexQuad(_tmpVerts);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|