2021-10-24 00:26:28 +08:00
|
|
|
// 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.
|
|
|
|
|
2021-10-24 01:27:07 +08:00
|
|
|
#nullable enable
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
2021-10-24 00:26:28 +08:00
|
|
|
using osu.Framework.Allocation;
|
2021-10-24 00:41:31 +08:00
|
|
|
using osu.Framework.Bindables;
|
2021-10-24 01:27:07 +08:00
|
|
|
using osu.Framework.Extensions.IEnumerableExtensions;
|
2021-10-24 00:26:28 +08:00
|
|
|
using osu.Framework.Graphics;
|
2021-10-24 01:27:07 +08:00
|
|
|
using osu.Framework.Graphics.Containers;
|
2021-10-24 00:26:28 +08:00
|
|
|
using osu.Framework.Graphics.Sprites;
|
2021-10-24 01:27:07 +08:00
|
|
|
using osu.Game.Audio;
|
2021-10-24 00:26:28 +08:00
|
|
|
using osu.Game.Graphics;
|
|
|
|
using osu.Game.Graphics.Containers;
|
2021-10-24 01:27:07 +08:00
|
|
|
using osu.Game.Graphics.UserInterface;
|
2021-10-24 00:26:28 +08:00
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
namespace osu.Game.Beatmaps.Drawables.Cards.Buttons
|
|
|
|
{
|
|
|
|
public class PlayButton : OsuHoverContainer
|
|
|
|
{
|
2021-10-24 01:27:07 +08:00
|
|
|
public IBindable<double> Progress => progress;
|
|
|
|
private readonly BindableDouble progress = new BindableDouble();
|
|
|
|
|
2021-10-24 00:41:31 +08:00
|
|
|
public BindableBool Playing { get; } = new BindableBool();
|
|
|
|
|
2021-10-24 01:27:07 +08:00
|
|
|
private readonly IBeatmapSetInfo beatmapSetInfo;
|
|
|
|
|
|
|
|
protected override IEnumerable<Drawable> EffectTargets => icon.Yield();
|
2021-10-24 00:41:31 +08:00
|
|
|
|
|
|
|
private readonly SpriteIcon icon;
|
2021-10-24 01:27:07 +08:00
|
|
|
private readonly LoadingSpinner loadingSpinner;
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private PreviewTrackManager previewTrackManager { get; set; } = null!;
|
2021-10-24 00:41:31 +08:00
|
|
|
|
2021-10-24 01:27:07 +08:00
|
|
|
private PreviewTrack? previewTrack;
|
|
|
|
|
|
|
|
public PlayButton(IBeatmapSetInfo beatmapSetInfo)
|
2021-10-24 00:26:28 +08:00
|
|
|
{
|
2021-10-24 00:41:31 +08:00
|
|
|
this.beatmapSetInfo = beatmapSetInfo;
|
|
|
|
|
2021-10-24 00:26:28 +08:00
|
|
|
Anchor = Origin = Anchor.Centre;
|
|
|
|
|
2021-10-24 00:41:31 +08:00
|
|
|
Children = new Drawable[]
|
2021-10-24 00:26:28 +08:00
|
|
|
{
|
2021-10-24 00:41:31 +08:00
|
|
|
icon = new SpriteIcon
|
|
|
|
{
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
Icon = FontAwesome.Solid.Play,
|
|
|
|
Size = new Vector2(14)
|
|
|
|
},
|
2021-10-24 01:27:07 +08:00
|
|
|
loadingSpinner = new LoadingSpinner
|
|
|
|
{
|
|
|
|
Size = new Vector2(14)
|
|
|
|
}
|
2021-10-24 00:26:28 +08:00
|
|
|
};
|
2021-10-24 00:41:31 +08:00
|
|
|
|
|
|
|
Action = () => Playing.Toggle();
|
2021-10-24 00:26:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(OsuColour colours)
|
|
|
|
{
|
|
|
|
HoverColour = colours.Yellow;
|
|
|
|
}
|
2021-10-24 00:41:31 +08:00
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
2021-10-24 01:27:07 +08:00
|
|
|
Playing.BindValueChanged(updateState, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
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);
|
2021-11-26 21:34:05 +08:00
|
|
|
|
2021-10-24 01:27:07 +08:00
|
|
|
LoadComponentAsync(previewTrack = previewTrackManager.Get(beatmapSetInfo), onPreviewLoaded);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tryStartPreview();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void stopPreview()
|
|
|
|
{
|
|
|
|
toggleLoading(false);
|
|
|
|
Playing.Value = false;
|
|
|
|
previewTrack?.Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onPreviewLoaded(PreviewTrack loadedPreview)
|
|
|
|
{
|
2021-11-26 21:34:05 +08:00
|
|
|
// Make sure that we schedule to after the next audio frame to fix crashes in single-threaded execution.
|
|
|
|
// See: https://github.com/ppy/osu-framework/issues/4692
|
|
|
|
Schedule(() =>
|
|
|
|
{
|
|
|
|
// another async load might have completed before this one.
|
|
|
|
// if so, do not make any changes.
|
|
|
|
if (loadedPreview != previewTrack)
|
|
|
|
return;
|
2021-10-24 01:27:07 +08:00
|
|
|
|
2021-11-26 21:34:05 +08:00
|
|
|
AddInternal(loadedPreview);
|
|
|
|
toggleLoading(false);
|
2021-10-24 01:27:07 +08:00
|
|
|
|
2021-11-26 21:34:05 +08:00
|
|
|
loadedPreview.Stopped += () => Schedule(() => Playing.Value = false);
|
2021-10-24 01:27:07 +08:00
|
|
|
|
2021-11-26 21:34:05 +08:00
|
|
|
if (Playing.Value)
|
|
|
|
tryStartPreview();
|
|
|
|
});
|
2021-10-24 01:27:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void tryStartPreview()
|
|
|
|
{
|
|
|
|
if (previewTrack?.Start() == false)
|
|
|
|
Playing.Value = false;
|
2021-10-24 00:41:31 +08:00
|
|
|
}
|
|
|
|
|
2021-10-24 01:27:07 +08:00
|
|
|
private void toggleLoading(bool loading)
|
2021-10-24 00:41:31 +08:00
|
|
|
{
|
2021-10-24 01:27:07 +08:00
|
|
|
Enabled.Value = !loading;
|
2021-12-21 15:26:19 +08:00
|
|
|
icon.FadeTo(loading ? 0 : 1, BeatmapCard.TRANSITION_DURATION, Easing.OutQuint);
|
2021-10-24 01:27:07 +08:00
|
|
|
loadingSpinner.State.Value = loading ? Visibility.Visible : Visibility.Hidden;
|
2021-10-24 00:41:31 +08:00
|
|
|
}
|
2021-10-24 00:26:28 +08:00
|
|
|
}
|
|
|
|
}
|