From 1bac1e2c0e48d5678a92ce6c2152924a01b5b4ce Mon Sep 17 00:00:00 2001 From: DrabWeb Date: Fri, 15 Sep 2017 13:47:03 -0300 Subject: [PATCH] Make PreviewButton async. --- .../OnlineBeatmapSet/PreviewButton.cs | 112 ++++++++++++++---- 1 file changed, 88 insertions(+), 24 deletions(-) diff --git a/osu.Game/Overlays/OnlineBeatmapSet/PreviewButton.cs b/osu.Game/Overlays/OnlineBeatmapSet/PreviewButton.cs index 1dddf4a4a3..b12ec9c7ae 100644 --- a/osu.Game/Overlays/OnlineBeatmapSet/PreviewButton.cs +++ b/osu.Game/Overlays/OnlineBeatmapSet/PreviewButton.cs @@ -14,17 +14,38 @@ using osu.Framework.Input; using osu.Game.Beatmaps; using osu.Game.Graphics; using osu.Game.Graphics.Containers; +using osu.Game.Graphics.UserInterface; namespace osu.Game.Overlays.OnlineBeatmapSet { public class PreviewButton : OsuClickableContainer { + private const float transition_duration = 500; + + private readonly Container audioWrapper; private readonly Box bg, progress; private readonly SpriteIcon icon; + private readonly LoadingAnimation loadingAnimation; - private AudioManager audio; 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; public BeatmapSetInfo BeatmapSet { @@ -36,7 +57,6 @@ namespace osu.Game.Overlays.OnlineBeatmapSet Playing = false; preview = null; - loadPreview(); } } @@ -49,22 +69,28 @@ namespace osu.Game.Overlays.OnlineBeatmapSet if (value == playing) return; playing = value; - if (progress == null) return; - - if (Playing) + if (preview == null) { - icon.Icon = FontAwesome.fa_stop; - progress.FadeIn(100); + loading = true; + audioWrapper.Child = new AsyncLoadWrapper(new AudioLoadWrapper(BeatmapSet) + { + OnLoadComplete = d => + { + loading = false; - loadPreview(); - preview.Start(); - } - else - { - icon.Icon = FontAwesome.fa_play; - progress.FadeOut(100); - preview.Stop(); + if (d is AudioLoadWrapper) + { + preview = (d as AudioLoadWrapper).Preview; + Playing = Playing; + updatePlayingState(); + } + }, + }); + + return; } + + updatePlayingState(); } } @@ -74,6 +100,7 @@ namespace osu.Game.Overlays.OnlineBeatmapSet Children = new Drawable[] { + audioWrapper = new Container(), bg = new Box { RelativeSizeAxes = Axes.Both, @@ -100,15 +127,19 @@ namespace osu.Game.Overlays.OnlineBeatmapSet Size = new Vector2(18), Shadow = false, }, + loadingAnimation = new LoadingAnimation + { + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + }, }; Action = () => Playing = !Playing; } [BackgroundDependencyLoader] - private void load(OsuColour colours, AudioManager audio) + private void load(OsuColour colours) { - this.audio = audio; progress.Colour = colours.Yellow; } @@ -116,10 +147,14 @@ namespace osu.Game.Overlays.OnlineBeatmapSet { base.Update(); - if (Playing) + if (Playing && preview != null) { 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); } - 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); - preview.Volume.Value = 0.5; + icon.Icon = FontAwesome.fa_stop; + progress.FadeIn(100); + + preview.Seek(0); + preview.Start(); } 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; + } } } }