OSDN Git Service

作業部屋#50802 画面キャプチャができなくなっていた問題を暫定対応(F12キー固定で対応中)
[dtxmaniaxg-verk/dtxmaniaxg-verk-git.git] / SlimDXc_Jun2010(VC++2008) / source / design / QuaternionConverter.cpp
1 #include "stdafx.h"
2 /*
3 * Copyright (c) 2007-2010 SlimDX Group
4
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24 #include "../InternalHelpers.h"
25 #include "../math/Quaternion.h"
26
27 #include "QuaternionConverter.h"
28 #include "FieldPropertyDescriptor.h"
29
30 using namespace System;
31 using namespace System::Collections;
32 using namespace System::Drawing;
33 using namespace System::ComponentModel;
34 using namespace System::ComponentModel::Design::Serialization;
35 using namespace System::Globalization;
36 using namespace System::Reflection;
37
38 namespace SlimDX
39 {
40 namespace Design
41 {
42         QuaternionConverter::QuaternionConverter()
43         {
44                 Type^ type = Quaternion::typeid;
45                 array<PropertyDescriptor^>^ propArray =
46                 {
47                         gcnew FieldPropertyDescriptor(type->GetField("X")),
48                         gcnew FieldPropertyDescriptor(type->GetField("Y")),
49                         gcnew FieldPropertyDescriptor(type->GetField("Z")),
50                         gcnew FieldPropertyDescriptor(type->GetField("W")),
51                 };
52
53                 m_Properties = gcnew PropertyDescriptorCollection(propArray);
54         }
55
56         bool QuaternionConverter::CanConvertTo(ITypeDescriptorContext^ context, Type^ destinationType)
57         {
58                 if( destinationType == String::typeid || destinationType == InstanceDescriptor::typeid )
59                         return true;
60                 else
61                         return ExpandableObjectConverter::CanConvertTo(context, destinationType);
62         }
63
64         bool QuaternionConverter::CanConvertFrom(ITypeDescriptorContext^ context, Type^ sourceType)
65         {
66                 if( sourceType == String::typeid )
67                         return true;
68                 else
69                         return ExpandableObjectConverter::CanConvertFrom(context, sourceType);
70         }
71
72         Object^ QuaternionConverter::ConvertTo(ITypeDescriptorContext^ context, CultureInfo^ culture, Object^ value, Type^ destinationType)
73         {
74                 if( destinationType == nullptr )
75                         throw gcnew ArgumentNullException( "destinationType" );
76
77                 if( culture == nullptr )
78                         culture = CultureInfo::CurrentCulture;
79
80                 Quaternion^ quaternion = dynamic_cast<Quaternion^>( value );
81
82                 if( destinationType == String::typeid && quaternion != nullptr )
83                 {
84                         String^ separator = culture->TextInfo->ListSeparator + " ";
85                         TypeConverter^ converter = TypeDescriptor::GetConverter(float::typeid);
86                         array<String^>^ stringArray = gcnew array<String^>( 4 );
87
88                         stringArray[0] = converter->ConvertToString( context, culture, quaternion->X );
89                         stringArray[1] = converter->ConvertToString( context, culture, quaternion->Y );
90                         stringArray[2] = converter->ConvertToString( context, culture, quaternion->Z );
91                         stringArray[3] = converter->ConvertToString( context, culture, quaternion->W );
92
93                         return String::Join( separator, stringArray );
94                 }
95                 else if( destinationType == InstanceDescriptor::typeid && quaternion != nullptr )
96                 {
97                         ConstructorInfo^ info = (Quaternion::typeid)->GetConstructor( gcnew array<Type^> { float::typeid, float::typeid, float::typeid, float::typeid } );
98                         if( info != nullptr )
99                                 return gcnew InstanceDescriptor( info, gcnew array<Object^> { quaternion->X, quaternion->Y, quaternion->Z, quaternion->W } );
100                 }
101
102                 return ExpandableObjectConverter::ConvertTo(context, culture, value, destinationType);
103         }
104
105         Object^ QuaternionConverter::ConvertFrom(ITypeDescriptorContext^ context, CultureInfo^ culture, Object^ value)
106         {
107                 if( culture == nullptr )
108                         culture = CultureInfo::CurrentCulture;
109
110                 String^ string = dynamic_cast<String^>( value );
111
112                 if( string != nullptr )
113                 {
114                         string = string->Trim();
115                         TypeConverter^ converter = TypeDescriptor::GetConverter(float::typeid);
116                         array<String^>^ stringArray = string->Split( culture->TextInfo->ListSeparator[0] );
117
118                         if( stringArray->Length != 4 )
119                                 throw gcnew ArgumentException("Invalid quaternion format.");
120
121                         float X = safe_cast<float>( converter->ConvertFromString( context, culture, stringArray[0] ) );
122                         float Y = safe_cast<float>( converter->ConvertFromString( context, culture, stringArray[1] ) );
123                         float Z = safe_cast<float>( converter->ConvertFromString( context, culture, stringArray[2] ) );
124                         float W = safe_cast<float>( converter->ConvertFromString( context, culture, stringArray[3] ) );
125
126                         return gcnew Quaternion(X, Y, Z, W);
127                 }
128
129                 return ExpandableObjectConverter::ConvertFrom(context, culture, value);
130         }
131
132         bool QuaternionConverter::GetCreateInstanceSupported(ITypeDescriptorContext^ context)
133         {
134                 SLIMDX_UNREFERENCED_PARAMETER(context);
135
136                 return true;
137         }
138
139         Object^ QuaternionConverter::CreateInstance(ITypeDescriptorContext^ context, IDictionary^ propertyValues)
140         {
141                 SLIMDX_UNREFERENCED_PARAMETER(context);
142
143                 if( propertyValues == nullptr )
144                         throw gcnew ArgumentNullException( "propertyValues" );
145
146                 return gcnew Quaternion( safe_cast<float>( propertyValues["X"] ), safe_cast<float>( propertyValues["Y"] ),
147                         safe_cast<float>( propertyValues["Z"] ), safe_cast<float>( propertyValues["W"] ) );
148         }
149
150         bool QuaternionConverter::GetPropertiesSupported(ITypeDescriptorContext^)
151         {
152                 return true;
153         }
154
155         PropertyDescriptorCollection^ QuaternionConverter::GetProperties(ITypeDescriptorContext^, Object^, array<Attribute^>^)
156         {
157                 return m_Properties;
158         }
159 }
160 }