using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using SCJMapper_V2.Common; namespace SCJMapper_V2.Devices.Joystick { public class JoystickList : List, IDisposable { #region Static Parts /// /// Reassigns the mapping color based on the jsAssignment list given /// i.e. prepare the mapping colors for a given jsN assignment /// /// List of 0.. tabs where the value is the jsN number static public void ReassignJsColor( List newJsList ) { // the default colors are aligned with the tabs - the tabs color is never changed but the jsN may // i.e. if the first Tab is assigned as js2 then the second MapColor must get the color of the first Tab int idx = 0; foreach ( int i in newJsList ) { if ( MyColors.TabColor[idx] == MyColors.GamepadColor ) { ; // skip the gamepad for joystick coloring } else { // walk through the tabs if ( i > 0 ) { // this is the jsN for the tab indexed (make it 0 based) MyColors.JsMapColor[i - 1] = MyColors.TabColor[idx]; } } idx++; } } static private Color DeviceColor( int dxnumber ) { int devNumber = 0; // this runs asynch due to the gamepad tab somewhere inbetween.. for ( int mapIndex = 0; mapIndex < MyColors.TabColor.Length; mapIndex++ ) { if ( MyColors.TabColor[mapIndex] == MyColors.GamepadColor ) { ; // skip the gamepad for joystick coloring } else if ( dxnumber == devNumber) { return MyColors.TabColor[mapIndex]; } else { devNumber++;// not found but advance the device } } return Color.Pink; // error but we should see the pink... } #endregion private FormReassign FR = null; public JsReassingList JsReassingList { get; set; } // index - oldJs, newJs public List NewJsList { get; set; } // index is this[idx] protected virtual void Dispose( bool disposing ) { if ( disposing ) { // dispose managed resources if ( FR != null ) FR.Dispose( ); } // free native resources } public void Dispose() { Dispose( true ); GC.SuppressFinalize( this ); } /// /// Deactivate all joysticks /// public void Deactivate( ) { foreach ( JoystickCls j in this ) j.Activated = false; } /// /// Activate all joysticks /// public void Activate( ) { foreach ( JoystickCls j in this ) j.Activated = true; } /// /// pushes the Activated state on a stack /// public void PushActiveState() { foreach ( JoystickCls j in this ) j.PushActiveState(); } /// /// Pop the Activated state from stack /// public void PopActiveState() { foreach ( JoystickCls j in this ) j.PopActiveState( ); } /// /// Show the jsN Reassign Dialog /// public DialogResult ShowReassign( ) { if ( FR == null ) { FR = new FormReassign( this ); JsReassingList = new JsReassingList( ); // used in ReassignJsN NewJsList = new List( ); } FR.ShowDialog( ); if ( FR.Canceled == false ) { int jIdx = 0; // update the new js indication in the tabs foreach ( JoystickCls js in this ) { js.JSAssignment = NewJsList[jIdx++]; if ( js.XmlInstance > 0 ) MyColors.JsMapColor[js.XmlInstance - 1] = DeviceColor( js.DevInstance ); } } return ( FR.Canceled ) ? DialogResult.Cancel : DialogResult.OK; } /// /// Reset the Js Assingment to the new mapping provided /// index of the map is the jsNumber (0 based - i.e. js1 ==> index 0) /// public void ResetJsNAssignment( string[] jsNGUID ) { ClearJsNAssignment( ); // for all supported jsN for ( int i = 0; i < JoystickCls.JSnum_MAX; i++ ) { JoystickCls js = null; if ( !string.IsNullOrEmpty( jsNGUID[i] ) ) js = Find_jsInstance( jsNGUID[i] ); if ( js != null ) { js.JSAssignment = i + 1; // i is 0 based ; jsN is 1 based if ( js.XmlInstance > 0 ) MyColors.JsMapColor[js.XmlInstance - 1] = DeviceColor( js.DevInstance ); } } } /// /// Set JsN to zero /// public void ClearJsNAssignment( ) { int devNum = 0; foreach ( JoystickCls js in this ) { js.JSAssignment = 0; MyColors.JsMapColor[devNum] = DeviceColor( devNum ); devNum++; } } /// /// Returns the Joystick instance for the given jsN /// /// The JsN /// The instance or null if not found public JoystickCls Find_InstanceForjsN( int n ) { foreach ( JoystickCls j in this ) { if ( j.JSAssignment == n ) return j; } return null; } /// /// Returns the JoystickCls Instance for an DevInstance number /// /// The instance [0..] /// The JoystickCls instance or null if not found public JoystickCls Find_jsNForInstance( int n ) { foreach ( JoystickCls j in this ) { if ( j.DevInstance == n ) return j; } return null; } /// /// Returns the Joystick instance for the given device name /// /// The device name /// The instance or null if not found public JoystickCls Find_jsDev( string devName ) { foreach ( JoystickCls j in this ) { if ( j.DevName == devName ) return j; } return null; } /// /// Returns the Joystick instance for the given device instance GUID /// /// The instance GUID /// The instance or null if not found public JoystickCls Find_jsInstance( string instGUID ) { foreach ( JoystickCls j in this ) { if ( j.DevInstanceGUID == instGUID ) return j; } return null; } } }