Repository navigation
Expand file tree
/
Copy pathFocusHandler.cs
More file actions
343 lines (292 loc) · 13 KB
/
Copy pathFocusHandler.cs
File metadata and controls
343 lines (292 loc) · 13 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
using System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace BeforeTheStormAccess
{
/// <summary>
/// Announces the focused menu element and its value.
///
/// The game does NOT use NGUI's hover for keyboard/controller menus: every menu
/// owns a button list (T_51AF6A60) with SelectedButtonIndex, updated by both
/// keyboard navigation and mouse hover. We poll every active list and announce
/// selection changes. While focus stays on one element, its full text (labels +
/// slider percentage + bound key) is re-polled so value changes speak too.
/// NGUI's UICamera.hoveredObject is kept as a secondary source for elements
/// outside any list.
/// </summary>
public class FocusHandler
{
private const float PollInterval = 0.12f;
private float _nextPollTime;
private GameObject _currentFocus;
private string _currentFocusText = string.Empty;
private int _lastHoveredId;
private readonly Dictionary<int, int> _listSelection = new Dictionary<int, int>();
/// <summary>
/// Polls menu button lists, NGUI hover, and the focused element's value.
/// Runs inside the watcher's coalesced ambient chain.
/// </summary>
public void Update()
{
if (Time.unscaledTime < _nextPollTime)
return;
_nextPollTime = Time.unscaledTime + PollInterval;
PollButtonLists();
PollNguiHover();
PollFocusedValue();
}
private void PollButtonLists()
{
T_51AF6A60[] lists = Object.FindObjectsOfType<T_51AF6A60>();
if (lists == null)
return;
for (int i = 0; i < lists.Length; i++)
{
T_51AF6A60 list = lists[i];
if (list == null || !list.enabled)
continue;
int listId = list.GetInstanceID();
int index = list.SelectedButtonIndex;
int lastIndex;
bool seen = _listSelection.TryGetValue(listId, out lastIndex);
if (seen && lastIndex == index)
continue;
_listSelection[listId] = index;
T_9E2FDFCF selected = list.SelectedButton;
if (selected == null)
continue;
Announce(selected.gameObject);
}
}
private void PollNguiHover()
{
GameObject hovered = T_D90B5C2A.hoveredObject;
int id = hovered != null ? hovered.GetInstanceID() : 0;
if (id == _lastHoveredId)
return;
_lastHoveredId = id;
if (hovered == null || (_currentFocus != null && hovered == _currentFocus))
return;
// Elements inside a button list are announced by the primary poll.
T_9E2FDFCF button = hovered.GetComponent<T_9E2FDFCF>();
if (button != null && button.m_menu != null)
return;
Announce(hovered);
}
/// <summary>
/// Re-reads the focused element while focus is steady: option cycling,
/// slider drags, and key rebinding all change its full text in place.
/// </summary>
private void PollFocusedValue()
{
if (_currentFocus == null)
return;
T_9E2FDFCF button = _currentFocus.GetComponent<T_9E2FDFCF>();
if (button != null && !button.Selected)
return;
string fullText = BuildFullText(_currentFocus);
if (fullText.Length == 0 || fullText == _currentFocusText)
return;
_currentFocusText = fullText;
DebugLogger.Log(LogCategory.Handler, "Focus", "value change -> " + fullText);
ScreenReader.SayCoalesced(fullText, true, true);
}
private void Announce(GameObject focused)
{
if (focused == _currentFocus)
return;
// Dialogue/backtalk choice buttons are read by the choice announcer
// with their direction names; skip them here to avoid double speech.
if (focused.GetComponent<T_B611E532>() != null)
return;
string text = BuildFullText(focused);
if (text.Length == 0)
return;
_currentFocus = focused;
_currentFocusText = text;
DebugLogger.Log(LogCategory.Handler, "Focus", focused.name + " -> " + text);
ScreenReader.SayCoalesced(text, true, true);
}
/// <summary>
/// Full spoken text for an element: visible labels left-to-right, then
/// slider percentage and bound key name when those components are present.
/// </summary>
private static string BuildFullText(GameObject focused)
{
StringBuilder builder = new StringBuilder(ComposeLabelText(focused));
T_AD683E92 sliderButton = focused.GetComponent<T_AD683E92>();
if (sliderButton != null && sliderButton.m_slider != null)
{
int percent = Mathf.RoundToInt(sliderButton.m_slider.value * 100f);
if (builder.Length > 0)
builder.Append(", ");
builder.Append(Loc.Get("slider_percent", percent));
}
T_55D727DD keybind = focused.GetComponent<T_55D727DD>();
if (keybind == null)
keybind = focused.GetComponentInParent<T_55D727DD>();
if (keybind != null)
{
if (builder.Length > 0)
builder.Append(", ");
builder.Append(keybind.m_isKeybinding
? Loc.Get("press_key_to_bind")
: PrettyKeyName(keybind.m_boundKey));
}
return builder.ToString();
}
/// <summary>
/// Builds text from an element's visible labels, left to right
/// (settings rows: Title label left, value label right).
/// </summary>
private static string ComposeLabelText(GameObject focused)
{
// SMS contact rows (burner phone + journal SMS tab) carry the contact name
// and unread count on the sibling T_EF99895D component, not under the
// button's own GameObject, so the generic label sweep finds nothing. Read
// the contact directly when the focused button belongs to one.
string contact = ComposeSmsContact(focused);
if (contact.Length > 0)
return contact;
T_A243E23D[] labels = focused.GetComponentsInChildren<T_A243E23D>(false);
if (labels == null || labels.Length == 0)
return string.Empty;
List<LabeledText> parts = new List<LabeledText>();
for (int i = 0; i < labels.Length; i++)
{
T_A243E23D label = labels[i];
if (label == null || label.alpha <= 0.01f)
continue;
string text = TextUtil.CleanLabel(label.text);
if (text.Length == 0)
continue;
LabeledText part = new LabeledText();
part.Text = text;
part.X = label.transform.position.x;
parts.Add(part);
}
if (parts.Count == 0)
return string.Empty;
parts.Sort(CompareByX);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < parts.Count; i++)
{
if (i > 0)
builder.Append(", ");
builder.Append(parts[i].Text);
}
return builder.ToString();
}
/// <summary>
/// "{contact name}[, N new messages]" when the focused element is an SMS
/// contact row, else empty. The contact name and unread count live on the
/// T_EF99895D component (found on the focused object or a parent), whose
/// name/new-message labels are toggled by selection — so read the resolved
/// name and unread count from its fields rather than sweeping child labels.
/// </summary>
private static string ComposeSmsContact(GameObject focused)
{
T_EF99895D contact = focused.GetComponent<T_EF99895D>();
if (contact == null)
contact = focused.GetComponentInParent<T_EF99895D>();
if (contact == null)
return string.Empty;
string name = contact.m_name != null ? TextUtil.CleanLabel(contact.m_name.text) : string.Empty;
if (name.Length == 0)
return string.Empty;
// The unread-count label ("1 new message" / "N new messages") is resolved
// and localized by the game; read whichever variant is currently shown.
string unread = ReadNewMessageLabel(contact);
if (unread.Length > 0)
return Loc.Get("sms_contact_unread", name, unread);
return name;
}
/// <summary>The active new-message label text on a contact row, or empty.</summary>
private static string ReadNewMessageLabel(T_EF99895D contact)
{
string text = LabelIfActive(contact.m_highlightedNewMessage);
if (text.Length == 0)
text = LabelIfActive(contact.m_newMessage);
return text;
}
private static string LabelIfActive(T_A243E23D label)
{
if (label == null || !label.gameObject.activeInHierarchy)
return string.Empty;
return TextUtil.CleanLabel(label.text);
}
/// <summary>
/// Localization keys the game's own keybinding row uses to name keys, so the
/// mod speaks the same translated names the settings screen shows.
/// </summary>
private static readonly Dictionary<KeyCode, string> _gameKeyNames = new Dictionary<KeyCode, string>
{
{ KeyCode.Mouse0, "GLOBAL_CODE_LEFT_MOUSE_BUTTON" },
{ KeyCode.Mouse1, "GLOBAL_CODE_RIGHT_MOUSE_BUTTON" },
{ KeyCode.Mouse2, "GLOBAL_CODE_MIDDLE_MOUSE_BUTTON" },
{ KeyCode.Mouse3, "GLOBAL_CODE_MOUSE_BUTTON_3" },
{ KeyCode.Mouse4, "GLOBAL_CODE_MOUSE_BUTTON_4" },
{ KeyCode.Mouse5, "GLOBAL_CODE_MOUSE_BUTTON_5" },
{ KeyCode.UpArrow, "GLOBAL_CODE_KEY_UPARROW" },
{ KeyCode.DownArrow, "GLOBAL_CODE_KEY_DOWNARROW" },
{ KeyCode.LeftArrow, "GLOBAL_CODE_KEY_LEFTARROW" },
{ KeyCode.RightArrow, "GLOBAL_CODE_KEY_RIGHTARROW" },
{ KeyCode.RightAlt, "GLOBAL_CODE_KEY_RIGHTALT" },
{ KeyCode.LeftAlt, "GLOBAL_CODE_KEY_LEFTALT" },
{ KeyCode.RightControl, "GLOBAL_CODE_KEY_RIGHTCTRL" },
{ KeyCode.LeftControl, "GLOBAL_CODE_KEY_LEFTCTRL" },
{ KeyCode.Delete, "GLOBAL_CODE_KEY_DELETE" },
{ KeyCode.End, "GLOBAL_CODE_KEY_END" },
{ KeyCode.Return, "GLOBAL_CODE_KEY_ENTER" },
{ KeyCode.Home, "GLOBAL_CODE_KEY_HOME" },
{ KeyCode.Insert, "GLOBAL_CODE_KEY_INSERT" },
{ KeyCode.PageUp, "GLOBAL_CODE_KEY_PAGEUP" },
{ KeyCode.PageDown, "GLOBAL_CODE_KEY_PAGEDOWN" },
{ KeyCode.RightShift, "GLOBAL_CODE_KEY_RIGHTSHIFT" },
{ KeyCode.LeftShift, "GLOBAL_CODE_KEY_LEFTSHIFT" },
{ KeyCode.Space, "GLOBAL_CODE_KEY_SPACE" },
{ KeyCode.Tab, "GLOBAL_CODE_KEY_TAB" },
};
/// <summary>
/// Speech-friendly key name in the game's text language: the game's own
/// translated name where it has one, digits for number-row keys, otherwise
/// the KeyCode name with camel-case splits (letters and F-keys need no
/// translation).
/// </summary>
internal static string PrettyKeyName(KeyCode key)
{
if (key == KeyCode.None)
return Loc.Get("key_unbound");
if (key == KeyCode.BackQuote)
return Loc.Get("key_grave");
string name = key.ToString();
string gameKey;
if (_gameKeyNames.TryGetValue(key, out gameKey))
{
string gameName = GameRefs.GameText(gameKey);
if (gameName.Length > 0)
return gameName;
}
if (key >= KeyCode.Alpha0 && key <= KeyCode.Alpha9)
return ((int)(key - KeyCode.Alpha0)).ToString();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < name.Length; i++)
{
char c = name[i];
if (i > 0 && char.IsUpper(c) && !char.IsUpper(name[i - 1]))
builder.Append(' ');
builder.Append(c);
}
return builder.ToString();
}
private struct LabeledText
{
public string Text;
public float X;
}
private static int CompareByX(LabeledText a, LabeledText b)
{
return a.X.CompareTo(b.X);
}
}
}