1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 15:07:44 +08:00

Make PreviewTrack a component and use LoadComponentAsync

This commit is contained in:
Roman Kapustin 2018-06-01 23:36:25 +03:00
parent 5566732664
commit 330ce19041
3 changed files with 30 additions and 25 deletions

View File

@ -2,25 +2,36 @@
// 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 System; using System;
using osu.Framework.Allocation;
using osu.Framework.Audio.Track; using osu.Framework.Audio.Track;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Game.Beatmaps;
namespace osu.Game.Audio 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; public readonly OverlayContainer Owner;
private readonly BeatmapSetInfo beatmapSetInfo;
public event Action Stopped; public event Action Stopped;
public event Action Started; public event Action Started;
public PreviewTrack(Track track, OverlayContainer owner) public PreviewTrack(BeatmapSetInfo beatmapSetInfo, OverlayContainer owner)
{ {
Track = track; this.beatmapSetInfo = beatmapSetInfo;
Owner = owner; Owner = owner;
} }
[BackgroundDependencyLoader]
private void load(PreviewTrackManager previewTrackManager)
{
Track = previewTrackManager.Get(this, beatmapSetInfo);
}
public void Start() public void Start()
{ {
Track.Restart(); Track.Restart();

View File

@ -6,7 +6,6 @@ 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.IO.Stores; using osu.Framework.IO.Stores;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
@ -32,12 +31,16 @@ namespace osu.Game.Audio
config.BindWith(FrameworkSetting.VolumeMusic, trackManager.Volume); config.BindWith(FrameworkSetting.VolumeMusic, trackManager.Volume);
} }
public PreviewTrack Get(BeatmapSetInfo beatmapSetInfo, OverlayContainer previewOwner) protected override void Update()
{ {
var previewTrack = new PreviewTrack( if (CurrentTrack?.Track.HasCompleted ?? false)
trackManager.Get($"https://b.ppy.sh/preview/{beatmapSetInfo?.OnlineBeatmapSetID}.mp3"), CurrentTrack.Stop();
previewOwner);
base.Update();
}
public Track Get(PreviewTrack previewTrack, BeatmapSetInfo beatmapSetInfo)
{
previewTrack.Started += () => previewTrack.Started += () =>
{ {
CurrentTrack?.Stop(); CurrentTrack?.Stop();
@ -51,15 +54,7 @@ namespace osu.Game.Audio
audio.Track.RemoveAdjustment(AdjustableProperty.Volume, muteBindable); audio.Track.RemoveAdjustment(AdjustableProperty.Volume, muteBindable);
}; };
return previewTrack; return trackManager.Get($"https://b.ppy.sh/preview/{beatmapSetInfo?.OnlineBeatmapSetID}.mp3");
}
protected override void Update()
{
if (CurrentTrack?.Track.HasCompleted ?? false)
CurrentTrack.Stop();
base.Update();
} }
} }
} }

View File

@ -1,7 +1,6 @@
// 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 System.Threading.Tasks;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Configuration; using osu.Framework.Configuration;
using osu.Framework.Graphics; using osu.Framework.Graphics;
@ -104,18 +103,18 @@ namespace osu.Game.Overlays.Direct
{ {
if (Preview == null) if (Preview == null)
{ {
Task.Run(() => loading = true;
{
loading = true; LoadComponentAsync(
return Preview = previewTrackManager.Get(beatmapSet, parentOverlayContainer); Preview = new PreviewTrack(beatmapSet, parentOverlayContainer),
}) t =>
.ContinueWith(t =>
{ {
Preview.Started += () => Playing.Value = true; Preview.Started += () => Playing.Value = true;
Preview.Stopped += () => Playing.Value = false; Preview.Stopped += () => Playing.Value = false;
Preview.Start(); Preview.Start();
loading = false; loading = false;
}); });
return true; return true;
} }