-
Notifications
You must be signed in to change notification settings - Fork 0
/
Text.cs
68 lines (59 loc) · 2.54 KB
/
Text.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System.Text;
using JetBrains.Annotations;
using UnityEngine.UIElements;
namespace UI.Li.Common
{
/// <summary>
/// Component representing <see cref="UnityEngine.UIElements.Label"/>.
/// </summary>
[PublicAPI] public sealed class Text: Element<Label>
{
[NotNull] private readonly string text;
private readonly string tooltip;
/// <summary>
/// Creates <see cref="Text"/> instance with given text.
/// </summary>
/// <param name="text">text to be displayed</param>
/// <param name="manipulators">manipulators <seealso cref="IManipulator"/></param>
/// <returns></returns>
[NotNull]
public static Text V([NotNull] string text, params IManipulator[] manipulators) =>
new(text, manipulators);
private static string ToLiteral(string input) {
var literal = new StringBuilder(input.Length + 2);
foreach (var c in input) {
switch (c) {
case '\"': literal.Append("\\\""); break;
case '\\': literal.Append(@"\\"); break;
case '\0': literal.Append(@"\0"); break;
case '\a': literal.Append(@"\a"); break;
case '\b': literal.Append(@"\b"); break;
case '\f': literal.Append(@"\f"); break;
case '\n': literal.Append(@"\n"); break;
case '\r': literal.Append(@"\r"); break;
case '\t': literal.Append(@"\t"); break;
case '\v': literal.Append(@"\v"); break;
default:
// ASCII printable character
if (c >= 0x20 && c <= 0x7e) {
literal.Append(c);
// As UTF16 escaped character
} else {
literal.Append(@"\u");
literal.Append(((int)c).ToString("x4"));
}
break;
}
}
return literal.ToString();
}
public override string ToString() => $"Text \"{ToLiteral(text)}\"";
public override bool StateLayoutEquals(IComponent other) => other is Text;
private Text([NotNull] string text, IManipulator[] manipulators) : base(manipulators) => this.text = text;
protected override Label PrepareElement(Label target)
{
target.text = text;
return target;
}
}
}