Make PreviewButton async.

This commit is contained in:
DrabWeb 2017-09-15 13:47:03 -03:00
parent 022e39b843
commit 1bac1e2c0e

View File

@ -14,17 +14,38 @@ using osu.Framework.Input;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Graphics.Containers; using osu.Game.Graphics.Containers;
using osu.Game.Graphics.UserInterface;
namespace osu.Game.Overlays.OnlineBeatmapSet namespace osu.Game.Overlays.OnlineBeatmapSet
{ {
public class PreviewButton : OsuClickableContainer public class PreviewButton : OsuClickableContainer
{ {
private const float transition_duration = 500;
private readonly Container audioWrapper;
private readonly Box bg, progress; private readonly Box bg, progress;
private readonly SpriteIcon icon; private readonly SpriteIcon icon;
private readonly LoadingAnimation loadingAnimation;
private AudioManager audio;
private Track preview; private Track preview;
private bool loading
{
set
{
if (value)
{
loadingAnimation.Show();
icon.FadeOut(transition_duration * 5, Easing.OutQuint);
}
else
{
loadingAnimation.Hide();
icon.FadeIn(transition_duration, Easing.OutQuint);
}
}
}
private BeatmapSetInfo beatmapSet; private BeatmapSetInfo beatmapSet;
public BeatmapSetInfo BeatmapSet public BeatmapSetInfo BeatmapSet
{ {
@ -36,7 +57,6 @@ namespace osu.Game.Overlays.OnlineBeatmapSet
Playing = false; Playing = false;
preview = null; preview = null;
loadPreview();
} }
} }
@ -49,22 +69,28 @@ namespace osu.Game.Overlays.OnlineBeatmapSet
if (value == playing) return; if (value == playing) return;
playing = value; playing = value;
if (progress == null) return; if (preview == null)
if (Playing)
{ {
icon.Icon = FontAwesome.fa_stop; loading = true;
progress.FadeIn(100); audioWrapper.Child = new AsyncLoadWrapper(new AudioLoadWrapper(BeatmapSet)
{
OnLoadComplete = d =>
{
loading = false;
loadPreview(); if (d is AudioLoadWrapper)
preview.Start(); {
} preview = (d as AudioLoadWrapper).Preview;
else Playing = Playing;
{ updatePlayingState();
icon.Icon = FontAwesome.fa_play; }
progress.FadeOut(100); },
preview.Stop(); });
return;
} }
updatePlayingState();
} }
} }
@ -74,6 +100,7 @@ namespace osu.Game.Overlays.OnlineBeatmapSet
Children = new Drawable[] Children = new Drawable[]
{ {
audioWrapper = new Container(),
bg = new Box bg = new Box
{ {
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
@ -100,15 +127,19 @@ namespace osu.Game.Overlays.OnlineBeatmapSet
Size = new Vector2(18), Size = new Vector2(18),
Shadow = false, Shadow = false,
}, },
loadingAnimation = new LoadingAnimation
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
}; };
Action = () => Playing = !Playing; Action = () => Playing = !Playing;
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(OsuColour colours, AudioManager audio) private void load(OsuColour colours)
{ {
this.audio = audio;
progress.Colour = colours.Yellow; progress.Colour = colours.Yellow;
} }
@ -116,10 +147,14 @@ namespace osu.Game.Overlays.OnlineBeatmapSet
{ {
base.Update(); base.Update();
if (Playing) if (Playing && preview != null)
{ {
progress.Width = (float)(preview.CurrentTime / preview.Length); progress.Width = (float)(preview.CurrentTime / preview.Length);
if (preview.HasCompleted) Playing = false; if (preview.HasCompleted)
{
Playing = false;
preview = null;
}
} }
} }
@ -141,16 +176,45 @@ namespace osu.Game.Overlays.OnlineBeatmapSet
base.OnHoverLost(state); base.OnHoverLost(state);
} }
private void loadPreview() private void updatePlayingState()
{ {
if (preview == null || preview.HasCompleted && BeatmapSet != null) if (preview == null) return;
if (Playing)
{ {
preview = audio.Track.Get(BeatmapSet.OnlineInfo.Preview); icon.Icon = FontAwesome.fa_stop;
preview.Volume.Value = 0.5; progress.FadeIn(100);
preview.Seek(0);
preview.Start();
} }
else else
{ {
preview.Seek(0); icon.Icon = FontAwesome.fa_play;
progress.FadeOut(100);
preview.Stop();
}
}
private class AudioLoadWrapper : Drawable
{
private readonly string preview;
public Track Preview;
public AudioLoadWrapper(BeatmapSetInfo set)
{
preview = set.OnlineInfo.Preview;
}
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
if (!string.IsNullOrEmpty(preview))
{
Preview = audio.Track.Get(preview);
Preview.Volume.Value = 0.5;
}
} }
} }
} }