-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathExtraMenusHandler.cs
More file actions
399 lines (331 loc) · 14.4 KB
/
ExtraMenusHandler.cs
File metadata and controls
399 lines (331 loc) · 14.4 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
using HarmonyLib;
using MelonLoader;
using TMPro;
using UnityEngine;
using System.Collections;
namespace MelatoninAccess
{
public static class ExtraMenuDebounce
{
public const float ActivationCooldown = 0.5f;
public const float PageActionCooldown = 0.35f;
public const float SelectionCooldown = 0.35f;
public static float LastCalibrationActivationTime = -10f;
}
// --- Calibration Tool ---
[HarmonyPatch(typeof(CalibrationTool), "Activate")]
public static class CalibrationTool_Activate_Patch
{
public static void Postfix(CalibrationTool __instance)
{
float now = Time.unscaledTime;
if (now - ExtraMenuDebounce.LastCalibrationActivationTime < ExtraMenuDebounce.ActivationCooldown) return;
ExtraMenuDebounce.LastCalibrationActivationTime = now;
ScreenReader.Say(Loc.Get("calibration_tool_intro"), true);
CalibrationHelper.AnnounceCalibration(__instance);
}
}
[HarmonyPatch(typeof(CalibrationTool), "Update")]
public static class CalibrationTool_Update_Patch
{
static string lastText = "";
public static void Postfix(CalibrationTool __instance)
{
bool isActivated = Traverse.Create(__instance).Field("isActivated").GetValue<bool>();
if (!isActivated) return;
if (__instance.number != null && __instance.number.CheckIsMeshRendered())
{
var tmp = __instance.number.GetComponent<TextMeshPro>();
if (tmp != null && tmp.text != lastText)
{
lastText = tmp.text;
ScreenReader.Say(Loc.Get("calibration_offset", lastText), true);
}
}
}
}
[HarmonyPatch(typeof(PingBar), "StopTimer")]
public static class PingBar_StopTimer_Patch
{
public static void Prefix(PingBar __instance)
{
if (CalibrationTool.env == null || !CalibrationTool.env.CheckIsActivated()) return;
float timer = Traverse.Create(__instance).Field("timer").GetValue<float>();
int deltaMs = Mathf.RoundToInt((timer - 0.11667f) * 1000f);
int absMs = Mathf.Abs(deltaMs);
if (absMs <= 5)
{
ScreenReader.Say(Loc.Get("calibration_timing_on_time"), true);
return;
}
string key = deltaMs < 0 ? "calibration_timing_early_ms" : "calibration_timing_late_ms";
ScreenReader.Say(Loc.Get(key, absMs), true);
}
}
public static class CalibrationHelper
{
private static string _lastDescription = "";
private static float _lastDescriptionTime = -10f;
public static void AnnounceCalibration(CalibrationTool tool)
{
if (tool.description != null)
{
var tmp = tool.description.GetComponent<TextMeshPro>();
if (tmp != null && !string.IsNullOrWhiteSpace(tmp.text))
{
string text = tmp.text.Trim();
float now = Time.unscaledTime;
if (text == _lastDescription && now - _lastDescriptionTime < ExtraMenuDebounce.ActivationCooldown) return;
_lastDescription = text;
_lastDescriptionTime = now;
ScreenReader.Say(text);
}
}
}
}
// --- Community Menu (Downloaded Levels) ---
[HarmonyPatch(typeof(CommunityMenu), "Activate")]
public static class CommunityMenu_Activate_Patch
{
private static bool isAnnouncing = false;
public static void Postfix(CommunityMenu __instance)
{
if (isAnnouncing) return;
isAnnouncing = true;
ScreenReader.Say(Loc.Get("downloaded_levels_loading"), true);
MelonCoroutines.Start(AnnounceWhenLoaded(__instance));
}
private static IEnumerator AnnounceWhenLoaded(CommunityMenu menu)
{
// Wait for download to finish
yield return new WaitUntil(() => !SteamWorkshop.mgr.CheckIsDownloading());
yield return CommunityMenuHelper.WaitForRowsReady(menu);
if (menu.CheckIsActivated())
{
CommunityMenuHelper.AnnounceLoadedSummary(menu);
}
isAnnouncing = false;
}
}
[HarmonyPatch(typeof(CommunityMenu), "Deactivate")]
public static class CommunityMenu_Deactivate_Patch
{
public static void Postfix()
{
// Reset state if needed, though isAnnouncing handles local loop
}
}
[HarmonyPatch(typeof(CommunityMenu), "Descend")]
public static class CommunityMenu_Descend_Patch
{
public static void Postfix(CommunityMenu __instance)
{
CommunityMenuHelper.AnnounceSelection(__instance);
}
}
[HarmonyPatch(typeof(CommunityMenu), "Ascend")]
public static class CommunityMenu_Ascend_Patch
{
public static void Postfix(CommunityMenu __instance)
{
CommunityMenuHelper.AnnounceSelection(__instance);
}
}
[HarmonyPatch(typeof(CommunityMenu), "NextPage")]
public static class CommunityMenu_NextPage_Patch
{
public static void Postfix(CommunityMenu __instance)
{
string action = Loc.Get("next_page");
if (!CommunityMenuHelper.ShouldAnnouncePageAction(action)) return;
MelonCoroutines.Start(CommunityMenuHelper.AnnouncePageActionWithFirstItem(__instance, action));
}
}
[HarmonyPatch(typeof(CommunityMenu), "PrevPage")]
public static class CommunityMenu_PrevPage_Patch
{
public static void Postfix(CommunityMenu __instance)
{
string action = Loc.Get("previous_page");
if (!CommunityMenuHelper.ShouldAnnouncePageAction(action)) return;
MelonCoroutines.Start(CommunityMenuHelper.AnnouncePageActionWithFirstItem(__instance, action));
}
}
public static class CommunityMenuHelper
{
private static string _lastPageAction = "";
private static float _lastPageActionTime = -10f;
private static string _lastSelectionText = "";
private static float _lastSelectionTime = -10f;
private static int _lastPageNum = -1;
private static int _lastPageTotal = -1;
private static float _lastPageAnnounceTime = -10f;
private static float _suppressSelectionUntil = -10f;
public static bool ShouldAnnouncePageAction(string action)
{
float now = Time.unscaledTime;
if (action == _lastPageAction && now - _lastPageActionTime < ExtraMenuDebounce.PageActionCooldown) return false;
_lastPageAction = action;
_lastPageActionTime = now;
return true;
}
public static void AnnounceSelection(CommunityMenu menu)
{
if (Time.unscaledTime < _suppressSelectionUntil) return;
int highlightNum = Traverse.Create(menu).Field("highlightNum").GetValue<int>();
if (menu.LevelRows != null && highlightNum >= 0 && highlightNum < menu.LevelRows.Length)
{
var row = menu.LevelRows[highlightNum];
if (row != null)
{
int visibleRows = 0;
foreach(var r in menu.LevelRows) if(r.gameObject.activeSelf) visibleRows++;
string rowText = BuildCommunityRowText(row);
string selection = ModConfig.AnnounceMenuPositions
? Loc.Get("item_of", rowText, highlightNum + 1, visibleRows)
: rowText;
float now = Time.unscaledTime;
if (selection == _lastSelectionText && now - _lastSelectionTime < ExtraMenuDebounce.SelectionCooldown) return;
_lastSelectionText = selection;
_lastSelectionTime = now;
ScreenReader.Say(selection, true);
}
}
}
public static void AnnounceLoadedSummary(CommunityMenu menu)
{
string summary = BuildLoadedSummary(menu);
if (string.IsNullOrWhiteSpace(summary)) return;
ScreenReader.Say(summary, false);
_suppressSelectionUntil = Time.unscaledTime + 0.8f;
}
public static IEnumerator AnnouncePageActionWithFirstItem(CommunityMenu menu, string action)
{
if (menu == null || string.IsNullOrWhiteSpace(action)) yield break;
int expectedPageNum = Traverse.Create(menu).Field("pageNum").GetValue<int>();
yield return WaitForRowsReady(menu, expectedPageNum);
if (!menu.CheckIsActivated()) yield break;
string firstItem = BuildHighlightedSelection(menu);
string message = string.IsNullOrWhiteSpace(firstItem)
? action
: action + ". " + firstItem;
ScreenReader.Say(message, true);
_suppressSelectionUntil = Time.unscaledTime + 0.8f;
}
public static IEnumerator WaitForRowsReady(CommunityMenu menu, int expectedPageNum = -1)
{
if (menu == null) yield break;
float start = Time.unscaledTime;
while (Time.unscaledTime - start < 2f)
{
if (!menu.CheckIsActivated()) yield break;
if (SteamWorkshop.mgr.CheckIsDownloading())
{
yield return null;
continue;
}
int rowCount = Traverse.Create(menu).Field("rowCount").GetValue<int>();
int pageNum = Traverse.Create(menu).Field("pageNum").GetValue<int>();
if (rowCount > 1 && (expectedPageNum < 1 || pageNum == expectedPageNum))
{
yield return null; // Ensure row text fragments are fully updated.
yield break;
}
yield return null;
}
}
public static void AnnouncePage(CommunityMenu menu)
{
if (!ModConfig.AnnounceMenuPositions) return;
int pageNum = Traverse.Create(menu).Field("pageNum").GetValue<int>();
int pageTotal = Traverse.Create(menu).Field("pageTotal").GetValue<int>();
float now = Time.unscaledTime;
if (pageNum == _lastPageNum && pageTotal == _lastPageTotal && now - _lastPageAnnounceTime < ExtraMenuDebounce.PageActionCooldown) return;
_lastPageNum = pageNum;
_lastPageTotal = pageTotal;
_lastPageAnnounceTime = now;
ScreenReader.Say(Loc.Get("page_of", pageNum, pageTotal), false);
}
private static string BuildLoadedSummary(CommunityMenu menu)
{
if (menu == null) return "";
int totalLevels = Traverse.Create(menu).Field("totalLevels").GetValue<int>();
int pageNum = Traverse.Create(menu).Field("pageNum").GetValue<int>();
int pageTotal = Traverse.Create(menu).Field("pageTotal").GetValue<int>();
string summary = ModConfig.AnnounceMenuPositions
? Loc.Get("downloaded_levels_page_total", totalLevels, pageNum, pageTotal)
: Loc.Get("downloaded_levels_total", totalLevels);
string firstItem = BuildHighlightedSelection(menu);
if (!string.IsNullOrWhiteSpace(firstItem))
{
summary += ". " + firstItem;
}
return summary;
}
private static string BuildHighlightedSelection(CommunityMenu menu)
{
if (menu == null || menu.LevelRows == null) return "";
int highlightNum = Traverse.Create(menu).Field("highlightNum").GetValue<int>();
if (highlightNum < 0 || highlightNum >= menu.LevelRows.Length || menu.LevelRows[highlightNum] == null)
{
highlightNum = 0;
}
LevelRow row = menu.LevelRows[highlightNum];
if (row == null || !row.gameObject.activeSelf)
{
for (int i = 0; i < menu.LevelRows.Length; i++)
{
LevelRow candidate = menu.LevelRows[i];
if (candidate != null && candidate.gameObject.activeSelf)
{
row = candidate;
highlightNum = i;
break;
}
}
}
if (row == null) return "";
string rowText = BuildCommunityRowText(row);
if (!ModConfig.AnnounceMenuPositions) return rowText;
int visibleRows = 0;
foreach (var menuRow in menu.LevelRows)
{
if (menuRow != null && menuRow.gameObject.activeSelf) visibleRows++;
}
if (visibleRows <= 0) visibleRows = 1;
return Loc.Get("item_of", rowText, highlightNum + 1, visibleRows);
}
private static string BuildCommunityRowText(LevelRow row)
{
string title = CleanFragmentText(row != null ? row.title : null);
string subtitle = CleanFragmentText(row != null ? row.subtitle : null);
string tags = CleanFragmentText(row != null ? row.tags : null);
if (string.IsNullOrWhiteSpace(title)) title = Loc.Get("unknown_level");
string combined = title;
if (!string.IsNullOrWhiteSpace(subtitle) && !string.Equals(subtitle, title, System.StringComparison.OrdinalIgnoreCase))
{
combined += ". " + subtitle;
}
if (!string.IsNullOrWhiteSpace(tags) &&
!string.Equals(tags, title, System.StringComparison.OrdinalIgnoreCase) &&
!string.Equals(tags, subtitle, System.StringComparison.OrdinalIgnoreCase))
{
combined += ". " + tags;
}
return combined;
}
private static string CleanFragmentText(textboxFragment fragment)
{
if (fragment == null) return "";
var tmp = fragment.GetComponent<TextMeshPro>();
if (tmp == null || string.IsNullOrWhiteSpace(tmp.text)) return "";
return tmp.text
.Replace("\r", " ")
.Replace("\n", " ")
.Replace("<br>", ", ")
.Replace("<br/>", ", ")
.Replace("<BR>", ", ")
.Trim();
}
}
}