mirror of
https://github.com/osukey/osukey.git
synced 2025-07-03 01:09:57 +09:00
Implement preview track playback
This commit is contained in:
@ -1,25 +1,43 @@
|
||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||||
// See the LICENCE file in the repository root for full licence text.
|
||||
|
||||
#nullable enable
|
||||
|
||||
using System.Collections.Generic;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Bindables;
|
||||
using osu.Framework.Extensions.IEnumerableExtensions;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Framework.Graphics.Sprites;
|
||||
using osu.Game.Audio;
|
||||
using osu.Game.Graphics;
|
||||
using osu.Game.Graphics.Containers;
|
||||
using osu.Game.Graphics.UserInterface;
|
||||
using osuTK;
|
||||
|
||||
namespace osu.Game.Beatmaps.Drawables.Cards.Buttons
|
||||
{
|
||||
public class PlayButton : OsuHoverContainer
|
||||
{
|
||||
public IBindable<double> Progress => progress;
|
||||
private readonly BindableDouble progress = new BindableDouble();
|
||||
|
||||
public BindableBool Playing { get; } = new BindableBool();
|
||||
|
||||
private readonly BeatmapSetInfo beatmapSetInfo;
|
||||
private readonly IBeatmapSetInfo beatmapSetInfo;
|
||||
|
||||
protected override IEnumerable<Drawable> EffectTargets => icon.Yield();
|
||||
|
||||
private readonly SpriteIcon icon;
|
||||
private readonly LoadingSpinner loadingSpinner;
|
||||
|
||||
public PlayButton(BeatmapSetInfo beatmapSetInfo)
|
||||
[Resolved]
|
||||
private PreviewTrackManager previewTrackManager { get; set; } = null!;
|
||||
|
||||
private PreviewTrack? previewTrack;
|
||||
|
||||
public PlayButton(IBeatmapSetInfo beatmapSetInfo)
|
||||
{
|
||||
this.beatmapSetInfo = beatmapSetInfo;
|
||||
|
||||
@ -34,6 +52,10 @@ namespace osu.Game.Beatmaps.Drawables.Cards.Buttons
|
||||
Icon = FontAwesome.Solid.Play,
|
||||
Size = new Vector2(14)
|
||||
},
|
||||
loadingSpinner = new LoadingSpinner
|
||||
{
|
||||
Size = new Vector2(14)
|
||||
}
|
||||
};
|
||||
|
||||
Action = () => Playing.Toggle();
|
||||
@ -49,12 +71,72 @@ namespace osu.Game.Beatmaps.Drawables.Cards.Buttons
|
||||
{
|
||||
base.LoadComplete();
|
||||
|
||||
Playing.BindValueChanged(_ => updateState(), true);
|
||||
Playing.BindValueChanged(updateState, true);
|
||||
}
|
||||
|
||||
private void updateState()
|
||||
protected override void Update()
|
||||
{
|
||||
icon.Icon = Playing.Value ? FontAwesome.Solid.Stop : FontAwesome.Solid.Play;
|
||||
base.Update();
|
||||
|
||||
if (Playing.Value && previewTrack != null && previewTrack.TrackLoaded)
|
||||
progress.Value = previewTrack.CurrentTime / previewTrack.Length;
|
||||
else
|
||||
progress.Value = 0;
|
||||
}
|
||||
|
||||
private void updateState(ValueChangedEvent<bool> playing)
|
||||
{
|
||||
icon.Icon = playing.NewValue ? FontAwesome.Solid.Stop : FontAwesome.Solid.Play;
|
||||
|
||||
if (!playing.NewValue)
|
||||
{
|
||||
stopPreview();
|
||||
return;
|
||||
}
|
||||
|
||||
if (previewTrack == null)
|
||||
{
|
||||
toggleLoading(true);
|
||||
LoadComponentAsync(previewTrack = previewTrackManager.Get(beatmapSetInfo), onPreviewLoaded);
|
||||
}
|
||||
else
|
||||
tryStartPreview();
|
||||
}
|
||||
|
||||
private void stopPreview()
|
||||
{
|
||||
toggleLoading(false);
|
||||
Playing.Value = false;
|
||||
previewTrack?.Stop();
|
||||
}
|
||||
|
||||
private void onPreviewLoaded(PreviewTrack loadedPreview)
|
||||
{
|
||||
// another async load might have completed before this one.
|
||||
// if so, do not make any changes.
|
||||
if (loadedPreview != previewTrack)
|
||||
return;
|
||||
|
||||
AddInternal(loadedPreview);
|
||||
toggleLoading(false);
|
||||
|
||||
loadedPreview.Stopped += () => Schedule(() => Playing.Value = false);
|
||||
|
||||
if (Playing.Value)
|
||||
tryStartPreview();
|
||||
}
|
||||
|
||||
private void tryStartPreview()
|
||||
{
|
||||
if (previewTrack?.Start() == false)
|
||||
Playing.Value = false;
|
||||
}
|
||||
|
||||
private void toggleLoading(bool loading)
|
||||
{
|
||||
Enabled.Value = !loading;
|
||||
icon.FadeTo(loading ? 0 : 1, BeatmapCard.TRANSITION_DURATION, Easing.OutQuint);
|
||||
loadingSpinner.State.Value = loading ? Visibility.Visible : Visibility.Hidden;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user