//-----------------------------------------------------------------------------
// EditCurveKeyCollection.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
namespace Xna.Tools
{
///
/// This class provides same functionality of CurveEditCollection but
/// this contains EditCurveKey instead of CurveKey.
/// You have to use any curve key operations via this class because this class
/// also manipulates original curve keys.
///
public class EditCurveKeyCollection : ICollection
{
#region Constructors.
///
/// Create new instance of EditCurveKeyCollection from Curve instance.
///
///
internal EditCurveKeyCollection(EditCurve owner)
{
// Generate EditCurveKey list from Curve class.
this.owner = owner;
foreach (CurveKey key in owner.OriginalCurve.Keys)
{
// Add EditCurveKey to keys.
int index = owner.OriginalCurve.Keys.IndexOf(key);
EditCurveKey newKey =
new EditCurveKey(EditCurveKey.GenerateUniqueId(), key);
keys.Insert(index, newKey);
idToKeyMap.Add(newKey.Id, newKey);
}
}
#endregion
#region IList like Members
///
/// Determines the index of a specfied CurveKey in the EditCurveKeyCollection.
///
/// CurveKey to locate in the EditCurveKeyCollection
/// The index of value if found in the EditCurveKeyCollection;
/// otherwise -1.
public int IndexOf(EditCurveKey item)
{
for (int i = 0; i < keys.Count; ++i)
{
if (keys[i].Id == item.Id) return i;
}
return -1;
}
///
/// Remove a CurveKey from at the specfied index.
///
/// The Zero-based index of the item to remove.
public void RemoveAt(int index)
{
EditCurveKey key = keys[index];
idToKeyMap.Remove(key.Id);
keys.RemoveAt(index);
owner.OriginalCurve.Keys.RemoveAt(index);
}
///
/// Gets or sets the element at the specfied index.
///
/// The zero-based index of the element to
/// get or set.
/// The element at the specfied index.
public EditCurveKey this[int index]
{
get
{
return keys[index];
}
set
{
if (value == null)
{
throw new System.ArgumentNullException();
}
// If new value has same position, it just change values.
float curPosition = keys[index].OriginalKey.Position;
if (curPosition == value.OriginalKey.Position)
{
keys[index] = value;
owner.OriginalCurve.Keys[index] = value.OriginalKey;
}
else
{
// Otherwise, remove given index key and add new one.
RemoveAt(index);
Add(value);
owner.Dirty = true;
}
}
}
#endregion
#region ICollection Members
///
/// Add an item to the EditCurveKeyCollection
///
/// CurveKey to add to the EditCurveKeyCollection
public void Add(EditCurveKey item)
{
if (item == null)
throw new System.ArgumentNullException("item");
// Add CurveKey to original curve.
owner.OriginalCurve.Keys.Add(item.OriginalKey);
// Add EditCurveKey to keys.
int index = owner.OriginalCurve.Keys.IndexOf(item.OriginalKey);
keys.Insert(index, item);
idToKeyMap.Add(item.Id, item);
owner.Dirty = true;
}
///
/// Removes all EditCurveKeys from the EditCurveKeyCollection.
///
public void Clear()
{
owner.OriginalCurve.Keys.Clear();
keys.Clear();
idToKeyMap.Clear();
owner.Dirty = true;
}
///
/// Determines whether the ExpandEnvironmentVariables
/// contains a specific EditCurveKey.
///
/// The EditCurveKey to locate in
/// the EditEditCurveKeyCollection.
/// true if the EditCurveKey is found in
/// the ExpandEnvironmentVariables; otherwise, false.
public bool Contains(EditCurveKey item)
{
if (item == null)
throw new System.ArgumentNullException("item");
return keys.Contains(item);
}
public void CopyTo(EditCurveKey[] array, int arrayIndex)
{
throw new NotImplementedException(
"The method or operation is not implemented.");
}
///
/// Gets the number of elements contained in the EditCurveKeyCollection.
///
public int Count
{
get { return keys.Count; }
}
///
/// Gets a value indicating whether the EditEditCurveKeyCollection is
/// read-only.
///
public bool IsReadOnly
{
get { return false; }
}
///
/// Removes the first occurrence of a specific EditCurveKey from
/// the EditCurveKeyCollection.
///
/// The EditCurveKey to remove from
/// the EditCurveKeyCollection.
/// true if item is successfully removed; otherwise, false.
/// This method also returns false if item was not found in
/// the EditCurveKeyCollection.
public bool Remove(EditCurveKey item)
{
if (item == null)
throw new System.ArgumentNullException("item");
bool result = owner.OriginalCurve.Keys.Remove(item.OriginalKey);
idToKeyMap.Remove(item.Id);
owner.Dirty = true;
return keys.Remove(item) && result;
}
#endregion
#region IEnumerable Members
///
/// Returns an enumerator that iterates through the EditCurveKeyCollection.
///
/// A IEnumerator>EditCurveKey<
/// for the EditCurveKeyCollection.
public IEnumerator GetEnumerator()
{
return keys.GetEnumerator();
}
#endregion
#region IEnumerable Members
///
/// Returns an enumerator that iterates through the EditCurveKeyCollection.
///
/// A IEnumerator for the EditCurveKeyCollection.
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return ((System.Collections.IEnumerable)keys).GetEnumerator();
}
#endregion
///
/// Tries to look up a EditCurveKey by id.
///
public bool TryGetValue(long keyId, out EditCurveKey value)
{
return idToKeyMap.TryGetValue(keyId, out value);
}
///
/// look up a EditCurveKey by id.
///
public EditCurveKey GetValue(long keyId)
{
return idToKeyMap[keyId];
}
///
/// Creates a new object that is a copy of the current instance.
///
/// A new object that is a copy of this instance.
public EditCurveKeyCollection Clone()
{
EditCurveKeyCollection newKeys = new EditCurveKeyCollection();
newKeys.keys = new List(keys);
return newKeys;
}
#region Private methods.
///
/// Private default construction.
///
private EditCurveKeyCollection()
{
}
#endregion
#region Private members.
///
/// Owner of this collection
///
private EditCurve owner;
///
/// EditCurveKey that contains EditCurveKey
///
private List keys = new List();
///
/// Id to EditCurveKey map.
///
private Dictionary idToKeyMap =
new Dictionary();
#endregion
}
}