-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreditsHandler.cs
More file actions
216 lines (183 loc) · 6.31 KB
/
CreditsHandler.cs
File metadata and controls
216 lines (183 loc) · 6.31 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
using TMPro;
using UnityEngine;
namespace BlippoAccess
{
/// <summary>
/// Announces Credits navigation context and current section while credits roll.
/// </summary>
public sealed class CreditsHandler
{
private const float SectionAnnouncementCooldownSeconds = 1.25f;
private bool _wasActive;
private bool _entryAnnounced;
private int _lastSectionIndex = -1;
private float _lastAnnouncementTime;
/// <summary>
/// Tracks Credits screen activity and reads section changes.
/// </summary>
public void Update()
{
var credits = GetCreditsScreen();
if (credits == null)
{
ResetState();
return;
}
_wasActive = true;
AnnounceEntry();
AnnounceCurrentSection(credits);
}
/// <summary>
/// Attempts to build the current credits-section announcement.
/// </summary>
/// <param name="announcement">Current section announcement when available.</param>
/// <returns>True when a section announcement could be built.</returns>
public static bool TryBuildCurrentSectionAnnouncement(out string announcement)
{
announcement = string.Empty;
var credits = GetCreditsScreen();
if (credits == null)
{
return false;
}
var currentSectionIndex = GetCurrentSectionIndex(credits);
if (currentSectionIndex < 0)
{
return false;
}
var total = credits.sections != null ? credits.sections.Count : 0;
var title = GetSectionTitle(credits, currentSectionIndex);
if (string.IsNullOrWhiteSpace(title))
{
title = Loc.Get("credits_section_unknown");
}
announcement = total > 0
? Loc.Get("credits_section", title, currentSectionIndex + 1, total)
: Loc.Get("credits_section_text", title);
return true;
}
private void ResetState()
{
if (!_wasActive)
{
return;
}
_wasActive = false;
_entryAnnounced = false;
_lastSectionIndex = -1;
_lastAnnouncementTime = 0f;
}
private void AnnounceEntry()
{
if (_entryAnnounced)
{
return;
}
_entryAnnounced = true;
ScreenReader.SayQueued(Loc.Get("credits_controls_hint"));
_lastAnnouncementTime = Time.unscaledTime;
DebugLogger.Log(LogCategory.Handler, "Credits entry hint announced");
}
private void AnnounceCurrentSection(Credits credits)
{
var currentSectionIndex = GetCurrentSectionIndex(credits);
if (currentSectionIndex < 0 || currentSectionIndex == _lastSectionIndex)
{
return;
}
if (Time.unscaledTime - _lastAnnouncementTime < SectionAnnouncementCooldownSeconds)
{
return;
}
_lastSectionIndex = currentSectionIndex;
var total = credits.sections != null ? credits.sections.Count : 0;
var title = GetSectionTitle(credits, currentSectionIndex);
if (string.IsNullOrWhiteSpace(title))
{
title = Loc.Get("credits_section_unknown");
}
if (total > 0)
{
ScreenReader.Say(Loc.Get("credits_section", title, currentSectionIndex + 1, total));
}
else
{
ScreenReader.Say(Loc.Get("credits_section_text", title));
}
_lastAnnouncementTime = Time.unscaledTime;
DebugLogger.Log(LogCategory.Handler, $"Credits section: {title} ({currentSectionIndex + 1}/{total})");
}
private static Credits GetCreditsScreen()
{
if (GameManager.instance == null || Bookshelf.instance == null)
{
return null;
}
if (GameManager.currentSystemScreen != SystemScreen.Type.CREDITS)
{
return null;
}
if (!Bookshelf.instance.systemScreens.ContainsKey(SystemScreen.Type.CREDITS))
{
return null;
}
var screen = Bookshelf.instance.systemScreens[SystemScreen.Type.CREDITS];
if (screen == null || !screen.screenEnabled)
{
return null;
}
return screen as Credits;
}
private static int GetCurrentSectionIndex(Credits credits)
{
if (credits.sections == null || credits.sections.Count == 0)
{
return -1;
}
for (var i = 0; i < credits.sections.Count; i++)
{
var section = credits.sections[i];
if (section == null)
{
continue;
}
if (section.position.y > -270f && section.position.y < 0f)
{
return i;
}
}
return -1;
}
private static string GetSectionTitle(Credits credits, int sectionIndex)
{
if (credits.sections == null || sectionIndex < 0 || sectionIndex >= credits.sections.Count)
{
return string.Empty;
}
var section = credits.sections[sectionIndex];
if (section == null)
{
return string.Empty;
}
var textFields = section.GetComponentsInChildren<TMP_Text>(true);
if (textFields == null || textFields.Length == 0)
{
return string.Empty;
}
for (var i = 0; i < textFields.Length; i++)
{
var text = UiTextHelper.GetText(textFields[i]);
if (string.IsNullOrWhiteSpace(text))
{
continue;
}
if (text.Length > 140)
{
continue;
}
return text;
}
return string.Empty;
}
}
}