diff --git a/osu.Game/Audio/AudioLoadWrapper.cs b/osu.Game/Audio/AudioLoadWrapper.cs deleted file mode 100644 index 67836d1690..0000000000 --- a/osu.Game/Audio/AudioLoadWrapper.cs +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (c) 2007-2017 ppy Pty Ltd . -// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE - -using osu.Framework.Allocation; -using osu.Framework.Audio; -using osu.Framework.Audio.Track; -using osu.Framework.Graphics; - -namespace osu.Game.Audio -{ - public class AudioLoadWrapper : Drawable - { - private readonly string preview; - - public Track Preview; - - public AudioLoadWrapper(string preview) - { - this.preview = preview; - } - - [BackgroundDependencyLoader] - private void load(AudioManager audio) - { - if (!string.IsNullOrEmpty(preview)) - { - Preview = audio.Track.Get(preview); - Preview.Volume.Value = 0.5; - } - } - } -} diff --git a/osu.Game/Overlays/BeatmapSet/PreviewButton.cs b/osu.Game/Overlays/BeatmapSet/PreviewButton.cs index ea49963f67..2fe2edaf1d 100644 --- a/osu.Game/Overlays/BeatmapSet/PreviewButton.cs +++ b/osu.Game/Overlays/BeatmapSet/PreviewButton.cs @@ -27,21 +27,13 @@ namespace osu.Game.Overlays.BeatmapSet private readonly Box bg, progress; private readonly PlayButton playButton; - private Track preview; - private readonly Bindable playing = new Bindable(); + private Track preview => playButton.Preview; + private Bindable playing => playButton.Playing; - private BeatmapSetInfo beatmapSet; public BeatmapSetInfo BeatmapSet { - get { return beatmapSet; } - set - { - if (value == beatmapSet) return; - beatmapSet = value; - - playing.Value = false; - preview = null; - } + get { return playButton.SetInfo; } + set { playButton.SetInfo = value; } } public PreviewButton() @@ -69,7 +61,7 @@ namespace osu.Game.Overlays.BeatmapSet Alpha = 0f, }, }, - playButton = new PlayButton(playing) + playButton = new PlayButton() { Anchor = Anchor.Centre, Origin = Anchor.Centre, @@ -78,7 +70,7 @@ namespace osu.Game.Overlays.BeatmapSet }; Action = () => playing.Value = !playing.Value; - playing.ValueChanged += updatePlayingState; + playing.ValueChanged += newValue => progress.FadeTo(newValue ? 1 : 0, 100); } [BackgroundDependencyLoader] @@ -94,11 +86,6 @@ namespace osu.Game.Overlays.BeatmapSet if (playing.Value && preview != null) { progress.Width = (float)(preview.CurrentTime / preview.Length); - if (preview.HasCompleted) - { - playing.Value = false; - preview = null; - } } } @@ -119,38 +106,5 @@ namespace osu.Game.Overlays.BeatmapSet bg.FadeColour(Color4.Black.Opacity(0.25f), 100); base.OnHoverLost(state); } - - private void updatePlayingState(bool newValue) - { - if (preview == null) - { - playButton.Loading = true; - audioWrapper.Child = new AsyncLoadWrapper(new AudioLoadWrapper(BeatmapSet.OnlineInfo.Preview) - { - OnLoadComplete = d => - { - playButton.Loading = false; - - preview = (d as AudioLoadWrapper)?.Preview; - playing.TriggerChange(); - }, - }); - } - else - { - if (newValue) - { - progress.FadeIn(100); - - preview.Seek(0); - preview.Start(); - } - else - { - progress.FadeOut(100); - preview.Stop(); - } - } - } } } diff --git a/osu.Game/Overlays/Direct/DirectGridPanel.cs b/osu.Game/Overlays/Direct/DirectGridPanel.cs index 10611cd5be..7464ee7fb8 100644 --- a/osu.Game/Overlays/Direct/DirectGridPanel.cs +++ b/osu.Game/Overlays/Direct/DirectGridPanel.cs @@ -192,7 +192,7 @@ namespace osu.Game.Overlays.Direct new Statistic(FontAwesome.fa_heart, SetInfo.OnlineInfo?.FavouriteCount ?? 0), }, }, - playButton = new PlayButton(PreviewPlaying) + playButton = new PlayButton(SetInfo) { Margin = new MarginPadding { Top = 5, Left = 10 }, Size = new Vector2(30), diff --git a/osu.Game/Overlays/Direct/DirectListPanel.cs b/osu.Game/Overlays/Direct/DirectListPanel.cs index 7f5fa3e3f0..5889a1bc12 100644 --- a/osu.Game/Overlays/Direct/DirectListPanel.cs +++ b/osu.Game/Overlays/Direct/DirectListPanel.cs @@ -63,7 +63,7 @@ namespace osu.Game.Overlays.Direct Spacing = new Vector2(10, 0), Children = new Drawable[] { - playButton = new PlayButton(PreviewPlaying) + playButton = new PlayButton(SetInfo) { Origin = Anchor.CentreLeft, Anchor = Anchor.CentreLeft, diff --git a/osu.Game/Overlays/Direct/DirectPanel.cs b/osu.Game/Overlays/Direct/DirectPanel.cs index 2195fc9ce7..eaabb0b539 100644 --- a/osu.Game/Overlays/Direct/DirectPanel.cs +++ b/osu.Game/Overlays/Direct/DirectPanel.cs @@ -41,10 +41,9 @@ namespace osu.Game.Overlays.Direct private BeatmapManager beatmaps; private NotificationOverlay notifications; private BeatmapSetOverlay beatmapSetOverlay; - private Container audioWrapper; - protected Track Preview; - public readonly Bindable PreviewPlaying = new Bindable(); + public Track Preview => PlayButton.Preview; + public Bindable PreviewPlaying => PlayButton.Playing; protected abstract PlayButton PlayButton { get; } protected abstract Box PreviewBar { get; } @@ -87,7 +86,6 @@ namespace osu.Game.Overlays.Direct EdgeEffect = edgeEffectNormal, Children = new[] { - audioWrapper = new Container(), // temporary blackness until the actual background loads. BlackBackground = new Box { @@ -112,39 +110,6 @@ namespace osu.Game.Overlays.Direct if (downloadRequest != null) attachDownload(downloadRequest); - - PreviewPlaying.ValueChanged += newValue => PlayButton.FadeTo(newValue || IsHovered ? 1 : 0, 120, Easing.InOutQuint); - PreviewPlaying.ValueChanged += newValue => PreviewBar.FadeTo(newValue ? 1 : 0, 120, Easing.InOutQuint); - PreviewPlaying.ValueChanged += setPlaying; - } - - private void setPlaying(bool newValue) - { - if (newValue) - { - if(Preview == null) - { - PlayButton.Loading = true; - audioWrapper.Child = new AsyncLoadWrapper(new AudioLoadWrapper("https://b.ppy.sh/preview/" + SetInfo.OnlineBeatmapSetID + ".mp3") - { - OnLoadComplete = d => - { - PlayButton.Loading = false; - Preview = (d as AudioLoadWrapper)?.Preview; - PreviewPlaying.TriggerChange(); - }, - }); - } - else - { - Preview.Seek(0); - Preview.Start(); - } - } - else - { - Preview?.Stop(); - } } protected override void Update() @@ -154,11 +119,6 @@ namespace osu.Game.Overlays.Direct if (PreviewPlaying && Preview != null) { PreviewBar.Width = (float)(Preview.CurrentTime / Preview.Length); - if (Preview.HasCompleted) - { - PreviewPlaying.Value = false; - Preview = null; - } } } @@ -184,6 +144,7 @@ namespace osu.Game.Overlays.Direct protected override bool OnClick(InputState state) { ShowInformation(); + PreviewPlaying.Value = false; return true; } @@ -244,6 +205,9 @@ namespace osu.Game.Overlays.Direct { base.LoadComplete(); this.FadeInFromZero(200, Easing.Out); + + PreviewPlaying.ValueChanged += newValue => PlayButton.FadeTo(newValue || IsHovered ? 1 : 0, 120, Easing.InOutQuint); + PreviewPlaying.ValueChanged += newValue => PreviewBar.FadeTo(newValue ? 1 : 0, 120, Easing.InOutQuint); } protected List GetDifficultyIcons() diff --git a/osu.Game/Overlays/Direct/PlayButton.cs b/osu.Game/Overlays/Direct/PlayButton.cs index 0405049c6a..dea6b1568d 100644 --- a/osu.Game/Overlays/Direct/PlayButton.cs +++ b/osu.Game/Overlays/Direct/PlayButton.cs @@ -3,10 +3,14 @@ using OpenTK.Graphics; using osu.Framework.Allocation; +using osu.Framework.Audio; +using osu.Framework.Audio.Track; using osu.Framework.Configuration; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Input; +using osu.Game.Audio; +using osu.Game.Beatmaps; using osu.Game.Graphics; using osu.Game.Graphics.UserInterface; @@ -14,11 +18,28 @@ namespace osu.Game.Overlays.Direct { public class PlayButton : Container { - private readonly Bindable playing; + public readonly Bindable Playing = new Bindable(); + public Track Preview { get; private set; } + + private BeatmapSetInfo setInfo; + public BeatmapSetInfo SetInfo + { + get { return setInfo; } + set + { + if (value == setInfo) return; + setInfo = value; + + Playing.Value = false; + Preview = null; + } + } private Color4 hoverColour; private readonly SpriteIcon icon; private readonly LoadingAnimation loadingAnimation; + private Container audioWrapper; + private const float transition_duration = 500; private bool loading; @@ -41,11 +62,12 @@ namespace osu.Game.Overlays.Direct } } - public PlayButton(Bindable playing) + public PlayButton(BeatmapSetInfo setInfo = null) { - this.playing = playing; + this.SetInfo = setInfo; AddRange(new Drawable[] { + audioWrapper = new Container(), icon = new SpriteIcon { Anchor = Anchor.Centre, @@ -57,9 +79,10 @@ namespace osu.Game.Overlays.Direct loadingAnimation = new LoadingAnimation(), }); - playing.ValueChanged += newValue => icon.Icon = newValue ? FontAwesome.fa_pause : FontAwesome.fa_play; + Playing.ValueChanged += newValue => icon.Icon = newValue ? FontAwesome.fa_pause : FontAwesome.fa_play; + Playing.ValueChanged += newValue => icon.FadeColour(newValue || IsHovered ? hoverColour : Color4.White, 120, Easing.InOutQuint); - playing.ValueChanged += newValue => icon.FadeColour(newValue || IsHovered ? hoverColour : Color4.White, 120, Easing.InOutQuint); + Playing.ValueChanged += updatePreviewTrack; } [BackgroundDependencyLoader] @@ -70,7 +93,7 @@ namespace osu.Game.Overlays.Direct protected override bool OnClick(InputState state) { - playing.Value = !playing.Value; + Playing.Value = !Playing.Value; return true; } @@ -82,9 +105,71 @@ namespace osu.Game.Overlays.Direct protected override void OnHoverLost(InputState state) { - if(!playing.Value) + if(!Playing.Value) icon.FadeColour(Color4.White, 120, Easing.InOutQuint); base.OnHoverLost(state); } + + protected override void Update() + { + base.Update(); + + if(Preview?.HasCompleted ?? false) + { + Playing.Value = false; + Preview = null; + } + } + + private void updatePreviewTrack(bool newValue) + { + if (newValue) + { + if (Preview == null) + { + Loading = true; + audioWrapper.Child = new AsyncLoadWrapper(new AudioLoadWrapper("https://b.ppy.sh/preview/" + SetInfo.OnlineBeatmapSetID + ".mp3") + { + OnLoadComplete = d => + { + Loading = false; + Preview = (d as AudioLoadWrapper)?.Preview; + Playing.TriggerChange(); + }, + }); + } + else + { + Preview.Seek(0); + Preview.Start(); + } + } + else + { + Preview?.Stop(); + } + } + + private class AudioLoadWrapper : Drawable + { + private readonly string preview; + + public Track Preview; + + public AudioLoadWrapper(string preview) + { + this.preview = preview; + } + + [BackgroundDependencyLoader] + private void load(AudioManager audio) + { + if (!string.IsNullOrEmpty(preview)) + { + Preview = audio.Track.Get(preview); + Preview.Volume.Value = 0.5; + } + } + } } } diff --git a/osu.Game/Overlays/DirectOverlay.cs b/osu.Game/Overlays/DirectOverlay.cs index 9d79e87b1d..71dc558b26 100644 --- a/osu.Game/Overlays/DirectOverlay.cs +++ b/osu.Game/Overlays/DirectOverlay.cs @@ -230,21 +230,21 @@ namespace osu.Game.Overlays }) }; - foreach (DirectPanel panel in newPanels.Children) - panel.PreviewPlaying.ValueChanged += newValue => - { - if (newValue) - { - if (playing != null && playing != panel) - playing.PreviewPlaying.Value = false; - playing = panel; - } - }; - LoadComponentAsync(newPanels, p => { if (panels != null) ScrollFlow.Remove(panels); ScrollFlow.Add(panels = newPanels); + + foreach (DirectPanel panel in p.Children) + panel.PreviewPlaying.ValueChanged += newValue => + { + if (newValue) + { + if (playing != null && playing != panel) + playing.PreviewPlaying.Value = false; + playing = panel; + } + }; }); } diff --git a/osu.Game/osu.Game.csproj b/osu.Game/osu.Game.csproj index 0b72e22d25..e374b7c274 100644 --- a/osu.Game/osu.Game.csproj +++ b/osu.Game/osu.Game.csproj @@ -238,7 +238,6 @@ -