97 lines
2.8 KiB
C#
97 lines
2.8 KiB
C#
using System;
|
|
using System.ComponentModel;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
|
|
namespace JD.Winforms.XmlGrid
|
|
{
|
|
[TypeConverter(typeof(Vector2VarConverter))]
|
|
class Vector2Var
|
|
{
|
|
private float x;
|
|
private float y;
|
|
|
|
public float X
|
|
{
|
|
get { return x; }
|
|
set { x = value; }
|
|
}
|
|
|
|
public float Y
|
|
{
|
|
get { return y; }
|
|
set { y = value; }
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return x.ToString() + " " + y.ToString();
|
|
}
|
|
}
|
|
|
|
public class Vector2VarConverter : ExpandableObjectConverter
|
|
{
|
|
public override bool CanConvertTo(ITypeDescriptorContext context,
|
|
System.Type destinationType)
|
|
{
|
|
if (destinationType == typeof(string))
|
|
return true;
|
|
|
|
return base.CanConvertTo(context, destinationType);
|
|
}
|
|
|
|
public override object ConvertTo(ITypeDescriptorContext context,
|
|
CultureInfo culture,
|
|
object value,
|
|
System.Type destinationType)
|
|
{
|
|
if (destinationType == typeof(System.String) && value is Vector2Var)
|
|
{
|
|
|
|
Vector2Var so = (Vector2Var)value;
|
|
return so.ToString();
|
|
}
|
|
|
|
return base.ConvertTo(context, culture, value, destinationType);
|
|
}
|
|
|
|
public override bool CanConvertFrom(ITypeDescriptorContext context,
|
|
System.Type sourceType)
|
|
{
|
|
if (sourceType == typeof(string))
|
|
return true;
|
|
|
|
return base.CanConvertFrom(context, sourceType);
|
|
}
|
|
|
|
public override object ConvertFrom(ITypeDescriptorContext context,
|
|
CultureInfo culture, object value)
|
|
{
|
|
if (value is string)
|
|
{
|
|
string val = (string)value;
|
|
string[] split = val.Split(' ');
|
|
Vector2Var ret = new Vector2Var();
|
|
|
|
if (split.Count() == 2)
|
|
{
|
|
CultureInfo ci = new CultureInfo(CultureInfo.CurrentCulture.Name, false);
|
|
NumberFormatInfo formatInfo = ci.NumberFormat;
|
|
formatInfo.NumberDecimalSeparator = ".";
|
|
formatInfo.NumberGroupSeparator = ",";
|
|
|
|
ret.X = float.Parse(split[0],formatInfo);
|
|
ret.Y = float.Parse(split[1],formatInfo);
|
|
}
|
|
else
|
|
{
|
|
ret.X = 0.0f;
|
|
ret.Y = 0.0f;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
return base.ConvertFrom(context, culture, value);
|
|
}
|
|
}
|
|
}
|