Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
FontTextWidget.cs
浏览该文件的文档.
1using Engine;
3using Engine.Media;
4
5namespace Game {
6 public class FontTextWidget : Widget {
7 public string m_text = string.Empty;
8
10
12
14
15 public float m_fontScale;
16
17 public int m_maxLines = int.MaxValue;
18
19 public bool m_wordWrap;
20
21 public bool m_ellipsis;
22
23 public List<string> m_lines = [];
24
26
27 public float? m_linesAvailableWidth;
28
30
31 public Vector2 Size { get; set; }
32
33 public virtual string Text {
34 get => m_text;
35 set {
36 if (m_text != value
37 && value != null) {
38 m_text = value;
39 m_linesSize = null;
40 }
41 }
42 }
43
44 public TextAnchor TextAnchor { get; set; }
45
47 get => m_textOrientation;
48 set {
49 if (value != m_textOrientation) {
50 m_textOrientation = value;
51 m_linesSize = null;
52 }
53 }
54 }
55
58 set {
59 if (value != m_font) {
60 m_font = value;
61 m_linesSize = null;
62 }
63 }
64 }
65
66 public float FontScale {
67 get => m_fontScale;
68 set {
69 if (value != m_fontScale) {
70 m_fontScale = value;
71 m_linesSize = null;
72 }
73 }
74 }
75
77 get => m_fontSpacing;
78 set {
79 if (value != m_fontSpacing) {
80 m_fontSpacing = value;
81 m_linesSize = null;
82 }
83 }
84 }
85
86 public bool WordWrap {
87 get => m_wordWrap;
88 set {
89 if (value != m_wordWrap) {
90 m_wordWrap = value;
91 m_linesSize = null;
92 }
93 }
94 }
95
96 public bool Ellipsis {
97 get => m_ellipsis;
98 set {
99 if (value != m_ellipsis) {
100 m_ellipsis = value;
101 m_linesSize = null;
102 }
103 }
104 }
105
106 public int MaxLines {
107 get => m_maxLines;
108 set {
109 if (value != m_maxLines) {
110 m_maxLines = value;
111 m_linesSize = null;
112 }
113 }
114 }
115
116 public Color Color { get; set; }
117
118 public bool DropShadow { get; set; }
119
120 public bool TextureLinearFilter { get; set; }
121 public bool m_usedebug;
122
123 public FontTextWidget() {
124 IsHitTestVisible = false;
125 FontScale = 1f;
126 Color = Color.White;
127 TextureLinearFilter = true;
128 Size = new Vector2(-1f);
129 }
130
131 public override void Draw(DrawContext dc) {
132 if (!string.IsNullOrEmpty(Text)
133 && Color.A != 0) {
135 FontBatch2D fontBatch2D = dc.PrimitivesRenderer2D.FontBatch(Font, 1, DepthStencilState.None, null, null, samplerState);
136 int count = fontBatch2D.TriangleVertices.Count;
137 float num = 0f;
138 if ((TextAnchor & TextAnchor.VerticalCenter) != 0) {
139 float num2 = Font.GlyphHeight * FontScale * Font.Scale
140 + (m_lines.Count - 1) * ((Font.GlyphHeight + Font.Spacing.Y) * FontScale * Font.Scale + FontSpacing.Y);
141 num = (ActualSize.Y - num2) / 2f;
142 }
143 else if ((TextAnchor & TextAnchor.Bottom) != 0) {
144 float num3 = Font.GlyphHeight * FontScale * Font.Scale
145 + (m_lines.Count - 1) * ((Font.GlyphHeight + Font.Spacing.Y) * FontScale * Font.Scale + FontSpacing.Y);
146 num = ActualSize.Y - num3;
147 }
150 float num4 = CalculateLineHeight();
151 foreach (string line in m_lines) {
152 float x = 0f;
153 if ((TextAnchor & TextAnchor.HorizontalCenter) != 0) {
154 x = ActualSize.X / 2f;
155 }
156 else if ((TextAnchor & TextAnchor.Right) != 0) {
157 x = ActualSize.X;
158 }
159 //bool flag = true;
160 Vector2 vector = Vector2.Zero;
161 float angle = 0f;
162 if (TextOrientation == TextOrientation.Horizontal) {
163 vector = new Vector2(x, num);
164 angle = 0f;
166 //flag = true;
167 }
168 else if (TextOrientation == TextOrientation.VerticalLeft) {
169 vector = new Vector2(x, ActualSize.Y + num);
170 angle = MathUtils.DegToRad(-90f);
171 //flag = true;
172 }
173 //if (flag)
174 //{
175 if (DropShadow) {
176 fontBatch2D.QueueText(
177 line,
178 vector + 1f * new Vector2(FontScale),
179 0f,
180 new Color((byte)0, (byte)0, (byte)0, color.A),
181 anchor,
182 new Vector2(FontScale),
184 angle
185 );
186 }
187 fontBatch2D.QueueText(
188 line,
189 vector,
190 0f,
191 color,
192 anchor,
193 new Vector2(FontScale),
195 angle
196 );
197 //}
198 num += num4;
199 }
200 fontBatch2D.TransformTriangles(GlobalTransform, count);
201 }
202 }
203
204 public override void MeasureOverride(Vector2 parentAvailableSize) {
205 IsDrawRequired = !string.IsNullOrEmpty(Text) && Color.A != 0;
206 if (TextOrientation == TextOrientation.Horizontal) {
207 UpdateLines(parentAvailableSize.X, parentAvailableSize.Y);
208 if (!m_linesSize.HasValue) {
209 return;
210 }
211 DesiredSize = new Vector2(Size.X < 0f ? m_linesSize.Value.X : Size.X, Size.Y < 0f ? m_linesSize.Value.Y : Size.Y);
212 }
213 else if (TextOrientation == TextOrientation.VerticalLeft) {
214 UpdateLines(parentAvailableSize.Y, parentAvailableSize.X);
215 if (!m_linesSize.HasValue) {
216 return;
217 }
218 DesiredSize = new Vector2(Size.X < 0f ? m_linesSize.Value.Y : Size.X, Size.Y < 0f ? m_linesSize.Value.X : Size.Y);
219 }
220 }
221
222 public float CalculateLineHeight() => (Font.GlyphHeight + Font.Spacing.Y + FontSpacing.Y) * FontScale * Font.Scale;
223
224 public virtual void UpdateLines(float availableWidth, float availableHeight) {
225 if (m_linesAvailableHeight.HasValue
226 && m_linesAvailableHeight == availableHeight
227 && m_linesAvailableWidth.HasValue
228 && m_linesSize.HasValue) {
229 float num = MathUtils.Min(m_linesSize.Value.X, m_linesAvailableWidth.Value) - 0.1f;
230 float num2 = MathUtils.Max(m_linesSize.Value.X, m_linesAvailableWidth.Value) + 0.1f;
231 if (availableWidth >= num
232 && availableWidth <= num2) {
233 return;
234 }
235 }
236 availableWidth += 0.1f;
237 m_lines.Clear();
238 string[] array = (Text ?? string.Empty).Split(["\n"], StringSplitOptions.None);
239 string text = "...";
240 float x = Font.MeasureText(text, new Vector2(FontScale), FontSpacing).X;
241 if (WordWrap) {
242 int num3 = (int)MathUtils.Min(MathF.Floor(availableHeight / CalculateLineHeight()), MaxLines);
243 for (int i = 0; i < array.Length; i++) {
244 string text2 = array[i].TrimEnd();
245 if (text2.Length == 0) {
246 m_lines.Add(string.Empty);
247 continue;
248 }
249 while (text2.Length > 0) {
250 bool flag;
251 int num4;
252 if (Ellipsis && m_lines.Count + 1 >= num3) {
253 num4 = Font.FitText(MathUtils.Max(availableWidth - x, 0f), text2, 0, text2.Length, FontScale, FontSpacing.X);
254 flag = true;
255 }
256 else {
257 num4 = Font.FitText(availableWidth, text2, 0, text2.Length, FontScale, FontSpacing.X);
258 num4 = MathUtils.Max(num4, 1);
259 flag = false;
260 if (num4 < text2.Length) {
261 int num5 = num4;
262 int num6 = num5 - 2;
263 while (num6 >= 0
264 && !char.IsWhiteSpace(text2[num6])
265 && !char.IsPunctuation(text2[num6])) {
266 num6--;
267 }
268 if (num6 < 0) {
269 num6 = num5 - 1;
270 }
271 num4 = num6 + 1;
272 }
273 }
274 string text3;
275 if (num4 == text2.Length) {
276 text3 = text2;
277 text2 = string.Empty;
278 }
279 else {
280 text3 = text2.Substring(0, num4).TrimEnd();
281 if (flag) {
282 text3 += text;
283 }
284 text2 = text2.Substring(num4, text2.Length - num4).TrimStart();
285 }
286 m_lines.Add(text3);
287 if (flag) {
288 break;
289 }
290 }
291 }
292 }
293 else if (Ellipsis) {
294 for (int j = 0; j < array.Length; j++) {
295 string text4 = array[j].TrimEnd();
296 int num7 = Font.FitText(MathUtils.Max(availableWidth - x, 0f), text4, 0, text4.Length, FontScale, FontSpacing.X);
297 if (num7 < text4.Length) {
298 m_lines.Add(text4.Substring(0, num7).TrimEnd() + text);
299 }
300 else {
301 m_lines.Add(text4);
302 }
303 }
304 }
305 else {
306 m_lines.AddRange(array);
307 }
308 if (m_lines.Count > MaxLines) {
309 m_lines = m_lines.Take(MaxLines).ToList();
310 }
311 Vector2 zero = Vector2.Zero;
312 for (int k = 0; k < m_lines.Count; k++) {
313 Vector2 vector = Font.MeasureText(m_lines[k], new Vector2(FontScale), FontSpacing);
314 zero.X = MathUtils.Max(zero.X, vector.X);
315 if (k < m_lines.Count - 1) {
316 zero.Y += (Font.GlyphHeight + Font.Spacing.Y + FontSpacing.Y) * FontScale * Font.Scale;
317 }
318 else {
319 zero.Y += Font.GlyphHeight * FontScale * Font.Scale;
320 }
321 }
322 m_linesSize = zero;
323 m_linesAvailableWidth = availableWidth;
324 m_linesAvailableHeight = availableHeight;
325 }
326 }
327}
readonly DynamicArray< VertexPositionColorTexture > TriangleVertices
static readonly DepthStencilState None
static Rectangle ScissorRectangle
FontBatch2D FontBatch(BitmapFont font=null, int layer=0, DepthStencilState depthStencilState=null, RasterizerState rasterizerState=null, BlendState blendState=null, SamplerState samplerState=null)
static int Min(int x1, int x2)
static int Max(int x1, int x2)
static float DegToRad(float degrees)
static BitmapFont DebugFont
override void Draw(DrawContext dc)
TextOrientation TextOrientation
TextOrientation m_textOrientation
virtual void UpdateLines(float availableWidth, float availableHeight)
override void MeasureOverride(Vector2 parentAvailableSize)
static BitmapFont BitmapFont
readonly PrimitivesRenderer2D PrimitivesRenderer2D
Color GlobalColorTransform
Vector2 DesiredSize
bool IsDrawRequired
virtual bool IsHitTestVisible
Vector2 ActualSize
Matrix GlobalTransform
static readonly Vector2 Zero