-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUiTextHelper.cs
More file actions
174 lines (146 loc) · 5.06 KB
/
UiTextHelper.cs
File metadata and controls
174 lines (146 loc) · 5.06 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using TMPro;
using UnityEngine;
namespace BlippoAccess
{
/// <summary>
/// Helpers for reading and normalizing UI text from decompiled menu components.
/// </summary>
internal static class UiTextHelper
{
public static string GetMenuButtonLabel(MenuButton menuButton)
{
if (menuButton == null)
{
return string.Empty;
}
var label = GetLocalizedText(menuButton.localizedText);
if (!string.IsNullOrWhiteSpace(label))
{
return label;
}
if (menuButton.button != null)
{
return CleanText(menuButton.button.gameObject.name);
}
return CleanText(menuButton.gameObject.name);
}
public static string GetMenuButtonValue(MenuButton menuButton)
{
if (menuButton == null)
{
return string.Empty;
}
var value = GetLocalizedText(menuButton.valueLocalizedText);
if (!string.IsNullOrWhiteSpace(value))
{
return value;
}
if (menuButton.valueLocalizedText != null && !string.IsNullOrWhiteSpace(menuButton.valueLocalizedText.overrideString))
{
return CleanText(menuButton.valueLocalizedText.overrideString);
}
return string.Empty;
}
public static string GetLocalizedText(NobleRobot.LocalizedText localizedText)
{
if (localizedText == null)
{
return string.Empty;
}
var appended = localizedText as NobleRobot.LocalizedTextControllerAppend;
if (appended != null && !string.IsNullOrWhiteSpace(appended.cachedTextAppend))
{
return CleanText(appended.cachedTextAppend);
}
if (!string.IsNullOrWhiteSpace(localizedText.cachedText))
{
return CleanText(localizedText.cachedText);
}
if (localizedText.textField != null && !string.IsNullOrWhiteSpace(localizedText.textField.text))
{
return CleanText(localizedText.textField.text);
}
localizedText.LanguageChangeHandler(false);
if (appended != null && !string.IsNullOrWhiteSpace(appended.cachedTextAppend))
{
return CleanText(appended.cachedTextAppend);
}
if (!string.IsNullOrWhiteSpace(localizedText.cachedText))
{
return CleanText(localizedText.cachedText);
}
return string.Empty;
}
public static string GetText(TMP_Text textField)
{
if (textField == null || string.IsNullOrWhiteSpace(textField.text))
{
return string.Empty;
}
return CleanText(textField.text);
}
public static bool IsSelectedObject(GameObject candidate, GameObject selectedObject)
{
return candidate != null &&
selectedObject != null &&
(selectedObject == candidate || selectedObject.transform.IsChildOf(candidate.transform));
}
public static bool IsMenuButtonInSubmenu(Submenu submenu, MenuButton menuButton)
{
if (submenu == null || menuButton == null)
{
return false;
}
if (submenu.menuButtons != null)
{
foreach (var button in submenu.menuButtons.Values)
{
if (button == menuButton)
{
return true;
}
}
}
if (menuButton.transform != null && submenu.transform != null && menuButton.transform.IsChildOf(submenu.transform))
{
return true;
}
var parentSubmenu = menuButton.GetComponentInParent<Submenu>(true);
return parentSubmenu == submenu;
}
public static string CleanText(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return string.Empty;
}
var input = value.Replace("(Clone)", string.Empty).Trim();
if (input.IndexOf('<') < 0 || input.IndexOf('>') < 0)
{
return input;
}
var buffer = new char[input.Length];
var index = 0;
var insideTag = false;
for (var i = 0; i < input.Length; i++)
{
var c = input[i];
if (c == '<')
{
insideTag = true;
continue;
}
if (c == '>')
{
insideTag = false;
continue;
}
if (!insideTag)
{
buffer[index++] = c;
}
}
return new string(buffer, 0, index).Trim();
}
}
}