mirror of
https://github.com/ppy/osu
synced 2024-12-29 02:12:43 +00:00
Use PreviewTrackManager in the PlayButton
This commit is contained in:
parent
ad50f7faf1
commit
c492f62eda
@ -23,6 +23,7 @@ using osu.Game.Online.API;
|
|||||||
using osu.Framework.Graphics.Performance;
|
using osu.Framework.Graphics.Performance;
|
||||||
using osu.Framework.Graphics.Textures;
|
using osu.Framework.Graphics.Textures;
|
||||||
using osu.Framework.Logging;
|
using osu.Framework.Logging;
|
||||||
|
using osu.Game.Audio;
|
||||||
using osu.Game.Database;
|
using osu.Game.Database;
|
||||||
using osu.Game.Graphics.Textures;
|
using osu.Game.Graphics.Textures;
|
||||||
using osu.Game.Input;
|
using osu.Game.Input;
|
||||||
@ -202,6 +203,10 @@ namespace osu.Game
|
|||||||
|
|
||||||
KeyBindingStore.Register(globalBinding);
|
KeyBindingStore.Register(globalBinding);
|
||||||
dependencies.Cache(globalBinding);
|
dependencies.Cache(globalBinding);
|
||||||
|
|
||||||
|
PreviewTrackManager previewTrackManager;
|
||||||
|
dependencies.Cache(previewTrackManager = new PreviewTrackManager());
|
||||||
|
Add(previewTrackManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void runMigrations()
|
private void runMigrations()
|
||||||
|
@ -1,24 +1,22 @@
|
|||||||
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
||||||
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
||||||
|
|
||||||
using OpenTK.Graphics;
|
|
||||||
using osu.Framework.Allocation;
|
using osu.Framework.Allocation;
|
||||||
using osu.Framework.Audio;
|
|
||||||
using osu.Framework.Audio.Track;
|
using osu.Framework.Audio.Track;
|
||||||
using osu.Framework.Configuration;
|
using osu.Framework.Configuration;
|
||||||
using osu.Framework.Graphics;
|
using osu.Framework.Graphics;
|
||||||
using osu.Framework.Graphics.Containers;
|
using osu.Framework.Graphics.Containers;
|
||||||
using osu.Framework.Input;
|
using osu.Framework.Input;
|
||||||
using osu.Framework.IO.Stores;
|
using osu.Game.Audio;
|
||||||
using osu.Game.Beatmaps;
|
using osu.Game.Beatmaps;
|
||||||
using osu.Game.Graphics;
|
using osu.Game.Graphics;
|
||||||
using osu.Game.Graphics.UserInterface;
|
using osu.Game.Graphics.UserInterface;
|
||||||
|
using OpenTK.Graphics;
|
||||||
|
|
||||||
namespace osu.Game.Overlays.Direct
|
namespace osu.Game.Overlays.Direct
|
||||||
{
|
{
|
||||||
public class PlayButton : Container
|
public class PlayButton : Container
|
||||||
{
|
{
|
||||||
private static PlayButton activeBeatmapPreview;
|
|
||||||
public readonly Bindable<bool> Playing = new Bindable<bool>();
|
public readonly Bindable<bool> Playing = new Bindable<bool>();
|
||||||
public Track Preview { get; private set; }
|
public Track Preview { get; private set; }
|
||||||
|
|
||||||
@ -33,17 +31,16 @@ namespace osu.Game.Overlays.Direct
|
|||||||
beatmapSet = value;
|
beatmapSet = value;
|
||||||
|
|
||||||
Playing.Value = false;
|
Playing.Value = false;
|
||||||
trackLoader = null;
|
|
||||||
Preview = null;
|
Preview = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private PreviewTrackManager previewTrackManager;
|
||||||
|
|
||||||
private Color4 hoverColour;
|
private Color4 hoverColour;
|
||||||
private readonly SpriteIcon icon;
|
private readonly SpriteIcon icon;
|
||||||
private readonly LoadingAnimation loadingAnimation;
|
private readonly LoadingAnimation loadingAnimation;
|
||||||
|
|
||||||
private readonly BindableDouble muteBindable = new BindableDouble();
|
|
||||||
|
|
||||||
private const float transition_duration = 500;
|
private const float transition_duration = 500;
|
||||||
|
|
||||||
private bool loading
|
private bool loading
|
||||||
@ -79,14 +76,20 @@ namespace osu.Game.Overlays.Direct
|
|||||||
loadingAnimation = new LoadingAnimation(),
|
loadingAnimation = new LoadingAnimation(),
|
||||||
});
|
});
|
||||||
|
|
||||||
Playing.ValueChanged += updatePreviewTrack;
|
Playing.ValueChanged += playingStateChanged;
|
||||||
}
|
}
|
||||||
|
|
||||||
[BackgroundDependencyLoader]
|
[BackgroundDependencyLoader]
|
||||||
private void load(OsuColour colour, AudioManager audio)
|
private void load(OsuColour colour, PreviewTrackManager previewTrackManager)
|
||||||
{
|
{
|
||||||
hoverColour = colour.Yellow;
|
hoverColour = colour.Yellow;
|
||||||
this.audio = audio;
|
this.previewTrackManager = previewTrackManager;
|
||||||
|
|
||||||
|
previewTrackManager.PlaybackStopped += () =>
|
||||||
|
{
|
||||||
|
if (Preview == previewTrackManager.CurrentTrack)
|
||||||
|
Playing.Value = false;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override bool OnClick(InputState state)
|
protected override bool OnClick(InputState state)
|
||||||
@ -108,18 +111,7 @@ namespace osu.Game.Overlays.Direct
|
|||||||
base.OnHoverLost(state);
|
base.OnHoverLost(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void Update()
|
private void playingStateChanged(bool playing)
|
||||||
{
|
|
||||||
base.Update();
|
|
||||||
|
|
||||||
if (Preview?.HasCompleted ?? false)
|
|
||||||
{
|
|
||||||
Playing.Value = false;
|
|
||||||
Preview = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updatePreviewTrack(bool playing)
|
|
||||||
{
|
{
|
||||||
if (playing && BeatmapSet == null)
|
if (playing && BeatmapSet == null)
|
||||||
{
|
{
|
||||||
@ -132,25 +124,18 @@ namespace osu.Game.Overlays.Direct
|
|||||||
|
|
||||||
if (playing)
|
if (playing)
|
||||||
{
|
{
|
||||||
if (activeBeatmapPreview != null)
|
|
||||||
activeBeatmapPreview.Playing.Value = false;
|
|
||||||
|
|
||||||
if (Preview == null)
|
if (Preview == null)
|
||||||
{
|
{
|
||||||
beginAudioLoad();
|
loading = true;
|
||||||
return;
|
Preview = previewTrackManager.Get(beatmapSet);
|
||||||
|
loading = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Preview.Restart();
|
previewTrackManager.Play(Preview);
|
||||||
|
|
||||||
audio.Track.AddAdjustment(AdjustableProperty.Volume, muteBindable);
|
|
||||||
activeBeatmapPreview = this;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
audio.Track.RemoveAdjustment(AdjustableProperty.Volume, muteBindable);
|
previewTrackManager.Stop();
|
||||||
|
|
||||||
Preview?.Stop();
|
|
||||||
loading = false;
|
loading = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -160,64 +145,5 @@ namespace osu.Game.Overlays.Direct
|
|||||||
base.Dispose(isDisposing);
|
base.Dispose(isDisposing);
|
||||||
Playing.Value = false;
|
Playing.Value = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private TrackLoader trackLoader;
|
|
||||||
private AudioManager audio;
|
|
||||||
|
|
||||||
private void beginAudioLoad()
|
|
||||||
{
|
|
||||||
if (trackLoader != null)
|
|
||||||
{
|
|
||||||
Preview = trackLoader.Preview;
|
|
||||||
Playing.TriggerChange();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
loading = true;
|
|
||||||
|
|
||||||
LoadComponentAsync(trackLoader = new TrackLoader($"https://b.ppy.sh/preview/{BeatmapSet.OnlineBeatmapSetID}.mp3"),
|
|
||||||
d =>
|
|
||||||
{
|
|
||||||
// We may have been replaced by another loader
|
|
||||||
if (trackLoader != d) return;
|
|
||||||
|
|
||||||
Preview = d?.Preview;
|
|
||||||
updatePreviewTrack(Playing);
|
|
||||||
loading = false;
|
|
||||||
|
|
||||||
Add(trackLoader);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private class TrackLoader : Drawable
|
|
||||||
{
|
|
||||||
private readonly string preview;
|
|
||||||
|
|
||||||
public Track Preview;
|
|
||||||
private TrackManager trackManager;
|
|
||||||
|
|
||||||
public TrackLoader(string preview)
|
|
||||||
{
|
|
||||||
this.preview = preview;
|
|
||||||
}
|
|
||||||
|
|
||||||
[BackgroundDependencyLoader]
|
|
||||||
private void load(AudioManager audio, FrameworkConfigManager config)
|
|
||||||
{
|
|
||||||
// create a local trackManager to bypass the mute we are applying above.
|
|
||||||
audio.AddItem(trackManager = new TrackManager(new OnlineStore()));
|
|
||||||
|
|
||||||
// add back the user's music volume setting (since we are no longer in the global TrackManager's hierarchy).
|
|
||||||
config.BindWith(FrameworkSetting.VolumeMusic, trackManager.Volume);
|
|
||||||
|
|
||||||
Preview = trackManager.Get(preview);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void Dispose(bool isDisposing)
|
|
||||||
{
|
|
||||||
base.Dispose(isDisposing);
|
|
||||||
trackManager?.Dispose();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user