270 lines
6.7 KiB
C#
Raw Permalink Normal View History

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
#endregion
// 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
namespace ANX.Framework.Content.Pipeline.Graphics
{
public abstract class VertexChannel : IList, ICollection, IEnumerable
{
2012-08-27 21:24:20 +00:00
#region Properties
public string Name
{
2012-08-27 21:24:20 +00:00
get;
private set;
}
2012-08-27 21:24:20 +00:00
protected IList Source
{
get; set;
}
2012-08-27 21:24:20 +00:00
public int Count
{
2012-08-27 21:24:20 +00:00
get { return Source.Count; }
}
2012-08-27 21:24:20 +00:00
bool ICollection.IsSynchronized
{
2012-08-27 21:24:20 +00:00
get { return false; }
}
2012-08-27 21:24:20 +00:00
object ICollection.SyncRoot
{
2012-08-27 21:24:20 +00:00
get { return Source.SyncRoot; }
}
bool IList.IsFixedSize
{
2012-08-27 21:24:20 +00:00
get { return true; }
}
bool IList.IsReadOnly
{
2012-08-27 21:24:20 +00:00
get { return false; }
}
2012-08-27 21:24:20 +00:00
public object this[int index]
{
2012-08-27 21:24:20 +00:00
get { return Source[index]; }
set { Source[index] = value; }
}
[ContentSerializerIgnore]
public abstract Type ElementType
{
get;
}
2012-08-27 21:24:20 +00:00
#endregion
2012-08-27 21:24:20 +00:00
#region Constructor
public VertexChannel(string name)
{
2012-08-27 21:24:20 +00:00
this.Name = name;
}
2012-08-27 21:24:20 +00:00
#endregion
2012-08-27 21:24:20 +00:00
#region Methods
int IList.Add(object value)
{
2012-08-27 21:24:20 +00:00
throw new NotSupportedException("Size is fixed");
}
internal void AddRange(IEnumerable vertices)
{
InsertRange(0, vertices);
}
internal void AddRange(int vertexCount)
{
for (int i = 0; i < vertexCount; i++)
Source.Add(Activator.CreateInstance(ElementType));
}
internal void InsertRange(int index, IEnumerable vertices)
{
if (vertices == null)
throw new ArgumentNullException("vertices");
foreach (object value in vertices)
{
if (value == null || value.GetType() != this.ElementType)
throw new ArgumentException("The added vertices are not of the same type as the vertex channel \"{0}\"", this.ElementType.Name);
Source.Insert(index, value);
index++;
}
}
internal void Insert(int index, object vertex)
{
if (vertex == null || vertex.GetType() != this.ElementType)
throw new ArgumentException("The added vertices are not of the same type as the vertex channel \"{0}\"", this.ElementType.Name);
Source.Insert(index, vertex);
}
internal void RemoveRange(int startIndex, int count)
{
if (startIndex < 0)
throw new ArgumentOutOfRangeException("startIndex");
for (int i = startIndex + count - 1; i >= startIndex; i--)
Source.RemoveAt(i);
}
internal void Clear()
{
Source.Clear();
}
2012-08-27 21:24:20 +00:00
void IList.Insert(int index, object value)
{
2012-08-27 21:24:20 +00:00
throw new NotSupportedException("Size is fixed");
}
2012-08-27 21:24:20 +00:00
void IList.Clear()
{
2012-08-27 21:24:20 +00:00
throw new NotSupportedException("Size is fixed");
}
2012-08-27 21:24:20 +00:00
void IList.Remove(object value)
{
2012-08-27 21:24:20 +00:00
throw new NotSupportedException("Size is fixed");
}
2012-08-27 21:24:20 +00:00
void IList.RemoveAt(int index)
{
2012-08-27 21:24:20 +00:00
throw new NotSupportedException("Size is fixed");
}
2012-08-27 21:24:20 +00:00
public bool Contains(object value)
{
2012-08-27 21:24:20 +00:00
return Source.Contains(value);
}
2012-08-27 21:24:20 +00:00
public int IndexOf(object value)
{
2012-08-27 21:24:20 +00:00
return Source.IndexOf(value);
}
2012-08-27 21:24:20 +00:00
public void CopyTo(Array array, int index)
{
2012-08-27 21:24:20 +00:00
Source.CopyTo(array, index);
}
2012-08-27 21:24:20 +00:00
public IEnumerator GetEnumerator()
{
2012-08-27 21:24:20 +00:00
return Source.GetEnumerator();
}
public abstract IEnumerable<TargetType> ReadConvertedContent<TargetType>();
2012-08-27 21:24:20 +00:00
#endregion
}
public class VertexChannel<T> : VertexChannel, IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable
2012-08-27 21:24:20 +00:00
{
private List<T> source;
2012-08-27 21:24:20 +00:00
#region Properties
public new T this[int index]
{
2012-08-27 21:24:20 +00:00
get { return source[index]; }
set { source[index] = value; }
}
2012-08-27 21:24:20 +00:00
bool ICollection<T>.IsReadOnly
{
2012-08-27 21:24:20 +00:00
get { return false; }
}
public override Type ElementType
{
get
{
return typeof(T);
}
}
2012-08-27 21:24:20 +00:00
#endregion
2012-08-27 21:24:20 +00:00
#region Constructor
public VertexChannel(string name)
: base(name)
{
2012-08-27 21:24:20 +00:00
source = new List<T>();
Source = source;
}
2012-08-27 21:24:20 +00:00
#endregion
2012-08-27 21:24:20 +00:00
#region Methods
void IList<T>.Insert(int index, T item)
{
2012-08-27 21:24:20 +00:00
throw new NotSupportedException("Size is fixed");
}
2012-08-27 21:24:20 +00:00
void IList<T>.RemoveAt(int index)
{
2012-08-27 21:24:20 +00:00
throw new NotSupportedException("Size is fixed");
}
2012-08-27 21:24:20 +00:00
void ICollection<T>.Add(T item)
{
2012-08-27 21:24:20 +00:00
throw new NotSupportedException("Size is fixed");
}
2012-08-27 21:24:20 +00:00
void ICollection<T>.Clear()
{
2012-08-27 21:24:20 +00:00
throw new NotSupportedException("Size is fixed");
}
bool ICollection<T>.Remove(T item)
{
2012-08-27 21:24:20 +00:00
throw new NotSupportedException("Size is fixed");
}
public int IndexOf(T item)
{
return source.IndexOf(item);
}
public int IndexOf(T item, int startIndex)
{
return source.IndexOf(item, startIndex);
}
2012-08-27 21:24:20 +00:00
public bool Contains(T item)
{
return source.Contains(item);
}
2012-08-27 21:24:20 +00:00
public void CopyTo(T[] array, int arrayIndex)
{
2012-08-27 21:24:20 +00:00
source.CopyTo(array, arrayIndex);
}
public new IEnumerator<T> GetEnumerator()
{
2012-08-27 21:24:20 +00:00
return source.GetEnumerator();
}
public override IEnumerable<TargetType> ReadConvertedContent<TargetType>()
{
Converter<T, TargetType> converter = VectorConverter.GetConverter<T, TargetType>();
foreach (T current in this.source)
{
yield return converter(current);
}
yield break;
}
2012-08-27 21:24:20 +00:00
#endregion
}
}