- Implemented all math type converters

This commit is contained in:
SND\GinieDp_cp 2011-11-15 22:31:20 +00:00
parent a36695b15b
commit f5367ebfbc
16 changed files with 958 additions and 1 deletions

View File

@ -130,6 +130,21 @@
<Compile Include="CurveKeyCollection.cs" />
<Compile Include="CurveLoopType.cs" />
<Compile Include="CurveTangent.cs" />
<Compile Include="Design\ANXFieldDescriptor.cs" />
<Compile Include="Design\ANXPropertyDescriptor.cs" />
<Compile Include="Design\BoundingBox.cs" />
<Compile Include="Design\BoundingSphereConverter.cs" />
<Compile Include="Design\ColorConverter.cs" />
<Compile Include="Design\MathTypeConverter.cs" />
<Compile Include="Design\MatrixConverter.cs" />
<Compile Include="Design\PlaneConverter.cs" />
<Compile Include="Design\PointConverter.cs" />
<Compile Include="Design\QuaternionConverter.cs" />
<Compile Include="Design\RayConverter.cs" />
<Compile Include="Design\RectangleConverter.cs" />
<Compile Include="Design\Vector2Converter.cs" />
<Compile Include="Design\Vector3Converter.cs" />
<Compile Include="Design\Vector4Converter.cs" />
<Compile Include="DisplayOrientation.cs" />
<Compile Include="DrawableGameComponent.cs" />
<Compile Include="Game.cs" />
@ -358,7 +373,6 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Content\MediaTypeReaders\" />
<Folder Include="Design\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@ -0,0 +1,66 @@
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
namespace ANX.Framework.Design
{
public class ANXFieldDescriptor : PropertyDescriptor
{
private FieldInfo field;
public ANXFieldDescriptor(FieldInfo field)
: base(field.Name, (Attribute[])field.GetCustomAttributes(typeof(Attribute), true))
{
if (field == null)
{
throw new ArgumentNullException("field");
}
this.field = field;
}
public override bool CanResetValue(object component)
{
return false;
}
public override Type ComponentType
{
get { return field.DeclaringType; }
}
public override object GetValue(object component)
{
return field.GetValue(component);
}
public override bool IsReadOnly
{
get { return false; }
}
public override Type PropertyType
{
get { return field.FieldType; }
}
public override void ResetValue(object component)
{
}
public override void SetValue(object component, object value)
{
this.field.SetValue(component, value);
this.OnValueChanged(component, EventArgs.Empty);
}
public override bool ShouldSerializeValue(object component)
{
return true;
}
}
}

View File

@ -0,0 +1,66 @@
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
namespace ANX.Framework.Design
{
public class ANXPropertyDescriptor : PropertyDescriptor
{
private PropertyInfo property;
public ANXPropertyDescriptor(PropertyInfo property)
: base(property.Name, (Attribute[])property.GetCustomAttributes(typeof(Attribute), true))
{
if (property == null)
{
throw new ArgumentNullException("property");
}
this.property = property;
}
public override bool CanResetValue(object component)
{
return false;
}
public override Type ComponentType
{
get { return property.DeclaringType; }
}
public override object GetValue(object component)
{
return property.GetValue(component, null);
}
public override bool IsReadOnly
{
get { return false; }
}
public override Type PropertyType
{
get { return property.PropertyType; }
}
public override void ResetValue(object component)
{
}
public override void SetValue(object component, object value)
{
this.property.SetValue(component, value, null);
this.OnValueChanged(component, EventArgs.Empty);
}
public override bool ShouldSerializeValue(object component)
{
return true;
}
}
}

View File

@ -0,0 +1,53 @@
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
namespace ANX.Framework.Design
{
public class BoundingBoxConverter : MathTypeConverter
{
public BoundingBoxConverter()
{
base.propertyDescriptions = MathTypeConverter.CreateFieldDescriptor<BoundingBox>("Min", "Max");
supportStringConvert = false;
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if (value is BoundingBox)
{
BoundingBox instance = (BoundingBox)value;
if (destinationType == typeof(InstanceDescriptor))
{
var constructor = typeof(BoundingBox).GetConstructor(new Type[] { typeof(Vector3), typeof(Vector3) });
return new InstanceDescriptor(constructor, new object[] { instance.Min, instance.Max });
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
{
if (propertyValues == null)
{
throw new ArgumentNullException("propertyValues");
}
return new BoundingBox((Vector3)propertyValues["Min"], (Vector3)propertyValues["Max"]);
}
}
}

View File

@ -0,0 +1,53 @@
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
namespace ANX.Framework.Design
{
public class BoundingSphereConverter : MathTypeConverter
{
public BoundingSphereConverter()
{
base.propertyDescriptions = MathTypeConverter.CreateFieldDescriptor<BoundingSphere>("Center", "Radius");
supportStringConvert = false;
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if (value is BoundingSphere)
{
BoundingSphere instance = (BoundingSphere)value;
if (destinationType == typeof(InstanceDescriptor))
{
var constructor = typeof(BoundingSphere).GetConstructor(new Type[] { typeof(Vector3), typeof(float) });
return new InstanceDescriptor(constructor, new object[] { instance.Center, instance.Radius });
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
{
if (propertyValues == null)
{
throw new ArgumentNullException("propertyValues");
}
return new BoundingSphere((Vector3)propertyValues["Center"], (float)propertyValues["Radius"]);
}
}
}

View File

@ -0,0 +1,61 @@
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
namespace ANX.Framework.Design
{
public class ColorConverter : MathTypeConverter
{
public ColorConverter()
{
base.propertyDescriptions = MathTypeConverter.CreateFieldDescriptor<Color>("R", "G", "B", "A");
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
byte[] values = MathTypeConverter.ConvertFromString<byte>(context, culture, value as String);
if (values != null && values.Length == 4)
{
return new Color(values[0], values[1], values[2], values[3]);
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if (value is Color)
{
Color instance = (Color)value;
if (destinationType == typeof(string))
{
return MathTypeConverter.ConvertToString<float>(context, culture, new float[] { instance.R, instance.G, instance.B, instance.A });
}
if (destinationType == typeof(InstanceDescriptor))
{
var constructor = typeof(Color).GetConstructor(new Type[] { typeof(float), typeof(float), typeof(float), typeof(float) });
return new InstanceDescriptor(constructor, new object[] { instance.R, instance.G, instance.B, instance.A });
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
{
if (propertyValues == null)
{
throw new ArgumentNullException("propertyValues");
}
return new Color((float)propertyValues["R"], (float)propertyValues["G"], (float)propertyValues["B"], (float)propertyValues["A"]);
}
}
}

View File

@ -0,0 +1,106 @@
using System;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
namespace ANX.Framework.Design
{
public class MathTypeConverter : ExpandableObjectConverter
{
protected PropertyDescriptorCollection propertyDescriptions;
protected bool supportStringConvert = true;
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (this.supportStringConvert && sourceType == typeof(String))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(InstanceDescriptor))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return true;
}
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
return propertyDescriptions;
}
protected static PropertyDescriptorCollection CreateFieldDescriptor<T>(params string[] fields)
{
Type type = typeof(T);
PropertyDescriptor[] descriptor = new PropertyDescriptor[fields.Length];
for (int i = 0; i < fields.Length; i++)
{
descriptor[i] = new ANXFieldDescriptor(type.GetField(fields[i]));
}
return new PropertyDescriptorCollection(descriptor);
}
protected static PropertyDescriptorCollection CreatePropertyDescriptor<T>(params string[] fields)
{
Type type = typeof(T);
PropertyDescriptor[] descriptor = new PropertyDescriptor[fields.Length];
for (int i = 0; i < fields.Length; i++)
{
descriptor[i] = new ANXPropertyDescriptor(type.GetProperty(fields[i]));
}
return new PropertyDescriptorCollection(descriptor);
}
protected static string ConvertToString<T>(ITypeDescriptorContext context, CultureInfo culture, T[] values)
{
string separator = "; ";
TypeConverter converter = TypeDescriptor.GetConverter(typeof(T));
StringBuilder builder = new StringBuilder();
foreach (var item in values)
{
builder.Append(converter.ConvertToString(context, culture, item));
builder.Append(separator);
}
return builder.ToString();
}
protected static T[] ConvertFromString<T>(ITypeDescriptorContext context, CultureInfo culture, string value)
{
if (value == null)
{
return null;
}
if (culture == null)
{
throw new ArgumentNullException("culture");
}
value = value.Trim();
string[] values = value.Split(';');
T[] result = new T[values.Length];
TypeConverter converter = TypeDescriptor.GetConverter(typeof(T));
for (int i = 0; i < values.Length; i++)
{
result[i] = (T)converter.ConvertFromString(context, culture, values[i]);
}
return result;
}
}
}

View File

@ -0,0 +1,71 @@
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
namespace ANX.Framework.Design
{
public class MatrixConverter : MathTypeConverter
{
public MatrixConverter()
{
base.propertyDescriptions = MathTypeConverter.CreateFieldDescriptor<Matrix>(
"M11", "M12", "M13", "M14",
"M21", "M22", "M23", "M24",
"M31", "M32", "M33", "M34",
"M41", "M42", "M43", "M44");
supportStringConvert = false;
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if (value is Matrix)
{
Matrix instance = (Matrix)value;
if (destinationType == typeof(InstanceDescriptor))
{
var constructor = typeof(Matrix).GetConstructor(new Type[] {
typeof(float), typeof(float), typeof(float), typeof(float),
typeof(float), typeof(float), typeof(float), typeof(float),
typeof(float), typeof(float), typeof(float), typeof(float),
typeof(float), typeof(float), typeof(float), typeof(float)
});
return new InstanceDescriptor(constructor, new object[] {
instance.M11, instance.M12, instance.M13, instance.M14,
instance.M21, instance.M22, instance.M23, instance.M24,
instance.M31, instance.M32, instance.M33, instance.M34,
instance.M41, instance.M42, instance.M43, instance.M44
});
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
{
if (propertyValues == null)
{
throw new ArgumentNullException("propertyValues");
}
return new Matrix(
(float)propertyValues["M11"], (float)propertyValues["M12"], (float)propertyValues["M13"], (float)propertyValues["M14"],
(float)propertyValues["M21"], (float)propertyValues["M22"], (float)propertyValues["M23"], (float)propertyValues["M24"],
(float)propertyValues["M31"], (float)propertyValues["M32"], (float)propertyValues["M33"], (float)propertyValues["M34"],
(float)propertyValues["M41"], (float)propertyValues["M42"], (float)propertyValues["M43"], (float)propertyValues["M44"]);
}
}
}

View File

@ -0,0 +1,48 @@
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
namespace ANX.Framework.Design
{
public class PlaneConverter : MathTypeConverter
{
public PlaneConverter()
{
base.propertyDescriptions = MathTypeConverter.CreateFieldDescriptor<Plane>("Normal", "D");
supportStringConvert = false;
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if (value is Plane)
{
Plane instance = (Plane)value;
if (destinationType == typeof(InstanceDescriptor))
{
var constructor = typeof(Plane).GetConstructor(new Type[] { typeof(Vector3), typeof(float) });
return new InstanceDescriptor(constructor, new object[] { instance.Normal, instance.D });
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
{
if (propertyValues == null)
{
throw new ArgumentNullException("propertyValues");
}
return new Vector4((Vector3)propertyValues["Normal"], (float)propertyValues["D"]);
}
}
}

View File

@ -0,0 +1,61 @@
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
namespace ANX.Framework.Design
{
public class PointConverter : MathTypeConverter
{
public PointConverter()
{
base.propertyDescriptions = MathTypeConverter.CreateFieldDescriptor<Point>("X", "Y");
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
int[] values = MathTypeConverter.ConvertFromString<int>(context, culture, value as String);
if (values != null && values.Length == 2)
{
return new Point(values[0], values[1]);
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if (value is Point)
{
Point instance = (Point)value;
if (destinationType == typeof(string))
{
return MathTypeConverter.ConvertToString<int>(context, culture, new int[] { instance.X, instance.Y });
}
if (destinationType == typeof(InstanceDescriptor))
{
var constructor = typeof(Point).GetConstructor(new Type[] { typeof(int), typeof(int) });
return new InstanceDescriptor(constructor, new object[] { instance.X, instance.Y });
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
{
if (propertyValues == null)
{
throw new ArgumentNullException("propertyValues");
}
return new Point((int)propertyValues["X"], (int)propertyValues["Y"]);
}
}
}

View File

@ -0,0 +1,61 @@
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
namespace ANX.Framework.Design
{
public class QuaternionConverter : MathTypeConverter
{
public QuaternionConverter()
{
base.propertyDescriptions = MathTypeConverter.CreateFieldDescriptor<Quaternion>("X", "Y", "Z", "W");
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
float[] values = MathTypeConverter.ConvertFromString<float>(context, culture, value as String);
if (values != null && values.Length == 4)
{
return new Quaternion(values[0], values[1], values[2], values[3]);
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if (value is Quaternion)
{
Quaternion instance = (Quaternion)value;
if (destinationType == typeof(string))
{
return MathTypeConverter.ConvertToString<float>(context, culture, new float[] { instance.X, instance.Y, instance.Z, instance.W });
}
if (destinationType == typeof(InstanceDescriptor))
{
var constructor = typeof(Quaternion).GetConstructor(new Type[] { typeof(float), typeof(float), typeof(float), typeof(float) });
return new InstanceDescriptor(constructor, new object[] { instance.X, instance.Y, instance.Z, instance.W });
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
{
if (propertyValues == null)
{
throw new ArgumentNullException("propertyValues");
}
return new Quaternion((float)propertyValues["X"], (float)propertyValues["Y"], (float)propertyValues["Z"], (float)propertyValues["W"]);
}
}
}

View File

@ -0,0 +1,53 @@
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
namespace ANX.Framework.Design
{
public class RayConverter : MathTypeConverter
{
public RayConverter()
{
base.propertyDescriptions = MathTypeConverter.CreateFieldDescriptor<Ray>("Position", "Direction");
supportStringConvert = false;
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if (value is Ray)
{
Ray instance = (Ray)value;
if (destinationType == typeof(InstanceDescriptor))
{
var constructor = typeof(Ray).GetConstructor(new Type[] { typeof(Vector3), typeof(Vector3) });
return new InstanceDescriptor(constructor, new object[] { instance.Position, instance.Direction });
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
{
if (propertyValues == null)
{
throw new ArgumentNullException("propertyValues");
}
return new Ray((Vector3)propertyValues["Position"], (Vector3)propertyValues["Direction"]);
}
}
}

View File

@ -0,0 +1,61 @@
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
namespace ANX.Framework.Design
{
public class RectangleConverter : MathTypeConverter
{
public RectangleConverter()
{
base.propertyDescriptions = MathTypeConverter.CreateFieldDescriptor<Rectangle>("X", "Y", "Width", "Height");
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
int[] values = MathTypeConverter.ConvertFromString<int>(context, culture, value as String);
if (values != null && values.Length == 4)
{
return new Rectangle(values[0], values[1], values[2], values[3]);
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if (value is Rectangle)
{
Rectangle instance = (Rectangle)value;
if (destinationType == typeof(string))
{
return MathTypeConverter.ConvertToString<int>(context, culture, new int[] { instance.X, instance.Y, instance.Width, instance.Height });
}
if (destinationType == typeof(InstanceDescriptor))
{
var constructor = typeof(Rectangle).GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int), typeof(int) });
return new InstanceDescriptor(constructor, new object[] { instance.X, instance.Y, instance.Width, instance.Height });
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
{
if (propertyValues == null)
{
throw new ArgumentNullException("propertyValues");
}
return new Rectangle((int)propertyValues["X"], (int)propertyValues["Y"], (int)propertyValues["Width"], (int)propertyValues["Height"]);
}
}
}

View File

@ -0,0 +1,61 @@
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
namespace ANX.Framework.Design
{
public class Vector2Converter : MathTypeConverter
{
public Vector2Converter()
{
base.propertyDescriptions = MathTypeConverter.CreateFieldDescriptor<Vector2>("X", "Y");
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
float[] values = MathTypeConverter.ConvertFromString<float>(context, culture, value as String);
if (values != null && values.Length == 2)
{
return new Vector2(values[0], values[1]);
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if (value is Vector2)
{
Vector2 vecValue = (Vector2)value;
if (destinationType == typeof(string))
{
return MathTypeConverter.ConvertToString<float>(context, culture, new float[] { vecValue.X, vecValue.Y });
}
if (destinationType == typeof(InstanceDescriptor))
{
var constructor = typeof(Vector2).GetConstructor(new Type[] { typeof(float), typeof(float) });
return new InstanceDescriptor(constructor, new object[] { vecValue.X, vecValue.Y });
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
{
if (propertyValues == null)
{
throw new ArgumentNullException("propertyValues");
}
return new Vector2((float)propertyValues["X"], (float)propertyValues["Y"]);
}
}
}

View File

@ -0,0 +1,61 @@
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
namespace ANX.Framework.Design
{
public class Vector3Converter : MathTypeConverter
{
public Vector3Converter()
{
base.propertyDescriptions = MathTypeConverter.CreateFieldDescriptor<Vector3>("X", "Y", "Z");
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
float[] values = MathTypeConverter.ConvertFromString<float>(context, culture, value as String);
if (values != null && values.Length == 3)
{
return new Vector3(values[0], values[1], values[2]);
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if (value is Vector3)
{
Vector3 vecValue = (Vector3)value;
if (destinationType == typeof(string))
{
return MathTypeConverter.ConvertToString<float>(context, culture, new float[] { vecValue.X, vecValue.Y, vecValue.Z });
}
if (destinationType == typeof(InstanceDescriptor))
{
var constructor = typeof(Vector3).GetConstructor(new Type[] { typeof(float), typeof(float), typeof(float) });
return new InstanceDescriptor(constructor, new object[] { vecValue.X, vecValue.Y, vecValue.Z });
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
{
if (propertyValues == null)
{
throw new ArgumentNullException("propertyValues");
}
return new Vector3((float)propertyValues["X"], (float)propertyValues["Y"], (float)propertyValues["Z"]);
}
}
}

View File

@ -0,0 +1,61 @@
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
namespace ANX.Framework.Design
{
public class Vector4Converter : MathTypeConverter
{
public Vector4Converter()
{
base.propertyDescriptions = MathTypeConverter.CreateFieldDescriptor<Vector4>("X", "Y", "Z", "W");
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
float[] values = MathTypeConverter.ConvertFromString<float>(context, culture, value as String);
if (values != null && values.Length == 4)
{
return new Vector4(values[0], values[1], values[2], values[3]);
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if (value is Vector4)
{
Vector4 instance = (Vector4)value;
if (destinationType == typeof(string))
{
return MathTypeConverter.ConvertToString<float>(context, culture, new float[] { instance.X, instance.Y, instance.Z, instance.W });
}
if (destinationType == typeof(InstanceDescriptor))
{
var constructor = typeof(Vector4).GetConstructor(new Type[] { typeof(float), typeof(float), typeof(float), typeof(float) });
return new InstanceDescriptor(constructor, new object[] { instance.X, instance.Y, instance.Z, instance.W });
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
{
if (propertyValues == null)
{
throw new ArgumentNullException("propertyValues");
}
return new Vector4((float)propertyValues["X"], (float)propertyValues["Y"], (float)propertyValues["Z"], (float)propertyValues["W"]);
}
}
}