-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConstructionPanelHandler.cs
More file actions
618 lines (532 loc) · 22.8 KB
/
Copy pathConstructionPanelHandler.cs
File metadata and controls
618 lines (532 loc) · 22.8 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
610
611
612
613
614
615
616
617
618
using Il2Cpp;
using UnityEngine;
namespace DigimonNOAccess
{
/// <summary>
/// Handles accessibility for the construction/builder panel (uConstructionPanel).
/// Covers the Grade Up menu (facility upgrades) and the Material donation menu.
/// </summary>
public class ConstructionPanelHandler : IAccessibilityHandler
{
private const string LogTag = "[Construction]";
public int Priority => 57;
private uConstructionPanel _panel;
private bool _wasActive;
// Grade Up panel tracking
private int _lastBlockIndex = -1;
private int _lastContentIndex = -1;
private uConstructionPanelGradeUp.State _lastGradeUpState = uConstructionPanelGradeUp.State.None;
private bool _pendingDialogReannounce;
// Material panel tracking
private int _lastKindIndex = -1;
private int _lastMaterialIndex = -1;
// Parent state tracking
private uConstructionPanel.State _lastParentState = uConstructionPanel.State.None;
// Debug: throttle per-frame logging
private float _lastDebugLogTime;
public bool IsOpen()
{
try
{
if (_panel == null)
_panel = Object.FindObjectOfType<uConstructionPanel>();
if (_panel == null) return false;
var state = _panel.state;
// Panel is active in GradeUpMain, MaterialMain, or Wait (transitional state)
return state == uConstructionPanel.State.GradeUpMain ||
state == uConstructionPanel.State.MaterialMain ||
state == uConstructionPanel.State.Wait;
}
catch
{
return false;
}
}
public void AnnounceStatus() { }
public void Update()
{
bool isActive = IsOpen();
if (isActive && !_wasActive)
OnOpen();
else if (!isActive && _wasActive)
OnClose();
else if (isActive)
OnUpdate();
_wasActive = isActive;
}
private void OnOpen()
{
ResetTracking();
var state = _panel.state;
_lastParentState = state;
DebugLogger.Log($"{LogTag} Panel opened, parent state={state}");
// Panel may open in Wait before transitioning to GradeUpMain/MaterialMain.
// The actual sub-panel determines what to announce.
if (state == uConstructionPanel.State.GradeUpMain)
AnnounceGradeUpOpen();
else if (state == uConstructionPanel.State.MaterialMain)
AnnounceMaterialOpen();
else if (state == uConstructionPanel.State.Wait)
{
// Detect which sub-panel is active by checking which one exists
// But skip announcement if sub-panel is in a transitional state (closing)
if (_panel.m_gradeUpPanel != null)
{
_lastParentState = uConstructionPanel.State.GradeUpMain;
var gradeState = _panel.m_gradeUpPanel.m_state;
if (gradeState != uConstructionPanelGradeUp.State.Wait &&
gradeState != uConstructionPanelGradeUp.State.Close &&
gradeState != uConstructionPanelGradeUp.State.None)
{
AnnounceGradeUpOpen();
}
else
{
DebugLogger.Log($"{LogTag} Panel opened in transitional GradeUp state={gradeState}, silent");
}
}
else if (_panel.m_materialPanel != null)
{
_lastParentState = uConstructionPanel.State.MaterialMain;
AnnounceMaterialOpen();
}
}
}
private void OnClose()
{
DebugLogger.Log($"{LogTag} Panel closed");
ResetTracking();
_panel = null;
}
private void OnUpdate()
{
try
{
var state = _panel.state;
// Detect parent state change (switching between grade up and material)
// Wait is transitional - don't react to it, just update _lastParentState
if (state != _lastParentState)
{
DebugLogger.Log($"{LogTag} Parent state changed: {_lastParentState} -> {state}");
if (state == uConstructionPanel.State.GradeUpMain)
{
// Only announce if truly switching from material to grade up
if (_lastParentState == uConstructionPanel.State.MaterialMain)
{
ResetGradeUpTracking();
AnnounceGradeUpOpen();
}
}
else if (state == uConstructionPanel.State.MaterialMain)
{
if (_lastParentState == uConstructionPanel.State.GradeUpMain)
{
ResetMaterialTracking();
AnnounceMaterialOpen();
}
}
_lastParentState = state;
}
// Route updates to the active sub-panel
// Use _lastParentState to determine which panel, since Wait is transitional
if (_lastParentState == uConstructionPanel.State.GradeUpMain ||
(_lastParentState == uConstructionPanel.State.Wait && _panel.m_gradeUpPanel != null))
UpdateGradeUp();
else if (_lastParentState == uConstructionPanel.State.MaterialMain)
UpdateMaterial();
}
catch { }
}
// ---- Grade Up Panel ----
private void AnnounceGradeUpOpen()
{
try
{
var gradeUp = _panel.m_gradeUpPanel;
if (gradeUp == null)
{
DebugLogger.Log($"{LogTag} m_gradeUpPanel is null");
return;
}
var gradeState = gradeUp.m_state;
_lastGradeUpState = gradeState;
DebugLogger.Log($"{LogTag} GradeUp sub-state: {gradeState}");
// Log cursor objects
var blockCursor = gradeUp.m_constructionBlockCursor;
var contentCursor = gradeUp.m_constructionContentCursor;
DebugLogger.Log($"{LogTag} blockCursor null={blockCursor == null}, contentCursor null={contentCursor == null}");
int blockIdx = 0;
int contentIdx = 0;
if (blockCursor != null)
{
blockIdx = blockCursor.index;
DebugLogger.Log($"{LogTag} blockCursor.index={blockIdx}, min={blockCursor.m_min}, max={blockCursor.m_max}");
}
if (contentCursor != null)
{
contentIdx = contentCursor.index;
DebugLogger.Log($"{LogTag} contentCursor.index={contentIdx}, min={contentCursor.m_min}, max={contentCursor.m_max}");
}
_lastBlockIndex = blockIdx;
_lastContentIndex = contentIdx;
// Log available blocks and contents
var blocks = gradeUp.m_constructionBlocks;
var contents = gradeUp.m_constructionContents;
DebugLogger.Log($"{LogTag} blocks null={blocks == null}, count={blocks?.Length ?? 0}");
DebugLogger.Log($"{LogTag} contents null={contents == null}, count={contents?.Length ?? 0}");
if (blocks != null)
{
for (int i = 0; i < blocks.Length; i++)
{
string txt = "null";
try { txt = blocks[i]?.m_textUI?.text ?? "empty"; } catch { }
DebugLogger.Log($"{LogTag} block[{i}]: {txt}");
}
}
if (contents != null)
{
for (int i = 0; i < contents.Length; i++)
{
string txt = "null";
try { txt = contents[i]?.m_nameUI?.text ?? "empty"; } catch { }
DebugLogger.Log($"{LogTag} content[{i}]: {txt}");
}
}
string blockName = GetBlockName(blockIdx);
string contentName = GetContentName(contentIdx);
string announcement = GetConstructionCaption(true);
if (!string.IsNullOrEmpty(blockName))
announcement += $". {blockName}";
if (!string.IsNullOrEmpty(contentName))
announcement += $". {contentName}";
ScreenReader.Say(announcement);
DebugLogger.Log($"{LogTag} Announced open: {announcement}");
}
catch (System.Exception ex)
{
DebugLogger.Log($"{LogTag} AnnounceGradeUpOpen error: {ex.Message}");
}
}
private void UpdateGradeUp()
{
try
{
var gradeUp = _panel.m_gradeUpPanel;
if (gradeUp == null) return;
var gradeState = gradeUp.m_state;
bool stateChanged = gradeState != _lastGradeUpState;
var prevState = _lastGradeUpState;
if (stateChanged)
{
_lastGradeUpState = gradeState;
DebugLogger.Log($"{LogTag} GradeUp state: {gradeState}");
}
// Track dialog states for re-announce on return
if (gradeState == uConstructionPanelGradeUp.State.GradeUpConfirm ||
gradeState == uConstructionPanelGradeUp.State.GradeUpConfirmWait)
_pendingDialogReannounce = true;
// Only track cursors in Main state
if (gradeState != uConstructionPanelGradeUp.State.Main)
return;
// Re-announce current item when returning to Main from dialog
if (_pendingDialogReannounce)
{
_pendingDialogReannounce = false;
_lastBlockIndex = -1;
_lastContentIndex = -1;
}
int blockIdx = gradeUp.m_constructionBlockCursor?.index ?? 0;
int contentIdx = gradeUp.m_constructionContentCursor?.index ?? 0;
// Periodic debug logging of cursor values
float now = Time.time;
if (now - _lastDebugLogTime > 2f)
{
DebugLogger.Log($"{LogTag} [tick] block={blockIdx} (last={_lastBlockIndex}), content={contentIdx} (last={_lastContentIndex})");
_lastDebugLogTime = now;
}
bool blockChanged = blockIdx != _lastBlockIndex;
bool contentChanged = contentIdx != _lastContentIndex;
if (!blockChanged && !contentChanged)
return;
DebugLogger.Log($"{LogTag} Cursor changed: block {_lastBlockIndex}->{blockIdx}, content {_lastContentIndex}->{contentIdx}");
_lastBlockIndex = blockIdx;
_lastContentIndex = contentIdx;
string contentName = GetContentName(contentIdx);
string materials = GetMaterialRequirements();
string term = GetConstructionTerm();
string announcement = "";
if (blockChanged)
{
string blockName = GetBlockName(blockIdx);
announcement = blockName ?? "";
if (!string.IsNullOrEmpty(contentName))
announcement += $". {contentName}";
}
else
{
announcement = contentName ?? "";
}
if (!string.IsNullOrEmpty(materials))
announcement += $". {materials}";
if (!string.IsNullOrEmpty(term))
announcement += $". {term}";
if (!string.IsNullOrEmpty(announcement))
{
ScreenReader.Say(announcement);
DebugLogger.Log($"{LogTag} Announced: {announcement}");
}
}
catch { }
}
private string GetBlockName(int index)
{
try
{
var blocks = _panel.m_gradeUpPanel?.m_constructionBlocks;
if (blocks == null || index < 0 || index >= blocks.Length) return null;
return TextUtilities.StripRichTextTags(blocks[index]?.m_textUI?.text);
}
catch { return null; }
}
private string GetContentName(int index)
{
try
{
var contents = _panel.m_gradeUpPanel?.m_constructionContents;
if (contents == null || index < 0 || index >= contents.Length) return null;
return TextUtilities.StripRichTextTags(contents[index]?.m_nameUI?.text);
}
catch { return null; }
}
private string GetMaterialRequirements()
{
try
{
var materials = _panel.m_gradeUpPanel?.m_constructionMaterials;
if (materials == null) return null;
var parts = new System.Collections.Generic.List<string>();
for (int i = 0; i < materials.Length; i++)
{
var mat = materials[i];
if (mat == null) continue;
string name = TextUtilities.StripRichTextTags(mat.m_name?.text);
if (string.IsNullOrEmpty(name)) continue;
string have = mat.m_currentNum?.text ?? "0";
string need = mat.m_needNum?.text ?? "0";
parts.Add($"{name}: {have}/{need}");
}
return parts.Count > 0 ? string.Join(", ", parts) : null;
}
catch { return null; }
}
private string GetConstructionTerm()
{
try
{
var term = _panel.m_gradeUpPanel?.m_constructionTerm;
if (term == null) return null;
string day = term.m_dayUI?.text;
string time = term.m_timeUI?.text;
if (!string.IsNullOrEmpty(day) || !string.IsNullOrEmpty(time))
return $"Term: {day ?? ""} {time ?? ""}".Trim();
return null;
}
catch { return null; }
}
// ---- Material Panel ----
private void AnnounceMaterialOpen()
{
try
{
var matPanel = _panel.m_materialPanel;
if (matPanel == null)
{
DebugLogger.Log($"{LogTag} m_materialPanel is null");
return;
}
DebugLogger.Log($"{LogTag} Material sub-state: {matPanel.m_state}");
var kindCursor = matPanel.m_materialKindCursor;
var contentCursor = matPanel.m_materialContentCursor;
DebugLogger.Log($"{LogTag} kindCursor null={kindCursor == null}, contentCursor null={contentCursor == null}");
int kindIdx = 0;
int matIdx = 0;
if (kindCursor != null)
{
kindIdx = kindCursor.index;
DebugLogger.Log($"{LogTag} kindCursor.index={kindIdx}, min={kindCursor.m_min}, max={kindCursor.m_max}");
}
if (contentCursor != null)
{
matIdx = contentCursor.index;
DebugLogger.Log($"{LogTag} contentCursor.index={matIdx}, min={contentCursor.m_min}, max={contentCursor.m_max}");
}
_lastKindIndex = kindIdx;
_lastMaterialIndex = matIdx;
string matName = GetMaterialName(matIdx);
string quantity = GetMaterialQuantity(matIdx);
string announcement = GetConstructionCaption(false);
if (!string.IsNullOrEmpty(matName))
{
announcement += $". {matName}";
if (!string.IsNullOrEmpty(quantity))
announcement += $", have {quantity}";
}
ScreenReader.Say(announcement);
DebugLogger.Log($"{LogTag} Announced material open: {announcement}");
}
catch (System.Exception ex)
{
DebugLogger.Log($"{LogTag} AnnounceMaterialOpen error: {ex.Message}");
}
}
private void UpdateMaterial()
{
try
{
var matPanel = _panel.m_materialPanel;
if (matPanel == null) return;
if (matPanel.m_state != uConstructionPanelMaterial.State.Main)
return;
int kindIdx = matPanel.m_materialKindCursor?.index ?? 0;
int matIdx = matPanel.m_materialContentCursor?.index ?? 0;
// Periodic debug logging
float now = Time.time;
if (now - _lastDebugLogTime > 2f)
{
DebugLogger.Log($"{LogTag} [tick] kind={kindIdx} (last={_lastKindIndex}), mat={matIdx} (last={_lastMaterialIndex})");
_lastDebugLogTime = now;
}
bool kindChanged = kindIdx != _lastKindIndex;
bool matChanged = matIdx != _lastMaterialIndex;
if (!kindChanged && !matChanged)
return;
DebugLogger.Log($"{LogTag} Material cursor changed: kind {_lastKindIndex}->{kindIdx}, mat {_lastMaterialIndex}->{matIdx}");
_lastKindIndex = kindIdx;
_lastMaterialIndex = matIdx;
string matName = GetMaterialName(matIdx);
string quantity = GetMaterialQuantity(matIdx);
string announcement = "";
if (!string.IsNullOrEmpty(matName))
{
announcement = matName;
if (!string.IsNullOrEmpty(quantity))
announcement += $", have {quantity}";
}
if (!string.IsNullOrEmpty(announcement))
{
ScreenReader.Say(announcement);
DebugLogger.Log($"{LogTag} Announced: {announcement}");
}
}
catch { }
}
private string GetMaterialName(int index)
{
try
{
var contents = _panel.m_materialPanel?.m_materialContents;
if (contents == null || index < 0 || index >= contents.Length) return null;
return TextUtilities.StripRichTextTags(contents[index]?.m_nameUI?.text);
}
catch { return null; }
}
private string GetMaterialQuantity(int index)
{
try
{
var nums = _panel.m_materialPanel?.m_materialNums;
if (nums == null || index < 0 || index >= nums.Length) return null;
return nums[index]?.m_numUI?.text;
}
catch { return null; }
}
private string GetMaterialDescription()
{
try
{
return TextUtilities.StripRichTextTags(
_panel.m_materialPanel?.m_materialDescription?.m_descriptionUI?.text);
}
catch { return null; }
}
// ---- Helpers ----
private string GetConstructionCaption(bool gradeUp)
{
string fallback = gradeUp
? "Construction, Grade Up"
: "Construction, Material Donation";
try
{
if (_panel == null)
{
DebugLogger.Log($"{LogTag} Caption: m_panel was null");
return fallback;
}
uCaptionBase caption;
string fieldName;
if (gradeUp)
{
var gradeUpPanel = _panel.m_gradeUpPanel;
if (gradeUpPanel == null)
{
DebugLogger.Log($"{LogTag} Caption: m_gradeUpPanel was null");
return fallback;
}
caption = gradeUpPanel.m_constructionCaption;
fieldName = "m_gradeUpPanel.m_constructionCaption";
}
else
{
var materialPanel = _panel.m_materialPanel;
if (materialPanel == null)
{
DebugLogger.Log($"{LogTag} Caption: m_materialPanel was null");
return fallback;
}
caption = materialPanel.m_materialCaption;
fieldName = "m_materialPanel.m_materialCaption";
}
if (caption == null)
{
DebugLogger.Log($"{LogTag} Caption: {fieldName} was null");
return fallback;
}
var text = caption.m_text;
if (text == null)
{
DebugLogger.Log($"{LogTag} Caption: {fieldName}.m_text was null");
return fallback;
}
string cleaned = TextUtilities.StripRichTextTags(ButtonHintCache.Filter(text.text))?.Trim();
if (string.IsNullOrWhiteSpace(cleaned) || TextUtilities.IsPlaceholderText(cleaned))
{
DebugLogger.Log($"{LogTag} Caption: {fieldName}.m_text.text unusable: {TextUtilities.DescribeUnusable(cleaned)}");
return fallback;
}
return cleaned;
}
catch (System.Exception ex)
{
DebugLogger.Log($"{LogTag} Caption read failed: {ex.Message}");
return fallback;
}
}
private void ResetTracking()
{
ResetGradeUpTracking();
ResetMaterialTracking();
_lastParentState = uConstructionPanel.State.None;
}
private void ResetGradeUpTracking()
{
_lastBlockIndex = -1;
_lastContentIndex = -1;
_lastGradeUpState = uConstructionPanelGradeUp.State.None;
}
private void ResetMaterialTracking()
{
_lastKindIndex = -1;
_lastMaterialIndex = -1;
}
}
}