104 lines
4.6 KiB
C#
104 lines
4.6 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.ComponentModel;
|
|
using System.ComponentModel.Design.Serialization;
|
|
using System.Drawing;
|
|
using System.Globalization;
|
|
using System.Reflection;
|
|
|
|
namespace CoreUnit.Common
|
|
{
|
|
public class PointFConverter : TypeConverter
|
|
{
|
|
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
|
|
{
|
|
return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
|
|
}
|
|
|
|
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
|
|
{
|
|
return ((destinationType == typeof(InstanceDescriptor)) || base.CanConvertTo(context, destinationType));
|
|
}
|
|
|
|
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
|
|
{
|
|
string str = value as string;
|
|
if (value == null) return base.ConvertFrom(context, culture, value);
|
|
str = str.Trim();
|
|
if (str.Length == 0) return null;
|
|
if (culture == null) culture = CultureInfo.CurrentCulture;
|
|
char ch = culture.TextInfo.ListSeparator[0];
|
|
string[] strArray = str.Split(new char[] { ch });
|
|
float[] numArray = new float[strArray.Length];
|
|
TypeConverter converter = TypeDescriptor.GetConverter(typeof(float));
|
|
bool result = false;
|
|
for (int i = 0; i < numArray.Length; i++) {
|
|
result = float.TryParse(strArray[i], out numArray[i]);
|
|
//numArray[i] = (float)converter.ConvertFromString(context, culture, strArray[i]);
|
|
}
|
|
if (numArray.Length < 2) {
|
|
return new PointF((float)Math.Round(numArray[0], 3), (float)Math.Round(0.0, 3));
|
|
//throw new ArgumentException("Invalid format");
|
|
}
|
|
return new PointF((float)Math.Round(numArray[0], 3), (float)Math.Round(numArray[1], 3));
|
|
}
|
|
|
|
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
|
|
{
|
|
if (destinationType == null) throw new ArgumentNullException("destinationType");
|
|
|
|
|
|
if (value is PointF) {
|
|
if (destinationType == typeof(string)) {
|
|
PointF point = (PointF)value;
|
|
if (culture == null) culture = CultureInfo.CurrentCulture;
|
|
string separator = culture.TextInfo.ListSeparator + " ";
|
|
TypeConverter converter = TypeDescriptor.GetConverter(typeof(float));
|
|
string[] strArray = new string[2];
|
|
int num = 0;
|
|
strArray[num++] = converter.ConvertToString(context, culture, Math.Round(point.X, 3));
|
|
strArray[num++] = converter.ConvertToString(context, culture, Math.Round(point.Y, 3));
|
|
|
|
return string.Join(separator, strArray);
|
|
|
|
|
|
}
|
|
if (destinationType == typeof(InstanceDescriptor)) {
|
|
PointF point2 = (PointF)value;
|
|
ConstructorInfo constructor = typeof(PointF).GetConstructor(new Type[] { typeof(float), typeof(float) });
|
|
if (constructor != null) return new InstanceDescriptor(constructor, new object[] { Math.Round(point2.X, 3), Math.Round(point2.Y, 3) });
|
|
}
|
|
}
|
|
|
|
return base.ConvertTo(context, culture, value, destinationType);
|
|
}
|
|
|
|
|
|
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
|
|
{
|
|
if (propertyValues == null) throw new ArgumentNullException("propertyValues");
|
|
object xvalue = propertyValues["X"];
|
|
object yvalue = propertyValues["Y"];
|
|
if (((xvalue == null) || (yvalue == null)) || (!(xvalue is float) || !(yvalue is float))) {
|
|
throw new ArgumentException("Invalid property value entry");
|
|
}
|
|
return new PointF((float)Math.Round((float)xvalue, 3), (float)Math.Round((float)yvalue, 3));
|
|
}
|
|
|
|
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
|
|
{
|
|
return TypeDescriptor.GetProperties(typeof(PointF), attributes).Sort(new string[] { "X", "Y" });
|
|
}
|
|
|
|
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
}
|