Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
FontBatch2D.cs
浏览该文件的文档.
1using Engine.Media;
2
3namespace Engine.Graphics {
4 public class FontBatch2D : BaseFontBatch {
12
13 public void QueueBatch(FontBatch2D batch, Matrix? matrix = null, Color? color = null) {
14 int count = TriangleVertices.Count;
15 TriangleVertices.AddRange(batch.TriangleVertices);
16 for (int i = 0; i < batch.TriangleIndices.Count; i++) {
17 TriangleIndices.Add(batch.TriangleIndices[i] + count);
18 }
19 if (matrix.HasValue
20 && matrix != Matrix.Identity) {
21 TransformTriangles(matrix.Value, count);
22 }
23 if (color.HasValue
24 && color != Color.White) {
25 TransformTrianglesColors(color.Value, count);
26 }
27 }
28
29 public void QueueText(string text, Vector2 position, float depth, Color color, TextAnchor anchor = TextAnchor.Default) {
31 text,
32 position,
33 depth,
34 color,
35 anchor,
38 );
39 }
40
41 public void QueueText(string text,
42 Vector2 position,
43 float depth,
44 Color color,
45 TextAnchor anchor,
46 Vector2 scale,
47 Vector2 spacing = default,
48 float angle = 0f) {
49 Vector2 vector;
50 Vector2 vector2;
51 Vector2 vector3;
52 Vector2 vector4;
53 if (angle != 0f) {
54 vector = new Vector2(MathF.Cos(angle), MathF.Sin(angle));
55 vector2 = position + new Vector2(0f - vector.Y, vector.X) * CalculateTextOffset(text, 0, text.Length, anchor, scale, spacing).Y;
56 vector3 = vector * (scale.X * Font.Scale);
57 vector4 = new Vector2(0f - vector.Y, vector.X) * (scale.Y * Font.Scale);
58 }
59 else {
60 vector = new Vector2(1f, 0f);
61 vector2 = position + new Vector2(0f, 1f) * CalculateTextOffset(text, 0, text.Length, anchor, scale, spacing).Y;
62 vector3 = new Vector2(scale.X * Font.Scale, 0f);
63 vector4 = new Vector2(0f, scale.Y * Font.Scale);
64 }
65 Vector2 vector5 = spacing + Font.Spacing;
66 vector2 += 0.5f * (vector3 * vector5.X + vector4 * vector5.Y);
67 int num = 0;
68 int num2 = 0;
69 for (int i = 0; i <= text.Length; i++) {
70 if (i >= text.Length
71 || text[i] == '\n') {
72 Vector2 vector6 = vector2;
73 vector6 += num * (Font.GlyphHeight + vector5.Y) * vector4;
74 vector6 += CalculateTextOffset(text, num2, i - num2, anchor, scale, spacing).X * vector;
75 if ((anchor & TextAnchor.DisableSnapToPixels) == 0) {
76 vector6 = Vector2.Round(vector6);
77 }
79 text,
80 num2,
81 i,
82 depth,
83 color,
84 vector5.X,
85 vector6,
86 vector3,
87 vector4
88 );
89 num++;
90 num2 = i + 1;
91 }
92 }
93 }
94
95 public void Flush(bool clearAfterFlush = true) {
96 Flush(Vector4.One, clearAfterFlush);
97 }
98
99 public void Flush(Vector4 color, bool clearAfterFlush = true) {
100 Flush(PrimitivesRenderer2D.ViewportMatrix(), color, clearAfterFlush);
101 }
102
103 void QueueLine(string text,
104 int begin,
105 int end,
106 float depth,
107 Color color,
108 float fullSpacing,
109 Vector2 corner,
110 Vector2 right,
111 Vector2 down) {
112 Vector2 vector = corner;
113 for (int i = begin; i < end; i++) {
114 char c = text[i];
115 if (c != '\r'
116 && c != '\u200b') {
117 if (c == '\u00a0') {
118 c = ' ';
119 }
120 BitmapFont.Glyph glyph = Font.GetGlyph(c);
121 if (!glyph.IsBlank) {
122 Vector2 vector2 = right * (glyph.TexCoord2.X - glyph.TexCoord1.X) * Font.Texture.Width;
123 Vector2 vector3 = down * (glyph.TexCoord2.Y - glyph.TexCoord1.Y) * Font.Texture.Height;
124 Vector2 vector4 = right * glyph.Offset.X + down * glyph.Offset.Y;
125 Vector2 vector5 = vector + vector4;
126 Vector2 vector6 = vector5 + vector2;
127 Vector2 vector7 = vector5 + vector3;
128 Vector2 vector8 = vector5 + vector2 + vector3;
129 int count = TriangleVertices.Count;
130 TriangleVertices.Count += 4;
132 new Vector3(vector5.X, vector5.Y, depth),
133 color,
134 new Vector2(glyph.TexCoord1.X, glyph.TexCoord1.Y)
135 );
136 TriangleVertices.Array[count + 1] = new VertexPositionColorTexture(
137 new Vector3(vector6.X, vector6.Y, depth),
138 color,
139 new Vector2(glyph.TexCoord2.X, glyph.TexCoord1.Y)
140 );
141 TriangleVertices.Array[count + 2] = new VertexPositionColorTexture(
142 new Vector3(vector8.X, vector8.Y, depth),
143 color,
144 new Vector2(glyph.TexCoord2.X, glyph.TexCoord2.Y)
145 );
146 TriangleVertices.Array[count + 3] = new VertexPositionColorTexture(
147 new Vector3(vector7.X, vector7.Y, depth),
148 color,
149 new Vector2(glyph.TexCoord1.X, glyph.TexCoord2.Y)
150 );
151 int count2 = TriangleIndices.Count;
152 TriangleIndices.Count += 6;
153 TriangleIndices.Array[count2] = count;
154 TriangleIndices.Array[count2 + 1] = count + 1;
155 TriangleIndices.Array[count2 + 2] = count + 2;
156 TriangleIndices.Array[count2 + 3] = count + 2;
157 TriangleIndices.Array[count2 + 4] = count + 3;
158 TriangleIndices.Array[count2 + 5] = count;
159 }
160 float num = i < text.Length - 1 ? Font.GetKerning(c, text[i + 1]) : 0f;
161 vector += right * (glyph.Width - num + fullSpacing);
162 }
163 }
164 }
165 }
166}
Engine.Vector3 Vector3
void TransformTrianglesColors(Color color, int start=0, int end=-1)
void TransformTriangles(Matrix matrix, int start=0, int end=-1)
readonly DynamicArray< int > TriangleIndices
readonly DynamicArray< VertexPositionColorTexture > TriangleVertices
Vector2 CalculateTextOffset(string text, int start, int count, TextAnchor anchor, Vector2 scale, Vector2 spacing)
static readonly BlendState AlphaBlend
static readonly DepthStencilState None
void Flush(Vector4 color, bool clearAfterFlush=true)
void QueueBatch(FontBatch2D batch, Matrix? matrix=null, Color? color=null)
void Flush(bool clearAfterFlush=true)
void QueueLine(string text, int begin, int end, float depth, Color color, float fullSpacing, Vector2 corner, Vector2 right, Vector2 down)
void QueueText(string text, Vector2 position, float depth, Color color, TextAnchor anchor, Vector2 scale, Vector2 spacing=default, float angle=0f)
void QueueText(string text, Vector2 position, float depth, Color color, TextAnchor anchor=TextAnchor.Default)
static readonly RasterizerState CullNoneScissor
static BitmapFont DebugFont
static Color White
static readonly Matrix Identity
static readonly Vector2 Zero
static Vector2 Round(Vector2 v)
static readonly Vector2 One
static readonly Vector4 One