GitHub
6 年前
当前提交
8ae57447
共有 20 个文件被更改,包括 262 次插入 和 313 次删除
-
2Assets/UIWidgets/Tests/Paragraph.cs
-
14Assets/UIWidgets/ui/painting/canvas.cs
-
24Assets/UIWidgets/ui/painting/canvas_impl.cs
-
16Assets/UIWidgets/ui/painting/draw_cmd.cs
-
10Assets/UIWidgets/ui/painting/picture.cs
-
2Assets/UIWidgets/ui/txt/linebreaker.cs
-
110Assets/UIWidgets/ui/txt/paragraph.cs
-
3Assets/UIWidgets/ui/painting/txt.meta
-
42Assets/UIWidgets/ui/txt/paint_record.cs
-
3Assets/UIWidgets/ui/txt/paint_record.cs.meta
-
37Assets/UIWidgets/ui/painting/txt/font_manager.cs
-
80Assets/UIWidgets/ui/painting/txt/mesh_generator.cs
-
3Assets/UIWidgets/ui/painting/txt/mesh_generator.cs.meta
-
31Assets/UIWidgets/ui/painting/txt/text_blob.cs
-
3Assets/UIWidgets/ui/painting/txt/text_blob.cs.meta
-
73Assets/UIWidgets/ui/txt/font_manager.cs
-
119Assets/UIWidgets/ui/txt/mesh.cs
-
3Assets/UIWidgets/ui/txt/mesh.cs.meta
-
0/Assets/UIWidgets/ui/painting/txt/font_manager.cs.meta
|
|||
fileFormatVersion: 2 |
|||
guid: 9438a28ff5974aac92d56928b06692ad |
|||
timeCreated: 1536299863 |
|
|||
using UIWidgets.ui.txt; |
|||
|
|||
namespace UIWidgets.ui |
|||
{ |
|||
public class PaintRecord |
|||
{ |
|||
public PaintRecord(TextStyle style, TextBlob _text, |
|||
int line, double runWidth) |
|||
{ |
|||
this._style = style; |
|||
this._text = _text; |
|||
this._line = line; |
|||
this._runWidth = runWidth; |
|||
} |
|||
|
|||
public TextBlob text |
|||
{ |
|||
get { return _text; } |
|||
} |
|||
|
|||
public TextStyle style |
|||
{ |
|||
get { return _style; } |
|||
} |
|||
|
|||
public int line |
|||
{ |
|||
get { return _line; } |
|||
} |
|||
|
|||
public double runWidth |
|||
{ |
|||
get { return _runWidth; } |
|||
} |
|||
|
|||
private TextStyle _style; |
|||
private TextBlob _text; |
|||
private int _line; |
|||
private double _runWidth; |
|||
|
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 2695fb8e8f284b95ad9a175da41a9101 |
|||
timeCreated: 1536300023 |
|
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using UnityEngine; |
|||
|
|||
namespace UIWidgets.ui |
|||
{ |
|||
|
|||
public class FontManager |
|||
{ |
|||
private List<Font> _fonts = new List<Font>(); |
|||
|
|||
public static readonly FontManager instance = new FontManager(); |
|||
|
|||
public Font getOrCreate(string[] names, int fontSize) |
|||
{ |
|||
var founded = _fonts.Find((font) => |
|||
( |
|||
font.fontSize == fontSize && |
|||
(names == font.fontNames || (names != null && names.SequenceEqual(font.fontNames))))); |
|||
if (founded != null) |
|||
{ |
|||
return founded; |
|||
} |
|||
|
|||
Debug.Log(string.Format("Create new Font names={0}, size={1}", names, fontSize)); |
|||
var newFont = Font.CreateDynamicFontFromOSFont(names, |
|||
fontSize); |
|||
_fonts.Add(newFont); |
|||
return newFont; |
|||
} |
|||
|
|||
public Font getOrCreate(string name, int fontSize) |
|||
{ |
|||
return getOrCreate(new []{name}, fontSize); |
|||
} |
|||
} |
|||
} |
|
|||
using UIWidgets.ui.txt; |
|||
using UnityEngine; |
|||
|
|||
namespace UIWidgets.ui.painting.txt |
|||
{ |
|||
public static class MeshGenrator |
|||
{ |
|||
public static Mesh generateMesh(TextBlob textBlob, double x, double y) |
|||
{ |
|||
var style = textBlob.style; |
|||
var font = FontManager.instance.getOrCreate(style.safeFontFamily, style.UnityFontSize); |
|||
var length = textBlob.end - textBlob.start; |
|||
var vertices = new Vector3[length * 4]; |
|||
var triangles = new int[length * 6]; |
|||
var uv = new Vector2[length * 4]; |
|||
var text = textBlob.text; |
|||
|
|||
var offset = new Vector3((float)Utils.PixelCorrectRound(x), (float)Utils.PixelCorrectRound(y), 0); |
|||
font.RequestCharactersInTexture(textBlob.text.Substring(textBlob.start, textBlob.end - textBlob.start), |
|||
style.UnityFontSize, style.UnityFontStyle); |
|||
for (int charIndex = 0; charIndex < length; ++charIndex) |
|||
{ |
|||
var ch = text[charIndex + textBlob.start]; |
|||
var position = textBlob.positions[charIndex + textBlob.start]; |
|||
|
|||
CharacterInfo charInfo = new CharacterInfo(); |
|||
if (Paragraph.isWordSpace(ch) || Paragraph.isLineEndSpace(ch) || ch== '\t') |
|||
{ |
|||
|
|||
vertices[4 * charIndex + 0] = vertices[4 * charIndex + 1] = |
|||
vertices[4 * charIndex + 2] = vertices[4 * charIndex + 3] = offset; |
|||
|
|||
uv[4 * charIndex + 0] = Vector2.zero; |
|||
uv[4 * charIndex + 1] = Vector2.zero; |
|||
uv[4 * charIndex + 2] = Vector2.zero; |
|||
uv[4 * charIndex + 3] = Vector2.zero; |
|||
} |
|||
else |
|||
{ |
|||
font.GetCharacterInfo(ch, out charInfo, style.UnityFontSize, style.UnityFontStyle); |
|||
vertices[4 * charIndex + 0] = offset + new Vector3((float)(position.x + charInfo.minX), |
|||
(float)(position.y - charInfo.maxY), 0); |
|||
vertices[4 * charIndex + 1] = offset + new Vector3((float)(position.x + charInfo.maxX), |
|||
(float)(position.y - charInfo.maxY), 0); |
|||
vertices[4 * charIndex + 2] = offset + new Vector3( |
|||
(float)(position.x + charInfo.maxX), (float)(position.y - charInfo.minY), 0); |
|||
vertices[4 * charIndex + 3] = offset + new Vector3( |
|||
(float)(position.x + charInfo.minX), (float)(position.y - charInfo.minY), 0); |
|||
|
|||
uv[4 * charIndex + 0] = charInfo.uvTopLeft; |
|||
uv[4 * charIndex + 1] = charInfo.uvTopRight; |
|||
uv[4 * charIndex + 2] = charInfo.uvBottomRight; |
|||
uv[4 * charIndex + 3] = charInfo.uvBottomLeft; |
|||
} |
|||
|
|||
triangles[6 * charIndex + 0] = 4 * charIndex + 0; |
|||
triangles[6 * charIndex + 1] = 4 * charIndex + 1; |
|||
triangles[6 * charIndex + 2] = 4 * charIndex + 2; |
|||
|
|||
triangles[6 * charIndex + 3] = 4 * charIndex + 0; |
|||
triangles[6 * charIndex + 4] = 4 * charIndex + 2; |
|||
triangles[6 * charIndex + 5] = 4 * charIndex + 3; |
|||
} |
|||
|
|||
var mesh = new Mesh() |
|||
{ |
|||
vertices = vertices, |
|||
triangles = triangles, |
|||
uv = uv |
|||
}; |
|||
var colors = new UnityEngine.Color[vertices.Length]; |
|||
for (var i = 0; i < colors.Length; i++) |
|||
{ |
|||
colors[i] = style.UnityColor; |
|||
} |
|||
mesh.colors = colors; |
|||
return mesh; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 79c7ef33071e4ec9b28b196b3e265bfa |
|||
timeCreated: 1536301684 |
|
|||
using System; |
|||
using UnityEngine; |
|||
|
|||
namespace UIWidgets.ui.txt |
|||
{ |
|||
public class TextBlob |
|||
{ |
|||
public TextBlob(string text, int start, int end, Vector2d[] positions, TextStyle style, Rect bounds) |
|||
{ |
|||
Debug.Assert(start < end); |
|||
this.text = text; |
|||
this.start = start; |
|||
this.end = end; |
|||
this.positions = positions; |
|||
this.style = style; |
|||
this.bounds = bounds; |
|||
} |
|||
|
|||
public Rect boundsInText |
|||
{ |
|||
get { return bounds.shift(new Offset(positions[start].x, positions[start].y)); } |
|||
} |
|||
|
|||
public readonly string text; |
|||
public readonly int start; |
|||
public readonly int end; |
|||
public readonly Vector2d[] positions; |
|||
public readonly TextStyle style; |
|||
public readonly Rect bounds; // bounds with positions[start] as origin
|
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 8fc95fa5caea426a80f19b2d0f99955f |
|||
timeCreated: 1536299888 |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using UnityEngine; |
|||
|
|||
namespace UIWidgets.ui |
|||
{ |
|||
public class FontEntry |
|||
{ |
|||
public readonly Font font; |
|||
private int _textureBuildVersion = 0; |
|||
|
|||
public FontEntry(Font font) |
|||
{ |
|||
this.font = font; |
|||
|
|||
} |
|||
|
|||
public int textureBuildVersion |
|||
{ |
|||
get { return _textureBuildVersion; } |
|||
} |
|||
|
|||
internal void onFontTextureRebuild() |
|||
{ |
|||
_textureBuildVersion++; |
|||
} |
|||
} |
|||
|
|||
public class FontManager |
|||
{ |
|||
private List<FontEntry> _fonts = new List<FontEntry>(); |
|||
|
|||
public static readonly FontManager instance = new FontManager(); |
|||
|
|||
private FontManager() |
|||
{ |
|||
Font.textureRebuilt += this.onFontTextureRebuilt; |
|||
} |
|||
|
|||
public FontEntry getOrCreate(string[] names, int fontSize) |
|||
{ |
|||
var founded = _fonts.Find((font) => |
|||
( |
|||
font.font.fontSize == fontSize && |
|||
(names == font.font.fontNames || (names != null && names.SequenceEqual(font.font.fontNames))))); |
|||
if (founded != null) |
|||
{ |
|||
return founded; |
|||
} |
|||
|
|||
Debug.Log(string.Format("Create new Font names={0}, size={1}", names, fontSize)); |
|||
var newFont = new FontEntry(Font.CreateDynamicFontFromOSFont(names, |
|||
fontSize)); |
|||
_fonts.Add(newFont); |
|||
return newFont; |
|||
} |
|||
|
|||
public FontEntry getOrCreate(string name, int fontSize) |
|||
{ |
|||
return getOrCreate(new []{name}, fontSize); |
|||
} |
|||
|
|||
private void onFontTextureRebuilt(Font font) |
|||
{ |
|||
var entry = _fonts.Find((f) => f.font == font); |
|||
if (entry != null) |
|||
{ |
|||
entry.onFontTextureRebuild(); |
|||
} |
|||
} |
|||
} |
|||
} |
|
|||
using UnityEngine; |
|||
|
|||
namespace UIWidgets.ui |
|||
{ |
|||
public class TextMesh: IMesh |
|||
{ |
|||
private Mesh _mesh; |
|||
private FontEntry _fontEntry; |
|||
private string _text; |
|||
private StyledRuns.Run _run; |
|||
private int _textureVersion; |
|||
|
|||
public TextMesh(Vector2d pos, string text, Vector2d[] _characterPositions, FontEntry fontEntry, StyledRuns.Run run) |
|||
{ |
|||
_fontEntry = fontEntry; |
|||
_text = text; |
|||
this._run = run; |
|||
|
|||
var vertices = new Vector3[_text.Length * 4]; |
|||
var triangles = new int[_text.Length * 6]; |
|||
var font = fontEntry.font; |
|||
var offset = new Vector3((float)Utils.PixelCorrectRound(pos.x), (float)Utils.PixelCorrectRound(pos.y), 0); |
|||
font.RequestCharactersInTexture(_text.Substring(_run.start, _run.end - _run.start), |
|||
_run.style.UnityFontSize, _run.style.UnityFontStyle); |
|||
for (int charIndex = _run.start; charIndex < _run.end; ++charIndex) |
|||
{ |
|||
CharacterInfo charInfo = new CharacterInfo(); |
|||
if (_text[charIndex] != '\n' && _text[charIndex] != '\t') |
|||
{ |
|||
Debug.Assert(font.GetCharacterInfo(_text[charIndex], out charInfo, _run.style.UnityFontSize, _run.style.UnityFontStyle)); |
|||
var position = _characterPositions[charIndex]; |
|||
vertices[4 * charIndex + 0] = offset + new Vector3((float)(position.x + charInfo.minX), |
|||
(float)(position.y - charInfo.maxY), 0); |
|||
vertices[4 * charIndex + 1] = offset + new Vector3((float)(position.x + charInfo.maxX), |
|||
(float)(position.y - charInfo.maxY), 0); |
|||
vertices[4 * charIndex + 2] = offset + new Vector3( |
|||
(float)(position.x + charInfo.maxX), (float)(position.y - charInfo.minY), 0); |
|||
vertices[4 * charIndex + 3] = offset + new Vector3( |
|||
(float)(position.x + charInfo.minX), (float)(position.y - charInfo.minY), 0); |
|||
} |
|||
else |
|||
{ |
|||
vertices[4 * charIndex + 0] = vertices[4 * charIndex + 1] = |
|||
vertices[4 * charIndex + 2] = vertices[4 * charIndex + 3] = offset; |
|||
} |
|||
|
|||
triangles[6 * charIndex + 0] = 4 * charIndex + 0; |
|||
triangles[6 * charIndex + 1] = 4 * charIndex + 1; |
|||
triangles[6 * charIndex + 2] = 4 * charIndex + 2; |
|||
|
|||
triangles[6 * charIndex + 3] = 4 * charIndex + 0; |
|||
triangles[6 * charIndex + 4] = 4 * charIndex + 2; |
|||
triangles[6 * charIndex + 5] = 4 * charIndex + 3; |
|||
} |
|||
|
|||
var uv = getTextureUV(); |
|||
var mesh = new Mesh() |
|||
{ |
|||
vertices = vertices, |
|||
triangles = triangles, |
|||
uv = uv |
|||
}; |
|||
var colors = new UnityEngine.Color[vertices.Length]; |
|||
for (var i = 0; i < colors.Length; i++) |
|||
{ |
|||
colors[i] = _run.style.UnityColor; |
|||
} |
|||
|
|||
mesh.colors = colors; |
|||
_textureVersion = _fontEntry.textureBuildVersion; |
|||
_mesh = mesh; |
|||
} |
|||
|
|||
public void syncTextureUV() |
|||
{ |
|||
if (_fontEntry.textureBuildVersion != _textureVersion) // texture has been rebuilt, update the texture uv
|
|||
{ |
|||
_mesh.uv = getTextureUV(); |
|||
} |
|||
} |
|||
|
|||
public Mesh mesh |
|||
{ |
|||
get { return _mesh; } |
|||
} |
|||
|
|||
private Vector2[] getTextureUV() |
|||
{ |
|||
var font = _fontEntry.font; |
|||
var uv = _mesh == null ? new Vector2[_text.Length * 4] : _mesh.uv; |
|||
|
|||
for (int charIndex = _run.start; charIndex < _run.end; ++charIndex) |
|||
{ |
|||
CharacterInfo charInfo = new CharacterInfo(); |
|||
if (_text[charIndex] != '\n' && _text[charIndex] != '\t') |
|||
{ |
|||
font.GetCharacterInfo(_text[charIndex], out charInfo, _run.style.UnityFontSize, |
|||
_run.style.UnityFontStyle); |
|||
} |
|||
|
|||
if (Paragraph.isWordSpace(_text[charIndex]) || Paragraph.isLineEndSpace(_text[charIndex]) || _text[charIndex] == '\t') |
|||
{ |
|||
uv[4 * charIndex + 0] = Vector2.zero; |
|||
uv[4 * charIndex + 1] = Vector2.zero; |
|||
uv[4 * charIndex + 2] = Vector2.zero; |
|||
uv[4 * charIndex + 3] = Vector2.zero; |
|||
} else |
|||
{ |
|||
uv[4 * charIndex + 0] = charInfo.uvTopLeft; |
|||
uv[4 * charIndex + 1] = charInfo.uvTopRight; |
|||
uv[4 * charIndex + 2] = charInfo.uvBottomRight; |
|||
uv[4 * charIndex + 3] = charInfo.uvBottomLeft; |
|||
} |
|||
} |
|||
|
|||
return uv; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 96ec1d4b2b7d4bf59d4b2db221323ae9 |
|||
timeCreated: 1536283931 |
撰写
预览
正在加载...
取消
保存
Reference in new issue