2 * Copyright (c) 2007-2010 SlimDX Group
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "EffectParameters.h"
27 using namespace System;
28 using namespace System::Runtime::InteropServices;
34 EffectParameters::EffectParameters( const DIEFFECT &effect )
36 Flags = static_cast<EffectFlags>( effect.dwFlags );
37 Duration = effect.dwDuration;
38 SamplePeriod = effect.dwSamplePeriod;
40 TriggerButton = effect.dwTriggerButton;
41 TriggerRepeatInterval = effect.dwTriggerRepeatInterval;
42 StartDelay = effect.dwStartDelay;
44 if( effect.rgdwAxes != NULL && effect.cAxes > 0 )
46 axes = gcnew array<int>( effect.cAxes );
47 for( UINT i = 0; i < effect.cAxes; i++ )
48 axes[i] = effect.rgdwAxes[i];
51 if( effect.rglDirection != NULL && effect.cAxes > 0 )
53 directions = gcnew array<int>( effect.cAxes );
54 for( UINT i = 0; i < effect.cAxes; i++ )
55 directions[i] = effect.rglDirection[i];
58 if( effect.lpEnvelope != NULL )
60 DirectInput::Envelope env;
61 env.AttackLevel = effect.lpEnvelope->dwAttackLevel;
62 env.AttackTime = effect.lpEnvelope->dwAttackTime;
63 env.FadeLevel = effect.lpEnvelope->dwFadeLevel;
64 env.FadeTime = effect.lpEnvelope->dwFadeTime;
69 if( effect.cbTypeSpecificParams > 0 && effect.lpvTypeSpecificParams != NULL )
70 Parameters = gcnew TypeSpecificParameters( effect.lpvTypeSpecificParams, effect.cbTypeSpecificParams );
73 DIEFFECT EffectParameters::ToUnmanaged()
76 memset( &result, 0, sizeof( DIEFFECT ) );
78 result.dwSize = sizeof( DIEFFECT );
79 result.dwFlags = static_cast<DWORD>( Flags );
80 result.dwDuration = Duration;
81 result.dwSamplePeriod = SamplePeriod;
83 result.dwTriggerButton = TriggerButton;
84 result.dwTriggerRepeatInterval = TriggerRepeatInterval;
85 result.dwStartDelay = StartDelay;
89 result.cAxes = axes->Length;
91 // Manual Allocation: cleaned up in Cleanup() method
92 result.rgdwAxes = new DWORD[axes->Length];
94 pin_ptr<int> pinnedAxes = &axes[0];
95 memcpy( result.rgdwAxes, pinnedAxes, sizeof( DWORD ) * axes->Length );
98 if( directions != nullptr )
100 // Manual Allocation: cleaned up in Cleanup() method
101 result.rglDirection = new LONG[axes->Length];
103 pin_ptr<int> pinnedDirections = &directions[0];
104 memcpy( result.rglDirection, pinnedDirections, sizeof( LONG ) * axes->Length );
107 if( Envelope.HasValue )
109 // Manual Allocation: cleaned up in Cleanup() method
110 DIENVELOPE *env = new DIENVELOPE();
111 env->dwSize = sizeof( DIENVELOPE );
112 env->dwAttackLevel = Envelope.Value.AttackLevel;
113 env->dwAttackTime = Envelope.Value.AttackTime;
114 env->dwFadeLevel = Envelope.Value.FadeLevel;
115 env->dwFadeTime = Envelope.Value.FadeTime;
117 result.lpEnvelope = env;
120 if( Parameters != nullptr )
122 result.cbTypeSpecificParams = Parameters->Size;
123 result.lpvTypeSpecificParams = Parameters->ToUnmanaged();
129 void EffectParameters::Cleanup( const DIEFFECT &effect )
131 if( effect.rgdwAxes != NULL )
132 delete[] effect.rgdwAxes;
134 if( effect.rglDirection != NULL )
135 delete[] effect.rglDirection;
137 if( effect.lpEnvelope != NULL )
138 delete effect.lpEnvelope;
140 if( effect.lpvTypeSpecificParams != NULL && Parameters != nullptr )
141 Parameters->Release( effect.lpvTypeSpecificParams );
144 void EffectParameters::GetAxes( [Out] array<int>^ %axes, [Out] array<int>^ %directions )
147 directions = this->directions;
150 void EffectParameters::SetAxes( array<int>^ axes, array<int>^ directions )
153 this->directions = directions;