@ -1,11 +1,11 @@
using System ;
using SharpDX ;
using SharpDX.DirectInput ;
using System ;
using System.Collections.Generic ;
using System.Reflection ;
using System.Text ;
using System.Windows.Forms ;
using SharpDX ;
using SharpDX.DirectInput ;
namespace SCJMapper_V2
{
/// <summary>
@ -62,7 +62,8 @@ namespace SCJMapper_V2
static public int JSNum ( String jsTag )
{
int retNum = 0 ;
if ( ! String . IsNullOrEmpty ( jsTag ) ) {
if ( ! String . IsNullOrEmpty ( jsTag ) )
{
int . TryParse ( jsTag . Substring ( 2 , 1 ) , out retNum ) ;
}
return retNum ;
@ -102,10 +103,12 @@ namespace SCJMapper_V2
// Set the cooperative level for the device.
m_device . SetCooperativeLevel ( m_hwnd , CooperativeLevel . Exclusive | CooperativeLevel . Foreground ) ;
// Enumerate all the objects on the device.
foreach ( DeviceObjectInstance d in m_device . GetObjects ( ) ) {
foreach ( DeviceObjectInstance d in m_device . GetObjects ( ) )
{
// For axes that are returned, set the DIPROP_RANGE property for the
// enumerated axis in order to scale min/max values.
if ( ( 0 ! = ( d . ObjectId . Flags & DeviceObjectTypeFlags . Axis ) ) ) {
if ( ( 0 ! = ( d . ObjectId . Flags & DeviceObjectTypeFlags . Axis ) ) )
{
// Set the range for the axis.
m_device . Properties . Range = new InputRange ( - 1000 , + 1000 ) ;
}
@ -132,32 +135,40 @@ namespace SCJMapper_V2
private void UpdateControls ( DeviceObjectInstance d )
{
// Set the UI to reflect what objects the joystick supports.
if ( ObjectGuid . XAxis = = d . ObjectType ) {
if ( ObjectGuid . XAxis = = d . ObjectType )
{
m_jPanel . Xe = true ;
m_jPanel . Xname = d . Name + ":" ;
}
if ( ObjectGuid . YAxis = = d . ObjectType ) {
if ( ObjectGuid . YAxis = = d . ObjectType )
{
m_jPanel . Ye = true ;
m_jPanel . Yname = d . Name + ":" ;
}
if ( ObjectGuid . ZAxis = = d . ObjectType ) {
if ( ObjectGuid . ZAxis = = d . ObjectType )
{
m_jPanel . Ze = true ;
m_jPanel . Zname = d . Name + ":" ;
}
if ( ObjectGuid . RxAxis = = d . ObjectType ) {
if ( ObjectGuid . RxAxis = = d . ObjectType )
{
m_jPanel . Xre = true ;
m_jPanel . Xrname = d . Name + ":" ;
}
if ( ObjectGuid . RyAxis = = d . ObjectType ) {
if ( ObjectGuid . RyAxis = = d . ObjectType )
{
m_jPanel . Yre = true ;
m_jPanel . Yrname = d . Name + ":" ;
}
if ( ObjectGuid . RzAxis = = d . ObjectType ) {
if ( ObjectGuid . RzAxis = = d . ObjectType )
{
m_jPanel . Zre = true ;
m_jPanel . Zrname = d . Name + ":" ;
}
if ( ObjectGuid . Slider = = d . ObjectType ) {
switch ( m_sliderCount + + ) {
if ( ObjectGuid . Slider = = d . ObjectType )
{
switch ( m_sliderCount + + )
{
case 0 :
m_jPanel . S1e = true ;
m_jPanel . S1name = d . Name + ":" ;
@ -169,8 +180,10 @@ namespace SCJMapper_V2
break ;
}
}
if ( ObjectGuid . PovController = = d . ObjectType ) {
switch ( m_numPOVs + + ) {
if ( ObjectGuid . PovController = = d . ObjectType )
{
switch ( m_numPOVs + + )
{
case 0 :
m_jPanel . H1e = true ;
m_jPanel . H1name = d . Name + ":" ;
@ -200,13 +213,24 @@ namespace SCJMapper_V2
/// <returns>The last action as CryEngine compatible string</returns>
public String GetLastChange ( )
{
if ( m_state . X ! = m_prevState . X ) m_lastItem = "x" ;
if ( m_state . Y ! = m_prevState . Y ) m_lastItem = "y" ;
if ( m_state . Z ! = m_prevState . Z ) m_lastItem = "throttlez" ; // this is not z because it usually maps the throttle
// TODO: Expand this out into a joystick class (see commit for details)
Dictionary < string , string > axies = new Dictionary < string , string > ( )
{
{ "X" , "x" } ,
{ "Y" , "y" } ,
{ "Z" , "throttlez" } , // this is not z because it usually maps the throttle
{ "RotationX" , "rotx" } ,
{ "RotationY" , "roty" } ,
{ "RotationZ" , "rotz" }
} ;
foreach ( KeyValuePair < string , string > entry in axies )
{
PropertyInfo axisProperty = typeof ( JoystickState ) . GetProperty ( entry . Key ) ;
if ( m_state . RotationX ! = m_prevState . RotationX ) m_lastItem = "rotx" ;
if ( m_state . RotationY ! = m_prevState . RotationY ) m_lastItem = "roty" ;
if ( m_state . RotationZ ! = m_prevState . RotationZ ) m_lastItem = "rotz" ;
if ( DidAxisChange ( ( int ) axisProperty . GetValue ( this . m_state , null ) , ( int ) axisProperty . GetValue ( this . m_prevState , null ) ) )
this . m_lastItem = entry . Value ;
}
int [ ] slider = m_state . Sliders ;
int [ ] pslider = m_prevState . Sliders ;
@ -221,12 +245,33 @@ namespace SCJMapper_V2
if ( pov [ 3 ] > = 0 ) if ( pov [ 3 ] ! = ppov [ 3 ] ) m_lastItem = "hat4_" + HatDir ( pov [ 0 ] ) ;
bool [ ] buttons = m_state . Buttons ;
for ( int bi = 0 ; bi < buttons . Length ; bi + + ) {
if ( buttons [ bi ] ) m_lastItem = "button" + ( bi + 1 ) . ToString ( ) ;
bool [ ] prevButtons = m_prevState . Buttons ;
for ( int bi = 0 ; bi < buttons . Length ; bi + + )
{
if ( buttons [ bi ] & & buttons [ bi ] ! = prevButtons [ bi ] )
m_lastItem = "button" + ( bi + 1 ) . ToString ( ) ;
}
return m_lastItem ;
}
///<summary>
/// Figure out if an axis changed enough to consider it as a changed state
/// </summary>
private bool DidAxisChange ( int current , int prev )
{
// determine if the axis drifts more than x% to account for bounce
// old-new/old
if ( current = = prev )
return false ;
if ( prev = = 0 )
prev = 1 ;
int changepct = Math . Abs ( prev ) - Math . Abs ( current ) / Math . Abs ( prev ) ;
// if the axis has changed more than 70% relative to it's last value
return changepct > 70 ? true : false ;
}
/// <summary>
/// Show the current props in the GUI
/// </summary>
@ -260,7 +305,8 @@ namespace SCJMapper_V2
bool [ ] buttons = m_state . Buttons ;
int button = 0 ;
foreach ( bool b in buttons ) {
foreach ( bool b in buttons )
{
if ( b )
strText + = ( button + 1 ) . ToString ( "00 " ) ; // buttons are 1 based
button + + ;
@ -279,18 +325,23 @@ namespace SCJMapper_V2
return ;
// Poll the device for info.
try {
try
{
m_device . Poll ( ) ;
}
catch ( SharpDXException e ) {
if ( ( e . ResultCode = = ResultCode . NotAcquired ) | | ( e . ResultCode = = ResultCode . InputLost ) ) {
catch ( SharpDXException e )
{
if ( ( e . ResultCode = = ResultCode . NotAcquired ) | | ( e . ResultCode = = ResultCode . InputLost ) )
{
// Check to see if either the app needs to acquire the device, or
// if the app lost the device to another process.
try {
try
{
// Acquire the device.
m_device . Acquire ( ) ;
}
catch ( SharpDXException ) {
catch ( SharpDXException )
{
// Failed to acquire the device.
// This could be because the app doesn't have focus.
return ;
@ -304,7 +355,8 @@ namespace SCJMapper_V2
try { m_state = m_device . GetCurrentState ( ) ; }
// Catch any exceptions. None will be handled here,
// any device re-aquisition will be handled above.
catch ( SharpDXException ) {
catch ( SharpDXException )
{
return ;
}