3 * Copyright (c) 2007-2010 SlimDX Group
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "../InternalHelpers.h"
25 #include "../math/Quaternion.h"
27 #include "QuaternionConverter.h"
28 #include "FieldPropertyDescriptor.h"
30 using namespace System;
31 using namespace System::Collections;
32 using namespace System::Drawing;
33 using namespace System::ComponentModel;
34 using namespace System::ComponentModel::Design::Serialization;
35 using namespace System::Globalization;
36 using namespace System::Reflection;
42 QuaternionConverter::QuaternionConverter()
44 Type^ type = Quaternion::typeid;
45 array<PropertyDescriptor^>^ propArray =
47 gcnew FieldPropertyDescriptor(type->GetField("X")),
48 gcnew FieldPropertyDescriptor(type->GetField("Y")),
49 gcnew FieldPropertyDescriptor(type->GetField("Z")),
50 gcnew FieldPropertyDescriptor(type->GetField("W")),
53 m_Properties = gcnew PropertyDescriptorCollection(propArray);
56 bool QuaternionConverter::CanConvertTo(ITypeDescriptorContext^ context, Type^ destinationType)
58 if( destinationType == String::typeid || destinationType == InstanceDescriptor::typeid )
61 return ExpandableObjectConverter::CanConvertTo(context, destinationType);
64 bool QuaternionConverter::CanConvertFrom(ITypeDescriptorContext^ context, Type^ sourceType)
66 if( sourceType == String::typeid )
69 return ExpandableObjectConverter::CanConvertFrom(context, sourceType);
72 Object^ QuaternionConverter::ConvertTo(ITypeDescriptorContext^ context, CultureInfo^ culture, Object^ value, Type^ destinationType)
74 if( destinationType == nullptr )
75 throw gcnew ArgumentNullException( "destinationType" );
77 if( culture == nullptr )
78 culture = CultureInfo::CurrentCulture;
80 Quaternion^ quaternion = dynamic_cast<Quaternion^>( value );
82 if( destinationType == String::typeid && quaternion != nullptr )
84 String^ separator = culture->TextInfo->ListSeparator + " ";
85 TypeConverter^ converter = TypeDescriptor::GetConverter(float::typeid);
86 array<String^>^ stringArray = gcnew array<String^>( 4 );
88 stringArray[0] = converter->ConvertToString( context, culture, quaternion->X );
89 stringArray[1] = converter->ConvertToString( context, culture, quaternion->Y );
90 stringArray[2] = converter->ConvertToString( context, culture, quaternion->Z );
91 stringArray[3] = converter->ConvertToString( context, culture, quaternion->W );
93 return String::Join( separator, stringArray );
95 else if( destinationType == InstanceDescriptor::typeid && quaternion != nullptr )
97 ConstructorInfo^ info = (Quaternion::typeid)->GetConstructor( gcnew array<Type^> { float::typeid, float::typeid, float::typeid, float::typeid } );
99 return gcnew InstanceDescriptor( info, gcnew array<Object^> { quaternion->X, quaternion->Y, quaternion->Z, quaternion->W } );
102 return ExpandableObjectConverter::ConvertTo(context, culture, value, destinationType);
105 Object^ QuaternionConverter::ConvertFrom(ITypeDescriptorContext^ context, CultureInfo^ culture, Object^ value)
107 if( culture == nullptr )
108 culture = CultureInfo::CurrentCulture;
110 String^ string = dynamic_cast<String^>( value );
112 if( string != nullptr )
114 string = string->Trim();
115 TypeConverter^ converter = TypeDescriptor::GetConverter(float::typeid);
116 array<String^>^ stringArray = string->Split( culture->TextInfo->ListSeparator[0] );
118 if( stringArray->Length != 4 )
119 throw gcnew ArgumentException("Invalid quaternion format.");
121 float X = safe_cast<float>( converter->ConvertFromString( context, culture, stringArray[0] ) );
122 float Y = safe_cast<float>( converter->ConvertFromString( context, culture, stringArray[1] ) );
123 float Z = safe_cast<float>( converter->ConvertFromString( context, culture, stringArray[2] ) );
124 float W = safe_cast<float>( converter->ConvertFromString( context, culture, stringArray[3] ) );
126 return gcnew Quaternion(X, Y, Z, W);
129 return ExpandableObjectConverter::ConvertFrom(context, culture, value);
132 bool QuaternionConverter::GetCreateInstanceSupported(ITypeDescriptorContext^ context)
134 SLIMDX_UNREFERENCED_PARAMETER(context);
139 Object^ QuaternionConverter::CreateInstance(ITypeDescriptorContext^ context, IDictionary^ propertyValues)
141 SLIMDX_UNREFERENCED_PARAMETER(context);
143 if( propertyValues == nullptr )
144 throw gcnew ArgumentNullException( "propertyValues" );
146 return gcnew Quaternion( safe_cast<float>( propertyValues["X"] ), safe_cast<float>( propertyValues["Y"] ),
147 safe_cast<float>( propertyValues["Z"] ), safe_cast<float>( propertyValues["W"] ) );
150 bool QuaternionConverter::GetPropertiesSupported(ITypeDescriptorContext^)
155 PropertyDescriptorCollection^ QuaternionConverter::GetProperties(ITypeDescriptorContext^, Object^, array<Attribute^>^)