-
Notifications
You must be signed in to change notification settings - Fork 45
/
VectorConverters.cs
156 lines (124 loc) · 6.19 KB
/
VectorConverters.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Numerics;
using System.Text.RegularExpressions;
namespace DirectX12GameEngine.Core.Assets
{
[GlobalTypeConverter(typeof(Vector3))]
public class Vector3Converter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) => sourceType == typeof(string);
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) => destinationType == typeof(string);
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string text)
{
float[] vector = Regex.Replace(text, @"\s+", "").Split(',').Select(n => float.Parse(n)).ToArray();
if (vector.Length == 3)
{
return new Vector3(vector[0], vector[1], vector[2]);
}
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string) && value is Vector3 vector)
{
return $"{vector.X},{vector.Y},{vector.Z}";
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override bool IsValid(ITypeDescriptorContext context, object value) => value is Vector3;
}
[GlobalTypeConverter(typeof(Vector4))]
public class Vector4Converter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) => sourceType == typeof(string);
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) => destinationType == typeof(string);
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string text)
{
float[] vector = Regex.Replace(text, @"\s+", "").Split(',').Select(n => float.Parse(n)).ToArray();
if (vector.Length == 4)
{
return new Vector4(vector[0], vector[1], vector[2], vector[3]);
}
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string) && value is Vector4 vector)
{
return $"{vector.X},{vector.Y},{vector.Z},{vector.W}";
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override bool IsValid(ITypeDescriptorContext context, object value) => value is Vector4;
}
[GlobalTypeConverter(typeof(Quaternion))]
public class QuaternionConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) => sourceType == typeof(string);
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) => destinationType == typeof(string);
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string text)
{
float[] vector = Regex.Replace(text, @"\s+", "").Split(',').Select(n => float.Parse(n)).ToArray();
if (vector.Length == 4)
{
return new Quaternion(vector[0], vector[1], vector[2], vector[3]);
}
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string) && value is Quaternion vector)
{
return $"{vector.X},{vector.Y},{vector.Z},{vector.W}";
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override bool IsValid(ITypeDescriptorContext context, object value) => value is Quaternion;
}
[GlobalTypeConverter(typeof(Matrix4x4))]
public class Matrix4x4Converter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) => sourceType == typeof(string);
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) => destinationType == typeof(string);
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string text)
{
float[] matrix = Regex.Replace(text, @"\s+", "").Split(',').Select(n => float.Parse(n)).ToArray();
if (matrix.Length == 16)
{
return new Matrix4x4(
matrix[0], matrix[1], matrix[2], matrix[3],
matrix[4], matrix[5], matrix[6], matrix[7],
matrix[8], matrix[9], matrix[10], matrix[11],
matrix[12], matrix[13], matrix[14], matrix[15]);
}
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string) && value is Matrix4x4 matrix)
{
return $"{matrix.M11},{matrix.M12},{matrix.M13},{matrix.M14},"
+ $"{matrix.M21},{matrix.M22},{matrix.M23},{matrix.M24},"
+ $"{matrix.M31},{matrix.M32},{matrix.M33},{matrix.M34},"
+ $"{matrix.M41},{matrix.M42},{matrix.M43},{matrix.M44}";
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override bool IsValid(ITypeDescriptorContext context, object value) => value is Matrix4x4;
}
}