// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System.Collections.Generic; using System.Linq; namespace osu.Framework.Audio { /// /// A collection of audio components which need central property control. /// public class AudioCollectionManager : AdjustableAudioComponent, IBassAudio where T : AudioComponent { internal List Items = new List(); public void AddItem(T item) { EnqueueAction(delegate { if (Items.Contains(item)) return; if (item is IAdjustableAudioComponent adjustable) adjustable.BindAdjustments(this); Items.Add(item); ItemAdded(item); }); } public void RemoveItem(T item) { EnqueueAction(() => { if (!Items.Contains(item)) return; if (item is IAdjustableAudioComponent adjustable) adjustable.UnbindAdjustments(this); Items.Remove(item); ItemRemoved(item); }); } void IBassAudio.UpdateDevice(int deviceIndex) => UpdateDevice(deviceIndex); internal virtual void UpdateDevice(int deviceIndex) { foreach (var item in Items.OfType()) item.UpdateDevice(deviceIndex); } protected override void UpdateChildren() { base.UpdateChildren(); for (int i = 0; i < Items.Count; i++) { var item = Items[i]; if (!item.IsAlive) { Items.RemoveAt(i--); ItemRemoved(item); continue; } item.Update(); } } /// /// An item has been added to . /// /// The item which was added. protected virtual void ItemAdded(T item) { } /// /// An item has been removed from . Implementations should dispose the removed item if required. /// /// The item which was removed. protected virtual void ItemRemoved(T item) { } protected override void Dispose(bool disposing) { // make the items queue their disposal, so they get disposed when UpdateChildren updates them. foreach (var i in Items) i.Dispose(); base.Dispose(disposing); } } }