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/Half4.h"
\r
29 #include "Half4Converter.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 Half4Converter::Half4Converter()
\r
46 Type^ type = Half4::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
52 gcnew FieldPropertyDescriptor(type->GetField("W")),
\r
55 m_Properties = gcnew PropertyDescriptorCollection(propArray);
\r
58 bool Half4Converter::CanConvertTo(ITypeDescriptorContext^ context, Type^ destinationType)
\r
60 if( destinationType == String::typeid || destinationType == InstanceDescriptor::typeid )
\r
63 return ExpandableObjectConverter::CanConvertTo(context, destinationType);
\r
66 bool Half4Converter::CanConvertFrom(ITypeDescriptorContext^ context, Type^ sourceType)
\r
68 if( sourceType == String::typeid )
\r
71 return ExpandableObjectConverter::CanConvertFrom(context, sourceType);
\r
74 Object^ Half4Converter::ConvertTo(ITypeDescriptorContext^ context, CultureInfo^ culture, Object^ value, Type^ destinationType)
\r
76 if( destinationType == nullptr )
\r
77 throw gcnew ArgumentNullException( "destinationType" );
\r
79 if( culture == nullptr )
\r
80 culture = CultureInfo::CurrentCulture;
\r
82 Half4^ half = dynamic_cast<Half4^>( value );
\r
84 if( destinationType == String::typeid && half != nullptr )
\r
86 String^ separator = culture->TextInfo->ListSeparator + " ";
\r
87 TypeConverter^ converter = TypeDescriptor::GetConverter(Half::typeid);
\r
88 array<String^>^ stringArray = gcnew array<String^>( 4 );
\r
90 stringArray[0] = converter->ConvertToString( context, culture, half->X );
\r
91 stringArray[1] = converter->ConvertToString( context, culture, half->Y );
\r
92 stringArray[2] = converter->ConvertToString( context, culture, half->Z );
\r
93 stringArray[3] = converter->ConvertToString( context, culture, half->W );
\r
95 return String::Join( separator, stringArray );
\r
97 else if( destinationType == InstanceDescriptor::typeid && half != nullptr )
\r
99 ConstructorInfo^ info = (Half4::typeid)->GetConstructor( gcnew array<Type^> { Half::typeid, Half::typeid, Half::typeid, Half::typeid } );
\r
100 if( info != nullptr )
\r
101 return gcnew InstanceDescriptor( info, gcnew array<Object^> { half->X, half->Y, half->Z, half->W } );
\r
104 return ExpandableObjectConverter::ConvertTo(context, culture, value, destinationType);
\r
107 Object^ Half4Converter::ConvertFrom(ITypeDescriptorContext^ context, CultureInfo^ culture, Object^ value)
\r
109 if( culture == nullptr )
\r
110 culture = CultureInfo::CurrentCulture;
\r
112 String^ string = dynamic_cast<String^>( value );
\r
114 if( string != nullptr )
\r
116 string = string->Trim();
\r
117 TypeConverter^ converter = TypeDescriptor::GetConverter(Half::typeid);
\r
118 array<String^>^ stringArray = string->Split( culture->TextInfo->ListSeparator[0] );
\r
120 if( stringArray->Length != 4 )
\r
121 throw gcnew ArgumentException("Invalid half format.");
\r
123 Half X = safe_cast<Half>( converter->ConvertFromString( context, culture, stringArray[0] ) );
\r
124 Half Y = safe_cast<Half>( converter->ConvertFromString( context, culture, stringArray[1] ) );
\r
125 Half Z = safe_cast<Half>( converter->ConvertFromString( context, culture, stringArray[2] ) );
\r
126 Half W = safe_cast<Half>( converter->ConvertFromString( context, culture, stringArray[3] ) );
\r
128 return gcnew Half4(X, Y, Z, W);
\r
131 return ExpandableObjectConverter::ConvertFrom(context, culture, value);
\r
134 bool Half4Converter::GetCreateInstanceSupported(ITypeDescriptorContext^ context)
\r
136 SLIMDX_UNREFERENCED_PARAMETER(context);
\r
141 Object^ Half4Converter::CreateInstance(ITypeDescriptorContext^ context, IDictionary^ propertyValues)
\r
143 SLIMDX_UNREFERENCED_PARAMETER(context);
\r
145 if( propertyValues == nullptr )
\r
146 throw gcnew ArgumentNullException( "propertyValues" );
\r
148 return gcnew Half4( safe_cast<Half>( propertyValues["X"] ), safe_cast<Half>( propertyValues["Y"] ),
\r
149 safe_cast<Half>( propertyValues["Z"] ), safe_cast<Half>( propertyValues["W"] ) );
\r
152 bool Half4Converter::GetPropertiesSupported(ITypeDescriptorContext^)
\r
157 PropertyDescriptorCollection^ Half4Converter::GetProperties(ITypeDescriptorContext^, Object^, array<Attribute^>^)
\r
159 return m_Properties;
\r