-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSavePanelHandler.cs
More file actions
609 lines (521 loc) · 21.7 KB
/
Copy pathSavePanelHandler.cs
File metadata and controls
609 lines (521 loc) · 21.7 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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
using Il2Cpp;
using UnityEngine;
namespace DigimonNOAccess
{
/// <summary>
/// Handles accessibility for the save/load menu.
/// Uses the parent uSavePanel state as the primary IsOpen() authority because
/// uSavePanelCommand.m_State goes to NONE during yes/no dialogs and save operations,
/// while uSavePanel stays active through the entire flow.
/// </summary>
public class SavePanelHandler : IAccessibilityHandler
{
public int Priority => 58;
private uSavePanelCommand _panel;
private uSavePanel _parentPanel;
private bool _wasActive = false;
private int _lastCursor = -1;
private uSavePanel.State _lastParentState = uSavePanel.State.NONE;
private string _lastMessageWindowText = "";
// Small delay so "Loading data" message can be detected first
private const float OpenAnnouncementDelay = 0.05f;
private float _openTime = 0f;
private bool _pendingOpenAnnouncement = false;
public bool IsOpen()
{
// Find parent panel - this is the authority for the save/load lifecycle.
// The command panel (uSavePanelCommand) goes to NONE during yes/no dialogs
// and save operations, but the parent stays active.
if (_parentPanel == null)
_parentPanel = Object.FindObjectOfType<uSavePanel>();
if (_parentPanel != null)
{
var parentState = _parentPanel.m_State;
if (parentState != uSavePanel.State.NONE &&
parentState != uSavePanel.State.CLOSE &&
parentState != uSavePanel.State.END)
{
// Parent is active - get command panel from parent's direct reference.
// FindObjectOfType won't find it when its gameObject is deactivated
// during save operations, but the parent's reference persists.
if (_panel == null)
_panel = _parentPanel.m_uLoadPanelCommand;
return true;
}
}
// Fallback: check command panel directly
if (_panel == null)
_panel = Object.FindObjectOfType<uSavePanelCommand>();
if (_panel != null)
{
var state = _panel.m_State;
if (state == uSavePanelCommand.State.MAIN ||
state == uSavePanelCommand.State.SAVE_CHECK ||
state == uSavePanelCommand.State.LOAD_CHECK ||
state == uSavePanelCommand.State.SAVE ||
state == uSavePanelCommand.State.LOAD)
return true;
}
return false;
}
/// <summary>
/// True when the player can navigate save slots.
/// Uses the parent's MAIN_SETTING state as the authority - this is the parent's
/// "slot list is active and accepting input" state. The command panel's m_State
/// may stay in NONE after save operations even though the slot list is functional.
/// </summary>
private bool IsNavigable()
{
if (_panel == null) return false;
if (_parentPanel == null) return false;
return _parentPanel.m_State == uSavePanel.State.MAIN_SETTING;
}
private static bool IsParentSaveLoadState(uSavePanel.State state)
{
return state == uSavePanel.State.SAVE ||
state == uSavePanel.State.POST_WAIT ||
state == uSavePanel.State.SYSTEM_SAVE ||
state == uSavePanel.State.SAVE_END ||
state == uSavePanel.State.LOAD;
}
public void Update()
{
bool isActive = IsOpen();
if (isActive && !_wasActive)
{
OnOpen();
}
else if (!isActive && _wasActive)
{
OnClose();
}
else if (isActive)
{
// Check if pending open announcement is ready
if (_pendingOpenAnnouncement && UnityEngine.Time.time - _openTime >= OpenAnnouncementDelay)
{
AnnounceCurrentSlot();
_pendingOpenAnnouncement = false;
}
// Track parent panel state and message window text
CheckParentState();
// Only track cursor when navigable
if (IsNavigable())
{
CheckCursorChange();
}
}
_wasActive = isActive;
}
/// <summary>
/// Track the parent uSavePanel state machine and its message window.
/// </summary>
private void CheckParentState()
{
try
{
if (_parentPanel == null)
return;
var parentState = _parentPanel.m_State;
// Monitor message window text changes for announcements
CheckParentMessageWindow();
if (parentState != _lastParentState)
{
DebugLogger.Log($"[SavePanel] Parent state: {_lastParentState} -> {parentState}");
// When parent exits a save/load operation back to MAIN_SETTING,
// force cursor re-announcement on the next navigable frame
if (parentState == uSavePanel.State.MAIN_SETTING &&
IsParentSaveLoadState(_lastParentState))
{
_lastCursor = -1; // Force re-announcement
DebugLogger.Log("[SavePanel] Save/load complete, will re-announce slot");
}
_lastParentState = parentState;
}
}
catch { }
}
/// <summary>
/// Monitor the parent panel's message window for text changes.
/// Catches auto-closing messages like "Save complete" that may not go through SetMessage.
/// </summary>
private void CheckParentMessageWindow()
{
try
{
var msgWindow = _parentPanel.m_messageWindow;
if (msgWindow == null)
return;
if (!msgWindow.m_isOpend)
{
_lastMessageWindowText = "";
return;
}
var label = msgWindow.m_label;
if (label == null)
return;
string text = label.text;
if (string.IsNullOrEmpty(text) || text == _lastMessageWindowText)
return;
_lastMessageWindowText = text;
if (TextUtilities.IsPlaceholderText(text))
return;
string cleaned = TextUtilities.CleanText(text);
if (!string.IsNullOrEmpty(cleaned))
{
ScreenReader.Say(cleaned);
DebugLogger.Log($"[SavePanel] Message window: {cleaned}");
}
}
catch { }
}
private void OnOpen()
{
_lastCursor = -1;
_lastParentState = uSavePanel.State.NONE;
_lastMessageWindowText = "";
_openTime = UnityEngine.Time.time;
_pendingOpenAnnouncement = true;
// Cache parent panel
if (_parentPanel == null)
_parentPanel = Object.FindObjectOfType<uSavePanel>();
if (_parentPanel != null)
_lastParentState = _parentPanel.m_State;
// Cache command panel and initial cursor
if (_panel == null)
_panel = Object.FindObjectOfType<uSavePanelCommand>();
if (_panel != null)
_lastCursor = GetCursorPosition();
DebugLogger.Log($"[SavePanel] Opened, parent={_lastParentState}, cursor={_lastCursor}");
}
private void OnClose()
{
_panel = null;
_parentPanel = null;
_lastCursor = -1;
_lastParentState = uSavePanel.State.NONE;
_lastMessageWindowText = "";
_pendingOpenAnnouncement = false;
DebugLogger.Log("[SavePanel] Closed");
}
private void AnnounceCurrentSlot()
{
if (_panel == null)
return;
// Only announce slot info when parent is in navigable state
if (_parentPanel == null || _parentPanel.m_State != uSavePanel.State.MAIN_SETTING)
return;
int cursor = GetCursorPosition();
int total = GetSlotCount();
string headline = GetHeadlineText();
string slotInfo = GetSlotInfo(cursor);
// Use SayQueued so any pre-open message speaks first
string announcement = $"{headline}. {slotInfo}, slot {cursor + 1} of {total}";
ScreenReader.SayQueued(announcement);
DebugLogger.Log($"[SavePanel] Open announcement: {announcement}");
_lastCursor = cursor;
}
private void CheckCursorChange()
{
if (_panel == null)
return;
int cursor = GetCursorPosition();
if (cursor != _lastCursor)
{
// Cancel pending open announcement - cursor change will announce
_pendingOpenAnnouncement = false;
int total = GetSlotCount();
string slotInfo = GetSlotInfo(cursor);
string announcement = $"{slotInfo}, slot {cursor + 1} of {total}";
ScreenReader.Say(announcement);
DebugLogger.Log($"[SavePanel] Cursor: slot {cursor + 1}: {slotInfo}");
_lastCursor = cursor;
}
}
private string GetStateAnnouncement(uSavePanel.State state)
{
return state switch
{
uSavePanel.State.SAVE_CHECK => GetConfirmationText(state, "Confirm save?"),
uSavePanel.State.LOAD_CHECK => GetConfirmationText(state, "Confirm load?"),
uSavePanel.State.SAVE => GetSaveLoadCaptionText(state, "Saving..."),
uSavePanel.State.LOAD => GetSaveLoadCaptionText(state, "Loading..."),
_ => ""
};
}
private string GetConfirmationText(uSavePanel.State state, string fallback)
{
try
{
if (_parentPanel == null)
{
DebugLogger.Log($"[SavePanel] {state}: parent panel is null");
return fallback;
}
var window = _parentPanel.m_YesNoWindow;
if (window == null)
{
DebugLogger.Log($"[SavePanel] {state}: m_YesNoWindow is null");
return fallback;
}
var message = window.m_message;
if (message == null)
{
DebugLogger.Log($"[SavePanel] {state}: m_YesNoWindow.m_message is null");
return fallback;
}
string text = TextUtilities.StripRichTextTags(ButtonHintCache.Filter(message.text))?.Trim();
if (string.IsNullOrWhiteSpace(text) || TextUtilities.IsPlaceholderText(text))
{
DebugLogger.Log($"[SavePanel] {state}: m_YesNoWindow.m_message.text is empty");
return fallback;
}
return text;
}
catch (System.Exception ex)
{
DebugLogger.Log($"[SavePanel] {state}: confirmation text read failed: {ex.Message}");
return fallback;
}
}
private string GetSaveLoadCaptionText(uSavePanel.State state, string fallback)
{
try
{
if (_parentPanel == null)
{
DebugLogger.Log($"[SavePanel] {state}: parent panel is null");
return fallback;
}
var caption = _parentPanel.m_uLoadPanelCaption;
if (caption == null)
{
DebugLogger.Log($"[SavePanel] {state}: m_uLoadPanelCaption is null");
return fallback;
}
var captionText = caption.m_Caption;
if (captionText == null)
{
DebugLogger.Log($"[SavePanel] {state}: m_uLoadPanelCaption.m_Caption is null");
return fallback;
}
string text = TextUtilities.StripRichTextTags(ButtonHintCache.Filter(captionText.text))?.Trim();
if (string.IsNullOrWhiteSpace(text) || TextUtilities.IsPlaceholderText(text))
{
DebugLogger.Log($"[SavePanel] {state}: m_uLoadPanelCaption.m_Caption.text is empty");
return fallback;
}
return text;
}
catch (System.Exception ex)
{
DebugLogger.Log($"[SavePanel] {state}: caption text read failed: {ex.Message}");
return fallback;
}
}
private int GetCursorPosition()
{
try
{
if (_panel != null)
return _panel.GetCorsorIndex();
}
catch (System.Exception ex)
{
DebugLogger.Log($"[SavePanel] Error getting cursor: {ex.Message}");
}
return 0;
}
private int GetSlotCount()
{
try
{
var items = _panel?.m_items;
if (items != null)
return items.Length;
}
catch (System.Exception ex)
{
DebugLogger.Log($"[SavePanel] Error in GetSlotCount: {ex.Message}");
}
return 3;
}
private string GetHeadlineText()
{
try
{
if (_parentPanel == null)
{
DebugLogger.Log("[SavePanel] Headline: parent panel is null");
return "Save/Load Menu";
}
var headline = _parentPanel.m_uLoadPanelHeadLine;
if (headline == null)
{
DebugLogger.Log("[SavePanel] Headline: m_uLoadPanelHeadLine is null");
return "Save/Load Menu";
}
var headlineText = headline.m_HeadLine;
if (headlineText == null)
{
DebugLogger.Log("[SavePanel] Headline: m_uLoadPanelHeadLine.m_HeadLine is null");
return "Save/Load Menu";
}
string text = TextUtilities.StripRichTextTags(ButtonHintCache.Filter(headlineText.text))?.Trim();
if (!string.IsNullOrWhiteSpace(text))
return text;
DebugLogger.Log("[SavePanel] Headline: m_uLoadPanelHeadLine.m_HeadLine.text is empty");
}
catch (System.Exception ex)
{
DebugLogger.Log($"[SavePanel] Error getting headline: {ex.Message}");
}
return "Save/Load Menu";
}
private string GetSlotInfo(int slotIndex)
{
try
{
if (_panel == null)
{
DebugLogger.Log("[SavePanel] Slot: panel is null");
return AnnouncementBuilder.FallbackItem("Slot", slotIndex);
}
var items = _panel.m_items;
if (items == null)
{
DebugLogger.Log("[SavePanel] Slot: m_items is null");
return AnnouncementBuilder.FallbackItem("Slot", slotIndex);
}
if (slotIndex < 0 || slotIndex >= items.Length)
{
DebugLogger.Log($"[SavePanel] Slot: index {slotIndex} is outside m_items");
return AnnouncementBuilder.FallbackItem("Slot", slotIndex);
}
var item = items[slotIndex];
if (item == null)
{
DebugLogger.Log($"[SavePanel] Slot: m_items[{slotIndex}] is null");
return AnnouncementBuilder.FallbackItem("Slot", slotIndex);
}
var saveItem = item.TryCast<uSavePanelItemSaveItem>();
if (saveItem == null)
{
DebugLogger.Log($"[SavePanel] Slot: m_items[{slotIndex}] is not a uSavePanelItemSaveItem");
return AnnouncementBuilder.FallbackItem("Slot", slotIndex);
}
return GetSaveItemDetails(saveItem);
}
catch (System.Exception ex)
{
DebugLogger.Log($"[SavePanel] Error getting slot info: {ex.Message}");
}
return AnnouncementBuilder.FallbackItem("Slot", slotIndex);
}
private string GetSaveItemDetails(uSavePanelItemSaveItem item)
{
try
{
// Check for empty slot
var noDataText = item.m_NoDataText;
if (noDataText == null)
{
DebugLogger.Log("[SavePanel] Slot: m_NoDataText is null");
}
else
{
var noDataObject = noDataText.gameObject;
if (noDataObject == null)
{
DebugLogger.Log("[SavePanel] Slot: m_NoDataText.gameObject is null");
}
else if (noDataObject.activeInHierarchy)
{
string noData = TextUtilities.StripRichTextTags(noDataText.text)?.Trim();
if (!string.IsNullOrWhiteSpace(noData))
return noData;
DebugLogger.Log("[SavePanel] Slot: m_NoDataText.text is empty");
return "No Data";
}
}
// Build slot details from available text fields
var parts = new System.Collections.Generic.List<string>();
var playerName = item.m_playerNameText;
if (playerName != null && !string.IsNullOrEmpty(playerName.text))
parts.Add(playerName.text);
var levelText = item.m_tamarLavelText;
if (levelText == null)
{
DebugLogger.Log("[SavePanel] Slot: m_tamarLavelText is null");
}
else
{
string level = TextUtilities.StripRichTextTags(ButtonHintCache.Filter(levelText.text))?.Trim();
if (string.IsNullOrWhiteSpace(level) || TextUtilities.IsPlaceholderText(level))
{
DebugLogger.Log("[SavePanel] Slot: m_tamarLavelText.text is empty");
}
else
{
string label = null;
var labelText = item.m_tamarLavelLabelText;
if (labelText == null)
{
DebugLogger.Log("[SavePanel] Slot: m_tamarLavelLabelText is null");
}
else
{
label = TextUtilities.StripRichTextTags(labelText.text)?.Trim();
if (string.IsNullOrWhiteSpace(label))
DebugLogger.Log("[SavePanel] Slot: m_tamarLavelLabelText.text is empty");
}
parts.Add($"{(string.IsNullOrWhiteSpace(label) ? "Level" : label)} {level}");
}
}
var area = item.m_areaText;
if (area != null && !string.IsNullOrEmpty(area.text))
parts.Add(area.text);
var playTime = item.m_playTimeText;
if (playTime != null && !string.IsNullOrEmpty(playTime.text))
parts.Add(playTime.text);
var timestamp = item.m_timeStampText;
if (timestamp != null && !string.IsNullOrEmpty(timestamp.text))
parts.Add(timestamp.text);
if (parts.Count > 0)
return string.Join(", ", parts);
}
catch (System.Exception ex)
{
DebugLogger.Log($"[SavePanel] Error reading save item: {ex.Message}");
}
return "Save Data";
}
public void AnnounceStatus()
{
if (!IsOpen())
return;
if (_parentPanel == null || _parentPanel.m_State != uSavePanel.State.MAIN_SETTING)
{
string stateHeadline = GetHeadlineText();
// During save/load operation, announce the parent state
if (_parentPanel != null)
{
string stateMsg = GetStateAnnouncement(_parentPanel.m_State);
if (!string.IsNullOrEmpty(stateMsg))
{
ScreenReader.Say($"{stateHeadline}. {stateMsg}");
return;
}
}
ScreenReader.Say(stateHeadline);
return;
}
int cursor = GetCursorPosition();
int total = GetSlotCount();
string headline = GetHeadlineText();
string slotInfo = GetSlotInfo(cursor);
string announcement = $"{headline}. {slotInfo}, slot {cursor + 1} of {total}";
ScreenReader.Say(announcement);
}
}
}