-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIAccessibility.cs
More file actions
545 lines (472 loc) · 19.5 KB
/
Copy pathUIAccessibility.cs
File metadata and controls
545 lines (472 loc) · 19.5 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace RavenfieldAccessibility
{
public class UIAccessibility : MonoBehaviour
{
private GameObject _lastSelected;
private string _lastDropdownValue;
private float _sliderRepeatTimer;
private bool _wasInMenu;
private const float SLIDER_REPEAT_DELAY = 0.3f;
private void Update()
{
// Update death loadout timer (must run even when not in menu)
if (GameManager.IsIngame())
Patches.ActorPatches.UpdateDeathLoadout();
if (EventSystem.current == null) return;
bool inMenu = IsInMenuContext();
// When transitioning from menu to gameplay, clear selection
if (_wasInMenu && !inMenu)
{
EventSystem.current.SetSelectedGameObject(null);
_lastSelected = null;
}
_wasInMenu = inMenu;
if (!inMenu) return;
// Handle Options tab switching with Ctrl+Tab / Ctrl+Shift+Tab
if (Options.IsOpen() && Options.instance != null)
{
if (Input.GetKeyDown(KeyCode.Tab) && (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)))
{
bool reverse = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
int current = Patches.MenuPatches.GetCurrentTabIndex();
int next = reverse ? current - 1 : current + 1;
if (next > 2) next = 0;
if (next < 0) next = 2;
Options.instance.OpenTab(next);
return;
}
}
// Defer to PickerKeyboardNav when a picker page is active
bool pickerActive = UIPickerBase.instance != null && UIPickerBase.instance.gameObject.activeInHierarchy;
if (!pickerActive)
{
HandleMenuKeyboardNavigation();
HandleSliderKeys();
}
GameObject selected = EventSystem.current.currentSelectedGameObject;
// Only announce elements that are actually on the current active panel
if (selected != _lastSelected)
{
_lastSelected = selected;
if (selected != null && selected.activeInHierarchy && IsOnActivePanel(selected))
{
AnnounceElement(selected);
}
else if (selected != null && !IsOnActivePanel(selected))
{
// Stale selection from a different page - clear it
EventSystem.current.SetSelectedGameObject(null);
_lastSelected = null;
}
}
if (selected != null)
{
CheckDropdownChange(selected);
}
}
/// <summary>
/// Strict check: are we in a context where menu navigation should be active?
/// Includes loadout UI during death/respawn for accessibility.
/// </summary>
private bool IsInMenuContext()
{
// Main menu scene
if (GameManager.IsInMainMenu()) return true;
// In-game: only specific menus count
if (GameManager.IsIngame())
{
if (IngameMenuUi.instance != null && IngameMenuUi.IsOpen()) return true;
if (Options.IsOpen()) return true;
// Loadout UI is handled by LoadoutKeyboardNav, not here
return false;
}
return false;
}
/// <summary>
/// Get the currently active menu panel GameObject so we can filter selectables.
/// </summary>
private GameObject GetActivePanel()
{
// Main menu: use the currently visible page
if (GameManager.IsInMainMenu() && MainMenu.instance != null)
{
return GetMainMenuActivePage();
}
// Pause menu
if (IngameMenuUi.instance != null && IngameMenuUi.IsOpen())
{
return IngameMenuUi.instance.pauseMenuContainer.activeInHierarchy
? IngameMenuUi.instance.pauseMenuContainer
: IngameMenuUi.instance.surrenderMenuContainer;
}
// Options - return the currently active tab canvas
if (Options.IsOpen() && Options.instance != null)
{
// Check which tab canvas is enabled (Game, Input, or Video)
Canvas[] tabCanvases = new Canvas[] {
Options.instance.gameOptions,
Options.instance.inputOptions,
Options.instance.videoOptions
};
foreach (var tabCanvas in tabCanvases)
{
if (tabCanvas != null && ((UnityEngine.Behaviour)tabCanvas).enabled)
return ((Component)tabCanvas).gameObject;
}
return Options.instance.gameObject;
}
return null;
}
private GameObject GetMainMenuActivePage()
{
// The pages array is private, but the page GameObjects are public fields.
// Check which one is active.
var mm = MainMenu.instance;
GameObject[] candidates = new GameObject[]
{
mm.splash, mm.mainMenu, mm.play, mm.instantAction, mm.campaign,
null, // options (index 5, handled separately)
mm.mods, mm.roadmap, mm.saveGameConfig, mm.featuredMods,
mm.mutatorConfig, mm.gameModePicker, mm.mapPicker, mm.mutators,
mm.vehiclePicker, mm.weaponPicker, mm.teamConfig, mm.skinPicker,
mm.campaignPicker, mm.campaignMapPicker
};
foreach (var page in candidates)
{
if (page != null && page.activeInHierarchy)
return page;
}
return mm.gameObject; // fallback
}
/// <summary>
/// Check if a selectable is a child of the currently active panel.
/// </summary>
private bool IsOnActivePanel(GameObject go)
{
GameObject panel = GetActivePanel();
if (panel == null) return go.activeInHierarchy;
// Walk up the hierarchy to see if go is a descendant of the panel
Transform t = go.transform;
Transform panelT = panel.transform;
while (t != null)
{
if (t == panelT) return true;
t = t.parent;
}
return false;
}
private void HandleMenuKeyboardNavigation()
{
GameObject current = EventSystem.current.currentSelectedGameObject;
// If nothing is selected or selection is stale, auto-select first on key press
if (current == null || !current.activeInHierarchy || !IsOnActivePanel(current))
{
if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.UpArrow) ||
Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter) ||
Input.GetKeyDown(KeyCode.Tab))
{
SelectFirstOnActivePanel();
return;
}
return;
}
Selectable sel = current.GetComponent<Selectable>();
if (sel == null) return;
// Tab to cycle through all elements on the panel
if (Input.GetKeyDown(KeyCode.Tab))
{
SelectNextOnPanel(sel, Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift));
return;
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
NavigateOrCycleVertical(sel, down: true);
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
NavigateOrCycleVertical(sel, down: false);
}
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
if (current.GetComponent<Slider>() == null)
{
// Cycle dropdown value with left/right arrows
if (CycleDropdown(current, forward: true)) { }
else
{
Selectable next = sel.FindSelectableOnRight();
if (next != null && next.IsInteractable() && IsOnActivePanel(next.gameObject))
{
EventSystem.current.SetSelectedGameObject(next.gameObject);
}
}
}
}
else if (Input.GetKeyDown(KeyCode.LeftArrow))
{
if (current.GetComponent<Slider>() == null)
{
if (CycleDropdown(current, forward: false)) { }
else
{
Selectable prev = sel.FindSelectableOnLeft();
if (prev != null && prev.IsInteractable() && IsOnActivePanel(prev.gameObject))
{
EventSystem.current.SetSelectedGameObject(prev.gameObject);
}
}
}
}
// Enter/Space to activate
if (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter) ||
Input.GetKeyDown(KeyCode.Space))
{
ActivateSelected(current);
}
}
private void NavigateOrCycleVertical(Selectable current, bool down)
{
Selectable next = down ? current.FindSelectableOnDown() : current.FindSelectableOnUp();
// Only navigate to elements on the active panel
if (next != null && next.IsInteractable() && IsOnActivePanel(next.gameObject))
{
EventSystem.current.SetSelectedGameObject(next.gameObject);
}
else
{
// If Unity's navigation didn't find anything, cycle through panel elements
SelectNextOnPanel(current, reverse: !down);
}
}
private void HandleSliderKeys()
{
GameObject current = EventSystem.current?.currentSelectedGameObject;
if (current == null) return;
var slider = current.GetComponent<Slider>();
if (slider == null) return;
bool left = Input.GetKey(KeyCode.LeftArrow);
bool right = Input.GetKey(KeyCode.RightArrow);
if (!left && !right)
{
_sliderRepeatTimer = 0f;
return;
}
if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.RightArrow))
{
AdjustSlider(slider, right);
_sliderRepeatTimer = SLIDER_REPEAT_DELAY;
}
else
{
_sliderRepeatTimer -= Time.unscaledDeltaTime;
if (_sliderRepeatTimer <= 0f)
{
AdjustSlider(slider, right);
_sliderRepeatTimer = SLIDER_REPEAT_DELAY * 0.5f;
}
}
}
private void AdjustSlider(Slider slider, bool increase)
{
float step = (slider.maxValue - slider.minValue) * 0.05f;
if (slider.wholeNumbers) step = Mathf.Max(1f, step);
slider.value = Mathf.Clamp(slider.value + (increase ? step : -step), slider.minValue, slider.maxValue);
string label = GetElementLabel(slider.gameObject);
ScreenReader.Speak($"{label}, {Mathf.RoundToInt(slider.value)}", true);
}
private void ActivateSelected(GameObject go)
{
var button = go.GetComponent<Button>();
if (button != null && button.IsInteractable())
{
button.onClick.Invoke();
return;
}
var toggle = go.GetComponent<Toggle>();
if (toggle != null && toggle.IsInteractable())
{
toggle.isOn = !toggle.isOn;
ScreenReader.Speak(toggle.isOn ? "checked" : "unchecked", true);
return;
}
var dropdown = go.GetComponent<Dropdown>();
if (dropdown != null && dropdown.IsInteractable())
{
dropdown.Show();
return;
}
var tmpDropdown = go.GetComponent<TMP_Dropdown>();
if (tmpDropdown != null && tmpDropdown.IsInteractable())
{
tmpDropdown.Show();
return;
}
}
/// <summary>
/// Get all interactable selectables on the currently active panel, sorted top-to-bottom.
/// </summary>
private List<Selectable> GetPanelSelectables()
{
var result = new List<Selectable>();
var selectables = Selectable.allSelectablesArray;
int count = Selectable.allSelectableCount;
for (int i = 0; i < count; i++)
{
var s = selectables[i];
if (s != null && s.IsInteractable() && s.gameObject.activeInHierarchy && IsOnActivePanel(s.gameObject))
{
result.Add(s);
}
}
// Sort top-to-bottom, left-to-right (by screen position)
result.Sort((a, b) =>
{
float yDiff = b.transform.position.y - a.transform.position.y;
if (Mathf.Abs(yDiff) > 0.01f) return yDiff > 0 ? 1 : -1;
return a.transform.position.x.CompareTo(b.transform.position.x);
});
return result;
}
private void SelectFirstOnActivePanel()
{
var items = GetPanelSelectables();
if (items.Count > 0)
{
EventSystem.current.SetSelectedGameObject(items[0].gameObject);
}
}
private void SelectNextOnPanel(Selectable current, bool reverse)
{
var items = GetPanelSelectables();
if (items.Count == 0) return;
int idx = items.IndexOf(current);
if (idx < 0)
{
EventSystem.current.SetSelectedGameObject(items[0].gameObject);
return;
}
int next = reverse ? idx - 1 : idx + 1;
if (next >= items.Count) next = 0;
if (next < 0) next = items.Count - 1;
EventSystem.current.SetSelectedGameObject(items[next].gameObject);
}
private void AnnounceElement(GameObject go)
{
string label = GetElementLabel(go);
string role = GetElementRole(go);
string value = GetElementValue(go);
string announcement = "";
if (!string.IsNullOrEmpty(label))
announcement = label;
if (!string.IsNullOrEmpty(value))
announcement += $", {value}";
if (!string.IsNullOrEmpty(role))
announcement += $", {role}";
if (!string.IsNullOrEmpty(announcement))
ScreenReader.Speak(announcement.TrimStart(',', ' '), true);
}
private string GetElementLabel(GameObject go)
{
var tmp = go.GetComponentInChildren<TextMeshProUGUI>();
if (tmp != null && !string.IsNullOrEmpty(tmp.text))
return tmp.text.Trim();
var text = go.GetComponentInChildren<Text>();
if (text != null && !string.IsNullOrEmpty(text.text))
return text.text.Trim();
return go.name;
}
private string GetElementRole(GameObject go)
{
if (go.GetComponent<Button>() != null)
return "button";
if (go.GetComponent<Toggle>() != null)
return "checkbox";
if (go.GetComponent<Slider>() != null)
return "slider";
if (go.GetComponent<Dropdown>() != null || go.GetComponent<TMP_Dropdown>() != null)
return "dropdown";
if (go.GetComponent<InputField>() != null || go.GetComponent<TMP_InputField>() != null)
return "text field";
return "";
}
private string GetElementValue(GameObject go)
{
var toggle = go.GetComponent<Toggle>();
if (toggle != null)
return toggle.isOn ? "checked" : "unchecked";
var slider = go.GetComponent<Slider>();
if (slider != null)
return $"{Mathf.RoundToInt(slider.value)}";
var dropdown = go.GetComponent<Dropdown>();
if (dropdown != null && dropdown.options.Count > 0)
{
_lastDropdownValue = dropdown.options[dropdown.value].text;
return _lastDropdownValue;
}
var tmpDropdown = go.GetComponent<TMP_Dropdown>();
if (tmpDropdown != null && tmpDropdown.options.Count > 0)
{
_lastDropdownValue = tmpDropdown.options[tmpDropdown.value].text;
return _lastDropdownValue;
}
return null;
}
private bool CycleDropdown(GameObject go, bool forward)
{
var dropdown = go.GetComponent<Dropdown>();
if (dropdown != null && dropdown.IsInteractable() && dropdown.options.Count > 1)
{
int newVal = dropdown.value + (forward ? 1 : -1);
if (newVal >= dropdown.options.Count) newVal = 0;
if (newVal < 0) newVal = dropdown.options.Count - 1;
dropdown.value = newVal;
string text = dropdown.options[newVal].text;
_lastDropdownValue = text;
ScreenReader.Speak(text, true);
return true;
}
var tmpDropdown = go.GetComponent<TMP_Dropdown>();
if (tmpDropdown != null && tmpDropdown.IsInteractable() && tmpDropdown.options.Count > 1)
{
int newVal = tmpDropdown.value + (forward ? 1 : -1);
if (newVal >= tmpDropdown.options.Count) newVal = 0;
if (newVal < 0) newVal = tmpDropdown.options.Count - 1;
tmpDropdown.value = newVal;
string text = tmpDropdown.options[newVal].text;
_lastDropdownValue = text;
ScreenReader.Speak(text, true);
return true;
}
return false;
}
private void CheckDropdownChange(GameObject go)
{
var dropdown = go.GetComponent<Dropdown>();
if (dropdown != null && dropdown.options.Count > 0)
{
string current = dropdown.options[dropdown.value].text;
if (current != _lastDropdownValue)
{
_lastDropdownValue = current;
ScreenReader.Speak(current, true);
}
return;
}
var tmpDropdown = go.GetComponent<TMP_Dropdown>();
if (tmpDropdown != null && tmpDropdown.options.Count > 0)
{
string current = tmpDropdown.options[tmpDropdown.value].text;
if (current != _lastDropdownValue)
{
_lastDropdownValue = current;
ScreenReader.Speak(current, true);
}
}
}
}
}