using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SCJMapper_V2.Layout { /// /// A list of ActionItems /// class ActionItemList : List { public class Tracker { public string PidVid { get; set; } = ""; private short m_lowN = 100; private short m_highN = 0; public short LowN { get => m_lowN; } public short HighN { get => m_highN; } /// /// Add a JsN number and keep track of low and high /// /// public void AddNumber( short n ) { m_lowN = ( n < m_lowN ) ? n : m_lowN; m_highN = ( n > m_highN ) ? n : m_highN; } } private Dictionary m_trackers = new Dictionary( ); private Tracker GetTracker( string pidVid ) { if ( m_trackers.ContainsKey( pidVid ) ) { return m_trackers[pidVid]; } return null; } /// /// Returns true if the jsN number is the lower one /// /// Device pidvid /// The JsN number /// public bool IsFirstInstance( string pidVid, short jsN ) { var tracker = GetTracker( pidVid ); if ( tracker == null ) return true; return ( jsN == tracker.LowN ); } /// /// Keep track of JsNs while adding stuff /// /// public new void Add( ActionItem actionItem ) { base.Add( actionItem ); if ( actionItem.InputTypeLetter != "J" ) return; // track only Joysticks if ( !m_trackers.ContainsKey( actionItem.DevicePidVid ) ) { // newly seen device m_trackers.Add( actionItem.DevicePidVid, new Tracker( ) { PidVid = actionItem.DevicePidVid } ); } m_trackers[actionItem.DevicePidVid].AddNumber( actionItem.InputTypeNumber ); } /// /// Get the Devices contained in the List /// public Dictionary Devices { get { var list = new Dictionary( ); foreach ( var si in this ) { if ( !list.ContainsKey( si.InputTypeNumber ) ) { list.Add( si.InputTypeNumber, si.DeviceName ); } } return list; } } /// /// Get the Joystick Devices contained in the List /// public Dictionary JsDevices { get { var list = new Dictionary( ); foreach ( var si in this ) { if (si.InputTypeLetter == "J" ) { if ( !list.ContainsKey( si.InputTypeNumber ) ) { list.Add( si.InputTypeNumber, si.DeviceName ); } } } return list; } } /// /// Override the current JS guids with the given one /// /// A list of GUIDs public void OverrideJsDevices (List guids ) { for (int aiidx=0; aiidx= ai.InputTypeNumber ) { this[aiidx].DeviceProdGuid = guids[ai.InputTypeNumber - 1]; // the guid list is 0 based, jsN is 1 based.. } } } } } }