2019-01-24 16:43:03 +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.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
|
#nullable disable
|
|
|
|
|
|
2017-09-18 04:39:34 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
2019-02-21 18:04:31 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2017-09-18 04:39:34 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2019-03-27 18:29:27 +08:00
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2018-10-02 11:02:47 +08:00
|
|
|
|
using osu.Framework.Input.Events;
|
2018-05-09 19:52:46 +08:00
|
|
|
|
using osu.Game.Audio;
|
2017-09-18 04:39:34 +08:00
|
|
|
|
using osu.Game.Graphics;
|
2017-09-30 05:08:30 +08:00
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
2021-10-29 16:58:46 +08:00
|
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
2018-11-20 15:51:59 +08:00
|
|
|
|
using osuTK;
|
|
|
|
|
using osuTK.Graphics;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-11-27 22:04:05 +08:00
|
|
|
|
namespace osu.Game.Overlays.BeatmapSet.Buttons
|
2017-09-18 04:39:34 +08:00
|
|
|
|
{
|
|
|
|
|
public class PlayButton : Container
|
|
|
|
|
{
|
2021-01-11 00:47:04 +08:00
|
|
|
|
public IBindable<bool> Playing => playing;
|
|
|
|
|
|
|
|
|
|
private readonly BindableBool playing = new BindableBool();
|
|
|
|
|
|
2018-05-25 05:37:53 +08:00
|
|
|
|
public PreviewTrack Preview { get; private set; }
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-10-29 16:58:46 +08:00
|
|
|
|
private APIBeatmapSet beatmapSet;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-10-29 16:58:46 +08:00
|
|
|
|
public APIBeatmapSet BeatmapSet
|
2017-10-07 03:00:23 +08:00
|
|
|
|
{
|
2019-02-28 12:58:19 +08:00
|
|
|
|
get => beatmapSet;
|
2017-10-07 03:00:23 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
2017-10-13 13:06:34 +08:00
|
|
|
|
if (value == beatmapSet) return;
|
2019-02-28 12:31:40 +08:00
|
|
|
|
|
2017-10-13 13:06:34 +08:00
|
|
|
|
beatmapSet = value;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-06-22 11:12:59 +08:00
|
|
|
|
Preview?.Stop();
|
|
|
|
|
Preview?.Expire();
|
|
|
|
|
Preview = null;
|
2018-06-02 02:06:37 +08:00
|
|
|
|
|
2021-01-11 00:47:04 +08:00
|
|
|
|
playing.Value = false;
|
2018-06-02 02:06:37 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-18 04:39:34 +08:00
|
|
|
|
private Color4 hoverColour;
|
|
|
|
|
private readonly SpriteIcon icon;
|
2020-02-21 14:33:31 +08:00
|
|
|
|
private readonly LoadingSpinner loadingSpinner;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-09-30 05:08:30 +08:00
|
|
|
|
private const float transition_duration = 500;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-10-13 13:06:34 +08:00
|
|
|
|
private bool loading
|
2017-09-30 05:08:30 +08:00
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value)
|
2018-06-22 13:27:36 +08:00
|
|
|
|
{
|
|
|
|
|
icon.FadeTo(0.5f, transition_duration, Easing.OutQuint);
|
2020-02-21 14:33:31 +08:00
|
|
|
|
loadingSpinner.Show();
|
2018-06-22 13:27:36 +08:00
|
|
|
|
}
|
2017-09-30 05:08:30 +08:00
|
|
|
|
else
|
2018-06-22 13:27:36 +08:00
|
|
|
|
{
|
|
|
|
|
icon.FadeTo(1, transition_duration, Easing.OutQuint);
|
2020-02-21 14:33:31 +08:00
|
|
|
|
loadingSpinner.Hide();
|
2018-06-22 13:27:36 +08:00
|
|
|
|
}
|
2017-09-30 05:08:30 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-10-29 16:58:46 +08:00
|
|
|
|
public PlayButton(APIBeatmapSet setInfo = null)
|
2017-09-18 04:39:34 +08:00
|
|
|
|
{
|
2017-10-13 13:06:34 +08:00
|
|
|
|
BeatmapSet = setInfo;
|
2017-09-30 05:08:30 +08:00
|
|
|
|
AddRange(new Drawable[]
|
2017-09-18 04:39:34 +08:00
|
|
|
|
{
|
2017-09-30 05:08:30 +08:00
|
|
|
|
icon = new SpriteIcon
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
FillMode = FillMode.Fit,
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2019-04-02 18:55:24 +08:00
|
|
|
|
Icon = FontAwesome.Solid.Play,
|
2017-09-30 05:08:30 +08:00
|
|
|
|
},
|
2020-02-21 14:33:31 +08:00
|
|
|
|
loadingSpinner = new LoadingSpinner
|
2018-06-22 13:27:36 +08:00
|
|
|
|
{
|
|
|
|
|
Size = new Vector2(15),
|
|
|
|
|
},
|
2017-09-18 04:39:34 +08:00
|
|
|
|
});
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-01-11 00:47:04 +08:00
|
|
|
|
playing.ValueChanged += playingStateChanged;
|
2017-09-18 04:39:34 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2020-02-14 21:14:00 +08:00
|
|
|
|
[Resolved]
|
|
|
|
|
private PreviewTrackManager previewTrackManager { get; set; }
|
2018-06-21 15:19:07 +08:00
|
|
|
|
|
2017-09-18 04:39:34 +08:00
|
|
|
|
[BackgroundDependencyLoader]
|
2020-02-14 21:14:00 +08:00
|
|
|
|
private void load(OsuColour colour)
|
2017-09-18 04:39:34 +08:00
|
|
|
|
{
|
|
|
|
|
hoverColour = colour.Yellow;
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-10-02 11:02:47 +08:00
|
|
|
|
protected override bool OnClick(ClickEvent e)
|
2017-09-18 04:39:34 +08:00
|
|
|
|
{
|
2021-01-11 00:47:04 +08:00
|
|
|
|
playing.Toggle();
|
2017-09-18 04:39:34 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-10-02 11:02:47 +08:00
|
|
|
|
protected override bool OnHover(HoverEvent e)
|
2017-09-18 04:39:34 +08:00
|
|
|
|
{
|
|
|
|
|
icon.FadeColour(hoverColour, 120, Easing.InOutQuint);
|
2018-10-02 11:02:47 +08:00
|
|
|
|
return base.OnHover(e);
|
2017-09-18 04:39:34 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-10-02 11:02:47 +08:00
|
|
|
|
protected override void OnHoverLost(HoverLostEvent e)
|
2017-09-18 04:39:34 +08:00
|
|
|
|
{
|
2021-01-11 00:47:04 +08:00
|
|
|
|
if (!playing.Value)
|
2017-09-18 04:39:34 +08:00
|
|
|
|
icon.FadeColour(Color4.White, 120, Easing.InOutQuint);
|
2018-10-02 11:02:47 +08:00
|
|
|
|
base.OnHoverLost(e);
|
2017-09-18 04:39:34 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-02-21 17:56:34 +08:00
|
|
|
|
private void playingStateChanged(ValueChangedEvent<bool> e)
|
2017-10-07 03:00:23 +08:00
|
|
|
|
{
|
2019-04-02 18:55:24 +08:00
|
|
|
|
icon.Icon = e.NewValue ? FontAwesome.Solid.Stop : FontAwesome.Solid.Play;
|
2019-02-21 17:56:34 +08:00
|
|
|
|
icon.FadeColour(e.NewValue || IsHovered ? hoverColour : Color4.White, 120, Easing.InOutQuint);
|
2018-04-18 15:04:02 +08:00
|
|
|
|
|
2019-02-21 17:56:34 +08:00
|
|
|
|
if (e.NewValue)
|
2017-10-07 03:00:23 +08:00
|
|
|
|
{
|
2018-06-22 11:12:59 +08:00
|
|
|
|
if (BeatmapSet == null)
|
|
|
|
|
{
|
2021-01-11 00:47:04 +08:00
|
|
|
|
playing.Value = false;
|
2018-06-22 11:12:59 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Preview != null)
|
|
|
|
|
{
|
2019-05-23 11:07:22 +08:00
|
|
|
|
attemptStart();
|
2018-06-22 11:12:59 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loading = true;
|
|
|
|
|
|
|
|
|
|
LoadComponentAsync(Preview = previewTrackManager.Get(beatmapSet), preview =>
|
|
|
|
|
{
|
2021-08-16 17:46:44 +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(() =>
|
|
|
|
|
{
|
|
|
|
|
// beatmapset may have changed.
|
|
|
|
|
if (Preview != preview)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
AddInternal(preview);
|
|
|
|
|
loading = false;
|
|
|
|
|
// make sure that the update of value of Playing (and the ensuing value change callbacks)
|
|
|
|
|
// are marshaled back to the update thread.
|
|
|
|
|
preview.Stopped += () => Schedule(() => playing.Value = false);
|
|
|
|
|
|
|
|
|
|
// user may have changed their mind.
|
|
|
|
|
if (playing.Value)
|
|
|
|
|
attemptStart();
|
|
|
|
|
});
|
2018-06-22 11:12:59 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Preview?.Stop();
|
2017-10-14 09:53:39 +08:00
|
|
|
|
loading = false;
|
2017-10-07 03:00:23 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-05-23 11:07:22 +08:00
|
|
|
|
private void attemptStart()
|
|
|
|
|
{
|
|
|
|
|
if (Preview?.Start() != true)
|
2021-01-11 00:47:04 +08:00
|
|
|
|
playing.Value = false;
|
2019-05-23 11:07:22 +08:00
|
|
|
|
}
|
2017-09-18 04:39:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|