Repository navigation
Expand file tree
/
Copy pathTitleMenuHandler.cs
More file actions
293 lines (248 loc) · 9.33 KB
/
Copy pathTitleMenuHandler.cs
File metadata and controls
293 lines (248 loc) · 9.33 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
using Il2Cpp;
using UnityEngine;
namespace DigimonNOAccess
{
/// <summary>
/// Handles accessibility for the title/main menu.
/// Uses m_playVoice to detect when menu voice has played - this indicates the menu
/// is truly ready for input (after "press any button" phase completes).
/// Also checks MainTitle.m_State == Idle to ensure no dialogs/loading are active.
/// Uses Localization.isActive to ensure text is ready before reading.
/// </summary>
public class TitleMenuHandler : IAccessibilityHandler
{
public int Priority => 35;
private uTitlePanel _titlePanel;
private MainTitle _mainTitle;
private bool _wasActive = false;
private int _lastCursor = -1;
private bool _voiceHasFinished = false;
private bool _wasOpen = false;
private bool _wasPlayingVoice = false;
/// <summary>
/// Check if title menu panel exists and is ready for input.
/// Waits for m_playVoice to go from True back to False (voice finished playing).
/// </summary>
private bool IsPanelReady()
{
if (_titlePanel == null)
{
_titlePanel = Object.FindObjectOfType<uTitlePanel>();
}
if (_titlePanel == null)
{
_voiceHasFinished = false;
_wasOpen = false;
_wasPlayingVoice = false;
return false;
}
bool isOpen = _titlePanel.m_isOpend;
bool playVoice = _titlePanel.m_playVoice;
// Reset flags when panel freshly opens
if (isOpen && !_wasOpen)
{
_voiceHasFinished = false;
_wasPlayingVoice = false;
DebugLogger.Log($"[TitleMenu] Panel opened, resetting voice tracking. playVoice={playVoice}");
}
_wasOpen = isOpen;
if (!isOpen)
{
return false;
}
// Track voice state transitions: True -> False means voice finished
if (playVoice && !_wasPlayingVoice)
{
// Voice just started playing
_wasPlayingVoice = true;
DebugLogger.Log("[TitleMenu] Voice started playing");
}
else if (!playVoice && _wasPlayingVoice && !_voiceHasFinished)
{
// Voice just finished (was playing, now stopped)
_voiceHasFinished = true;
DebugLogger.Log("[TitleMenu] Voice finished playing, menu now ready");
}
return _voiceHasFinished;
}
/// <summary>
/// Check if title menu is actually usable (can accept input).
/// Panel must be ready (voice has played) AND MainTitle must be in Idle state.
/// </summary>
public bool IsUsable()
{
if (!IsPanelReady())
return false;
// Find MainTitle if not cached
if (_mainTitle == null)
{
_mainTitle = Object.FindObjectOfType<MainTitle>();
}
if (_mainTitle == null)
return false;
// Menu is only truly usable when MainTitle state is Idle
// Other states: LoadWait, ErrorMsg, Option, SelectDifficulty, etc.
return _mainTitle.m_State == MainTitle.State.Idle;
}
/// <summary>
/// Check if localization is loaded and ready.
/// </summary>
private bool IsLocalizationReady()
{
return TextUtilities.IsLocalizationReady();
}
/// <summary>
/// Called every frame to track menu state.
/// </summary>
public void Update()
{
bool isUsable = IsUsable();
// Menu just became usable
if (isUsable && !_wasActive)
{
OnBecameUsable();
}
// Menu just became unusable
else if (!isUsable && _wasActive)
{
OnBecameUnusable();
}
// Menu is usable, check for cursor changes
else if (isUsable)
{
CheckCursorChange();
}
_wasActive = isUsable;
}
private void OnBecameUsable()
{
_lastCursor = -1; // Reset to force announcement
if (_titlePanel == null)
return;
// Wait for localization to be ready
if (!IsLocalizationReady())
{
DebugLogger.Log("[TitleMenu] Menu usable but localization not ready yet");
return;
}
int cursor = _titlePanel.cursorPosition;
string itemText = GetMenuItemText(cursor);
int total = GetMenuItemCount();
string announcement = AnnouncementBuilder.MenuOpen("Title Menu", itemText, cursor, total);
ScreenReader.Say(announcement);
DebugLogger.Log($"[TitleMenu] Menu now usable (Idle state), cursor={cursor}, text={itemText}");
_lastCursor = cursor;
}
private void OnBecameUnusable()
{
_titlePanel = null;
_mainTitle = null;
_lastCursor = -1;
_voiceHasFinished = false;
_wasOpen = false;
_wasPlayingVoice = false;
DebugLogger.Log("[TitleMenu] Menu no longer usable");
}
private void CheckCursorChange()
{
if (_titlePanel == null)
return;
// Don't announce if localization isn't ready
if (!IsLocalizationReady())
return;
int cursor = _titlePanel.cursorPosition;
if (cursor != _lastCursor)
{
string itemText = GetMenuItemText(cursor);
int total = GetMenuItemCount();
string announcement = AnnouncementBuilder.CursorPosition(itemText, cursor, total);
ScreenReader.Say(announcement);
DebugLogger.Log($"[TitleMenu] Cursor changed: {itemText}");
_lastCursor = cursor;
}
}
private string GetMenuItemText(int index)
{
if (_titlePanel == null)
{
DebugLogger.Log($"[TitleMenu] Menu item: _titlePanel was null for index {index}");
return GetFallbackText(index);
}
// Only try to read from UI if localization is ready
if (!IsLocalizationReady())
{
DebugLogger.Log($"[TitleMenu] Menu item: m_Text[{index}] unavailable because localization was not ready");
return GetFallbackText(index);
}
try
{
var textArray = _titlePanel.m_Text;
if (textArray == null)
{
DebugLogger.Log("[TitleMenu] Menu item: m_Text was null");
return GetFallbackText(index);
}
if (index < 0 || index >= textArray.Length)
{
DebugLogger.Log($"[TitleMenu] Menu item: m_Text index {index} was out of range");
return GetFallbackText(index);
}
var textComponent = textArray[index];
if (textComponent == null)
{
DebugLogger.Log($"[TitleMenu] Menu item: m_Text[{index}] was null");
return GetFallbackText(index);
}
string menuText = (TextUtilities.StripRichTextTags(textComponent.text) ?? "").Trim();
if (!string.IsNullOrWhiteSpace(menuText))
return menuText;
DebugLogger.Log($"[TitleMenu] Menu item: m_Text[{index}].text was empty");
}
catch (System.Exception ex)
{
DebugLogger.Log($"[TitleMenu] Error reading text: {ex.Message}");
}
return GetFallbackText(index);
}
private string GetFallbackText(int index)
{
// Fallback to known menu items
return index switch
{
0 => "New Game",
1 => "Load Game",
2 => "System Settings",
3 => "Quit Game",
_ => AnnouncementBuilder.FallbackItem("Item", index)
};
}
private int GetMenuItemCount()
{
// Title menu has 4 items: New Game, Load Game, System Settings, Quit
return 4;
}
/// <summary>
/// Announce current menu state (for repeat key).
/// </summary>
public void AnnounceStatus()
{
if (!IsUsable())
return;
if (!IsLocalizationReady())
return;
int cursor = _titlePanel.cursorPosition;
string itemText = GetMenuItemText(cursor);
int total = GetMenuItemCount();
string announcement = AnnouncementBuilder.MenuOpen("Title Menu", itemText, cursor, total);
ScreenReader.Say(announcement);
}
/// <summary>
/// Check if title menu is currently open (for external callers).
/// Returns true only when menu is actually usable.
/// </summary>
public bool IsOpen()
{
return IsUsable();
}
}
}