A game framework written with osu! in mind.
at master 63 lines 1.8 kB view raw
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. 2// See the LICENCE file in the repository root for full licence text. 3 4using System.Collections.Generic; 5using System.Linq; 6 7namespace osu.Framework.Audio 8{ 9 /// <summary> 10 /// A collection of audio components which need central property control. 11 /// </summary> 12 public class AudioCollectionManager<T> : AdjustableAudioComponent, IBassAudio 13 where T : AdjustableAudioComponent 14 { 15 internal List<T> Items = new List<T>(); 16 17 public void AddItem(T item) 18 { 19 EnqueueAction(delegate 20 { 21 if (Items.Contains(item)) return; 22 23 item.BindAdjustments(this); 24 Items.Add(item); 25 }); 26 } 27 28 void IBassAudio.UpdateDevice(int deviceIndex) => UpdateDevice(deviceIndex); 29 30 internal virtual void UpdateDevice(int deviceIndex) 31 { 32 foreach (var item in Items.OfType<IBassAudio>()) 33 item.UpdateDevice(deviceIndex); 34 } 35 36 protected override void UpdateChildren() 37 { 38 base.UpdateChildren(); 39 40 for (int i = 0; i < Items.Count; i++) 41 { 42 var item = Items[i]; 43 44 if (!item.IsAlive) 45 { 46 Items.RemoveAt(i--); 47 continue; 48 } 49 50 item.Update(); 51 } 52 } 53 54 protected override void Dispose(bool disposing) 55 { 56 // make the items queue their disposal, so they get disposed when UpdateChildren updates them. 57 foreach (var i in Items) 58 i.Dispose(); 59 60 base.Dispose(disposing); 61 } 62 } 63}