Repository navigation
Expand file tree
/
Copy pathFarmPanelHandler.cs
More file actions
672 lines (581 loc) · 24 KB
/
Copy pathFarmPanelHandler.cs
File metadata and controls
672 lines (581 loc) · 24 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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
using Il2Cpp;
using UnityEngine;
namespace DigimonNOAccess
{
/// <summary>
/// Handles accessibility for the farm panel (farm goods management).
/// Tracks both the outer uFarmPanel (confirmation flow) and inner
/// uFarmPanelCommand (slot selection + item selection).
/// </summary>
public class FarmPanelHandler : IAccessibilityHandler
{
private const string LogTag = "[FarmPanel]";
public int Priority => 60;
// Debug flag for slot cursor detection (set true to diagnose)
private static bool _debugSlotCursor = false;
private uFarmPanel _farmPanel;
private uFarmPanelCommand _command;
private bool _wasActive;
// Outer panel state tracking
private uFarmPanel.State _lastFarmState = uFarmPanel.State.None;
// Command panel state tracking
private uFarmPanelCommand.State _lastCommandState = uFarmPanelCommand.State.None;
private int _lastSlotCursor = -1;
// Item selection tracking (when in Item state)
private int _lastItemSelectNo = -1;
public bool IsOpen()
{
try
{
// Find uFarmPanelCommand first (reliable via FindObjectOfType),
// then get the parent uFarmPanel from it
if (_command == null)
_command = Object.FindObjectOfType<uFarmPanelCommand>();
if (_command == null)
return false;
if (_command.gameObject == null || !_command.gameObject.activeInHierarchy)
return false;
// Get the parent uFarmPanel from the command
if (_farmPanel == null)
_farmPanel = _command.m_farmPanel;
// Check command state for basic open detection
var cmdState = _command.m_state;
if (cmdState != uFarmPanelCommand.State.None)
return true;
// Also check parent panel state (covers confirmation phases
// before the command panel activates)
if (_farmPanel != null)
{
var farmState = _farmPanel.m_state;
return farmState != uFarmPanel.State.None &&
farmState != uFarmPanel.State.Close;
}
return false;
}
catch
{
return false;
}
}
public void Update()
{
bool isActive = IsOpen();
if (isActive && !_wasActive)
OnOpen();
else if (!isActive && _wasActive)
OnClose();
else if (isActive)
OnUpdate();
_wasActive = isActive;
}
private void OnOpen()
{
_lastCommandState = uFarmPanelCommand.State.None;
_lastSlotCursor = -1;
_lastItemSelectNo = -1;
// Record current states but don't announce yet.
// The panel typically opens in Wait state before quickly transitioning
// to the interactive state. Let OnUpdate() handle the first real announcement
// so we don't say "Please wait" for a split second.
_lastFarmState = _farmPanel != null ? _farmPanel.m_state : uFarmPanel.State.None;
_lastCommandState = _command != null ? _command.m_state : uFarmPanelCommand.State.None;
DebugLogger.Log($"{LogTag} Panel opened, farmState={_lastFarmState}, cmdState={_lastCommandState}");
}
private void OnClose()
{
_farmPanel = null;
_command = null;
_wasActive = false;
_lastFarmState = uFarmPanel.State.None;
_lastCommandState = uFarmPanelCommand.State.None;
_lastSlotCursor = -1;
_lastItemSelectNo = -1;
DebugLogger.Log($"{LogTag} Panel closed");
}
private void OnUpdate()
{
if (_command == null)
return;
// Track outer panel state if available
if (_farmPanel != null)
{
var farmState = _farmPanel.m_state;
if (farmState != _lastFarmState)
{
var prevState = _lastFarmState;
_lastFarmState = farmState;
_lastCommandState = uFarmPanelCommand.State.None;
_lastSlotCursor = -1;
_lastItemSelectNo = -1;
if (farmState == uFarmPanel.State.CommandMain)
{
var cmdState = _command.m_state;
_lastCommandState = cmdState;
// Include "Farm." prefix on first real interactive state
AnnounceCommandState(cmdState, true);
}
else if (farmState != uFarmPanel.State.Wait)
{
// Don't announce transient Wait states
string stateText = GetFarmStateText(farmState);
ScreenReader.Say(stateText);
}
DebugLogger.Log($"{LogTag} Farm state changed to {farmState}");
return;
}
// Only track command panel in CommandMain
if (farmState != uFarmPanel.State.CommandMain &&
farmState != uFarmPanel.State.None)
return;
}
// Track command panel state changes
if (_command == null)
return;
var currentCmdState = _command.m_state;
// Command state changed
if (currentCmdState != _lastCommandState)
{
_lastCommandState = currentCmdState;
_lastSlotCursor = -1;
_lastItemSelectNo = -1;
// Skip announcing transient Wait states between Main↔Item
if (currentCmdState != uFarmPanelCommand.State.Wait)
AnnounceCommandState(currentCmdState, false);
DebugLogger.Log($"{LogTag} Command state changed to {currentCmdState}");
return;
}
// Track cursor depending on current command state
if (currentCmdState == uFarmPanelCommand.State.Main)
CheckSlotCursorChange();
else if (currentCmdState == uFarmPanelCommand.State.Item)
CheckItemSelectionChange();
}
private void AnnounceCommandState(uFarmPanelCommand.State cmdState, bool includeMenuName)
{
string prefix = includeMenuName ? $"{GetFarmTitle()}. " : "";
if (cmdState == uFarmPanelCommand.State.Main)
{
int cursor = GetSlotCursor();
string slotText = GetSlotDescription(cursor);
int total = GetSlotCount();
_lastSlotCursor = cursor;
ScreenReader.Say($"{prefix}Select farm slot. {slotText}, {cursor + 1} of {total}");
}
else if (cmdState == uFarmPanelCommand.State.Item)
{
int selectNo = GetItemSelectNo();
string itemText = GetSelectedItemName();
int total = GetItemCount();
_lastItemSelectNo = selectNo;
ScreenReader.Say($"{prefix}Select seed. {itemText}, {selectNo + 1} of {total}");
}
else if (cmdState == uFarmPanelCommand.State.Wait)
{
ScreenReader.Say($"{prefix}Please wait");
}
}
private void CheckSlotCursorChange()
{
int cursor = GetSlotCursor();
if (cursor == _lastSlotCursor || cursor < 0)
return;
string slotText = GetSlotDescription(cursor);
int total = GetSlotCount();
ScreenReader.Say(AnnouncementBuilder.CursorPosition(slotText, cursor, total));
DebugLogger.Log($"{LogTag} Slot cursor: {slotText}");
_lastSlotCursor = cursor;
}
private void CheckItemSelectionChange()
{
int selectNo = GetItemSelectNo();
if (selectNo == _lastItemSelectNo || selectNo < 0)
return;
_lastItemSelectNo = selectNo;
string itemText = GetSelectedItemName();
int total = GetItemCount();
ScreenReader.Say(AnnouncementBuilder.CursorPosition(itemText, selectNo, total));
DebugLogger.Log($"{LogTag} Item selection: {itemText}");
}
// --- Slot data reading ---
private int GetSlotCursor()
{
try
{
var contents = _command?.m_farmContents;
int activeCursorSlot = -1;
int farmCursorIndex = -1;
// Method 1: scan m_farmContents for active m_cursor GameObject
if (contents != null)
{
for (int i = 0; i < contents.Length; i++)
{
var content = contents[i];
if (content?.m_cursor != null && content.m_cursor.activeSelf)
{
activeCursorSlot = i;
break;
}
}
}
// Method 2: FarmCursor.index
var cursor = _command?.m_farmCursor;
if (cursor != null)
farmCursorIndex = cursor.index;
// Log both values so we can see which one works
if (_debugSlotCursor)
DebugLogger.Log($"{LogTag} SlotCursor debug: activeCursorSlot={activeCursorSlot}, farmCursorIndex={farmCursorIndex}");
// Prefer the active cursor scan, fall back to FarmCursor
if (activeCursorSlot >= 0)
return activeCursorSlot;
if (farmCursorIndex >= 0)
return farmCursorIndex;
}
catch { }
return 0;
}
private int GetSlotCount()
{
try
{
var contents = _command?.m_farmContents;
if (contents != null)
return contents.Length;
}
catch { }
return 1;
}
private string GetSlotDescription(int index)
{
try
{
// Try reading from FarmData for reliable info
var farmData = uFarmPanel.GetFarmData(index);
if (farmData != null && farmData.m_id != 0)
{
// Slot has something planted - get name from ParameterFarmData
string name = GetFarmItemName(farmData.m_id, index);
string condition = ParameterFarmData.GetCondition(farmData.m_condition);
int harvestCount = farmData.m_pick_num;
// Build description with all available info
string desc = name;
if (!string.IsNullOrEmpty(condition))
desc += $", {condition}";
// Add time/day info from UI (game formats this)
string dayText = GetSlotDayText(index);
if (!string.IsNullOrEmpty(dayText))
desc += $", {dayText}";
if (harvestCount > 0)
desc += $", harvest {harvestCount}";
return desc;
}
else
{
// Empty slot
string emptyName = ParameterFarmData.GetEmptyName();
string cleanedEmptyName = TextUtilities.StripRichTextTags(emptyName)?.Trim();
if (!string.IsNullOrWhiteSpace(cleanedEmptyName))
return cleanedEmptyName;
DebugLogger.Log($"{LogTag} Slot name: ParameterFarmData.GetEmptyName() was empty");
return GetFarmContentName(index, "Empty");
}
}
catch (System.Exception ex)
{
DebugLogger.Log($"{LogTag} Error reading slot {index}: {ex.Message}");
}
// Fallback: try UI text
return GetSlotTextFromUI(index);
}
private string GetFarmItemName(uint itemId, int slotIndex)
{
try
{
if (_farmPanel != null)
{
var paramData = _farmPanel.GetParameterFarmData(itemId);
if (paramData != null)
{
string name = TextUtilities.StripRichTextTags(paramData.GetName())?.Trim();
if (!string.IsNullOrWhiteSpace(name))
return name;
DebugLogger.Log($"{LogTag} Slot name: ParameterFarmData.GetName() was empty for item {itemId}");
}
else
{
DebugLogger.Log($"{LogTag} Slot name: GetParameterFarmData({itemId}) returned null");
}
}
else
{
DebugLogger.Log($"{LogTag} Slot name: m_farmPanel was null");
}
}
catch (System.Exception ex)
{
DebugLogger.Log($"{LogTag} Slot name read failed for item {itemId}: {ex.Message}");
}
return GetFarmContentName(slotIndex, $"Item {itemId}");
}
private string GetSlotDayText(int index)
{
try
{
var contents = _command?.m_farmContents;
if (contents != null && index >= 0 && index < contents.Length)
{
var content = contents[index];
string dayText = null;
string timeText = null;
if (content?.m_day != null && !string.IsNullOrEmpty(content.m_day.text))
dayText = content.m_day.text.Trim();
if (content?.m_time != null && !string.IsNullOrEmpty(content.m_time.text))
timeText = content.m_time.text.Trim();
// Format days with proper singular/plural
string formattedDays = FormatDays(dayText);
if (!string.IsNullOrEmpty(formattedDays) && !string.IsNullOrEmpty(timeText))
return $"{formattedDays} at {timeText}";
if (!string.IsNullOrEmpty(formattedDays))
return formattedDays;
if (!string.IsNullOrEmpty(timeText))
return timeText;
}
}
catch { }
return null;
}
private string FormatDays(string dayText)
{
if (string.IsNullOrEmpty(dayText))
return null;
if (int.TryParse(dayText, out int days))
return days == 1 ? "1 day" : $"{days} days";
return $"{dayText} days";
}
private string GetSlotTextFromUI(int index)
{
return GetFarmContentName(index, AnnouncementBuilder.FallbackItem("Farm slot", index));
}
// --- Item selection reading (seed list) ---
private FarmItem GetScrollView()
{
try
{
return _command?.m_farmPanelItem?.m_itemScrollView;
}
catch { }
return null;
}
private int GetItemSelectNo()
{
try
{
var scrollView = GetScrollView();
if (scrollView != null)
return scrollView.m_selectNo;
}
catch { }
return -1;
}
private int GetItemCount()
{
try
{
var scrollView = GetScrollView();
if (scrollView?.m_itemList != null)
return scrollView.m_itemList.Count;
}
catch { }
return 1;
}
private string GetSelectedItemName()
{
const string fallback = "Unknown item";
try
{
if (_command == null)
{
DebugLogger.Log($"{LogTag} Selected item: m_command was null");
return fallback;
}
var itemPanel = _command.m_farmPanelItem;
if (itemPanel == null)
{
DebugLogger.Log($"{LogTag} Selected item: m_farmPanelItem was null");
return fallback;
}
var scrollView = itemPanel.m_itemScrollView;
if (scrollView == null)
{
DebugLogger.Log($"{LogTag} Selected item: m_itemScrollView was null");
return fallback;
}
// Use GetSelectItemParam (from uItemBase) for the name.
var paramItem = scrollView.GetSelectItemParam();
if (paramItem != null)
{
string name = TextUtilities.StripRichTextTags(paramItem.GetName())?.Trim();
if (!string.IsNullOrWhiteSpace(name))
{
// Get quantity from the selected ItemData
var selectItem = scrollView.m_selectItem;
if (selectItem != null)
{
int count = selectItem.m_itemNum;
return count > 0 ? $"{name}, have {count}" : name;
}
DebugLogger.Log($"{LogTag} Selected item quantity: m_selectItem was null");
return name;
}
DebugLogger.Log($"{LogTag} Selected item: ParameterItemData.GetName() was empty");
}
else
{
DebugLogger.Log($"{LogTag} Selected item: GetSelectItemParam() returned null");
}
// Fallback: read the logical selection's rendered uItemParts name.
int selectNo = scrollView.m_selectNo;
var parts = scrollView.GetSelectItemParts(selectNo);
if (parts == null)
{
DebugLogger.Log($"{LogTag} Selected item: GetSelectItemParts({selectNo}) returned null");
return fallback;
}
var nameText = parts.m_name;
if (nameText == null)
{
DebugLogger.Log($"{LogTag} Selected item: selected uItemParts.m_name was null");
return fallback;
}
string renderedName = TextUtilities.StripRichTextTags(ButtonHintCache.Filter(nameText.text))?.Trim();
if (string.IsNullOrWhiteSpace(renderedName) || TextUtilities.IsPlaceholderText(renderedName))
{
DebugLogger.Log($"{LogTag} Selected item: selected uItemParts.m_name.text unusable: {TextUtilities.DescribeUnusable(renderedName)}");
return fallback;
}
return renderedName;
}
catch (System.Exception ex)
{
DebugLogger.Log($"{LogTag} Error reading item selection: {ex.Message}");
}
return fallback;
}
// --- Helpers ---
private string GetFarmTitle()
{
const string fallback = "Farm";
try
{
if (_command == null)
{
DebugLogger.Log($"{LogTag} Title: m_command was null");
return fallback;
}
var caption = _command.m_farmCaption;
if (caption == null)
{
DebugLogger.Log($"{LogTag} Title: m_farmCaption was null");
return fallback;
}
var text = caption.m_text;
if (text == null)
{
DebugLogger.Log($"{LogTag} Title: m_farmCaption.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} Title: m_farmCaption.m_text.text unusable: {TextUtilities.DescribeUnusable(cleaned)}");
return fallback;
}
return cleaned;
}
catch (System.Exception ex)
{
DebugLogger.Log($"{LogTag} Title read failed: {ex.Message}");
return fallback;
}
}
private string GetFarmContentName(int index, string fallback)
{
try
{
if (_command == null)
{
DebugLogger.Log($"{LogTag} Slot name: m_command was null");
return fallback;
}
var contents = _command.m_farmContents;
if (contents == null)
{
DebugLogger.Log($"{LogTag} Slot name: m_farmContents was null");
return fallback;
}
if (index < 0 || index >= contents.Length)
{
DebugLogger.Log($"{LogTag} Slot name: index {index} was outside m_farmContents length {contents.Length}");
return fallback;
}
var content = contents[index];
if (content == null)
{
DebugLogger.Log($"{LogTag} Slot name: m_farmContents[{index}] was null");
return fallback;
}
var nameText = content.m_name;
if (nameText == null)
{
DebugLogger.Log($"{LogTag} Slot name: m_farmContents[{index}].m_name was null");
return fallback;
}
string cleaned = TextUtilities.StripRichTextTags(ButtonHintCache.Filter(nameText.text))?.Trim();
if (string.IsNullOrWhiteSpace(cleaned) || TextUtilities.IsPlaceholderText(cleaned))
{
DebugLogger.Log($"{LogTag} Slot name: m_farmContents[{index}].m_name.text unusable: {TextUtilities.DescribeUnusable(cleaned)}");
return fallback;
}
return cleaned;
}
catch (System.Exception ex)
{
DebugLogger.Log($"{LogTag} Slot name read failed for index {index}: {ex.Message}");
return fallback;
}
}
private string GetFarmStateText(uFarmPanel.State state)
{
switch (state)
{
case uFarmPanel.State.ConfirmationInit:
case uFarmPanel.State.ConfirmationMain:
return "Confirm";
case uFarmPanel.State.CommandMain:
return "Select farm slot";
case uFarmPanel.State.Wait:
return "Please wait";
default:
return GetFarmTitle();
}
}
public void AnnounceStatus()
{
if (!IsOpen())
return;
if (_command != null && _command.m_state != uFarmPanelCommand.State.None)
{
AnnounceCommandState(_command.m_state, true);
return;
}
if (_farmPanel != null)
{
string stateText = GetFarmStateText(_farmPanel.m_state);
ScreenReader.Say($"{GetFarmTitle()}. {stateText}");
}
else
{
ScreenReader.Say(GetFarmTitle());
}
}
}
}