mirror of https://github.com/ppy/osu
Make PreviewTrack a component and use LoadComponentAsync
This commit is contained in:
parent
5566732664
commit
330ce19041
|
@ -2,25 +2,36 @@
|
|||
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
||||
|
||||
using System;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Audio.Track;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Game.Beatmaps;
|
||||
|
||||
namespace osu.Game.Audio
|
||||
{
|
||||
public class PreviewTrack
|
||||
public class PreviewTrack : Component
|
||||
{
|
||||
public readonly Track Track;
|
||||
public Track Track { get; private set; }
|
||||
public readonly OverlayContainer Owner;
|
||||
|
||||
private readonly BeatmapSetInfo beatmapSetInfo;
|
||||
|
||||
public event Action Stopped;
|
||||
public event Action Started;
|
||||
|
||||
public PreviewTrack(Track track, OverlayContainer owner)
|
||||
public PreviewTrack(BeatmapSetInfo beatmapSetInfo, OverlayContainer owner)
|
||||
{
|
||||
Track = track;
|
||||
this.beatmapSetInfo = beatmapSetInfo;
|
||||
Owner = owner;
|
||||
}
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(PreviewTrackManager previewTrackManager)
|
||||
{
|
||||
Track = previewTrackManager.Get(this, beatmapSetInfo);
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
Track.Restart();
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
using osu.Framework.Audio.Track;
|
||||
using osu.Framework.Configuration;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Framework.IO.Stores;
|
||||
using osu.Game.Beatmaps;
|
||||
|
||||
|
@ -32,12 +31,16 @@ private void load(AudioManager audio, FrameworkConfigManager config)
|
|||
config.BindWith(FrameworkSetting.VolumeMusic, trackManager.Volume);
|
||||
}
|
||||
|
||||
public PreviewTrack Get(BeatmapSetInfo beatmapSetInfo, OverlayContainer previewOwner)
|
||||
protected override void Update()
|
||||
{
|
||||
var previewTrack = new PreviewTrack(
|
||||
trackManager.Get($"https://b.ppy.sh/preview/{beatmapSetInfo?.OnlineBeatmapSetID}.mp3"),
|
||||
previewOwner);
|
||||
if (CurrentTrack?.Track.HasCompleted ?? false)
|
||||
CurrentTrack.Stop();
|
||||
|
||||
base.Update();
|
||||
}
|
||||
|
||||
public Track Get(PreviewTrack previewTrack, BeatmapSetInfo beatmapSetInfo)
|
||||
{
|
||||
previewTrack.Started += () =>
|
||||
{
|
||||
CurrentTrack?.Stop();
|
||||
|
@ -51,15 +54,7 @@ public PreviewTrack Get(BeatmapSetInfo beatmapSetInfo, OverlayContainer previewO
|
|||
audio.Track.RemoveAdjustment(AdjustableProperty.Volume, muteBindable);
|
||||
};
|
||||
|
||||
return previewTrack;
|
||||
}
|
||||
|
||||
protected override void Update()
|
||||
{
|
||||
if (CurrentTrack?.Track.HasCompleted ?? false)
|
||||
CurrentTrack.Stop();
|
||||
|
||||
base.Update();
|
||||
return trackManager.Get($"https://b.ppy.sh/preview/{beatmapSetInfo?.OnlineBeatmapSetID}.mp3");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
||||
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
||||
|
||||
using System.Threading.Tasks;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Configuration;
|
||||
using osu.Framework.Graphics;
|
||||
|
@ -104,18 +103,18 @@ protected override bool OnClick(InputState state)
|
|||
{
|
||||
if (Preview == null)
|
||||
{
|
||||
Task.Run(() =>
|
||||
{
|
||||
loading = true;
|
||||
return Preview = previewTrackManager.Get(beatmapSet, parentOverlayContainer);
|
||||
})
|
||||
.ContinueWith(t =>
|
||||
loading = true;
|
||||
|
||||
LoadComponentAsync(
|
||||
Preview = new PreviewTrack(beatmapSet, parentOverlayContainer),
|
||||
t =>
|
||||
{
|
||||
Preview.Started += () => Playing.Value = true;
|
||||
Preview.Stopped += () => Playing.Value = false;
|
||||
Preview.Start();
|
||||
loading = false;
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue