Client LuaCsForBarotrauma
BarotraumaShared/SharedSource/Utils/RichTextData.cs
1 using Microsoft.Xna.Framework;
2 using System.Collections.Generic;
3 using System.Collections.Immutable;
4 
5 namespace Barotrauma
6 {
7  public class RichTextData
8  {
9  public int StartIndex, EndIndex;
10  public Color? Color;
11  public string Metadata;
12 
13  public float Alpha = 1.0f;
14 
15  private const char definitionIndicator = '‖';
16  private const char attributeSeparator = ';';
17  private const char keyValueSeparator = ':';
18  //private const char lineChangeIndicator = '\n';
19 
20  private const string colorDefinition = "color";
21  private const string metadataDefinition = "metadata";
22  private const string endDefinition = "end";
23 
24  public static ImmutableArray<RichTextData>? GetRichTextData(string text, out string sanitizedText)
25  {
26  sanitizedText = text;
27  if (!string.IsNullOrEmpty(text) && text.Contains(definitionIndicator, System.StringComparison.Ordinal))
28  {
29  text = text.Replace("\r", "");
30  string[] segments = text.Split(definitionIndicator);
31 
32  sanitizedText = string.Empty;
33 
34  List<RichTextData> textColors = new List<RichTextData>();
35  RichTextData tempData = null;
36 
37  int prevIndex = 0;
38  int currIndex = 0;
39  for (int i = 0; i < segments.Length; i++)
40  {
41  if (i % 2 == 0)
42  {
43  sanitizedText += segments[i];
44  prevIndex = currIndex;
45  currIndex += segments[i].Replace("\n", "").Replace("\r", "").Length;
46  }
47  else
48  {
49  string[] attributes = segments[i].Split(attributeSeparator);
50  for (int j = 0; j < attributes.Length; j++)
51  {
52  if (attributes[j].Contains(endDefinition, System.StringComparison.OrdinalIgnoreCase))
53  {
54  if (tempData != null)
55  {
56  tempData.StartIndex = prevIndex;
57  tempData.EndIndex = currIndex - 1;
58  textColors.Add(tempData);
59  }
60  tempData = null;
61  }
62  else if (attributes[j].StartsWith(colorDefinition, System.StringComparison.OrdinalIgnoreCase))
63  {
64  if (tempData == null) { tempData = new RichTextData(); }
65  string valueStr = attributes[j].Substring(attributes[j].IndexOf(keyValueSeparator) + 1);
66  if (valueStr.Equals("null", System.StringComparison.InvariantCultureIgnoreCase))
67  {
68  tempData.Color = null;
69  }
70  else
71  {
72  tempData.Color = XMLExtensions.ParseColor(valueStr);
73  }
74  }
75  else if (attributes[j].StartsWith(metadataDefinition, System.StringComparison.OrdinalIgnoreCase))
76  {
77  if (tempData == null) { tempData = new RichTextData(); }
78  tempData.Metadata = attributes[j].Substring(attributes[j].IndexOf(keyValueSeparator) + 1);
79  }
80  }
81  }
82  }
83  return textColors.ToImmutableArray();
84  }
85  return null;
86  }
87  }
88 }
static ? ImmutableArray< RichTextData > GetRichTextData(string text, out string sanitizedText)