Skip to content

Commit

Permalink
misc: some lyric magic
Browse files Browse the repository at this point in the history
misc: 尝试使LyricLineHandler异步加载内容
  • Loading branch information
MATRIX-feather committed Sep 22, 2024
1 parent 893d7ca commit bce3545
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@ public static class StringExtensions
{
public static int ToMilliseconds(this string src)
{
string[] spilt = src.Split(":");
string[] spilt = src.Contains(':')
? src.Split(":")
: src.Split(".", 2);

if (spilt.Length < 2)
{
Logging.Log($"无效的时间: \"{src}\"");
return 0;
}

int.TryParse(spilt[0], out int minutes);
double.TryParse(spilt[1], out double seconds);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#nullable disable

using System.Linq;
using System.Threading;
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.Color4Extensions;
Expand All @@ -19,10 +16,10 @@ namespace osu.Game.Rulesets.IGPlayer.Feature.Player.Plugins.Bundle.CloudMusic.UI
{
public partial class LyricLineHandler : CompositeDrawable
{
private OsuSpriteText currentLine;
private OsuSpriteText currentLineTranslated;
private string currentRawText;
private string currentRawTranslateText;
private OsuSpriteText? currentLine;
private OsuSpriteText? currentLineTranslated;
private string currentRawText = string.Empty;
private string currentRawTranslateText = string.Empty;
private readonly BufferedContainer outlineEffectContainer;

private readonly Container lyricContainer = new Container
Expand All @@ -38,8 +35,9 @@ public partial class LyricLineHandler : CompositeDrawable
private Easing fadeOutEasing => Easing.OutQuint;
private Easing fadeInEasing => Easing.OutQuint;

[CanBeNull]
private CancellationTokenSource cancellationTokenSource;
private CancellationTokenSource? textCancellationTokenSource;

private CancellationTokenSource? translateTextCancellationTokenSource;

public string Text
{
Expand All @@ -49,14 +47,15 @@ public string Text
currentLine?.MoveToY(5, fadeOutDuration.Value, fadeOutEasing)
.FadeOut(fadeOutDuration.Value, fadeOutEasing).Then().Expire();

cancellationTokenSource?.Cancel();
cancellationTokenSource = new CancellationTokenSource();
currentLine = null;

textCancellationTokenSource?.Cancel();

var tokenSource = new CancellationTokenSource();
textCancellationTokenSource = tokenSource;

Schedule(() =>
{
if (cancellationTokenSource.Token.IsCancellationRequested)
return;

LoadComponentAsync(new OsuSpriteText
{
Text = value,
Expand All @@ -66,17 +65,17 @@ public string Text
Origin = configDirection.Value,
Font = OsuFont.GetFont(size: 30, weight: FontWeight.Black),
Margin = getMargin(false)
}, complete =>
}, text =>
{
if (cancellationTokenSource.Token.IsCancellationRequested)
if (tokenSource.Token.IsCancellationRequested)
return;

lyricContainer.Add(complete);
complete.MoveToY(0, fadeInDuration.Value, fadeInEasing)
.FadeIn(fadeInDuration.Value, fadeInEasing);
lyricContainer.Add(text);
text.MoveToY(0, fadeInDuration.Value, fadeInEasing)
.FadeIn(fadeInDuration.Value, fadeInEasing);

currentLine = complete;
}, cancellationTokenSource.Token);
currentLine = text;
}, tokenSource.Token);
});

currentRawText = value;
Expand All @@ -94,18 +93,36 @@ public string TranslatedText
.Then()
.Expire();

lyricContainer.Add(currentLineTranslated = new OsuSpriteText
currentLineTranslated = null;

this.translateTextCancellationTokenSource?.Cancel();

var tokenSource = new CancellationTokenSource();
this.translateTextCancellationTokenSource = tokenSource;

Schedule(() =>
{
Text = value,
Alpha = 0,
Y = -5,
Font = OsuFont.GetFont(size: 30, weight: FontWeight.Black),
Anchor = configDirection.Value,
Origin = configDirection.Value,
Margin = getMargin(true)
LoadComponentAsync(new OsuSpriteText
{
Text = value,
Alpha = 0,
Y = -5,
Font = OsuFont.GetFont(size: 30, weight: FontWeight.Black),
Anchor = configDirection.Value,
Origin = configDirection.Value,
Margin = getMargin(true)
}, text =>
{
if (tokenSource.Token.IsCancellationRequested)
return;

text.MoveToY(0, fadeInDuration.Value, fadeInEasing)
.FadeIn(fadeInDuration.Value, fadeInEasing);

lyricContainer.Add(text);
currentLineTranslated = text;
});
});
currentLineTranslated.MoveToY(0, fadeInDuration.Value, fadeInEasing)
.FadeIn(fadeInDuration.Value, fadeInEasing);

currentRawTranslateText = value;
checkIfEmpty();
Expand Down

0 comments on commit bce3545

Please sign in to comment.