OSDN Git Service

DTXMania089リリースに際してのtag付け。
[dtxmania/dtxmania.git] / 110401(DTXMania089) / SlimDXc_Jun2010(VC++2008) / source / design / Half3Converter.cpp
1 #include "stdafx.h"\r
2 /*\r
3 * Copyright (c) 2007-2010 SlimDX Group\r
4\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
11\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
14\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
21 * THE SOFTWARE.\r
22 */\r
23 #include <d3dx9.h>\r
24 \r
25 #include "../InternalHelpers.h"\r
26 #include "../math/Half.h"\r
27 #include "../math/Half3.h"\r
28 \r
29 #include "Half3Converter.h"\r
30 #include "FieldPropertyDescriptor.h"\r
31 \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
39 \r
40 namespace SlimDX\r
41 {\r
42 namespace Design\r
43 {\r
44         Half3Converter::Half3Converter()\r
45         {\r
46                 Type^ type = Half3::typeid;\r
47                 array<PropertyDescriptor^>^ propArray =\r
48                 {\r
49                         gcnew FieldPropertyDescriptor(type->GetField("X")),\r
50                         gcnew FieldPropertyDescriptor(type->GetField("Y")),\r
51                         gcnew FieldPropertyDescriptor(type->GetField("Z")),\r
52                 };\r
53 \r
54                 m_Properties = gcnew PropertyDescriptorCollection(propArray);\r
55         }\r
56 \r
57         bool Half3Converter::CanConvertTo(ITypeDescriptorContext^ context, Type^ destinationType)\r
58         {\r
59                 if( destinationType == String::typeid || destinationType == InstanceDescriptor::typeid )\r
60                         return true;\r
61                 else\r
62                         return ExpandableObjectConverter::CanConvertTo(context, destinationType);\r
63         }\r
64 \r
65         bool Half3Converter::CanConvertFrom(ITypeDescriptorContext^ context, Type^ sourceType)\r
66         {\r
67                 if( sourceType == String::typeid )\r
68                         return true;\r
69                 else\r
70                         return ExpandableObjectConverter::CanConvertFrom(context, sourceType);\r
71         }\r
72 \r
73         Object^ Half3Converter::ConvertTo(ITypeDescriptorContext^ context, CultureInfo^ culture, Object^ value, Type^ destinationType)\r
74         {\r
75                 if( destinationType == nullptr )\r
76                         throw gcnew ArgumentNullException( "destinationType" );\r
77 \r
78                 if( culture == nullptr )\r
79                         culture = CultureInfo::CurrentCulture;\r
80 \r
81                 Half3^ half = dynamic_cast<Half3^>( value );\r
82 \r
83                 if( destinationType == String::typeid && half != nullptr )\r
84                 {\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
88 \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
92 \r
93                         return String::Join( separator, stringArray );\r
94                 }\r
95                 else if( destinationType == InstanceDescriptor::typeid && half != nullptr )\r
96                 {\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
100                 }\r
101 \r
102                 return ExpandableObjectConverter::ConvertTo(context, culture, value, destinationType);\r
103         }\r
104 \r
105         Object^ Half3Converter::ConvertFrom(ITypeDescriptorContext^ context, CultureInfo^ culture, Object^ value)\r
106         {\r
107                 if( culture == nullptr )\r
108                         culture = CultureInfo::CurrentCulture;\r
109 \r
110                 String^ string = dynamic_cast<String^>( value );\r
111 \r
112                 if( string != nullptr )\r
113                 {\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
117 \r
118                         if( stringArray->Length != 3 )\r
119                                 throw gcnew ArgumentException("Invalid half format.");\r
120 \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
124 \r
125                         return gcnew Half3(X, Y, Z);\r
126                 }\r
127 \r
128                 return ExpandableObjectConverter::ConvertFrom(context, culture, value);\r
129         }\r
130 \r
131         bool Half3Converter::GetCreateInstanceSupported(ITypeDescriptorContext^ context)\r
132         {\r
133                 SLIMDX_UNREFERENCED_PARAMETER(context);\r
134 \r
135                 return true;\r
136         }\r
137 \r
138         Object^ Half3Converter::CreateInstance(ITypeDescriptorContext^ context, IDictionary^ propertyValues)\r
139         {\r
140                 SLIMDX_UNREFERENCED_PARAMETER(context);\r
141 \r
142                 if( propertyValues == nullptr )\r
143                         throw gcnew ArgumentNullException( "propertyValues" );\r
144 \r
145                 return gcnew Half3( safe_cast<Half>( propertyValues["X"] ), safe_cast<Half>( propertyValues["Y"] ),\r
146                         safe_cast<Half>( propertyValues["Z"] ) );\r
147         }\r
148 \r
149         bool Half3Converter::GetPropertiesSupported(ITypeDescriptorContext^)\r
150         {\r
151                 return true;\r
152         }\r
153 \r
154         PropertyDescriptorCollection^ Half3Converter::GetProperties(ITypeDescriptorContext^, Object^, array<Attribute^>^)\r
155         {\r
156                 return m_Properties;\r
157         }\r
158 }\r
159 }