anx.framework/ANX.Framework/GameComponentCollection.cs

70 lines
2.3 KiB
C#
Raw Permalink Normal View History

#region Using Statements
2011-10-31 05:36:24 +00:00
using System;
using System.Collections.ObjectModel;
using ANX.Framework.NonXNA.Development;
2011-10-31 05:36:24 +00:00
#endregion // Using Statements
// This file is part of the ANX.Framework created by the
// "ANX.Framework developer group" and released under the Ms-PL license.
// For details see: http://anxframework.codeplex.com/license
2011-10-31 05:36:24 +00:00
namespace ANX.Framework
{
[PercentageComplete(100)]
[TestState(TestStateAttribute.TestState.Tested)]
[Developer("Glatzemann, AstrorEnales")]
2011-10-31 05:36:24 +00:00
public sealed class GameComponentCollection : Collection<IGameComponent>
{
#region Events
2011-10-31 05:36:24 +00:00
public event EventHandler<GameComponentCollectionEventArgs> ComponentAdded;
public event EventHandler<GameComponentCollectionEventArgs> ComponentRemoved;
#endregion
2011-10-31 05:36:24 +00:00
protected override void ClearItems()
{
for (int i = 0; i < Count; i++)
OnComponentRemoved(base[i]);
base.ClearItems();
2011-10-31 05:36:24 +00:00
}
protected override void InsertItem(int index, IGameComponent item)
2011-10-31 05:36:24 +00:00
{
if (IndexOf(item) != -1)
throw new ArgumentException(
"Cannot add the same game component to a game component collection multiple times.");
base.InsertItem(index, item);
if(item != null)
OnComponentAdded(item);
2011-10-31 05:36:24 +00:00
}
protected override void RemoveItem(int index)
2011-10-31 05:36:24 +00:00
{
IGameComponent component = base[index];
base.RemoveItem(index);
if (component != null)
OnComponentRemoved(component);
2011-10-31 05:36:24 +00:00
}
protected override void SetItem(int index, IGameComponent item)
2011-10-31 05:36:24 +00:00
{
throw new NotSupportedException(
"Cannot set a value using operator[] on GameComponentCollection. Use Add/Remove instead.");
2011-10-31 05:36:24 +00:00
}
private void OnComponentAdded(IGameComponent component)
2011-10-31 05:36:24 +00:00
{
if (ComponentAdded != null)
ComponentAdded(this, new GameComponentCollectionEventArgs(component));
2011-10-31 05:36:24 +00:00
}
private void OnComponentRemoved(IGameComponent component)
2011-10-31 05:36:24 +00:00
{
if (ComponentRemoved != null)
ComponentRemoved(this, new GameComponentCollectionEventArgs(component));
2011-10-31 05:36:24 +00:00
}
}
}