-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuHandler.cs
More file actions
425 lines (381 loc) · 17.7 KB
/
MenuHandler.cs
File metadata and controls
425 lines (381 loc) · 17.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
using HarmonyLib;
using MelonLoader;
using UnityEngine;
using TMPro;
using System.Collections;
using System.Collections.Generic;
namespace MelatoninAccess
{
public static class MenuHandler
{
private static float lastTitleTime = 0f;
private const float PendingTitleCombineWindowSeconds = 0.8f;
private const float PendingPrefaceCombineWindowSeconds = 1.2f;
private const float FirstOptionSuppressSeconds = 1.0f;
private static string _pendingMenuTitle = "";
private static float _pendingMenuTitleTime = -10f;
private static string _pendingPrefaceAnnouncement = "";
private static float _pendingPrefaceAnnouncementTime = -10f;
private static string _suppressedFirstOptionText = "";
private static float _suppressedFirstOptionUntilTime = -10f;
// --- Menu Title ---
[HarmonyPatch(typeof(MenuTitle), "Activate")]
public static class MenuTitle_Activate_Patch
{
public static void Postfix(MenuTitle __instance)
{
if (__instance.title != null)
{
var tmp = __instance.title.GetComponent<TextMeshPro>();
if (tmp != null)
{
lastTitleTime = Time.time;
_pendingMenuTitle = Loc.Get("menu_suffix", tmp.text);
_pendingMenuTitleTime = Time.unscaledTime;
}
}
}
}
// --- Option Navigation ---
[HarmonyPatch(typeof(Option), "Enable")]
public static class Option_Enable_Patch
{
public static void Postfix(Option __instance)
{
if (!string.IsNullOrWhiteSpace(_pendingMenuTitle) &&
Time.unscaledTime - _pendingMenuTitleTime > PendingTitleCombineWindowSeconds)
{
_pendingMenuTitle = "";
}
if (__instance.label != null)
{
var tmp = __instance.label.GetComponent<TextMeshPro>();
if (tmp != null)
{
string text = tmp.text;
int functionNum = Traverse.Create(__instance).Field("functionNum").GetValue<int>();
int chapterNum = __instance.chapterNum;
if (functionNum == 9)
{
if (SaveManager.mgr.GetChapterNum() < chapterNum)
{
text = Loc.Get("chapter_locked", chapterNum);
}
}
else if (functionNum == 36)
{
text = Loc.Get("action_key_current_binding", text, GetActionPromptLabel());
}
// Check if it's a slider
bool isSlider = false;
if (__instance.num != null && __instance.num.CheckIsMeshRendered())
{
var numTmp = __instance.num.GetComponent<TextMeshPro>();
if (numTmp != null && !string.IsNullOrEmpty(numTmp.text))
{
if (numTmp.text.Trim() != "9")
{
text = Loc.Get("option_with_slider", text, numTmp.text);
isSlider = true;
}
}
}
if (!isSlider && __instance.tip != null && __instance.tip.CheckIsMeshRendered())
{
var tipTmp = __instance.tip.GetComponent<TextMeshPro>();
if (tipTmp != null && !string.IsNullOrEmpty(tipTmp.text))
{
text += ". " + tipTmp.text;
}
}
// Append Switch State (On/Off)
if (__instance.lightSwitch != null && __instance.lightSwitch.CheckIsSpriteRendered())
{
bool state = false;
bool known = true;
switch (functionNum)
{
case 12: // Fullscreen
text += ": " + GetWindowModeText();
known = false;
break;
case 18: // Visual Assist
state = SaveManager.mgr.CheckIsVisualAssisting();
break;
case 19: // Audio Assist
state = SaveManager.mgr.CheckIsAudioAssisting();
break;
case 37: // WASD
state = SaveManager.mgr.CheckIsDirectionKeysAlt();
break;
case 41: // Vibration
state = !SaveManager.mgr.CheckIsVibrationDisabled();
break;
case 42: // Wiggle Room
state = SaveManager.mgr.CheckIsBiggerHitWindows();
break;
case 44: // Warmth
state = SaveManager.mgr.CheckIsWarmth();
break;
case 45: // Easy Scoring
state = SaveManager.mgr.CheckIsEasyScoring();
break;
case 47: // Perfects Only
state = SaveManager.mgr.CheckIsPerfectsOnly();
break;
case 50: // V-Sync
state = SaveManager.mgr.CheckIsVsynced();
break;
default:
known = false;
break;
}
if (known)
{
text += ": " + GetToggleStateText(state);
}
}
// Position in Menu (X of Y)
if (ModConfig.AnnounceMenuPositions && __instance.transform.parent != null)
{
var menu = __instance.transform.parent.GetComponent<Menu>();
if (menu != null)
{
var functioningOptions = Traverse.Create(menu).Field("functioningOptions").GetValue<List<Option>>();
if (functioningOptions != null)
{
int index = functioningOptions.IndexOf(__instance);
int count = functioningOptions.Count;
if (index >= 0)
{
text += ", " + Loc.Get("order_of", index + 1, count);
}
}
}
}
// Suppress immediate repeated first-item announcement right after a combined
// "Menu title + first option" utterance.
if (!string.IsNullOrWhiteSpace(_suppressedFirstOptionText))
{
if (Time.unscaledTime > _suppressedFirstOptionUntilTime)
{
_suppressedFirstOptionText = "";
_suppressedFirstOptionUntilTime = -10f;
}
else if (text == _suppressedFirstOptionText)
{
return;
}
}
// Combine first option with pending menu title when a menu just opened.
// Otherwise (normal navigation), interrupt as usual.
bool shouldCombineWithMenuTitle =
!string.IsNullOrWhiteSpace(_pendingMenuTitle) &&
Time.unscaledTime - _pendingMenuTitleTime <= PendingTitleCombineWindowSeconds;
if (shouldCombineWithMenuTitle)
{
string firstOptionText = text;
text = $"{_pendingMenuTitle}. {firstOptionText}";
_pendingMenuTitle = "";
_pendingMenuTitleTime = -10f;
_suppressedFirstOptionText = firstOptionText;
_suppressedFirstOptionUntilTime = Time.unscaledTime + FirstOptionSuppressSeconds;
string preface = ConsumePrefaceAnnouncementIfPending();
if (!string.IsNullOrWhiteSpace(preface))
{
text = $"{preface} {text}";
}
ScreenReader.Say(text, true);
return;
}
bool interrupt = (Time.time - lastTitleTime > 0.5f);
ScreenReader.Say(text, interrupt);
}
}
}
}
// --- Option Interaction ---
[HarmonyPatch(typeof(Option), "Select")]
public static class Option_Select_Patch
{
public static void Prefix(Option __instance, out OptionValueSnapshot __state)
{
__state = OptionHelper.CaptureSnapshot(__instance);
}
public static void Postfix(Option __instance, OptionValueSnapshot __state)
{
MelonCoroutines.Start(OptionHelper.AnnounceValueChangeDelayed(__instance, __state));
}
}
[HarmonyPatch(typeof(Option), "Reverse")]
public static class Option_Reverse_Patch
{
public static void Prefix(Option __instance, out OptionValueSnapshot __state)
{
__state = OptionHelper.CaptureSnapshot(__instance);
}
public static void Postfix(Option __instance, OptionValueSnapshot __state)
{
MelonCoroutines.Start(OptionHelper.AnnounceValueChangeDelayed(__instance, __state));
}
}
public struct OptionValueSnapshot
{
public int FunctionNum;
public string ValueText;
}
public static class OptionHelper
{
private const float ValueChangeReadDelay = 0.03f;
public static OptionValueSnapshot CaptureSnapshot(Option option)
{
if (option == null) return default;
int functionNum = Traverse.Create(option).Field("functionNum").GetValue<int>();
string valueText = "";
if (option.num != null && option.num.CheckIsMeshRendered())
{
var numTmp = option.num.GetComponent<TextMeshPro>();
if (numTmp != null)
{
valueText = numTmp.text ?? "";
}
}
return new OptionValueSnapshot
{
FunctionNum = functionNum,
ValueText = valueText
};
}
public static IEnumerator AnnounceValueChangeDelayed(Option option, OptionValueSnapshot snapshot)
{
if (option == null) yield break;
// Resolution (function 13) updates via Refresh; wait until we observe a changed value text
// or a short timeout so we read the post-adjusted value when available.
if (snapshot.FunctionNum == 13)
{
const int maxFramesToWait = 12;
for (int i = 0; i < maxFramesToWait; i++)
{
yield return null;
if (option.num == null || !option.num.CheckIsMeshRendered()) continue;
var numTmp = option.num.GetComponent<TextMeshPro>();
string current = numTmp != null ? (numTmp.text ?? "") : "";
if (!string.IsNullOrWhiteSpace(current) && current != snapshot.ValueText)
{
break;
}
}
AnnounceValueChange(option);
yield break;
}
yield return new WaitForSecondsRealtime(ValueChangeReadDelay);
AnnounceValueChange(option);
}
public static void AnnounceValueChange(Option option)
{
if (option == null) return;
int functionNum = Traverse.Create(option).Field("functionNum").GetValue<int>();
if (option.num != null && option.num.CheckIsMeshRendered())
{
var numTmp = option.num.GetComponent<TextMeshPro>();
if (numTmp != null && numTmp.text.Trim() != "9")
{
ScreenReader.Say(Loc.Get("slider_value", numTmp.text), true);
return;
}
}
bool state = false;
bool isToggle = false;
switch (functionNum)
{
case 12:
ScreenReader.Say(GetWindowModeText(), true);
return;
case 18:
isToggle = true;
state = SaveManager.mgr.CheckIsVisualAssisting();
break;
case 19:
isToggle = true;
state = SaveManager.mgr.CheckIsAudioAssisting();
break;
case 37:
isToggle = true;
state = SaveManager.mgr.CheckIsDirectionKeysAlt();
break;
case 41:
isToggle = true;
state = !SaveManager.mgr.CheckIsVibrationDisabled();
break;
case 42:
isToggle = true;
state = SaveManager.mgr.CheckIsBiggerHitWindows();
break;
case 44:
isToggle = true;
state = SaveManager.mgr.CheckIsWarmth();
break;
case 45:
isToggle = true;
state = SaveManager.mgr.CheckIsEasyScoring();
break;
case 47:
isToggle = true;
state = SaveManager.mgr.CheckIsPerfectsOnly();
break;
case 50:
isToggle = true;
state = SaveManager.mgr.CheckIsVsynced();
break;
}
if (isToggle)
{
ScreenReader.Say(GetToggleStateText(state), true);
}
}
}
private static string GetWindowModeText()
{
return Screen.fullScreen ? Loc.Get("fullscreen") : Loc.Get("windowed");
}
private static string GetActionPromptLabel()
{
string key = SaveManager.mgr != null ? SaveManager.mgr.GetActionKey() : "SPACE";
if (string.IsNullOrWhiteSpace(key)) return Loc.Get("cue_space");
return key.Trim().ToUpperInvariant() switch
{
"SPACE" => Loc.Get("cue_space"),
"ENTER" => Loc.Get("key_enter"),
"PERIOD" => Loc.Get("key_period"),
"SLASH" => Loc.Get("key_slash"),
_ => key.Trim().Length == 1
? key.Trim().ToUpperInvariant()
: char.ToUpperInvariant(key.Trim()[0]) + key.Trim().Substring(1).ToLowerInvariant()
};
}
private static string GetToggleStateText(bool state)
{
return state ? Loc.Get("state_on") : Loc.Get("state_off");
}
public static void QueuePrefaceAnnouncement(string text)
{
if (string.IsNullOrWhiteSpace(text)) return;
_pendingPrefaceAnnouncement = text.Trim();
_pendingPrefaceAnnouncementTime = Time.unscaledTime;
}
private static string ConsumePrefaceAnnouncementIfPending()
{
if (string.IsNullOrWhiteSpace(_pendingPrefaceAnnouncement)) return "";
if (Time.unscaledTime - _pendingPrefaceAnnouncementTime > PendingPrefaceCombineWindowSeconds)
{
_pendingPrefaceAnnouncement = "";
_pendingPrefaceAnnouncementTime = -10f;
return "";
}
string result = _pendingPrefaceAnnouncement;
_pendingPrefaceAnnouncement = "";
_pendingPrefaceAnnouncementTime = -10f;
return result;
}
}
}