From be1e868a2a6570a8c33dc7643c89280e13fd2f9c Mon Sep 17 00:00:00 2001 From: Jorolf Date: Sun, 17 Sep 2017 22:39:34 +0200 Subject: [PATCH 01/11] add previews to osu!direct --- osu.Game/Overlays/Direct/DirectGridPanel.cs | 34 +++++++ osu.Game/Overlays/Direct/DirectListPanel.cs | 83 +++++++++++++--- osu.Game/Overlays/Direct/DirectPanel.cs | 7 ++ osu.Game/Overlays/Direct/PlayButton.cs | 105 ++++++++++++++++++++ osu.Game/Overlays/DirectOverlay.cs | 18 ++++ osu.Game/osu.Game.csproj | 1 + 6 files changed, 232 insertions(+), 16 deletions(-) create mode 100644 osu.Game/Overlays/Direct/PlayButton.cs diff --git a/osu.Game/Overlays/Direct/DirectGridPanel.cs b/osu.Game/Overlays/Direct/DirectGridPanel.cs index 3a9e75bd38..b638e21c4b 100644 --- a/osu.Game/Overlays/Direct/DirectGridPanel.cs +++ b/osu.Game/Overlays/Direct/DirectGridPanel.cs @@ -13,6 +13,8 @@ using osu.Game.Graphics.Sprites; using osu.Framework.Graphics.Shapes; using osu.Game.Beatmaps; using osu.Framework.Input; +using osu.Framework.Audio; +using osu.Framework.Configuration; namespace osu.Game.Overlays.Direct { @@ -22,6 +24,11 @@ namespace osu.Game.Overlays.Direct private const float vertical_padding = 5; private FillFlowContainer bottomPanel; + private PlayButton playButton; + private Box progressBar; + + protected override PlayButton PlayButton => playButton; + public override Bindable PreviewPlaying { get; } = new Bindable(); public DirectGridPanel(BeatmapSetInfo beatmap) : base(beatmap) { @@ -88,6 +95,15 @@ namespace osu.Game.Overlays.Direct { RelativeSizeAxes = Axes.Both, }, + progressBar = new Box + { + Origin = Anchor.BottomLeft, + RelativeSizeAxes = Axes.X, + BypassAutoSizeAxes = Axes.Both, + Size = new Vector2(0, 3), + Alpha = 0, + Colour = colours.Yellow, + }, new FillFlowContainer { RelativeSizeAxes = Axes.X, @@ -170,7 +186,25 @@ namespace osu.Game.Overlays.Direct new Statistic(FontAwesome.fa_heart, SetInfo.OnlineInfo?.FavouriteCount ?? 0), }, }, + playButton = new PlayButton(PreviewPlaying) + { + Margin = new MarginPadding { Top = 5, Left = 10 }, + Size = new Vector2(30), + Alpha = 0, + TrackURL = "https://b.ppy.sh/preview/" + SetInfo.OnlineBeatmapSetID + ".mp3", + }, }); + + PreviewPlaying.ValueChanged += newValue => playButton.FadeTo(newValue || IsHovered ? 1 : 0, 120, Easing.InOutQuint); + PreviewPlaying.ValueChanged += newValue => progressBar.FadeTo(newValue ? 1 : 0, 120, Easing.InOutQuint); + } + + protected override void Update() + { + base.Update(); + + if (PreviewPlaying && playButton.Track != null) + progressBar.Width = (float)(playButton.Track.CurrentTime / playButton.Track.Length); } protected override bool OnClick(InputState state) diff --git a/osu.Game/Overlays/Direct/DirectListPanel.cs b/osu.Game/Overlays/Direct/DirectListPanel.cs index b3502b0827..7f233f2113 100644 --- a/osu.Game/Overlays/Direct/DirectListPanel.cs +++ b/osu.Game/Overlays/Direct/DirectListPanel.cs @@ -15,6 +15,8 @@ using osu.Framework.Input; using osu.Framework.Graphics.Shapes; using osu.Game.Beatmaps; using osu.Game.Graphics.Containers; +using osu.Framework.Configuration; +using osu.Framework.Audio; namespace osu.Game.Overlays.Direct { @@ -30,8 +32,14 @@ namespace osu.Game.Overlays.Direct Height = height; } + private PlayButton playButton; + private Box progressBar; + + protected override PlayButton PlayButton => playButton; + public override Bindable PreviewPlaying { get; } = new Bindable(); + [BackgroundDependencyLoader] - private void load(LocalisationEngine localisation) + private void load(LocalisationEngine localisation, OsuColour colours) { Content.CornerRadius = 5; @@ -50,29 +58,51 @@ namespace osu.Game.Overlays.Direct { new FillFlowContainer { + Origin = Anchor.CentreLeft, + Anchor = Anchor.CentreLeft, AutoSizeAxes = Axes.Both, - Direction = FillDirection.Vertical, + Direction = FillDirection.Horizontal, + LayoutEasing = Easing.OutQuint, + LayoutDuration = 120, + Spacing = new Vector2(10, 0), Children = new Drawable[] { - new OsuSpriteText + playButton = new PlayButton(PreviewPlaying) { - Current = localisation.GetUnicodePreference(SetInfo.Metadata.TitleUnicode, SetInfo.Metadata.Title), - TextSize = 18, - Font = @"Exo2.0-BoldItalic", - }, - new OsuSpriteText - { - Current = localisation.GetUnicodePreference(SetInfo.Metadata.ArtistUnicode, SetInfo.Metadata.Artist), - Font = @"Exo2.0-BoldItalic", + Origin = Anchor.CentreLeft, + Anchor = Anchor.CentreLeft, + Size = new Vector2(height / 2), + FillMode = FillMode.Fit, + Alpha = 0, + TrackURL = "https://b.ppy.sh/preview/" + SetInfo.OnlineBeatmapSetID + ".mp3", }, new FillFlowContainer { - AutoSizeAxes = Axes.X, - Height = 20, - Margin = new MarginPadding { Top = vertical_padding, Bottom = vertical_padding }, - Children = GetDifficultyIcons(), + AutoSizeAxes = Axes.Both, + Direction = FillDirection.Vertical, + Children = new Drawable[] + { + new OsuSpriteText + { + Current = localisation.GetUnicodePreference(SetInfo.Metadata.TitleUnicode, SetInfo.Metadata.Title), + TextSize = 18, + Font = @"Exo2.0-BoldItalic", + }, + new OsuSpriteText + { + Current = localisation.GetUnicodePreference(SetInfo.Metadata.ArtistUnicode, SetInfo.Metadata.Artist), + Font = @"Exo2.0-BoldItalic", + }, + new FillFlowContainer + { + AutoSizeAxes = Axes.X, + Height = 20, + Margin = new MarginPadding { Top = vertical_padding, Bottom = vertical_padding }, + Children = GetDifficultyIcons(), + }, + }, }, - }, + } }, new FillFlowContainer { @@ -128,7 +158,28 @@ namespace osu.Game.Overlays.Direct }, }, }, + progressBar = new Box + { + Anchor = Anchor.BottomLeft, + Origin = Anchor.BottomLeft, + RelativeSizeAxes = Axes.X, + BypassAutoSizeAxes = Axes.Y, + Size = new Vector2(0, 3), + Alpha = 0, + Colour = colours.Yellow, + }, }); + + PreviewPlaying.ValueChanged += newValue => playButton.FadeTo(newValue || IsHovered ? 1 : 0, 120, Easing.InOutQuint); + PreviewPlaying.ValueChanged += newValue => progressBar.FadeTo(newValue ? 1 : 0, 120, Easing.InOutQuint); + } + + protected override void Update() + { + base.Update(); + + if (PreviewPlaying && playButton.Track != null) + progressBar.Width = (float)(playButton.Track.CurrentTime / playButton.Track.Length); } private class DownloadButton : OsuClickableContainer diff --git a/osu.Game/Overlays/Direct/DirectPanel.cs b/osu.Game/Overlays/Direct/DirectPanel.cs index 6f1f581d0b..24cd8dc54e 100644 --- a/osu.Game/Overlays/Direct/DirectPanel.cs +++ b/osu.Game/Overlays/Direct/DirectPanel.cs @@ -20,6 +20,7 @@ using osu.Game.Online.API; using osu.Framework.Logging; using osu.Game.Overlays.Notifications; using osu.Game.Online.API.Requests; +using osu.Framework.Configuration; namespace osu.Game.Overlays.Direct { @@ -38,6 +39,9 @@ namespace osu.Game.Overlays.Direct private BeatmapManager beatmaps; private NotificationOverlay notifications; + public abstract Bindable PreviewPlaying { get; } + protected abstract PlayButton PlayButton { get; } + protected override Container Content => content; protected DirectPanel(BeatmapSetInfo setInfo) @@ -106,6 +110,7 @@ namespace osu.Game.Overlays.Direct { content.TweenEdgeEffectTo(edgeEffectHovered, hover_transition_time, Easing.OutQuint); content.MoveToY(-4, hover_transition_time, Easing.OutQuint); + PlayButton.FadeIn(120, Easing.InOutQuint); return base.OnHover(state); } @@ -114,6 +119,8 @@ namespace osu.Game.Overlays.Direct { content.TweenEdgeEffectTo(edgeEffectNormal, hover_transition_time, Easing.OutQuint); content.MoveToY(0, hover_transition_time, Easing.OutQuint); + if (!PreviewPlaying) + PlayButton.FadeOut(120, Easing.InOutQuint); base.OnHoverLost(state); } diff --git a/osu.Game/Overlays/Direct/PlayButton.cs b/osu.Game/Overlays/Direct/PlayButton.cs new file mode 100644 index 0000000000..8cfe1a30cb --- /dev/null +++ b/osu.Game/Overlays/Direct/PlayButton.cs @@ -0,0 +1,105 @@ +// Copyright (c) 2007-2017 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +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.Beatmaps; +using osu.Game.Graphics; +using System.Threading.Tasks; + +namespace osu.Game.Overlays.Direct +{ + public class PlayButton : Container + { + public string TrackURL; + + public Bindable Playing; + + public Track Track; + private Bindable gameBeatmap; + private AudioManager audio; + + private Color4 hoverColour; + private readonly SpriteIcon icon; + + public PlayButton(Bindable playing) + { + Playing = playing; + Add(icon = new SpriteIcon() + { + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + FillMode = FillMode.Fit, + RelativeSizeAxes = Axes.Both, + Icon = FontAwesome.fa_play, + }); + + Playing.ValueChanged += newValue => icon.Icon = newValue ? (Track == null ? FontAwesome.fa_spinner : FontAwesome.fa_pause) : FontAwesome.fa_play; + + Playing.ValueChanged += newValue => + { + if (newValue) + Track?.Start(); + else + Track?.Stop(); + }; + + Playing.ValueChanged += newValue => icon.FadeColour(newValue || IsHovered ? hoverColour : Color4.White, 120, Easing.InOutQuint); + } + + [BackgroundDependencyLoader] + private void load(OsuColour colour, OsuGameBase game, AudioManager audio) + { + hoverColour = colour.Yellow; + gameBeatmap = game.Beatmap; + this.audio = audio; + } + + private Task loadTask; + + protected override bool OnClick(InputState state) + { + gameBeatmap.Value.Track.Stop(); + + Playing.Value = !Playing.Value; + + if (loadTask == null) + { + icon.Spin(2000, RotationDirection.Clockwise); + + loadTask = Task.Run(() => + { + Track = audio.Track.Get(TrackURL); + Track.Looping = true; + if (Playing) + Track.Start(); + + icon.ClearTransforms(); + icon.Rotation = 0; + Playing.TriggerChange(); + }); + } + + return true; + } + + protected override bool OnHover(InputState state) + { + icon.FadeColour(hoverColour, 120, Easing.InOutQuint); + return base.OnHover(state); + } + + protected override void OnHoverLost(InputState state) + { + if(!Playing) + icon.FadeColour(Color4.White, 120, Easing.InOutQuint); + base.OnHoverLost(state); + } + } +} diff --git a/osu.Game/Overlays/DirectOverlay.cs b/osu.Game/Overlays/DirectOverlay.cs index 9c07e1087f..9bb2afe127 100644 --- a/osu.Game/Overlays/DirectOverlay.cs +++ b/osu.Game/Overlays/DirectOverlay.cs @@ -32,6 +32,7 @@ namespace osu.Game.Overlays private readonly FillFlowContainer resultCountsContainer; private readonly OsuSpriteText resultCountsText; private FillFlowContainer panels; + private DirectPanel playing; protected override Color4 BackgroundColour => OsuColour.FromHex(@"485e74"); protected override Color4 TrianglesColourLight => OsuColour.FromHex(@"465b71"); @@ -201,6 +202,12 @@ namespace osu.Game.Overlays panels.FadeOut(200); panels.Expire(); panels = null; + + if (playing != null) + { + playing.PreviewPlaying.Value = false; + playing = null; + } } if (BeatmapSets == null) return; @@ -223,6 +230,17 @@ 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); diff --git a/osu.Game/osu.Game.csproj b/osu.Game/osu.Game.csproj index 65ec7d31b3..f75db14f52 100644 --- a/osu.Game/osu.Game.csproj +++ b/osu.Game/osu.Game.csproj @@ -105,6 +105,7 @@ + From 3e8ae93b340093b89724ecc34c95e2e0a9676929 Mon Sep 17 00:00:00 2001 From: Jorolf Date: Sun, 17 Sep 2017 22:54:23 +0200 Subject: [PATCH 02/11] appveyor --- osu.Game/Overlays/Direct/DirectGridPanel.cs | 3 +-- osu.Game/Overlays/Direct/DirectListPanel.cs | 3 +-- osu.Game/Overlays/Direct/PlayButton.cs | 6 +++--- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/osu.Game/Overlays/Direct/DirectGridPanel.cs b/osu.Game/Overlays/Direct/DirectGridPanel.cs index b638e21c4b..24ccd8b7eb 100644 --- a/osu.Game/Overlays/Direct/DirectGridPanel.cs +++ b/osu.Game/Overlays/Direct/DirectGridPanel.cs @@ -13,7 +13,6 @@ using osu.Game.Graphics.Sprites; using osu.Framework.Graphics.Shapes; using osu.Game.Beatmaps; using osu.Framework.Input; -using osu.Framework.Audio; using osu.Framework.Configuration; namespace osu.Game.Overlays.Direct @@ -191,7 +190,7 @@ namespace osu.Game.Overlays.Direct Margin = new MarginPadding { Top = 5, Left = 10 }, Size = new Vector2(30), Alpha = 0, - TrackURL = "https://b.ppy.sh/preview/" + SetInfo.OnlineBeatmapSetID + ".mp3", + TrackUrl = "https://b.ppy.sh/preview/" + SetInfo.OnlineBeatmapSetID + ".mp3", }, }); diff --git a/osu.Game/Overlays/Direct/DirectListPanel.cs b/osu.Game/Overlays/Direct/DirectListPanel.cs index 7f233f2113..7112e927bd 100644 --- a/osu.Game/Overlays/Direct/DirectListPanel.cs +++ b/osu.Game/Overlays/Direct/DirectListPanel.cs @@ -16,7 +16,6 @@ using osu.Framework.Graphics.Shapes; using osu.Game.Beatmaps; using osu.Game.Graphics.Containers; using osu.Framework.Configuration; -using osu.Framework.Audio; namespace osu.Game.Overlays.Direct { @@ -74,7 +73,7 @@ namespace osu.Game.Overlays.Direct Size = new Vector2(height / 2), FillMode = FillMode.Fit, Alpha = 0, - TrackURL = "https://b.ppy.sh/preview/" + SetInfo.OnlineBeatmapSetID + ".mp3", + TrackUrl = "https://b.ppy.sh/preview/" + SetInfo.OnlineBeatmapSetID + ".mp3", }, new FillFlowContainer { diff --git a/osu.Game/Overlays/Direct/PlayButton.cs b/osu.Game/Overlays/Direct/PlayButton.cs index 8cfe1a30cb..b0d011a81c 100644 --- a/osu.Game/Overlays/Direct/PlayButton.cs +++ b/osu.Game/Overlays/Direct/PlayButton.cs @@ -17,7 +17,7 @@ namespace osu.Game.Overlays.Direct { public class PlayButton : Container { - public string TrackURL; + public string TrackUrl; public Bindable Playing; @@ -31,7 +31,7 @@ namespace osu.Game.Overlays.Direct public PlayButton(Bindable playing) { Playing = playing; - Add(icon = new SpriteIcon() + Add(icon = new SpriteIcon { Anchor = Anchor.Centre, Origin = Anchor.Centre, @@ -75,7 +75,7 @@ namespace osu.Game.Overlays.Direct loadTask = Task.Run(() => { - Track = audio.Track.Get(TrackURL); + Track = audio.Track.Get(TrackUrl); Track.Looping = true; if (Playing) Track.Start(); From 1f2a82b7ab3b3dba01bf1a44e3fe5e4b15e31db8 Mon Sep 17 00:00:00 2001 From: Jorolf Date: Tue, 26 Sep 2017 12:21:00 +0200 Subject: [PATCH 03/11] make PreviewPlaying readonly instead of abstract --- osu.Game/Overlays/Direct/DirectGridPanel.cs | 1 - osu.Game/Overlays/Direct/DirectListPanel.cs | 1 - osu.Game/Overlays/Direct/DirectPanel.cs | 2 +- 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/osu.Game/Overlays/Direct/DirectGridPanel.cs b/osu.Game/Overlays/Direct/DirectGridPanel.cs index 24ccd8b7eb..3c51235fad 100644 --- a/osu.Game/Overlays/Direct/DirectGridPanel.cs +++ b/osu.Game/Overlays/Direct/DirectGridPanel.cs @@ -27,7 +27,6 @@ namespace osu.Game.Overlays.Direct private Box progressBar; protected override PlayButton PlayButton => playButton; - public override Bindable PreviewPlaying { get; } = new Bindable(); public DirectGridPanel(BeatmapSetInfo beatmap) : base(beatmap) { diff --git a/osu.Game/Overlays/Direct/DirectListPanel.cs b/osu.Game/Overlays/Direct/DirectListPanel.cs index 7112e927bd..3aa8c8a7c2 100644 --- a/osu.Game/Overlays/Direct/DirectListPanel.cs +++ b/osu.Game/Overlays/Direct/DirectListPanel.cs @@ -35,7 +35,6 @@ namespace osu.Game.Overlays.Direct private Box progressBar; protected override PlayButton PlayButton => playButton; - public override Bindable PreviewPlaying { get; } = new Bindable(); [BackgroundDependencyLoader] private void load(LocalisationEngine localisation, OsuColour colours) diff --git a/osu.Game/Overlays/Direct/DirectPanel.cs b/osu.Game/Overlays/Direct/DirectPanel.cs index 24cd8dc54e..65808c32a1 100644 --- a/osu.Game/Overlays/Direct/DirectPanel.cs +++ b/osu.Game/Overlays/Direct/DirectPanel.cs @@ -39,7 +39,7 @@ namespace osu.Game.Overlays.Direct private BeatmapManager beatmaps; private NotificationOverlay notifications; - public abstract Bindable PreviewPlaying { get; } + public readonly Bindable PreviewPlaying = new Bindable(); protected abstract PlayButton PlayButton { get; } protected override Container Content => content; From 647304c14bd1b247c6ac1df2dce51bcb3339e0ca Mon Sep 17 00:00:00 2001 From: Jorolf Date: Fri, 29 Sep 2017 23:08:30 +0200 Subject: [PATCH 04/11] move logic to DirectPanel and reuse stuff for the PreviewButton --- osu.Game/Audio/AudioLoadWrapper.cs | 35 +++++ osu.Game/Overlays/BeatmapSet/PreviewButton.cs | 128 +++++------------- osu.Game/Overlays/Direct/DirectGridPanel.cs | 13 +- osu.Game/Overlays/Direct/DirectListPanel.cs | 14 +- osu.Game/Overlays/Direct/DirectPanel.cs | 53 ++++++++ osu.Game/Overlays/Direct/PlayButton.cs | 91 ++++++------- osu.Game/osu.Game.csproj | 1 + 7 files changed, 166 insertions(+), 169 deletions(-) create mode 100644 osu.Game/Audio/AudioLoadWrapper.cs diff --git a/osu.Game/Audio/AudioLoadWrapper.cs b/osu.Game/Audio/AudioLoadWrapper.cs new file mode 100644 index 0000000000..8c013cf70f --- /dev/null +++ b/osu.Game/Audio/AudioLoadWrapper.cs @@ -0,0 +1,35 @@ +using osu.Framework.Allocation; +using osu.Framework.Audio; +using osu.Framework.Audio.Track; +using osu.Framework.Graphics; +using osu.Game.Beatmaps; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +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 bdb06106f0..9bb8b9a1a5 100644 --- a/osu.Game/Overlays/BeatmapSet/PreviewButton.cs +++ b/osu.Game/Overlays/BeatmapSet/PreviewButton.cs @@ -15,6 +15,9 @@ using osu.Game.Graphics.Containers; using osu.Game.Graphics.UserInterface; using OpenTK; using OpenTK.Graphics; +using osu.Game.Audio; +using osu.Game.Overlays.Direct; +using osu.Framework.Configuration; namespace osu.Game.Overlays.BeatmapSet { @@ -24,27 +27,10 @@ namespace osu.Game.Overlays.BeatmapSet private readonly Container audioWrapper; private readonly Box bg, progress; - private readonly SpriteIcon icon; - private readonly LoadingAnimation loadingAnimation; + private readonly PlayButton playButton; 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 readonly Bindable playing = new Bindable(); private BeatmapSetInfo beatmapSet; public BeatmapSetInfo BeatmapSet @@ -55,42 +41,11 @@ namespace osu.Game.Overlays.BeatmapSet if (value == beatmapSet) return; beatmapSet = value; - Playing = false; + playing.Value = false; preview = null; } } - private bool playing; - public bool Playing - { - get { return playing; } - set - { - if (value == playing) return; - playing = value; - - if (preview == null) - { - loading = true; - audioWrapper.Child = new AsyncLoadWrapper(new AudioLoadWrapper(BeatmapSet) - { - OnLoadComplete = d => - { - loading = false; - - preview = (d as AudioLoadWrapper)?.Preview; - Playing = Playing; - updatePlayingState(); - }, - }); - - return; - } - - updatePlayingState(); - } - } - public PreviewButton() { Height = 42; @@ -116,22 +71,16 @@ namespace osu.Game.Overlays.BeatmapSet Alpha = 0f, }, }, - icon = new SpriteIcon + playButton = new PlayButton(playing) { Anchor = Anchor.Centre, Origin = Anchor.Centre, - Icon = FontAwesome.fa_play, Size = new Vector2(18), - Shadow = false, - }, - loadingAnimation = new LoadingAnimation - { - Anchor = Anchor.Centre, - Origin = Anchor.Centre, }, }; - Action = () => Playing = !Playing; + Action = () => playing.Value = !playing.Value; + playing.ValueChanged += updatePlayingState; } [BackgroundDependencyLoader] @@ -144,12 +93,12 @@ namespace osu.Game.Overlays.BeatmapSet { base.Update(); - if (Playing && preview != null) + if (playing.Value && preview != null) { progress.Width = (float)(preview.CurrentTime / preview.Length); if (preview.HasCompleted) { - Playing = false; + playing.Value = false; preview = null; } } @@ -157,7 +106,7 @@ namespace osu.Game.Overlays.BeatmapSet protected override void Dispose(bool isDisposing) { - Playing = false; + playing.Value = false; base.Dispose(isDisposing); } @@ -173,44 +122,35 @@ namespace osu.Game.Overlays.BeatmapSet base.OnHoverLost(state); } - private void updatePlayingState() + private void updatePlayingState(bool newValue) { - if (preview == null) return; - - if (Playing) + if (preview == null) { - icon.Icon = FontAwesome.fa_stop; - progress.FadeIn(100); + playButton.Loading = true; + audioWrapper.Child = new AsyncLoadWrapper(new AudioLoadWrapper(BeatmapSet.OnlineInfo.Preview) + { + OnLoadComplete = d => + { + playButton.Loading = false; - preview.Seek(0); - preview.Start(); + preview = (d as AudioLoadWrapper)?.Preview; + playing.TriggerChange(); + }, + }); } else { - 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)) + if (newValue) { - Preview = audio.Track.Get(preview); - Preview.Volume.Value = 0.5; + 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 8a26ae906d..3b8cd20200 100644 --- a/osu.Game/Overlays/Direct/DirectGridPanel.cs +++ b/osu.Game/Overlays/Direct/DirectGridPanel.cs @@ -26,6 +26,7 @@ namespace osu.Game.Overlays.Direct private Box progressBar; protected override PlayButton PlayButton => playButton; + protected override Box PreviewBar => progressBar; public DirectGridPanel(BeatmapSetInfo beatmap) : base(beatmap) { @@ -197,20 +198,8 @@ namespace osu.Game.Overlays.Direct Margin = new MarginPadding { Top = 5, Left = 10 }, Size = new Vector2(30), Alpha = 0, - TrackUrl = "https://b.ppy.sh/preview/" + SetInfo.OnlineBeatmapSetID + ".mp3", }, }); - - PreviewPlaying.ValueChanged += newValue => playButton.FadeTo(newValue || IsHovered ? 1 : 0, 120, Easing.InOutQuint); - PreviewPlaying.ValueChanged += newValue => progressBar.FadeTo(newValue ? 1 : 0, 120, Easing.InOutQuint); - } - - protected override void Update() - { - base.Update(); - - if (PreviewPlaying && playButton.Track != null) - progressBar.Width = (float)(playButton.Track.CurrentTime / playButton.Track.Length); } } } diff --git a/osu.Game/Overlays/Direct/DirectListPanel.cs b/osu.Game/Overlays/Direct/DirectListPanel.cs index a9d7993f7a..6a73c15ee6 100644 --- a/osu.Game/Overlays/Direct/DirectListPanel.cs +++ b/osu.Game/Overlays/Direct/DirectListPanel.cs @@ -14,6 +14,7 @@ using osu.Framework.Localisation; using osu.Framework.Graphics.Shapes; using osu.Game.Beatmaps; using osu.Framework.Configuration; +using System; namespace osu.Game.Overlays.Direct { @@ -33,6 +34,7 @@ namespace osu.Game.Overlays.Direct private Box progressBar; protected override PlayButton PlayButton => playButton; + protected override Box PreviewBar => progressBar; [BackgroundDependencyLoader] private void load(LocalisationEngine localisation, OsuColour colours) @@ -70,7 +72,6 @@ namespace osu.Game.Overlays.Direct Size = new Vector2(height / 2), FillMode = FillMode.Fit, Alpha = 0, - TrackUrl = "https://b.ppy.sh/preview/" + SetInfo.OnlineBeatmapSetID + ".mp3", }, new FillFlowContainer { @@ -165,17 +166,6 @@ namespace osu.Game.Overlays.Direct Colour = colours.Yellow, }, }); - - PreviewPlaying.ValueChanged += newValue => playButton.FadeTo(newValue || IsHovered ? 1 : 0, 120, Easing.InOutQuint); - PreviewPlaying.ValueChanged += newValue => progressBar.FadeTo(newValue ? 1 : 0, 120, Easing.InOutQuint); - } - - protected override void Update() - { - base.Update(); - - if (PreviewPlaying && playButton.Track != null) - progressBar.Width = (float)(playButton.Track.CurrentTime / playButton.Track.Length); } } } diff --git a/osu.Game/Overlays/Direct/DirectPanel.cs b/osu.Game/Overlays/Direct/DirectPanel.cs index c4c20da297..6b1e06fd3a 100644 --- a/osu.Game/Overlays/Direct/DirectPanel.cs +++ b/osu.Game/Overlays/Direct/DirectPanel.cs @@ -21,6 +21,8 @@ using osu.Framework.Logging; using osu.Game.Overlays.Notifications; using osu.Game.Online.API.Requests; using osu.Framework.Configuration; +using osu.Framework.Audio.Track; +using osu.Game.Audio; namespace osu.Game.Overlays.Direct { @@ -39,9 +41,12 @@ 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(); protected abstract PlayButton PlayButton { get; } + protected abstract Box PreviewBar { get; } protected override Container Content => content; @@ -82,6 +87,7 @@ namespace osu.Game.Overlays.Direct EdgeEffect = edgeEffectNormal, Children = new[] { + audioWrapper = new Container(), // temporary blackness until the actual background loads. BlackBackground = new Box { @@ -106,6 +112,53 @@ 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; + Preview.Start(); + }, + }); + } + else + { + Preview.Start(); + } + } + else + { + Preview?.Stop(); + } + } + + protected override void Update() + { + base.Update(); + + if (PreviewPlaying && Preview != null) + { + PreviewBar.Width = (float)(Preview.CurrentTime / Preview.Length); + if (Preview.HasCompleted) + { + PreviewPlaying.Value = false; + Preview = null; + } + } } protected override bool OnHover(InputState state) diff --git a/osu.Game/Overlays/Direct/PlayButton.cs b/osu.Game/Overlays/Direct/PlayButton.cs index b0d011a81c..34b0ace1c5 100644 --- a/osu.Game/Overlays/Direct/PlayButton.cs +++ b/osu.Game/Overlays/Direct/PlayButton.cs @@ -11,81 +11,70 @@ using osu.Framework.Graphics.Containers; using osu.Framework.Input; using osu.Game.Beatmaps; using osu.Game.Graphics; +using osu.Game.Graphics.UserInterface; using System.Threading.Tasks; namespace osu.Game.Overlays.Direct { public class PlayButton : Container { - public string TrackUrl; - - public Bindable Playing; - - public Track Track; - private Bindable gameBeatmap; - private AudioManager audio; + private readonly Bindable playing; private Color4 hoverColour; private readonly SpriteIcon icon; + private readonly LoadingAnimation loadingAnimation; + private const float transition_duration = 500; + + private bool loading; + public bool Loading + { + get { return loading; } + set + { + loading = value; + if (value) + { + loadingAnimation.Show(); + icon.FadeOut(transition_duration * 5, Easing.OutQuint); + } + else + { + loadingAnimation.Hide(); + icon.FadeIn(transition_duration, Easing.OutQuint); + } + } + } public PlayButton(Bindable playing) { - Playing = playing; - Add(icon = new SpriteIcon + this.playing = playing; + AddRange(new Drawable[] { - Anchor = Anchor.Centre, - Origin = Anchor.Centre, - FillMode = FillMode.Fit, - RelativeSizeAxes = Axes.Both, - Icon = FontAwesome.fa_play, + icon = new SpriteIcon + { + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + FillMode = FillMode.Fit, + RelativeSizeAxes = Axes.Both, + Icon = FontAwesome.fa_play, + }, + loadingAnimation = new LoadingAnimation(), }); - Playing.ValueChanged += newValue => icon.Icon = newValue ? (Track == null ? FontAwesome.fa_spinner : FontAwesome.fa_pause) : FontAwesome.fa_play; + playing.ValueChanged += newValue => icon.Icon = newValue ? FontAwesome.fa_pause : FontAwesome.fa_play; - Playing.ValueChanged += newValue => - { - if (newValue) - Track?.Start(); - else - Track?.Stop(); - }; - - 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); } [BackgroundDependencyLoader] - private void load(OsuColour colour, OsuGameBase game, AudioManager audio) + private void load(OsuColour colour) { hoverColour = colour.Yellow; - gameBeatmap = game.Beatmap; - this.audio = audio; } - private Task loadTask; - protected override bool OnClick(InputState state) { - gameBeatmap.Value.Track.Stop(); - - Playing.Value = !Playing.Value; - - if (loadTask == null) - { - icon.Spin(2000, RotationDirection.Clockwise); - - loadTask = Task.Run(() => - { - Track = audio.Track.Get(TrackUrl); - Track.Looping = true; - if (Playing) - Track.Start(); - - icon.ClearTransforms(); - icon.Rotation = 0; - Playing.TriggerChange(); - }); - } - + playing.Value = !playing.Value; return true; } @@ -97,7 +86,7 @@ namespace osu.Game.Overlays.Direct protected override void OnHoverLost(InputState state) { - if(!Playing) + if(!playing.Value) icon.FadeColour(Color4.White, 120, Easing.InOutQuint); base.OnHoverLost(state); } diff --git a/osu.Game/osu.Game.csproj b/osu.Game/osu.Game.csproj index 95a15c06ba..3d81c5a539 100644 --- a/osu.Game/osu.Game.csproj +++ b/osu.Game/osu.Game.csproj @@ -239,6 +239,7 @@ + From 26e7a3f157ced6e6fb0ab01f05b9ec2abe2ab862 Mon Sep 17 00:00:00 2001 From: Jorolf Date: Fri, 29 Sep 2017 23:12:12 +0200 Subject: [PATCH 05/11] add license header --- osu.Game/Audio/AudioLoadWrapper.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/osu.Game/Audio/AudioLoadWrapper.cs b/osu.Game/Audio/AudioLoadWrapper.cs index 8c013cf70f..69bf9d8147 100644 --- a/osu.Game/Audio/AudioLoadWrapper.cs +++ b/osu.Game/Audio/AudioLoadWrapper.cs @@ -1,4 +1,7 @@ -using osu.Framework.Allocation; +// 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; From 2457df8e18b88006b3ecae32dd1ab0ac970a79e1 Mon Sep 17 00:00:00 2001 From: Jorolf Date: Fri, 29 Sep 2017 23:26:16 +0200 Subject: [PATCH 06/11] remove unused usings and reset the track to the start when playing again --- osu.Game/Audio/AudioLoadWrapper.cs | 5 ----- osu.Game/Overlays/BeatmapSet/PreviewButton.cs | 2 -- osu.Game/Overlays/Direct/DirectGridPanel.cs | 1 - osu.Game/Overlays/Direct/DirectListPanel.cs | 2 -- osu.Game/Overlays/Direct/DirectPanel.cs | 3 ++- osu.Game/Overlays/Direct/PlayButton.cs | 4 ---- 6 files changed, 2 insertions(+), 15 deletions(-) diff --git a/osu.Game/Audio/AudioLoadWrapper.cs b/osu.Game/Audio/AudioLoadWrapper.cs index 69bf9d8147..efdde48564 100644 --- a/osu.Game/Audio/AudioLoadWrapper.cs +++ b/osu.Game/Audio/AudioLoadWrapper.cs @@ -6,11 +6,6 @@ using osu.Framework.Audio; using osu.Framework.Audio.Track; using osu.Framework.Graphics; using osu.Game.Beatmaps; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace osu.Game.Audio { diff --git a/osu.Game/Overlays/BeatmapSet/PreviewButton.cs b/osu.Game/Overlays/BeatmapSet/PreviewButton.cs index 9bb8b9a1a5..ea49963f67 100644 --- a/osu.Game/Overlays/BeatmapSet/PreviewButton.cs +++ b/osu.Game/Overlays/BeatmapSet/PreviewButton.cs @@ -2,7 +2,6 @@ // 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.Extensions.Color4Extensions; using osu.Framework.Graphics; @@ -12,7 +11,6 @@ using osu.Framework.Input; using osu.Game.Beatmaps; using osu.Game.Graphics; using osu.Game.Graphics.Containers; -using osu.Game.Graphics.UserInterface; using OpenTK; using OpenTK.Graphics; using osu.Game.Audio; diff --git a/osu.Game/Overlays/Direct/DirectGridPanel.cs b/osu.Game/Overlays/Direct/DirectGridPanel.cs index 3b8cd20200..10611cd5be 100644 --- a/osu.Game/Overlays/Direct/DirectGridPanel.cs +++ b/osu.Game/Overlays/Direct/DirectGridPanel.cs @@ -12,7 +12,6 @@ using osu.Game.Graphics; using osu.Game.Graphics.Sprites; using osu.Framework.Graphics.Shapes; using osu.Game.Beatmaps; -using osu.Framework.Configuration; namespace osu.Game.Overlays.Direct { diff --git a/osu.Game/Overlays/Direct/DirectListPanel.cs b/osu.Game/Overlays/Direct/DirectListPanel.cs index 6a73c15ee6..7f5fa3e3f0 100644 --- a/osu.Game/Overlays/Direct/DirectListPanel.cs +++ b/osu.Game/Overlays/Direct/DirectListPanel.cs @@ -13,8 +13,6 @@ using osu.Framework.Allocation; using osu.Framework.Localisation; using osu.Framework.Graphics.Shapes; using osu.Game.Beatmaps; -using osu.Framework.Configuration; -using System; namespace osu.Game.Overlays.Direct { diff --git a/osu.Game/Overlays/Direct/DirectPanel.cs b/osu.Game/Overlays/Direct/DirectPanel.cs index 6b1e06fd3a..2195fc9ce7 100644 --- a/osu.Game/Overlays/Direct/DirectPanel.cs +++ b/osu.Game/Overlays/Direct/DirectPanel.cs @@ -131,12 +131,13 @@ namespace osu.Game.Overlays.Direct { PlayButton.Loading = false; Preview = (d as AudioLoadWrapper)?.Preview; - Preview.Start(); + PreviewPlaying.TriggerChange(); }, }); } else { + Preview.Seek(0); Preview.Start(); } } diff --git a/osu.Game/Overlays/Direct/PlayButton.cs b/osu.Game/Overlays/Direct/PlayButton.cs index 34b0ace1c5..0405049c6a 100644 --- a/osu.Game/Overlays/Direct/PlayButton.cs +++ b/osu.Game/Overlays/Direct/PlayButton.cs @@ -3,16 +3,12 @@ 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.Beatmaps; using osu.Game.Graphics; using osu.Game.Graphics.UserInterface; -using System.Threading.Tasks; namespace osu.Game.Overlays.Direct { From 59247bcf1eeef982a1ab442afcef0c3afa055561 Mon Sep 17 00:00:00 2001 From: Jorolf Date: Fri, 29 Sep 2017 23:31:42 +0200 Subject: [PATCH 07/11] another unused using --- osu.Game/Audio/AudioLoadWrapper.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/osu.Game/Audio/AudioLoadWrapper.cs b/osu.Game/Audio/AudioLoadWrapper.cs index efdde48564..67836d1690 100644 --- a/osu.Game/Audio/AudioLoadWrapper.cs +++ b/osu.Game/Audio/AudioLoadWrapper.cs @@ -5,7 +5,6 @@ using osu.Framework.Allocation; using osu.Framework.Audio; using osu.Framework.Audio.Track; using osu.Framework.Graphics; -using osu.Game.Beatmaps; namespace osu.Game.Audio { From 5a8b8dacbb99c6dfb17f702cd4aedf823bc85878 Mon Sep 17 00:00:00 2001 From: Jorolf Date: Fri, 6 Oct 2017 21:00:23 +0200 Subject: [PATCH 08/11] move stuff thats duplicated in PreviewButton and DirectPanel to PlayButton --- osu.Game/Audio/AudioLoadWrapper.cs | 32 ------ osu.Game/Overlays/BeatmapSet/PreviewButton.cs | 58 ++--------- osu.Game/Overlays/Direct/DirectGridPanel.cs | 2 +- osu.Game/Overlays/Direct/DirectListPanel.cs | 2 +- osu.Game/Overlays/Direct/DirectPanel.cs | 48 ++------- osu.Game/Overlays/Direct/PlayButton.cs | 99 +++++++++++++++++-- osu.Game/Overlays/DirectOverlay.cs | 22 ++--- osu.Game/osu.Game.csproj | 1 - 8 files changed, 117 insertions(+), 147 deletions(-) delete mode 100644 osu.Game/Audio/AudioLoadWrapper.cs 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 @@ - From f3ca6cc38756d3c2d121cb6a553bd72db37f5413 Mon Sep 17 00:00:00 2001 From: Jorolf Date: Fri, 6 Oct 2017 21:06:37 +0200 Subject: [PATCH 09/11] remove redundant stuff --- osu.Game/Overlays/BeatmapSet/PreviewButton.cs | 5 +---- osu.Game/Overlays/Direct/DirectPanel.cs | 1 - osu.Game/Overlays/Direct/PlayButton.cs | 5 ++--- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/osu.Game/Overlays/BeatmapSet/PreviewButton.cs b/osu.Game/Overlays/BeatmapSet/PreviewButton.cs index 2fe2edaf1d..d040c89a91 100644 --- a/osu.Game/Overlays/BeatmapSet/PreviewButton.cs +++ b/osu.Game/Overlays/BeatmapSet/PreviewButton.cs @@ -13,7 +13,6 @@ using osu.Game.Graphics; using osu.Game.Graphics.Containers; using OpenTK; using OpenTK.Graphics; -using osu.Game.Audio; using osu.Game.Overlays.Direct; using osu.Framework.Configuration; @@ -23,7 +22,6 @@ namespace osu.Game.Overlays.BeatmapSet { private const float transition_duration = 500; - private readonly Container audioWrapper; private readonly Box bg, progress; private readonly PlayButton playButton; @@ -42,7 +40,6 @@ namespace osu.Game.Overlays.BeatmapSet Children = new Drawable[] { - audioWrapper = new Container(), bg = new Box { RelativeSizeAxes = Axes.Both, @@ -61,7 +58,7 @@ namespace osu.Game.Overlays.BeatmapSet Alpha = 0f, }, }, - playButton = new PlayButton() + playButton = new PlayButton { Anchor = Anchor.Centre, Origin = Anchor.Centre, diff --git a/osu.Game/Overlays/Direct/DirectPanel.cs b/osu.Game/Overlays/Direct/DirectPanel.cs index eaabb0b539..ef89c0022b 100644 --- a/osu.Game/Overlays/Direct/DirectPanel.cs +++ b/osu.Game/Overlays/Direct/DirectPanel.cs @@ -22,7 +22,6 @@ using osu.Game.Overlays.Notifications; using osu.Game.Online.API.Requests; using osu.Framework.Configuration; using osu.Framework.Audio.Track; -using osu.Game.Audio; namespace osu.Game.Overlays.Direct { diff --git a/osu.Game/Overlays/Direct/PlayButton.cs b/osu.Game/Overlays/Direct/PlayButton.cs index dea6b1568d..9ccce134d8 100644 --- a/osu.Game/Overlays/Direct/PlayButton.cs +++ b/osu.Game/Overlays/Direct/PlayButton.cs @@ -9,7 +9,6 @@ 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; @@ -38,7 +37,7 @@ namespace osu.Game.Overlays.Direct private Color4 hoverColour; private readonly SpriteIcon icon; private readonly LoadingAnimation loadingAnimation; - private Container audioWrapper; + private readonly Container audioWrapper; private const float transition_duration = 500; @@ -64,7 +63,7 @@ namespace osu.Game.Overlays.Direct public PlayButton(BeatmapSetInfo setInfo = null) { - this.SetInfo = setInfo; + SetInfo = setInfo; AddRange(new Drawable[] { audioWrapper = new Container(), From abf54180800e6e207ba8af5da5cea91dbff1a0f7 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 13 Oct 2017 14:06:34 +0900 Subject: [PATCH 10/11] Apply some renames and refactoring of loading logic Reduced publicly facing properties where possible. Also fixes a potentially bad state issue when the beatmapset was changed while a load was in progress. --- osu.Game/Overlays/BeatmapSet/PreviewButton.cs | 4 +- osu.Game/Overlays/Direct/PlayButton.cs | 79 +++++++++++-------- 2 files changed, 46 insertions(+), 37 deletions(-) diff --git a/osu.Game/Overlays/BeatmapSet/PreviewButton.cs b/osu.Game/Overlays/BeatmapSet/PreviewButton.cs index d040c89a91..f77a1f4a0a 100644 --- a/osu.Game/Overlays/BeatmapSet/PreviewButton.cs +++ b/osu.Game/Overlays/BeatmapSet/PreviewButton.cs @@ -30,8 +30,8 @@ namespace osu.Game.Overlays.BeatmapSet public BeatmapSetInfo BeatmapSet { - get { return playButton.SetInfo; } - set { playButton.SetInfo = value; } + get { return playButton.BeatmapSet; } + set { playButton.BeatmapSet = value; } } public PreviewButton() diff --git a/osu.Game/Overlays/Direct/PlayButton.cs b/osu.Game/Overlays/Direct/PlayButton.cs index 9ccce134d8..a98681234a 100644 --- a/osu.Game/Overlays/Direct/PlayButton.cs +++ b/osu.Game/Overlays/Direct/PlayButton.cs @@ -20,16 +20,17 @@ namespace osu.Game.Overlays.Direct public readonly Bindable Playing = new Bindable(); public Track Preview { get; private set; } - private BeatmapSetInfo setInfo; - public BeatmapSetInfo SetInfo + private BeatmapSetInfo beatmapSet; + public BeatmapSetInfo BeatmapSet { - get { return setInfo; } + get { return beatmapSet; } set { - if (value == setInfo) return; - setInfo = value; + if (value == beatmapSet) return; + beatmapSet = value; Playing.Value = false; + trackLoader = null; Preview = null; } } @@ -41,13 +42,10 @@ namespace osu.Game.Overlays.Direct private const float transition_duration = 500; - private bool loading; - public bool Loading + private bool loading { - get { return loading; } set { - loading = value; if (value) { loadingAnimation.Show(); @@ -63,7 +61,7 @@ namespace osu.Game.Overlays.Direct public PlayButton(BeatmapSetInfo setInfo = null) { - SetInfo = setInfo; + BeatmapSet = setInfo; AddRange(new Drawable[] { audioWrapper = new Container(), @@ -78,10 +76,12 @@ namespace osu.Game.Overlays.Direct loadingAnimation = new LoadingAnimation(), }); - 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 += updatePreviewTrack; + Playing.ValueChanged += playing => + { + icon.Icon = playing ? FontAwesome.fa_pause : FontAwesome.fa_play; + icon.FadeColour(playing || IsHovered ? hoverColour : Color4.White, 120, Easing.InOutQuint); + updatePreviewTrack(playing); + }; } [BackgroundDependencyLoader] @@ -104,7 +104,7 @@ 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); } @@ -113,35 +113,25 @@ namespace osu.Game.Overlays.Direct { base.Update(); - if(Preview?.HasCompleted ?? false) + if (Preview?.HasCompleted ?? false) { Playing.Value = false; Preview = null; } } - private void updatePreviewTrack(bool newValue) + private void updatePreviewTrack(bool playing) { - if (newValue) + if (playing) { 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(); + beginAudioLoad(); + return; } + + Preview.Seek(0); + Preview.Start(); } else { @@ -149,13 +139,32 @@ namespace osu.Game.Overlays.Direct } } - private class AudioLoadWrapper : Drawable + private TrackLoader trackLoader; + + private void beginAudioLoad() + { + if (trackLoader != null) return; + + Add(new AsyncLoadWrapper(trackLoader = new TrackLoader($"https://b.ppy.sh/preview/{BeatmapSet.OnlineBeatmapSetID}.mp3") + { + OnLoadComplete = d => + { + // we may have been replaced by another loader + if (trackLoader != d) return; + + Preview = (d as TrackLoader)?.Preview; + Playing.TriggerChange(); + }, + })); + } + + private class TrackLoader : Drawable { private readonly string preview; public Track Preview; - public AudioLoadWrapper(string preview) + public TrackLoader(string preview) { this.preview = preview; } From 5b16f5d3b53b64146839ec87d7fdfa2ce4ecec6f Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 13 Oct 2017 14:15:44 +0900 Subject: [PATCH 11/11] Remove unused field --- osu.Game/Overlays/Direct/PlayButton.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/osu.Game/Overlays/Direct/PlayButton.cs b/osu.Game/Overlays/Direct/PlayButton.cs index a98681234a..32435a4873 100644 --- a/osu.Game/Overlays/Direct/PlayButton.cs +++ b/osu.Game/Overlays/Direct/PlayButton.cs @@ -38,7 +38,6 @@ namespace osu.Game.Overlays.Direct private Color4 hoverColour; private readonly SpriteIcon icon; private readonly LoadingAnimation loadingAnimation; - private readonly Container audioWrapper; private const float transition_duration = 500; @@ -64,7 +63,6 @@ namespace osu.Game.Overlays.Direct BeatmapSet = setInfo; AddRange(new Drawable[] { - audioWrapper = new Container(), icon = new SpriteIcon { Anchor = Anchor.Centre,