OSDN Git Service

DTXMania089リリースに際してのtag付け。
[dtxmania/dtxmania.git] / 110401(DTXMania089) / SlimDXc_Jun2010(VC++2008) / source / design / Color4Converter.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 \r
24 #include "../InternalHelpers.h"\r
25 #include "../math/Color4.h"\r
26 \r
27 #include "Color4Converter.h"\r
28 #include "FieldPropertyDescriptor.h"\r
29 \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
37 \r
38 namespace SlimDX\r
39 {\r
40 namespace Design\r
41 {\r
42         Color4Converter::Color4Converter()\r
43         {\r
44                 Type^ type = Color4::typeid;\r
45                 array<PropertyDescriptor^>^ propArray =\r
46                 {\r
47                         gcnew FieldPropertyDescriptor(type->GetField("Red")),\r
48                         gcnew FieldPropertyDescriptor(type->GetField("Green")),\r
49                         gcnew FieldPropertyDescriptor(type->GetField("Blue")),\r
50                         gcnew FieldPropertyDescriptor(type->GetField("Alpha")),\r
51                 };\r
52 \r
53                 m_Properties = gcnew PropertyDescriptorCollection(propArray);\r
54         }\r
55 \r
56         bool Color4Converter::CanConvertTo(ITypeDescriptorContext^ context, Type^ destinationType)\r
57         {\r
58                 if( destinationType == String::typeid || destinationType == InstanceDescriptor::typeid )\r
59                         return true;\r
60                 else\r
61                         return ExpandableObjectConverter::CanConvertTo(context, destinationType);\r
62         }\r
63 \r
64         bool Color4Converter::CanConvertFrom(ITypeDescriptorContext^ context, Type^ sourceType)\r
65         {\r
66                 if( sourceType == String::typeid )\r
67                         return true;\r
68                 else\r
69                         return ExpandableObjectConverter::CanConvertFrom(context, sourceType);\r
70         }\r
71 \r
72         Object^ Color4Converter::ConvertTo(ITypeDescriptorContext^ context, CultureInfo^ culture, Object^ value, Type^ destinationType)\r
73         {\r
74                 if( destinationType == nullptr )\r
75                         throw gcnew ArgumentNullException( "destinationType" );\r
76 \r
77                 if( culture == nullptr )\r
78                         culture = CultureInfo::CurrentCulture;\r
79 \r
80                 Color4^ color = dynamic_cast<Color4^>( value );\r
81 \r
82                 if( destinationType == String::typeid && color != nullptr )\r
83                 {\r
84                         String^ separator = culture->TextInfo->ListSeparator + " ";\r
85                         TypeConverter^ converter = TypeDescriptor::GetConverter(float::typeid);\r
86                         array<String^>^ stringArray = gcnew array<String^>( 4 );\r
87 \r
88                         stringArray[0] = converter->ConvertToString( context, culture, color->Alpha );\r
89                         stringArray[1] = converter->ConvertToString( context, culture, color->Red );\r
90                         stringArray[2] = converter->ConvertToString( context, culture, color->Green );\r
91                         stringArray[3] = converter->ConvertToString( context, culture, color->Blue );\r
92 \r
93                         return String::Join( separator, stringArray );\r
94                 }\r
95                 else if( destinationType == InstanceDescriptor::typeid && color != nullptr )\r
96                 {\r
97                         ConstructorInfo^ info = (Color4::typeid)->GetConstructor( gcnew array<Type^> { float::typeid, float::typeid, float::typeid, float::typeid } );\r
98                         if( info != nullptr )\r
99                                 return gcnew InstanceDescriptor( info, gcnew array<Object^> { color->Alpha, color->Red, color->Green, color->Blue } );\r
100                 }\r
101 \r
102                 return ExpandableObjectConverter::ConvertTo(context, culture, value, destinationType);\r
103         }\r
104 \r
105         Object^ Color4Converter::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^ floatConverter = TypeDescriptor::GetConverter(float::typeid);\r
116                         array<String^>^ stringArray = string->Split( culture->TextInfo->ListSeparator[0] );\r
117 \r
118                         if( stringArray->Length == 1 )\r
119                         {\r
120                                 int number = 0;\r
121                                 if( int::TryParse( string, number ) )\r
122                                         return gcnew Color4( number );\r
123 \r
124                                 TypeConverter^ colorConverter = TypeDescriptor::GetConverter(Color::typeid);\r
125                                 return gcnew Color4( safe_cast<Color>( colorConverter->ConvertFromString( context, culture, string ) ) );\r
126                         }\r
127                         else if( stringArray->Length == 3 )\r
128                         {\r
129                                 int red;\r
130                                 int green;\r
131                                 int blue;\r
132                                 if( int::TryParse( stringArray[0], red ) && int::TryParse( stringArray[1], green ) && int::TryParse( stringArray[2], blue ) )\r
133                                         return gcnew Color4( Color::FromArgb( red, green, blue ) );\r
134 \r
135                                 float r = safe_cast<float>( floatConverter->ConvertFromString( context, culture, stringArray[0] ) );\r
136                                 float g = safe_cast<float>( floatConverter->ConvertFromString( context, culture, stringArray[1] ) );\r
137                                 float b = safe_cast<float>( floatConverter->ConvertFromString( context, culture, stringArray[2] ) );\r
138 \r
139                                 return gcnew Color4( r, g, b );\r
140                         }\r
141                         else if( stringArray->Length == 4 )\r
142                         {\r
143                                 int red;\r
144                                 int green;\r
145                                 int blue;\r
146                                 int alpha;\r
147                                 if( int::TryParse( stringArray[0], alpha ) && int::TryParse( stringArray[1], red ) && int::TryParse( stringArray[2], green ) && int::TryParse( stringArray[3], blue ) )\r
148                                         return gcnew Color4( Color::FromArgb( alpha, red, green, blue ) );\r
149 \r
150                                 float a = safe_cast<float>( floatConverter->ConvertFromString( context, culture, stringArray[0] ) );\r
151                                 float r = safe_cast<float>( floatConverter->ConvertFromString( context, culture, stringArray[1] ) );\r
152                                 float g = safe_cast<float>( floatConverter->ConvertFromString( context, culture, stringArray[2] ) );\r
153                                 float b = safe_cast<float>( floatConverter->ConvertFromString( context, culture, stringArray[3] ) );\r
154 \r
155                                 return gcnew Color4( a, r, g, b );\r
156                         }\r
157                         else\r
158                                 throw gcnew ArgumentException("Invalid color format.");\r
159                 }\r
160 \r
161                 return ExpandableObjectConverter::ConvertFrom(context, culture, value);\r
162         }\r
163 \r
164         bool Color4Converter::GetCreateInstanceSupported(ITypeDescriptorContext^ context)\r
165         {\r
166                 SLIMDX_UNREFERENCED_PARAMETER(context);\r
167 \r
168                 return true;\r
169         }\r
170 \r
171         Object^ Color4Converter::CreateInstance(ITypeDescriptorContext^ context, IDictionary^ propertyValues)\r
172         {\r
173                 SLIMDX_UNREFERENCED_PARAMETER(context);\r
174 \r
175                 if( propertyValues == nullptr )\r
176                         throw gcnew ArgumentNullException( "propertyValues" );\r
177 \r
178                 return gcnew Color4( safe_cast<float>( propertyValues["Alpha"] ), safe_cast<float>( propertyValues["Red"] ),\r
179                         safe_cast<float>( propertyValues["Green"] ), safe_cast<float>( propertyValues["Blue"] ) );\r
180         }\r
181 \r
182         bool Color4Converter::GetPropertiesSupported(ITypeDescriptorContext^)\r
183         {\r
184                 return true;\r
185         }\r
186 \r
187         PropertyDescriptorCollection^ Color4Converter::GetProperties(ITypeDescriptorContext^, Object^, array<Attribute^>^)\r
188         {\r
189                 return m_Properties;\r
190         }\r
191 }\r
192 }