// Copyright (c) 2007-2018 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using System; using System.Diagnostics; using JetBrains.Annotations; using osu.Framework.Audio; using osu.Framework.Audio.Track; using osu.Framework.Configuration; namespace osu.Game.Beatmaps { /// /// A for the beatmap. /// This should be used sparingly in-favour of . /// public abstract class BindableBeatmap : NonNullableBindable, IBindableBeatmap { private AudioManager audioManager; private WorkingBeatmap lastBeatmap; protected BindableBeatmap(WorkingBeatmap defaultValue) : base(defaultValue) { } /// /// Registers an for s to be added to. /// /// The to register. protected void RegisterAudioManager([NotNull] AudioManager audioManager) { if (this.audioManager != null) throw new InvalidOperationException($"Cannot register multiple {nameof(AudioManager)}s."); this.audioManager = audioManager; ValueChanged += registerAudioTrack; // If the track has changed prior to this being called, let's register it if (Value != Default) registerAudioTrack(Value); } 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; } [NotNull] IBindableBeatmap IBindableBeatmap.GetBoundCopy() => GetBoundCopy(); /// /// Retrieve a new instance weakly bound to this . /// If you are further binding to events of the retrieved , ensure a local reference is held. /// [NotNull] public abstract BindableBeatmap GetBoundCopy(); } }