OSDN Git Service

#36897 [DTXC] MIDIインポート機能の呼び出し口を、ファイルメニュー内にも配置。
[dtxmania/dtxmania.git] / SlimDXc_Jun2010(VC++2008) / source / design / BoundingSphereConverter.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/BoundingSphere.h"\r
26 \r
27 #include "BoundingSphereConverter.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         BoundingSphereConverter::BoundingSphereConverter()\r
43         {\r
44                 Type^ type = BoundingSphere::typeid;\r
45                 array<PropertyDescriptor^>^ propArray =\r
46                 {\r
47                         gcnew FieldPropertyDescriptor(type->GetField("Center")),\r
48                         gcnew FieldPropertyDescriptor(type->GetField("Radius")),\r
49                 };\r
50 \r
51                 m_Properties = gcnew PropertyDescriptorCollection(propArray);\r
52         }\r
53 \r
54         bool BoundingSphereConverter::CanConvertTo(ITypeDescriptorContext^ context, Type^ destinationType)\r
55         {\r
56                 if( destinationType == String::typeid || destinationType == InstanceDescriptor::typeid )\r
57                         return true;\r
58                 else\r
59                         return ExpandableObjectConverter::CanConvertTo(context, destinationType);\r
60         }\r
61 \r
62         bool BoundingSphereConverter::CanConvertFrom(ITypeDescriptorContext^ context, Type^ sourceType)\r
63         {\r
64                 if( sourceType == String::typeid )\r
65                         return true;\r
66                 else\r
67                         return ExpandableObjectConverter::CanConvertFrom(context, sourceType);\r
68         }\r
69 \r
70         Object^ BoundingSphereConverter::ConvertTo(ITypeDescriptorContext^ context, CultureInfo^ culture, Object^ value, Type^ destinationType)\r
71         {\r
72                 if( destinationType == nullptr )\r
73                         throw gcnew ArgumentNullException( "destinationType" );\r
74 \r
75                 if( culture == nullptr )\r
76                         culture = CultureInfo::CurrentCulture;\r
77 \r
78                 BoundingSphere^ sphere = dynamic_cast<BoundingSphere^>( value );\r
79 \r
80                 if( destinationType == String::typeid && sphere != nullptr )\r
81                 {\r
82                         String^ separator = culture->TextInfo->ListSeparator + " ";\r
83                         TypeConverter^ vector3Converter = TypeDescriptor::GetConverter(Vector3::typeid);\r
84                         TypeConverter^ floatConverter = TypeDescriptor::GetConverter(float::typeid);\r
85                         array<String^>^ stringArray = gcnew array<String^>( 2 );\r
86 \r
87                         stringArray[0] = vector3Converter->ConvertToString( context, culture, sphere->Center );\r
88                         stringArray[1] = floatConverter->ConvertToString( context, culture, sphere->Radius );\r
89 \r
90                         return String::Join( separator, stringArray );\r
91                 }\r
92                 else if( destinationType == InstanceDescriptor::typeid && sphere != nullptr )\r
93                 {\r
94                         ConstructorInfo^ info = (BoundingSphere::typeid)->GetConstructor( gcnew array<Type^> { Vector3::typeid, float::typeid } );\r
95                         if( info != nullptr )\r
96                                 return gcnew InstanceDescriptor( info, gcnew array<Object^> { sphere->Center, sphere->Radius } );\r
97                 }\r
98 \r
99                 return ExpandableObjectConverter::ConvertTo(context, culture, value, destinationType);\r
100         }\r
101 \r
102         Object^ BoundingSphereConverter::ConvertFrom(ITypeDescriptorContext^ context, CultureInfo^ culture, Object^ value)\r
103         {\r
104                 if( culture == nullptr )\r
105                         culture = CultureInfo::CurrentCulture;\r
106 \r
107                 String^ string = dynamic_cast<String^>( value );\r
108 \r
109                 if( string != nullptr )\r
110                 {\r
111                         string = string->Trim();\r
112                         TypeConverter^ vector3Converter = TypeDescriptor::GetConverter(Vector3::typeid);\r
113                         TypeConverter^ floatConverter = TypeDescriptor::GetConverter(float::typeid);\r
114                         array<String^>^ stringArray = string->Split( culture->TextInfo->ListSeparator[0] );\r
115 \r
116                         if( stringArray->Length != 2 )\r
117                                 throw gcnew ArgumentException("Invalid sphere format.");\r
118 \r
119                         Vector3 Center = safe_cast<Vector3>( vector3Converter->ConvertFromString( context, culture, stringArray[0] ) );\r
120                         float Radius = safe_cast<float>( floatConverter->ConvertFromString( context, culture, stringArray[1] ) );\r
121 \r
122                         return gcnew BoundingSphere(Center, Radius);\r
123                 }\r
124 \r
125                 return ExpandableObjectConverter::ConvertFrom(context, culture, value);\r
126         }\r
127 \r
128         bool BoundingSphereConverter::GetCreateInstanceSupported(ITypeDescriptorContext^ context)\r
129         {\r
130                 SLIMDX_UNREFERENCED_PARAMETER(context);\r
131 \r
132                 return true;\r
133         }\r
134 \r
135         Object^ BoundingSphereConverter::CreateInstance(ITypeDescriptorContext^ context, IDictionary^ propertyValues)\r
136         {\r
137                 SLIMDX_UNREFERENCED_PARAMETER(context);\r
138 \r
139                 if( propertyValues == nullptr )\r
140                         throw gcnew ArgumentNullException( "propertyValues" );\r
141 \r
142                 return gcnew BoundingSphere( safe_cast<Vector3>( propertyValues["Center"] ), safe_cast<float>( propertyValues["Radius"] ) );\r
143         }\r
144 \r
145         bool BoundingSphereConverter::GetPropertiesSupported(ITypeDescriptorContext^)\r
146         {\r
147                 return true;\r
148         }\r
149 \r
150         PropertyDescriptorCollection^ BoundingSphereConverter::GetProperties(ITypeDescriptorContext^, Object^, array<Attribute^>^)\r
151         {\r
152                 return m_Properties;\r
153         }\r
154 }\r
155 }