1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 00:47:24 +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
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();

View File

@ -6,7 +6,6 @@ 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.IO.Stores;
using osu.Game.Beatmaps;
@ -32,12 +31,16 @@ namespace osu.Game.Audio
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 @@ namespace osu.Game.Audio
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");
}
}
}

View File

@ -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 @@ namespace osu.Game.Overlays.Direct
{
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;
}