1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-19 01:42:54 +08:00
osu-lazer/osu.Game/Beatmaps/Drawables/Cards/Buttons/PlayButton.cs

153 lines
4.6 KiB
C#
Raw Normal View History

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
using System.Collections.Generic;
2021-10-24 00:26:28 +08:00
using osu.Framework.Allocation;
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
{
2022-11-24 13:32:20 +08:00
public partial class PlayButton : OsuHoverContainer
2021-10-24 00:26:28 +08:00
{
2021-10-24 01:27:07 +08:00
public IBindable<double> Progress => progress;
private readonly BindableDouble progress = new BindableDouble();
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();
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 01:27:07 +08:00
private PreviewTrack? previewTrack;
public PlayButton(IBeatmapSetInfo beatmapSetInfo)
2021-10-24 00:26:28 +08:00
{
this.beatmapSetInfo = beatmapSetInfo;
2021-10-24 00:26:28 +08:00
Anchor = Origin = Anchor.Centre;
// needed for touch input to work when card is not hovered/expanded
AlwaysPresent = true;
Children = new Drawable[]
2021-10-24 00:26:28 +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
};
Action = () => Playing.Toggle();
2021-10-24 00:26:28 +08:00
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
HoverColour = colours.Yellow;
}
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-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)
{
// 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)
{
loadedPreview.Dispose();
return;
}
2021-10-24 01:27:07 +08:00
AddInternal(loadedPreview);
toggleLoading(false);
2021-10-24 01:27:07 +08:00
loadedPreview.Stopped += () => Schedule(() => Playing.Value = false);
2021-10-24 01:27:07 +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 01:27:07 +08:00
private void toggleLoading(bool loading)
{
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:26:28 +08:00
}
}