2019-01-24 16:43:03 +08:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-05-23 16:37:39 +08:00
|
|
|
|
|
2018-05-28 16:55:41 +08:00
|
|
|
|
using System;
|
2018-05-23 17:52:09 +08:00
|
|
|
|
using System.Diagnostics;
|
2018-05-28 16:55:41 +08:00
|
|
|
|
using JetBrains.Annotations;
|
2018-05-23 17:52:09 +08:00
|
|
|
|
using osu.Framework.Audio;
|
2018-05-28 16:55:41 +08:00
|
|
|
|
using osu.Framework.Audio.Track;
|
2018-05-23 16:37:39 +08:00
|
|
|
|
using osu.Framework.Configuration;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Beatmaps
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A <see cref="Bindable{WorkingBeatmap}"/> for the <see cref="OsuGame"/> beatmap.
|
2018-06-06 19:16:20 +08:00
|
|
|
|
/// This should be used sparingly in-favour of <see cref="IBindableBeatmap"/>.
|
2018-05-23 16:37:39 +08:00
|
|
|
|
/// </summary>
|
2018-06-06 19:16:20 +08:00
|
|
|
|
public abstract class BindableBeatmap : NonNullableBindable<WorkingBeatmap>, IBindableBeatmap
|
2018-05-23 16:37:39 +08:00
|
|
|
|
{
|
2018-05-28 16:55:41 +08:00
|
|
|
|
private AudioManager audioManager;
|
2018-05-23 17:52:09 +08:00
|
|
|
|
private WorkingBeatmap lastBeatmap;
|
|
|
|
|
|
2018-06-06 19:16:20 +08:00
|
|
|
|
protected BindableBeatmap(WorkingBeatmap defaultValue)
|
2018-05-23 16:37:39 +08:00
|
|
|
|
: base(defaultValue)
|
|
|
|
|
{
|
2018-05-28 16:55:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registers an <see cref="AudioManager"/> for <see cref="Track"/>s to be added to.
|
|
|
|
|
/// </summary>
|
2018-06-04 10:11:07 +08:00
|
|
|
|
/// <param name="audioManager">The <see cref="AudioManager"/> to register.</param>
|
2018-05-28 16:55:41 +08:00
|
|
|
|
protected void RegisterAudioManager([NotNull] AudioManager audioManager)
|
|
|
|
|
{
|
|
|
|
|
if (this.audioManager != null) throw new InvalidOperationException($"Cannot register multiple {nameof(AudioManager)}s.");
|
|
|
|
|
|
2018-05-23 17:52:09 +08:00
|
|
|
|
this.audioManager = audioManager;
|
|
|
|
|
|
|
|
|
|
ValueChanged += registerAudioTrack;
|
2018-05-28 16:55:41 +08:00
|
|
|
|
|
|
|
|
|
// If the track has changed prior to this being called, let's register it
|
|
|
|
|
if (Value != Default)
|
|
|
|
|
registerAudioTrack(Value);
|
2018-05-23 17:52:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void registerAudioTrack(WorkingBeatmap beatmap)
|
|
|
|
|
{
|
|
|
|
|
var trackLoaded = lastBeatmap?.TrackLoaded ?? false;
|
|
|
|
|
|
|
|
|
|
// compare to last beatmap as sometimes the two may share a track representation (optimisation, see WorkingBeatmap.TransferTo)
|
|
|
|
|
if (!trackLoaded || lastBeatmap?.Track != beatmap.Track)
|
|
|
|
|
{
|
|
|
|
|
if (trackLoaded)
|
|
|
|
|
{
|
|
|
|
|
Debug.Assert(lastBeatmap != null);
|
|
|
|
|
Debug.Assert(lastBeatmap.Track != null);
|
|
|
|
|
|
|
|
|
|
lastBeatmap.RecycleTrack();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
audioManager.Track.AddItem(beatmap.Track);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lastBeatmap = beatmap;
|
2018-05-23 16:37:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-28 16:55:41 +08:00
|
|
|
|
[NotNull]
|
2018-06-06 19:16:20 +08:00
|
|
|
|
IBindableBeatmap IBindableBeatmap.GetBoundCopy() => GetBoundCopy();
|
2018-05-24 11:42:58 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2018-06-06 19:16:20 +08:00
|
|
|
|
/// Retrieve a new <see cref="BindableBeatmap"/> instance weakly bound to this <see cref="BindableBeatmap"/>.
|
|
|
|
|
/// If you are further binding to events of the retrieved <see cref="BindableBeatmap"/>, ensure a local reference is held.
|
2018-05-24 11:42:58 +08:00
|
|
|
|
/// </summary>
|
2018-05-28 16:55:41 +08:00
|
|
|
|
[NotNull]
|
2018-06-06 19:16:20 +08:00
|
|
|
|
public abstract BindableBeatmap GetBoundCopy();
|
2018-05-23 16:37:39 +08:00
|
|
|
|
}
|
|
|
|
|
}
|