Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ChatManager.cs
Go to the documentation of this file.
1using System;
8using Terraria.Chat;
10
11namespace Terraria.UI.Chat;
12
13public static class ChatManager
14{
15 public static class Regexes
16 {
17 public static readonly Regex Format = new Regex("(?<!\\\\)\\[(?<tag>[a-zA-Z]{1,10})(\\/(?<options>[^:]+))?:(?<text>.+?)(?<!\\\\)\\]", RegexOptions.Compiled);
18 }
19
20 public static readonly ChatCommandProcessor Commands = new ChatCommandProcessor();
21
23
24 public static readonly Vector2[] ShadowDirections = new Vector2[4]
25 {
29 Vector2.UnitY
30 };
31
32 public static Color WaveColor(Color color)
33 {
34 float num = (float)(int)Main.mouseTextColor / 255f;
35 color = Color.Lerp(color, Color.Black, 1f - num);
36 color.A = Main.mouseTextColor;
37 return color;
38 }
39
40 public static void ConvertNormalSnippets(TextSnippet[] snippets)
41 {
42 for (int i = 0; i < snippets.Length; i++)
43 {
44 TextSnippet textSnippet = snippets[i];
45 if (snippets[i].GetType() == typeof(TextSnippet))
46 {
48 snippets[i] = plainSnippet;
49 }
50 }
51 }
52
53 public static void Register<T>(params string[] names) where T : ITagHandler, new()
54 {
55 T val = new T();
56 for (int i = 0; i < names.Length; i++)
57 {
58 _handlers[names[i].ToLower()] = val;
59 }
60 }
61
62 private static ITagHandler GetHandler(string tagName)
63 {
64 string key = tagName.ToLower();
65 if (_handlers.ContainsKey(key))
66 {
67 return _handlers[key];
68 }
69 return null;
70 }
71
73 {
74 text = text.Replace("\r", "");
77 int num = 0;
78 foreach (Match item in matchCollection)
79 {
80 if (item.Index > num)
81 {
82 list.Add(new TextSnippet(text.Substring(num, item.Index - num), baseColor));
83 }
84 num = item.Index + item.Length;
85 string value = item.Groups["tag"].Value;
86 string value2 = item.Groups["text"].Value;
87 string value3 = item.Groups["options"].Value;
88 ITagHandler handler = GetHandler(value);
89 if (handler != null)
90 {
91 list.Add(handler.Parse(value2, baseColor, value3));
92 list[list.Count - 1].TextOriginal = item.ToString();
93 }
94 else
95 {
97 }
98 }
99 if (text.Length > num)
100 {
101 list.Add(new TextSnippet(text.Substring(num, text.Length - num), baseColor));
102 }
103 return list;
104 }
105
107 {
108 int num = 470;
109 num = Main.screenWidth - 330;
110 if (GetStringSize(font, Main.chatText + text, baseScale).X > (float)num)
111 {
112 return false;
113 }
114 Main.chatText += text;
115 return true;
116 }
117
119 {
120 TextSnippet[] snippets = ParseMessage(text, Color.White).ToArray();
121 return GetStringSize(font, snippets, baseScale, maxWidth);
122 }
123
125 {
129 Vector2 result = vector;
130 float x = font.MeasureString(" ").X;
131 float num = 1f;
132 float num2 = 0f;
133 foreach (TextSnippet textSnippet in snippets)
134 {
135 textSnippet.Update();
136 num = textSnippet.Scale;
137 if (textSnippet.UniqueDraw(justCheckingString: true, out var size, null))
138 {
139 vector.X += size.X * baseScale.X * num;
140 result.X = Math.Max(result.X, vector.X);
141 result.Y = Math.Max(result.Y, vector.Y + size.Y);
142 continue;
143 }
144 string[] array = textSnippet.Text.Split('\n');
145 string[] array2 = array;
146 for (int j = 0; j < array2.Length; j++)
147 {
148 string[] array3 = array2[j].Split(' ');
149 for (int k = 0; k < array3.Length; k++)
150 {
151 if (k != 0)
152 {
153 vector.X += x * baseScale.X * num;
154 }
155 if (maxWidth > 0f)
156 {
157 float num3 = font.MeasureString(array3[k]).X * baseScale.X * num;
158 if (vector.X - zero.X + num3 > maxWidth)
159 {
160 vector.X = zero.X;
161 vector.Y += (float)font.LineSpacing * num2 * baseScale.Y;
162 result.Y = Math.Max(result.Y, vector.Y);
163 num2 = 0f;
164 }
165 }
166 if (num2 < num)
167 {
168 num2 = num;
169 }
170 Vector2 vector2 = font.MeasureString(array3[k]);
171 vec.Between(vector, vector + vector2);
172 vector.X += vector2.X * baseScale.X * num;
173 result.X = Math.Max(result.X, vector.X);
174 result.Y = Math.Max(result.Y, vector.Y + vector2.Y);
175 }
176 if (array.Length > 1)
177 {
178 vector.X = zero.X;
179 vector.Y += (float)font.LineSpacing * num2 * baseScale.Y;
180 result.Y = Math.Max(result.Y, vector.Y);
181 num2 = 0f;
182 }
183 }
184 }
185 return result;
186 }
187
188 public static void DrawColorCodedStringShadow(SpriteBatch spriteBatch, DynamicSpriteFont font, TextSnippet[] snippets, Vector2 position, Color baseColor, float rotation, Vector2 origin, Vector2 baseScale, float maxWidth = -1f, float spread = 2f)
189 {
190 for (int i = 0; i < ShadowDirections.Length; i++)
191 {
192 DrawColorCodedString(spriteBatch, font, snippets, position + ShadowDirections[i] * spread, baseColor, rotation, origin, baseScale, out var _, maxWidth, ignoreColors: true);
193 }
194 }
195
196 public static Vector2 DrawColorCodedString(SpriteBatch spriteBatch, DynamicSpriteFont font, TextSnippet[] snippets, Vector2 position, Color baseColor, float rotation, Vector2 origin, Vector2 baseScale, out int hoveredSnippet, float maxWidth, bool ignoreColors = false)
197 {
198 int num = -1;
200 Vector2 vector = position;
201 Vector2 result = vector;
202 float x = font.MeasureString(" ").X;
203 Color color = baseColor;
204 float num2 = 1f;
205 float num3 = 0f;
206 for (int i = 0; i < snippets.Length; i++)
207 {
208 TextSnippet textSnippet = snippets[i];
209 textSnippet.Update();
210 if (!ignoreColors)
211 {
212 color = textSnippet.GetVisibleColor();
213 }
214 num2 = textSnippet.Scale;
215 if (textSnippet.UniqueDraw(justCheckingString: false, out var size, spriteBatch, vector, color, num2))
216 {
217 if (vec.Between(vector, vector + size))
218 {
219 num = i;
220 }
221 vector.X += size.X * baseScale.X * num2;
222 result.X = Math.Max(result.X, vector.X);
223 continue;
224 }
225 string[] array = textSnippet.Text.Split('\n');
226 array = Regex.Split(textSnippet.Text, "(\n)");
227 bool flag = true;
228 foreach (string text in array)
229 {
230 string[] array2 = Regex.Split(text, "( )");
231 array2 = text.Split(' ');
232 if (text == "\n")
233 {
234 vector.Y += (float)font.LineSpacing * num3 * baseScale.Y;
235 vector.X = position.X;
236 result.Y = Math.Max(result.Y, vector.Y);
237 num3 = 0f;
238 flag = false;
239 continue;
240 }
241 for (int k = 0; k < array2.Length; k++)
242 {
243 if (k != 0)
244 {
245 vector.X += x * baseScale.X * num2;
246 }
247 if (maxWidth > 0f)
248 {
249 float num4 = font.MeasureString(array2[k]).X * baseScale.X * num2;
250 if (vector.X - position.X + num4 > maxWidth)
251 {
252 vector.X = position.X;
253 vector.Y += (float)font.LineSpacing * num3 * baseScale.Y;
254 result.Y = Math.Max(result.Y, vector.Y);
255 num3 = 0f;
256 }
257 }
258 if (num3 < num2)
259 {
260 num3 = num2;
261 }
262 DynamicSpriteFontExtensionMethods.DrawString(spriteBatch, font, array2[k], vector, color, rotation, origin, baseScale * textSnippet.Scale * num2, SpriteEffects.None, 0f);
263 Vector2 vector2 = font.MeasureString(array2[k]);
264 if (vec.Between(vector, vector + vector2))
265 {
266 num = i;
267 }
268 vector.X += vector2.X * baseScale.X * num2;
269 result.X = Math.Max(result.X, vector.X);
270 }
271 if (array.Length > 1 && flag)
272 {
273 vector.Y += (float)font.LineSpacing * num3 * baseScale.Y;
274 vector.X = position.X;
275 result.Y = Math.Max(result.Y, vector.Y);
276 num3 = 0f;
277 }
278 flag = true;
279 }
280 }
281 hoveredSnippet = num;
282 return result;
283 }
284
285 public static Vector2 DrawColorCodedStringWithShadow(SpriteBatch spriteBatch, DynamicSpriteFont font, TextSnippet[] snippets, Vector2 position, float rotation, Vector2 origin, Vector2 baseScale, out int hoveredSnippet, float maxWidth = -1f, float spread = 2f)
286 {
287 DrawColorCodedStringShadow(spriteBatch, font, snippets, position, Color.Black, rotation, origin, baseScale, maxWidth, spread);
288 return DrawColorCodedString(spriteBatch, font, snippets, position, Color.White, rotation, origin, baseScale, out hoveredSnippet, maxWidth);
289 }
290
291 public static Vector2 DrawColorCodedStringWithShadow(SpriteBatch spriteBatch, DynamicSpriteFont font, TextSnippet[] snippets, Vector2 position, float rotation, Color color, Vector2 origin, Vector2 baseScale, out int hoveredSnippet, float maxWidth = -1f, float spread = 2f)
292 {
293 DrawColorCodedStringShadow(spriteBatch, font, snippets, position, Color.Black, rotation, origin, baseScale, maxWidth, spread);
294 return DrawColorCodedString(spriteBatch, font, snippets, position, color, rotation, origin, baseScale, out hoveredSnippet, maxWidth, ignoreColors: true);
295 }
296
297 public static void DrawColorCodedStringShadow(SpriteBatch spriteBatch, DynamicSpriteFont font, string text, Vector2 position, Color baseColor, float rotation, Vector2 origin, Vector2 baseScale, float maxWidth = -1f, float spread = 2f)
298 {
299 for (int i = 0; i < ShadowDirections.Length; i++)
300 {
301 DrawColorCodedString(spriteBatch, font, text, position + ShadowDirections[i] * spread, baseColor, rotation, origin, baseScale, maxWidth, ignoreColors: true);
302 }
303 }
304
305 public static Vector2 DrawColorCodedString(SpriteBatch spriteBatch, DynamicSpriteFont font, string text, Vector2 position, Color baseColor, float rotation, Vector2 origin, Vector2 baseScale, float maxWidth = -1f, bool ignoreColors = false)
306 {
307 Vector2 vector = position;
308 Vector2 result = vector;
309 string[] array = text.Split('\n');
310 float x = font.MeasureString(" ").X;
311 Color color = baseColor;
312 float num = 1f;
313 float num2 = 0f;
314 string[] array2 = array;
315 for (int i = 0; i < array2.Length; i++)
316 {
317 string[] array3 = array2[i].Split(':');
318 foreach (string text2 in array3)
319 {
320 if (text2.StartsWith("sss"))
321 {
322 if (text2.StartsWith("sss1"))
323 {
324 if (!ignoreColors)
325 {
326 color = Color.Red;
327 }
328 }
329 else if (text2.StartsWith("sss2"))
330 {
331 if (!ignoreColors)
332 {
333 color = Color.Blue;
334 }
335 }
336 else if (text2.StartsWith("sssr") && !ignoreColors)
337 {
338 color = Color.White;
339 }
340 continue;
341 }
342 string[] array4 = text2.Split(' ');
343 for (int k = 0; k < array4.Length; k++)
344 {
345 if (k != 0)
346 {
347 vector.X += x * baseScale.X * num;
348 }
349 if (maxWidth > 0f)
350 {
351 float num3 = font.MeasureString(array4[k]).X * baseScale.X * num;
352 if (vector.X - position.X + num3 > maxWidth)
353 {
354 vector.X = position.X;
355 vector.Y += (float)font.LineSpacing * num2 * baseScale.Y;
356 result.Y = Math.Max(result.Y, vector.Y);
357 num2 = 0f;
358 }
359 }
360 if (num2 < num)
361 {
362 num2 = num;
363 }
364 DynamicSpriteFontExtensionMethods.DrawString(spriteBatch, font, array4[k], vector, color, rotation, origin, baseScale * num, SpriteEffects.None, 0f);
365 vector.X += font.MeasureString(array4[k]).X * baseScale.X * num;
366 result.X = Math.Max(result.X, vector.X);
367 }
368 }
369 vector.X = position.X;
370 vector.Y += (float)font.LineSpacing * num2 * baseScale.Y;
371 result.Y = Math.Max(result.Y, vector.Y);
372 num2 = 0f;
373 }
374 return result;
375 }
376
377 public static Vector2 DrawColorCodedStringWithShadow(SpriteBatch spriteBatch, DynamicSpriteFont font, string text, Vector2 position, Color baseColor, float rotation, Vector2 origin, Vector2 baseScale, float maxWidth = -1f, float spread = 2f)
378 {
379 TextSnippet[] snippets = ParseMessage(text, baseColor).ToArray();
380 ConvertNormalSnippets(snippets);
381 DrawColorCodedStringShadow(spriteBatch, font, snippets, position, new Color(0, 0, 0, baseColor.A), rotation, origin, baseScale, maxWidth, spread);
382 int hoveredSnippet;
383 return DrawColorCodedString(spriteBatch, font, snippets, position, Color.White, rotation, origin, baseScale, out hoveredSnippet, maxWidth);
384 }
385}
static void DrawString(this SpriteBatch spriteBatch, DynamicSpriteFont spriteFont, string text, Vector2 position, Color color)
static byte Max(byte val1, byte val2)
Definition Math.cs:738
static string[] Split(string input, string pattern)
Definition Regex.cs:656
static string chatText
Definition Main.cs:1743
static byte mouseTextColor
Definition Main.cs:1751
static int mouseY
Definition Main.cs:606
static int mouseX
Definition Main.cs:604
static void DrawColorCodedStringShadow(SpriteBatch spriteBatch, DynamicSpriteFont font, string text, Vector2 position, Color baseColor, float rotation, Vector2 origin, Vector2 baseScale, float maxWidth=-1f, float spread=2f)
static void DrawColorCodedStringShadow(SpriteBatch spriteBatch, DynamicSpriteFont font, TextSnippet[] snippets, Vector2 position, Color baseColor, float rotation, Vector2 origin, Vector2 baseScale, float maxWidth=-1f, float spread=2f)
static readonly Vector2[] ShadowDirections
static bool AddChatText(DynamicSpriteFont font, string text, Vector2 baseScale)
static Color WaveColor(Color color)
static Vector2 DrawColorCodedStringWithShadow(SpriteBatch spriteBatch, DynamicSpriteFont font, TextSnippet[] snippets, Vector2 position, float rotation, Color color, Vector2 origin, Vector2 baseScale, out int hoveredSnippet, float maxWidth=-1f, float spread=2f)
static Vector2 GetStringSize(DynamicSpriteFont font, TextSnippet[] snippets, Vector2 baseScale, float maxWidth=-1f)
static Vector2 DrawColorCodedString(SpriteBatch spriteBatch, DynamicSpriteFont font, TextSnippet[] snippets, Vector2 position, Color baseColor, float rotation, Vector2 origin, Vector2 baseScale, out int hoveredSnippet, float maxWidth, bool ignoreColors=false)
static ConcurrentDictionary< string, ITagHandler > _handlers
static Vector2 DrawColorCodedStringWithShadow(SpriteBatch spriteBatch, DynamicSpriteFont font, TextSnippet[] snippets, Vector2 position, float rotation, Vector2 origin, Vector2 baseScale, out int hoveredSnippet, float maxWidth=-1f, float spread=2f)
static Vector2 DrawColorCodedString(SpriteBatch spriteBatch, DynamicSpriteFont font, string text, Vector2 position, Color baseColor, float rotation, Vector2 origin, Vector2 baseScale, float maxWidth=-1f, bool ignoreColors=false)
static Vector2 DrawColorCodedStringWithShadow(SpriteBatch spriteBatch, DynamicSpriteFont font, string text, Vector2 position, Color baseColor, float rotation, Vector2 origin, Vector2 baseScale, float maxWidth=-1f, float spread=2f)
static void Register< T >(params string[] names)
static void ConvertNormalSnippets(TextSnippet[] snippets)
static Vector2 GetStringSize(DynamicSpriteFont font, string text, Vector2 baseScale, float maxWidth=-1f)
static ITagHandler GetHandler(string tagName)
static List< TextSnippet > ParseMessage(string text, Color baseColor)
TextSnippet Parse(string text, Color baseColor=default(Color), string options=null)
static Color Lerp(Color value1, Color value2, float amount)
Definition Color.cs:491