OSDN Git Service

作業部屋#50802 画面キャプチャができなくなっていた問題を暫定対応(F12キー固定で対応中)
[dtxmaniaxg-verk/dtxmaniaxg-verk-git.git] / SlimDXc_Jun2010(VC++2008) / source / design / RayConverter.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/Ray.h"
26
27 #include "RayConverter.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         RayConverter::RayConverter()
43         {
44                 Type^ type = Ray::typeid;
45                 array<PropertyDescriptor^>^ propArray =
46                 {
47                         gcnew FieldPropertyDescriptor(type->GetField("Position")),
48                         gcnew FieldPropertyDescriptor(type->GetField("Direction")),
49                 };
50
51                 m_Properties = gcnew PropertyDescriptorCollection(propArray);
52         }
53
54         bool RayConverter::CanConvertTo(ITypeDescriptorContext^ context, Type^ destinationType)
55         {
56                 if( destinationType == String::typeid || destinationType == InstanceDescriptor::typeid )
57                         return true;
58                 else
59                         return ExpandableObjectConverter::CanConvertTo(context, destinationType);
60         }
61
62         bool RayConverter::CanConvertFrom(ITypeDescriptorContext^ context, Type^ sourceType)
63         {
64                 if( sourceType == String::typeid )
65                         return true;
66                 else
67                         return ExpandableObjectConverter::CanConvertFrom(context, sourceType);
68         }
69
70         Object^ RayConverter::ConvertTo(ITypeDescriptorContext^ context, CultureInfo^ culture, Object^ value, Type^ destinationType)
71         {
72                 if( destinationType == nullptr )
73                         throw gcnew ArgumentNullException( "destinationType" );
74
75                 if( culture == nullptr )
76                         culture = CultureInfo::CurrentCulture;
77
78                 Ray^ ray = dynamic_cast<Ray^>( value );
79
80                 if( destinationType == String::typeid && ray != nullptr )
81                 {
82                         String^ separator = culture->TextInfo->ListSeparator + " ";
83                         TypeConverter^ converter = TypeDescriptor::GetConverter(Vector3::typeid);
84                         array<String^>^ stringArray = gcnew array<String^>( 2 );
85
86                         stringArray[0] = converter->ConvertToString( context, culture, ray->Position );
87                         stringArray[1] = converter->ConvertToString( context, culture, ray->Direction );
88
89                         return String::Join( separator, stringArray );
90                 }
91                 else if( destinationType == InstanceDescriptor::typeid && ray != nullptr )
92                 {
93                         ConstructorInfo^ info = (Ray::typeid)->GetConstructor( gcnew array<Type^> { Vector3::typeid, Vector3::typeid } );
94                         if( info != nullptr )
95                                 return gcnew InstanceDescriptor( info, gcnew array<Object^> { ray->Position, ray->Direction } );
96                 }
97
98                 return ExpandableObjectConverter::ConvertTo(context, culture, value, destinationType);
99         }
100
101         Object^ RayConverter::ConvertFrom(ITypeDescriptorContext^ context, CultureInfo^ culture, Object^ value)
102         {
103                 if( culture == nullptr )
104                         culture = CultureInfo::CurrentCulture;
105
106                 String^ string = dynamic_cast<String^>( value );
107
108                 if( string != nullptr )
109                 {
110                         string = string->Trim();
111                         TypeConverter^ converter = TypeDescriptor::GetConverter(Vector3::typeid);
112                         array<String^>^ stringArray = string->Split( culture->TextInfo->ListSeparator[0] );
113
114                         if( stringArray->Length != 2 )
115                                 throw gcnew ArgumentException("Invalid ray format.");
116
117                         Vector3 Position = safe_cast<Vector3>( converter->ConvertFromString( context, culture, stringArray[0] ) );
118                         Vector3 Direction = safe_cast<Vector3>( converter->ConvertFromString( context, culture, stringArray[1] ) );
119
120                         return gcnew Ray(Position, Direction);
121                 }
122
123                 return ExpandableObjectConverter::ConvertFrom(context, culture, value);
124         }
125
126         bool RayConverter::GetCreateInstanceSupported(ITypeDescriptorContext^ context)
127         {
128                 SLIMDX_UNREFERENCED_PARAMETER(context);
129
130                 return true;
131         }
132
133         Object^ RayConverter::CreateInstance(ITypeDescriptorContext^ context, IDictionary^ propertyValues)
134         {
135                 SLIMDX_UNREFERENCED_PARAMETER(context);
136
137                 if( propertyValues == nullptr )
138                         throw gcnew ArgumentNullException( "propertyValues" );
139
140                 return gcnew Ray( safe_cast<Vector3>( propertyValues["Position"] ), safe_cast<Vector3>( propertyValues["Direction"] ) );
141         }
142
143         bool RayConverter::GetPropertiesSupported(ITypeDescriptorContext^)
144         {
145                 return true;
146         }
147
148         PropertyDescriptorCollection^ RayConverter::GetProperties(ITypeDescriptorContext^, Object^, array<Attribute^>^)
149         {
150                 return m_Properties;
151         }
152 }
153 }