OSDN Git Service

SlimDXの改造後コード。(June2010を変更したもの。)
[dtxmania/dtxmania.git] / SlimDXc_Jun2010(VC++2008) / source / design / HalfConverter.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 \r
28 #include "HalfConverter.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         bool HalfConverter::CanConvertTo(ITypeDescriptorContext^ context, Type^ destinationType)\r
43         {\r
44                 if( destinationType == String::typeid || destinationType == InstanceDescriptor::typeid )\r
45                         return true;\r
46                 else\r
47                         return ExpandableObjectConverter::CanConvertTo(context, destinationType);\r
48         }\r
49 \r
50         bool HalfConverter::CanConvertFrom(ITypeDescriptorContext^ context, Type^ sourceType)\r
51         {\r
52                 if( sourceType == String::typeid )\r
53                         return true;\r
54                 else\r
55                         return ExpandableObjectConverter::CanConvertFrom(context, sourceType);\r
56         }\r
57 \r
58         Object^ HalfConverter::ConvertTo(ITypeDescriptorContext^ context, CultureInfo^ culture, Object^ value, Type^ destinationType)\r
59         {\r
60                 if( destinationType == nullptr )\r
61                         throw gcnew ArgumentNullException( "destinationType" );\r
62 \r
63                 if( culture == nullptr )\r
64                         culture = CultureInfo::CurrentCulture;\r
65 \r
66                 Half^ half = dynamic_cast<Half^>( value );\r
67 \r
68                 if( destinationType == String::typeid && half != nullptr )\r
69                 {\r
70                         String^ separator = culture->TextInfo->ListSeparator + " ";\r
71                         TypeConverter^ converter = TypeDescriptor::GetConverter(float::typeid);\r
72                         array<String^>^ stringArray = gcnew array<String^>( 1 );\r
73 \r
74                         stringArray[0] = converter->ConvertToString( context, culture, safe_cast<float>( *half ) );\r
75 \r
76                         return String::Join( separator, stringArray );\r
77                 }\r
78                 else if( destinationType == InstanceDescriptor::typeid && half != nullptr )\r
79                 {\r
80                         ConstructorInfo^ info = (Half::typeid)->GetConstructor( gcnew array<Type^> { float::typeid } );\r
81                         if( info != nullptr )\r
82                                 return gcnew InstanceDescriptor( info, gcnew array<Object^> { safe_cast<float>( *half ) } );\r
83                 }\r
84 \r
85                 return ExpandableObjectConverter::ConvertTo(context, culture, value, destinationType);\r
86         }\r
87 \r
88         Object^ HalfConverter::ConvertFrom(ITypeDescriptorContext^ context, CultureInfo^ culture, Object^ value)\r
89         {\r
90                 if( culture == nullptr )\r
91                         culture = CultureInfo::CurrentCulture;\r
92 \r
93                 String^ string = dynamic_cast<String^>( value );\r
94 \r
95                 if( string != nullptr )\r
96                 {\r
97                         string = string->Trim();\r
98                         TypeConverter^ converter = TypeDescriptor::GetConverter(float::typeid);\r
99                         array<String^>^ stringArray = string->Split( culture->TextInfo->ListSeparator[0] );\r
100 \r
101                         if( stringArray->Length != 1 )\r
102                                 throw gcnew ArgumentException("Invalid half format.");\r
103 \r
104                         float H = safe_cast<float>( converter->ConvertFromString( context, culture, stringArray[0] ) );\r
105 \r
106                         return gcnew Half(H);\r
107                 }\r
108 \r
109                 return ExpandableObjectConverter::ConvertFrom(context, culture, value);\r
110         }\r
111 }\r
112 }