3 * Copyright (c) 2007-2010 SlimDX Group
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
\r
6 * of this software and associated documentation files (the "Software"), to deal
\r
7 * in the Software without restriction, including without limitation the rights
\r
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
9 * copies of the Software, and to permit persons to whom the Software is
\r
10 * furnished to do so, subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in
\r
13 * all copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
24 #include "../InternalHelpers.h"
\r
25 #include "../math/Ray.h"
\r
27 #include "RayConverter.h"
\r
28 #include "FieldPropertyDescriptor.h"
\r
30 using namespace System;
\r
31 using namespace System::Collections;
\r
32 using namespace System::Drawing;
\r
33 using namespace System::ComponentModel;
\r
34 using namespace System::ComponentModel::Design::Serialization;
\r
35 using namespace System::Globalization;
\r
36 using namespace System::Reflection;
\r
42 RayConverter::RayConverter()
\r
44 Type^ type = Ray::typeid;
\r
45 array<PropertyDescriptor^>^ propArray =
\r
47 gcnew FieldPropertyDescriptor(type->GetField("Position")),
\r
48 gcnew FieldPropertyDescriptor(type->GetField("Direction")),
\r
51 m_Properties = gcnew PropertyDescriptorCollection(propArray);
\r
54 bool RayConverter::CanConvertTo(ITypeDescriptorContext^ context, Type^ destinationType)
\r
56 if( destinationType == String::typeid || destinationType == InstanceDescriptor::typeid )
\r
59 return ExpandableObjectConverter::CanConvertTo(context, destinationType);
\r
62 bool RayConverter::CanConvertFrom(ITypeDescriptorContext^ context, Type^ sourceType)
\r
64 if( sourceType == String::typeid )
\r
67 return ExpandableObjectConverter::CanConvertFrom(context, sourceType);
\r
70 Object^ RayConverter::ConvertTo(ITypeDescriptorContext^ context, CultureInfo^ culture, Object^ value, Type^ destinationType)
\r
72 if( destinationType == nullptr )
\r
73 throw gcnew ArgumentNullException( "destinationType" );
\r
75 if( culture == nullptr )
\r
76 culture = CultureInfo::CurrentCulture;
\r
78 Ray^ ray = dynamic_cast<Ray^>( value );
\r
80 if( destinationType == String::typeid && ray != nullptr )
\r
82 String^ separator = culture->TextInfo->ListSeparator + " ";
\r
83 TypeConverter^ converter = TypeDescriptor::GetConverter(Vector3::typeid);
\r
84 array<String^>^ stringArray = gcnew array<String^>( 2 );
\r
86 stringArray[0] = converter->ConvertToString( context, culture, ray->Position );
\r
87 stringArray[1] = converter->ConvertToString( context, culture, ray->Direction );
\r
89 return String::Join( separator, stringArray );
\r
91 else if( destinationType == InstanceDescriptor::typeid && ray != nullptr )
\r
93 ConstructorInfo^ info = (Ray::typeid)->GetConstructor( gcnew array<Type^> { Vector3::typeid, Vector3::typeid } );
\r
94 if( info != nullptr )
\r
95 return gcnew InstanceDescriptor( info, gcnew array<Object^> { ray->Position, ray->Direction } );
\r
98 return ExpandableObjectConverter::ConvertTo(context, culture, value, destinationType);
\r
101 Object^ RayConverter::ConvertFrom(ITypeDescriptorContext^ context, CultureInfo^ culture, Object^ value)
\r
103 if( culture == nullptr )
\r
104 culture = CultureInfo::CurrentCulture;
\r
106 String^ string = dynamic_cast<String^>( value );
\r
108 if( string != nullptr )
\r
110 string = string->Trim();
\r
111 TypeConverter^ converter = TypeDescriptor::GetConverter(Vector3::typeid);
\r
112 array<String^>^ stringArray = string->Split( culture->TextInfo->ListSeparator[0] );
\r
114 if( stringArray->Length != 2 )
\r
115 throw gcnew ArgumentException("Invalid ray format.");
\r
117 Vector3 Position = safe_cast<Vector3>( converter->ConvertFromString( context, culture, stringArray[0] ) );
\r
118 Vector3 Direction = safe_cast<Vector3>( converter->ConvertFromString( context, culture, stringArray[1] ) );
\r
120 return gcnew Ray(Position, Direction);
\r
123 return ExpandableObjectConverter::ConvertFrom(context, culture, value);
\r
126 bool RayConverter::GetCreateInstanceSupported(ITypeDescriptorContext^ context)
\r
128 SLIMDX_UNREFERENCED_PARAMETER(context);
\r
133 Object^ RayConverter::CreateInstance(ITypeDescriptorContext^ context, IDictionary^ propertyValues)
\r
135 SLIMDX_UNREFERENCED_PARAMETER(context);
\r
137 if( propertyValues == nullptr )
\r
138 throw gcnew ArgumentNullException( "propertyValues" );
\r
140 return gcnew Ray( safe_cast<Vector3>( propertyValues["Position"] ), safe_cast<Vector3>( propertyValues["Direction"] ) );
\r
143 bool RayConverter::GetPropertiesSupported(ITypeDescriptorContext^)
\r
148 PropertyDescriptorCollection^ RayConverter::GetProperties(ITypeDescriptorContext^, Object^, array<Attribute^>^)
\r
150 return m_Properties;
\r