Skip to content

Commit 82b7695

Browse files
Create AnimatedBroadcastMethod.cs
1 parent cbdb747 commit 82b7695

1 file changed

Lines changed: 157 additions & 0 deletions

File tree

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
using JetBrains.Annotations;
2+
using LabApi.Features.Wrappers;
3+
using SER.Code.ArgumentSystem.Arguments;
4+
using SER.Code.ArgumentSystem.BaseArguments;
5+
using SER.Code.MethodSystem.BaseMethods.Synchronous;
6+
using SER.Code.MethodSystem.MethodDescriptors;
7+
8+
namespace SER.Code.MethodSystem.Methods.BroadcastMethods;
9+
10+
[UsedImplicitly]
11+
public class AnimatedBroadcastMethod : SynchronousMethod, IAdditionalDescription
12+
{
13+
public override string Description => "Sends an animated broadcast to all players.";
14+
15+
public override Argument[] ExpectedArguments { get; } =
16+
[
17+
new DurationArgument("duration"),
18+
new TextArgument("content")
19+
];
20+
21+
public override void Execute()
22+
{
23+
var content = Args.GetText("content");
24+
var duration = Args.GetDuration("duration").TotalSeconds;
25+
Announcer.Clear();
26+
Announcer.Message(
27+
$"$SLEEP_{duration} .",
28+
Helper.FormatToCassieCentralScreenSubtitles(content, true),
29+
false,
30+
696969,
31+
0
32+
);
33+
}
34+
35+
public string AdditionalDescription =>
36+
"Uses CASSIE to make an animated broadcast - if there is CASSIE playing, it will be stopped. " +
37+
"Keep custom formatting to a minimum, this system is very limited.";
38+
}
39+
40+
public static class Helper
41+
{
42+
public static string FormatToRawCassieSubtitles(string text, bool addWaits)
43+
{
44+
var result = "";
45+
var index = 72;
46+
47+
foreach (var line in text.Split('\n'))
48+
{
49+
// Skip empty lines
50+
if (line.Length == 0)
51+
{
52+
index -= 1;
53+
continue;
54+
}
55+
56+
// Calculate actual length excluding HTML tags
57+
var len = CalculateTextLength(line);
58+
var parts = new List<string>();
59+
60+
// Split long lines
61+
if (len > 80)
62+
{
63+
SplitLongLine(line, parts);
64+
}
65+
else
66+
{
67+
parts.Add(line);
68+
}
69+
70+
// Add all parts to result with proper formatting
71+
foreach (var part in parts)
72+
{
73+
index -= 1;
74+
result += FormatLine(part, index);
75+
}
76+
}
77+
78+
return result;
79+
}
80+
81+
private static int CalculateTextLength(string line)
82+
{
83+
var len = 0;
84+
var isTag = false;
85+
86+
foreach (var c in line)
87+
{
88+
switch (c)
89+
{
90+
case '<':
91+
isTag = true;
92+
continue;
93+
case '>':
94+
isTag = false;
95+
continue;
96+
}
97+
98+
if (!isTag) len++;
99+
}
100+
101+
return len;
102+
}
103+
104+
private static void SplitLongLine(string line, List<string> parts)
105+
{
106+
int? lastUnusedSpaceIndex = null;
107+
108+
for (var i = 0; i < line.Length; i++)
109+
{
110+
if (!char.IsWhiteSpace(line[i])) continue;
111+
112+
if (i <= 50)
113+
{
114+
lastUnusedSpaceIndex = i;
115+
continue;
116+
}
117+
118+
var lastAvailableSpaceIndex = lastUnusedSpaceIndex ?? i;
119+
var leftPart = line[..lastAvailableSpaceIndex].Trim();
120+
parts.Add(leftPart);
121+
122+
var rightPart = line[(lastAvailableSpaceIndex + 1)..].Trim();
123+
if (CalculateTextLength(rightPart) > 80)
124+
{
125+
SplitLongLine(rightPart, parts);
126+
}
127+
else
128+
{
129+
parts.Add(rightPart);
130+
}
131+
132+
return;
133+
}
134+
135+
parts.Add(line);
136+
}
137+
138+
private static string FormatLine(string text, int index)
139+
{
140+
const string waitReplacement = "<cspace=0em><size=0>.........................</size></cspace>";
141+
return $"<voffset={index}em>{text.Replace("[wait]", waitReplacement)}</voffset>\\n";
142+
}
143+
144+
public static string FormatToCassieCentralScreenSubtitles(string text, bool addWaits)
145+
{
146+
return
147+
@"<line-height=2800>\n</line-height></size><align=center><size=30><line-height=0%>\n"
148+
+ FormatToRawCassieSubtitles(text, addWaits);
149+
}
150+
151+
public static string FormatToCassieSpeechSubtitles(string text, bool addWaits)
152+
{
153+
return @"<line-height=3500>\n</line-height></size><align=left><size=25><line-height=0%>\n"
154+
+ FormatToRawCassieSubtitles(text, addWaits);
155+
}
156+
}
157+

0 commit comments

Comments
 (0)