-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDialogueHandler.cs
More file actions
168 lines (141 loc) · 5.26 KB
/
DialogueHandler.cs
File metadata and controls
168 lines (141 loc) · 5.26 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
using HarmonyLib;
using MelonLoader;
using TMPro;
using UnityEngine;
using System.Collections;
namespace MelatoninAccess
{
// --- Dialogue ---
[HarmonyPatch(typeof(DialogBox), "SetText")]
public static class DialogBox_SetText_Patch
{
public static void Postfix(DialogBox __instance, string newText)
{
DialogHelper.ReadDialogText(newText);
}
}
[HarmonyPatch(typeof(DialogBox), "ChangeDialogState")]
public static class DialogBox_ChangeDialogState_Patch
{
public static void Postfix(DialogBox __instance)
{
MelonCoroutines.Start(DialogHelper.ReadDialogDelayed(__instance, 0.2f));
}
}
[HarmonyPatch(typeof(DialogBox), "SetDialogState")]
public static class DialogBox_SetDialogState_Patch
{
public static void Postfix(DialogBox __instance)
{
MelonCoroutines.Start(DialogHelper.ReadDialogDelayed(__instance, 0.12f));
}
}
[HarmonyPatch(typeof(DialogBox), "ChangeToGraphic")]
public static class DialogBox_ChangeToGraphic_Patch
{
public static void Postfix(DialogBox __instance, int graphicNum, int newStateLeft, int newStateRight)
{
DialogHelper.ReadGraphicDialog(__instance);
}
}
[HarmonyPatch(typeof(DialogBox), "Show")]
public static class DialogBox_Show_Patch
{
public static void Postfix(DialogBox __instance)
{
MelonCoroutines.Start(DialogHelper.ReadDialogDelayed(__instance, 0.12f));
}
}
[HarmonyPatch(typeof(DialogBox), "Activate")]
public static class DialogBox_Activate_Patch
{
public static void Postfix(DialogBox __instance)
{
MelonCoroutines.Start(DialogHelper.ReadDialogDelayed(__instance, 0.08f));
}
}
[HarmonyPatch(typeof(DialogBox), "ActivateDelayed")]
public static class DialogBox_ActivateDelayed_Patch
{
public static void Postfix(DialogBox __instance, float delta)
{
// Wait for the delay (approx) then read.
// The game waits (0.11667f - delta).
float waitTime = Mathf.Max(0.11667f - delta, 0f) + 0.05f;
MelonCoroutines.Start(DialogHelper.ReadDialogDelayed(__instance, waitTime));
}
}
public static class DialogHelper
{
private const float DialogRepeatBlockSeconds = 0.8f;
private static string _lastDialogText = "";
private static float _lastDialogTime = -10f;
private static void SpeakDialog(string text, bool interrupt)
{
if (!ModConfig.AnnounceTutorialDialog) return;
if (string.IsNullOrWhiteSpace(text)) return;
text = text.Replace("\r", " ").Replace("\n", " ").Trim();
if (string.IsNullOrEmpty(text)) return;
float now = Time.unscaledTime;
if (text == _lastDialogText && now - _lastDialogTime < DialogRepeatBlockSeconds) return;
_lastDialogText = text;
_lastDialogTime = now;
ScreenReader.Say(text, interrupt);
}
public static void ReadDialogText(string text)
{
SpeakDialog(text, true);
}
public static void ReadDialog(DialogBox box)
{
if (box == null) return;
string dialogText = GetFragmentText(box.dialog);
if (string.IsNullOrWhiteSpace(dialogText))
{
ReadGraphicDialog(box);
return;
}
SpeakDialog(dialogText, true);
}
public static void ReadGraphicDialog(DialogBox box)
{
if (box == null) return;
string left = GetFragmentText(box.leftGraphicText);
string right = GetFragmentText(box.rightGraphicText);
if (string.IsNullOrWhiteSpace(left) && string.IsNullOrWhiteSpace(right)) return;
string text = string.IsNullOrWhiteSpace(left)
? right
: string.IsNullOrWhiteSpace(right)
? left
: JoinDialogParts(left, right);
SpeakDialog(text, true);
}
public static IEnumerator ReadDialogDelayed(DialogBox box, float delay)
{
yield return new WaitForSecondsRealtime(delay);
ReadDialog(box);
}
public static IEnumerator ReadGraphicDialogDelayed(DialogBox box, float delay)
{
yield return new WaitForSecondsRealtime(delay);
ReadGraphicDialog(box);
}
private static string GetFragmentText(textboxFragment fragment)
{
if (fragment == null) return "";
var tmp = fragment.GetComponent<TextMeshPro>();
if (tmp == null || string.IsNullOrWhiteSpace(tmp.text)) return "";
return tmp.text.Trim();
}
private static string JoinDialogParts(string left, string right)
{
if (string.IsNullOrWhiteSpace(left)) return right;
if (string.IsNullOrWhiteSpace(right)) return left;
char last = left[left.Length - 1];
bool hasSentencePunctuation = last == '.' || last == '!' || last == '?' || last == '…';
return hasSentencePunctuation
? $"{left} {right}"
: $"{left}. {right}";
}
}
}