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
25 #include "../InternalHelpers.h"
\r
26 #include "../math/Half.h"
\r
27 #include "../math/Half3.h"
\r
29 #include "Half3Converter.h"
\r
30 #include "FieldPropertyDescriptor.h"
\r
32 using namespace System;
\r
33 using namespace System::Collections;
\r
34 using namespace System::Drawing;
\r
35 using namespace System::ComponentModel;
\r
36 using namespace System::ComponentModel::Design::Serialization;
\r
37 using namespace System::Globalization;
\r
38 using namespace System::Reflection;
\r
44 Half3Converter::Half3Converter()
\r
46 Type^ type = Half3::typeid;
\r
47 array<PropertyDescriptor^>^ propArray =
\r
49 gcnew FieldPropertyDescriptor(type->GetField("X")),
\r
50 gcnew FieldPropertyDescriptor(type->GetField("Y")),
\r
51 gcnew FieldPropertyDescriptor(type->GetField("Z")),
\r
54 m_Properties = gcnew PropertyDescriptorCollection(propArray);
\r
57 bool Half3Converter::CanConvertTo(ITypeDescriptorContext^ context, Type^ destinationType)
\r
59 if( destinationType == String::typeid || destinationType == InstanceDescriptor::typeid )
\r
62 return ExpandableObjectConverter::CanConvertTo(context, destinationType);
\r
65 bool Half3Converter::CanConvertFrom(ITypeDescriptorContext^ context, Type^ sourceType)
\r
67 if( sourceType == String::typeid )
\r
70 return ExpandableObjectConverter::CanConvertFrom(context, sourceType);
\r
73 Object^ Half3Converter::ConvertTo(ITypeDescriptorContext^ context, CultureInfo^ culture, Object^ value, Type^ destinationType)
\r
75 if( destinationType == nullptr )
\r
76 throw gcnew ArgumentNullException( "destinationType" );
\r
78 if( culture == nullptr )
\r
79 culture = CultureInfo::CurrentCulture;
\r
81 Half3^ half = dynamic_cast<Half3^>( value );
\r
83 if( destinationType == String::typeid && half != nullptr )
\r
85 String^ separator = culture->TextInfo->ListSeparator + " ";
\r
86 TypeConverter^ converter = TypeDescriptor::GetConverter(Half::typeid);
\r
87 array<String^>^ stringArray = gcnew array<String^>( 3 );
\r
89 stringArray[0] = converter->ConvertToString( context, culture, half->X );
\r
90 stringArray[1] = converter->ConvertToString( context, culture, half->Y );
\r
91 stringArray[2] = converter->ConvertToString( context, culture, half->Z );
\r
93 return String::Join( separator, stringArray );
\r
95 else if( destinationType == InstanceDescriptor::typeid && half != nullptr )
\r
97 ConstructorInfo^ info = (Half3::typeid)->GetConstructor( gcnew array<Type^> { Half::typeid, Half::typeid, Half::typeid } );
\r
98 if( info != nullptr )
\r
99 return gcnew InstanceDescriptor( info, gcnew array<Object^> { half->X, half->Y, half->Z } );
\r
102 return ExpandableObjectConverter::ConvertTo(context, culture, value, destinationType);
\r
105 Object^ Half3Converter::ConvertFrom(ITypeDescriptorContext^ context, CultureInfo^ culture, Object^ value)
\r
107 if( culture == nullptr )
\r
108 culture = CultureInfo::CurrentCulture;
\r
110 String^ string = dynamic_cast<String^>( value );
\r
112 if( string != nullptr )
\r
114 string = string->Trim();
\r
115 TypeConverter^ converter = TypeDescriptor::GetConverter(Half::typeid);
\r
116 array<String^>^ stringArray = string->Split( culture->TextInfo->ListSeparator[0] );
\r
118 if( stringArray->Length != 3 )
\r
119 throw gcnew ArgumentException("Invalid half format.");
\r
121 Half X = safe_cast<Half>( converter->ConvertFromString( context, culture, stringArray[0] ) );
\r
122 Half Y = safe_cast<Half>( converter->ConvertFromString( context, culture, stringArray[1] ) );
\r
123 Half Z = safe_cast<Half>( converter->ConvertFromString( context, culture, stringArray[2] ) );
\r
125 return gcnew Half3(X, Y, Z);
\r
128 return ExpandableObjectConverter::ConvertFrom(context, culture, value);
\r
131 bool Half3Converter::GetCreateInstanceSupported(ITypeDescriptorContext^ context)
\r
133 SLIMDX_UNREFERENCED_PARAMETER(context);
\r
138 Object^ Half3Converter::CreateInstance(ITypeDescriptorContext^ context, IDictionary^ propertyValues)
\r
140 SLIMDX_UNREFERENCED_PARAMETER(context);
\r
142 if( propertyValues == nullptr )
\r
143 throw gcnew ArgumentNullException( "propertyValues" );
\r
145 return gcnew Half3( safe_cast<Half>( propertyValues["X"] ), safe_cast<Half>( propertyValues["Y"] ),
\r
146 safe_cast<Half>( propertyValues["Z"] ) );
\r
149 bool Half3Converter::GetPropertiesSupported(ITypeDescriptorContext^)
\r
154 PropertyDescriptorCollection^ Half3Converter::GetProperties(ITypeDescriptorContext^, Object^, array<Attribute^>^)
\r
156 return m_Properties;
\r