using System; using System.Collections.Generic; using System.Text; using System.ComponentModel; using Microsoft.Xna.Framework; using System.Globalization; namespace Paralax { class Vector2Converter: ExpandableObjectConverter { public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { return sourceType == typeof(String); } public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { string sValue = value as string; object retVal = null; if (sValue != null) { sValue = sValue.Trim(); if ( sValue.Length != 0 ) { // Parse the string if ( null == culture ) culture = CultureInfo.CurrentCulture ; // Split the string based on the cultures list separator string[] parms = sValue.Split ( new char[] { culture.TextInfo.ListSeparator[0] } ) ; if ( parms.Length == 2 ) { // Should have an integer and a string. float x = Convert.ToSingle ( parms[0] ) ; float y = Convert.ToSingle(parms[1]); // And finally create the object retVal = new Vector2 ( x , y ) ; } } } else retVal = base.ConvertFrom ( context , culture , value ) ; return retVal ; } public override object CreateInstance(ITypeDescriptorContext context, System.Collections.IDictionary propertyValues) { return new Vector2((float) propertyValues["X"], (float) propertyValues["Y"]); } public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) { PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value, attributes); string[] sortOrder = new string[2]; sortOrder[0] = "X"; sortOrder[1] = "Y"; // Return a sorted list of properties return properties.Sort(sortOrder); } public override bool GetPropertiesSupported(ITypeDescriptorContext context) { return true; } } }