-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactoryResetHandler.cs
More file actions
255 lines (217 loc) · 8.15 KB
/
FactoryResetHandler.cs
File metadata and controls
255 lines (217 loc) · 8.15 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
using System;
using UnityEngine;
using UnityEngine.EventSystems;
namespace BlippoAccess
{
/// <summary>
/// Adds safety announcements for Factory Reset focus and confirmation flow in Control Menu.
/// </summary>
public sealed class FactoryResetHandler
{
private const float FocusWarningCooldownSeconds = 2f;
private const float SubmitResultTimeoutSeconds = 8f;
private bool _wasControlMenuActive;
private int _lastSelectionId = int.MinValue;
private float _lastFocusWarningTime;
private Submenu _lastSubmenu;
private bool _awaitingFactoryResetResult;
private float _awaitingFactoryResetResultUntil;
/// <summary>
/// Tracks Factory Reset-related focus and announces high-risk context clearly.
/// </summary>
public void Update()
{
var menu = GetControlMenu();
if (menu == null || menu.currentSubmenu == null)
{
HandlePendingResultOutsideMenu();
ResetMenuState();
return;
}
_wasControlMenuActive = true;
AnnounceSubmenuWarningIfNeeded(menu.currentSubmenu);
AnnounceFactoryResetFocusWarning(menu.currentSubmenu);
TrackFactoryResetSubmit(menu.currentSubmenu);
}
private void ResetMenuState()
{
if (!_wasControlMenuActive)
{
return;
}
_wasControlMenuActive = false;
_lastSelectionId = int.MinValue;
_lastSubmenu = null;
_lastFocusWarningTime = 0f;
}
private void HandlePendingResultOutsideMenu()
{
if (!_awaitingFactoryResetResult)
{
return;
}
if (GameManager.currentSystemScreen == SystemScreen.Type.PACKETTE_LOAD)
{
ScreenReader.Say(Loc.Get("factory_reset_completed"));
DebugLogger.Log(LogCategory.Handler, "Factory reset completed");
_awaitingFactoryResetResult = false;
return;
}
if (Time.unscaledTime >= _awaitingFactoryResetResultUntil)
{
_awaitingFactoryResetResult = false;
}
}
private void AnnounceSubmenuWarningIfNeeded(Submenu submenu)
{
if (submenu == null || submenu == _lastSubmenu)
{
return;
}
_lastSubmenu = submenu;
if (!SubmenuContainsFactoryResetAction(submenu))
{
return;
}
ScreenReader.SayQueued(Loc.Get("factory_reset_submenu_warning"));
ScreenReader.SayQueued(Loc.Get("factory_reset_cancel_hint"));
DebugLogger.Log(LogCategory.Handler, "Factory reset submenu warning announced");
}
private void AnnounceFactoryResetFocusWarning(Submenu submenu)
{
var selectedObject = EventSystem.current != null ? EventSystem.current.currentSelectedGameObject : null;
if (selectedObject == null)
{
return;
}
var menuButton = selectedObject.GetComponent<MenuButton>() ?? selectedObject.GetComponentInParent<MenuButton>();
if (menuButton == null)
{
_lastSelectionId = selectedObject.GetInstanceID();
return;
}
var selectionId = selectedObject.GetInstanceID();
if (selectionId == _lastSelectionId)
{
return;
}
_lastSelectionId = selectionId;
if (!IsFactoryResetActionButton(menuButton))
{
return;
}
if (Time.unscaledTime - _lastFocusWarningTime < FocusWarningCooldownSeconds)
{
return;
}
_lastFocusWarningTime = Time.unscaledTime;
ScreenReader.SayQueued(Loc.Get("factory_reset_focus_warning"));
ScreenReader.SayQueued(Loc.Get("factory_reset_cancel_hint"));
DebugLogger.Log(LogCategory.Handler, "Factory reset focus warning announced");
}
private void TrackFactoryResetSubmit(Submenu submenu)
{
if (submenu == null)
{
return;
}
var selectedObject = EventSystem.current != null ? EventSystem.current.currentSelectedGameObject : null;
if (selectedObject == null)
{
return;
}
var menuButton = selectedObject.GetComponent<MenuButton>() ?? selectedObject.GetComponentInParent<MenuButton>();
if (!IsFactoryResetActionButton(menuButton))
{
return;
}
var submitPressed = Input.GetKeyDown(KeyCode.Return) ||
Input.GetKeyDown(KeyCode.KeypadEnter);
if (!submitPressed)
{
submitPressed = Input.GetKeyDown(KeyCode.JoystickButton0);
}
if (!submitPressed)
{
return;
}
_awaitingFactoryResetResult = true;
_awaitingFactoryResetResultUntil = Time.unscaledTime + SubmitResultTimeoutSeconds;
ScreenReader.Say(Loc.Get("factory_reset_submitting"));
DebugLogger.Log(LogCategory.Handler, "Factory reset submit detected");
}
private static ControlMenu GetControlMenu()
{
if (GameManager.instance == null || Bookshelf.instance == null)
{
return null;
}
if (GameManager.currentSystemScreen != SystemScreen.Type.CONTROL_MENU)
{
return null;
}
if (!Bookshelf.instance.systemScreens.ContainsKey(SystemScreen.Type.CONTROL_MENU))
{
return null;
}
var screen = Bookshelf.instance.systemScreens[SystemScreen.Type.CONTROL_MENU];
if (screen == null || !screen.screenEnabled)
{
return null;
}
return screen as ControlMenu;
}
private static bool SubmenuContainsFactoryResetAction(Submenu submenu)
{
if (submenu == null || submenu.menuButtons == null || submenu.menuButtons.Count == 0)
{
return false;
}
foreach (var pair in submenu.menuButtons)
{
if (IsFactoryResetActionButton(pair.Value))
{
return true;
}
}
return false;
}
internal static bool IsFactoryResetContext(Submenu submenu, GameObject selectedObject)
{
if (SubmenuContainsFactoryResetAction(submenu))
{
return true;
}
if (selectedObject == null)
{
return false;
}
var menuButton = selectedObject.GetComponent<MenuButton>() ?? selectedObject.GetComponentInParent<MenuButton>();
return IsFactoryResetActionButton(menuButton);
}
private static bool IsFactoryResetActionButton(MenuButton menuButton)
{
if (menuButton == null || menuButton.button == null || menuButton.button.onClick == null)
{
return false;
}
var onClick = menuButton.button.onClick;
var persistentCount = onClick.GetPersistentEventCount();
for (var i = 0; i < persistentCount; i++)
{
var methodName = onClick.GetPersistentMethodName(i);
if (string.Equals(methodName, "FactoryReset", StringComparison.Ordinal))
{
return true;
}
}
var label = UiTextHelper.GetMenuButtonLabel(menuButton);
if (!string.IsNullOrWhiteSpace(label) &&
label.IndexOf("factory reset", StringComparison.OrdinalIgnoreCase) >= 0)
{
return true;
}
return menuButton.gameObject.name.IndexOf("factory reset", StringComparison.OrdinalIgnoreCase) >= 0;
}
}
}