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/Ray.h"
27 #include "RayConverter.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 RayConverter::RayConverter()
44 Type^ type = Ray::typeid;
45 array<PropertyDescriptor^>^ propArray =
47 gcnew FieldPropertyDescriptor(type->GetField("Position")),
48 gcnew FieldPropertyDescriptor(type->GetField("Direction")),
51 m_Properties = gcnew PropertyDescriptorCollection(propArray);
54 bool RayConverter::CanConvertTo(ITypeDescriptorContext^ context, Type^ destinationType)
56 if( destinationType == String::typeid || destinationType == InstanceDescriptor::typeid )
59 return ExpandableObjectConverter::CanConvertTo(context, destinationType);
62 bool RayConverter::CanConvertFrom(ITypeDescriptorContext^ context, Type^ sourceType)
64 if( sourceType == String::typeid )
67 return ExpandableObjectConverter::CanConvertFrom(context, sourceType);
70 Object^ RayConverter::ConvertTo(ITypeDescriptorContext^ context, CultureInfo^ culture, Object^ value, Type^ destinationType)
72 if( destinationType == nullptr )
73 throw gcnew ArgumentNullException( "destinationType" );
75 if( culture == nullptr )
76 culture = CultureInfo::CurrentCulture;
78 Ray^ ray = dynamic_cast<Ray^>( value );
80 if( destinationType == String::typeid && ray != nullptr )
82 String^ separator = culture->TextInfo->ListSeparator + " ";
83 TypeConverter^ converter = TypeDescriptor::GetConverter(Vector3::typeid);
84 array<String^>^ stringArray = gcnew array<String^>( 2 );
86 stringArray[0] = converter->ConvertToString( context, culture, ray->Position );
87 stringArray[1] = converter->ConvertToString( context, culture, ray->Direction );
89 return String::Join( separator, stringArray );
91 else if( destinationType == InstanceDescriptor::typeid && ray != nullptr )
93 ConstructorInfo^ info = (Ray::typeid)->GetConstructor( gcnew array<Type^> { Vector3::typeid, Vector3::typeid } );
95 return gcnew InstanceDescriptor( info, gcnew array<Object^> { ray->Position, ray->Direction } );
98 return ExpandableObjectConverter::ConvertTo(context, culture, value, destinationType);
101 Object^ RayConverter::ConvertFrom(ITypeDescriptorContext^ context, CultureInfo^ culture, Object^ value)
103 if( culture == nullptr )
104 culture = CultureInfo::CurrentCulture;
106 String^ string = dynamic_cast<String^>( value );
108 if( string != nullptr )
110 string = string->Trim();
111 TypeConverter^ converter = TypeDescriptor::GetConverter(Vector3::typeid);
112 array<String^>^ stringArray = string->Split( culture->TextInfo->ListSeparator[0] );
114 if( stringArray->Length != 2 )
115 throw gcnew ArgumentException("Invalid ray format.");
117 Vector3 Position = safe_cast<Vector3>( converter->ConvertFromString( context, culture, stringArray[0] ) );
118 Vector3 Direction = safe_cast<Vector3>( converter->ConvertFromString( context, culture, stringArray[1] ) );
120 return gcnew Ray(Position, Direction);
123 return ExpandableObjectConverter::ConvertFrom(context, culture, value);
126 bool RayConverter::GetCreateInstanceSupported(ITypeDescriptorContext^ context)
128 SLIMDX_UNREFERENCED_PARAMETER(context);
133 Object^ RayConverter::CreateInstance(ITypeDescriptorContext^ context, IDictionary^ propertyValues)
135 SLIMDX_UNREFERENCED_PARAMETER(context);
137 if( propertyValues == nullptr )
138 throw gcnew ArgumentNullException( "propertyValues" );
140 return gcnew Ray( safe_cast<Vector3>( propertyValues["Position"] ), safe_cast<Vector3>( propertyValues["Direction"] ) );
143 bool RayConverter::GetPropertiesSupported(ITypeDescriptorContext^)
148 PropertyDescriptorCollection^ RayConverter::GetProperties(ITypeDescriptorContext^, Object^, array<Attribute^>^)