OSDN Git Service

SlimDXの改造後コード。(June2010を変更したもの。)
[dtxmania/dtxmania.git] / SlimDXc_Jun2010(VC++2008) / source / design / RayConverter.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/Ray.h"\r
26 \r
27 #include "RayConverter.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         RayConverter::RayConverter()\r
43         {\r
44                 Type^ type = Ray::typeid;\r
45                 array<PropertyDescriptor^>^ propArray =\r
46                 {\r
47                         gcnew FieldPropertyDescriptor(type->GetField("Position")),\r
48                         gcnew FieldPropertyDescriptor(type->GetField("Direction")),\r
49                 };\r
50 \r
51                 m_Properties = gcnew PropertyDescriptorCollection(propArray);\r
52         }\r
53 \r
54         bool RayConverter::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 RayConverter::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^ RayConverter::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                 Ray^ ray = dynamic_cast<Ray^>( value );\r
79 \r
80                 if( destinationType == String::typeid && ray != nullptr )\r
81                 {\r
82                         String^ separator = culture->TextInfo->ListSeparator + " ";\r
83                         TypeConverter^ converter = TypeDescriptor::GetConverter(Vector3::typeid);\r
84                         array<String^>^ stringArray = gcnew array<String^>( 2 );\r
85 \r
86                         stringArray[0] = converter->ConvertToString( context, culture, ray->Position );\r
87                         stringArray[1] = converter->ConvertToString( context, culture, ray->Direction );\r
88 \r
89                         return String::Join( separator, stringArray );\r
90                 }\r
91                 else if( destinationType == InstanceDescriptor::typeid && ray != nullptr )\r
92                 {\r
93                         ConstructorInfo^ info = (Ray::typeid)->GetConstructor( gcnew array<Type^> { Vector3::typeid, Vector3::typeid } );\r
94                         if( info != nullptr )\r
95                                 return gcnew InstanceDescriptor( info, gcnew array<Object^> { ray->Position, ray->Direction } );\r
96                 }\r
97 \r
98                 return ExpandableObjectConverter::ConvertTo(context, culture, value, destinationType);\r
99         }\r
100 \r
101         Object^ RayConverter::ConvertFrom(ITypeDescriptorContext^ context, CultureInfo^ culture, Object^ value)\r
102         {\r
103                 if( culture == nullptr )\r
104                         culture = CultureInfo::CurrentCulture;\r
105 \r
106                 String^ string = dynamic_cast<String^>( value );\r
107 \r
108                 if( string != nullptr )\r
109                 {\r
110                         string = string->Trim();\r
111                         TypeConverter^ converter = TypeDescriptor::GetConverter(Vector3::typeid);\r
112                         array<String^>^ stringArray = string->Split( culture->TextInfo->ListSeparator[0] );\r
113 \r
114                         if( stringArray->Length != 2 )\r
115                                 throw gcnew ArgumentException("Invalid ray format.");\r
116 \r
117                         Vector3 Position = safe_cast<Vector3>( converter->ConvertFromString( context, culture, stringArray[0] ) );\r
118                         Vector3 Direction = safe_cast<Vector3>( converter->ConvertFromString( context, culture, stringArray[1] ) );\r
119 \r
120                         return gcnew Ray(Position, Direction);\r
121                 }\r
122 \r
123                 return ExpandableObjectConverter::ConvertFrom(context, culture, value);\r
124         }\r
125 \r
126         bool RayConverter::GetCreateInstanceSupported(ITypeDescriptorContext^ context)\r
127         {\r
128                 SLIMDX_UNREFERENCED_PARAMETER(context);\r
129 \r
130                 return true;\r
131         }\r
132 \r
133         Object^ RayConverter::CreateInstance(ITypeDescriptorContext^ context, IDictionary^ propertyValues)\r
134         {\r
135                 SLIMDX_UNREFERENCED_PARAMETER(context);\r
136 \r
137                 if( propertyValues == nullptr )\r
138                         throw gcnew ArgumentNullException( "propertyValues" );\r
139 \r
140                 return gcnew Ray( safe_cast<Vector3>( propertyValues["Position"] ), safe_cast<Vector3>( propertyValues["Direction"] ) );\r
141         }\r
142 \r
143         bool RayConverter::GetPropertiesSupported(ITypeDescriptorContext^)\r
144         {\r
145                 return true;\r
146         }\r
147 \r
148         PropertyDescriptorCollection^ RayConverter::GetProperties(ITypeDescriptorContext^, Object^, array<Attribute^>^)\r
149         {\r
150                 return m_Properties;\r
151         }\r
152 }\r
153 }