/// <summary> /// Create a tuningOptionID from devClass and xmlInstance (jsN for joysticks, 1 for gamepad) /// </summary> /// <param name="deviceClass">Joysstick or Gamepad class name</param> /// <param name="instance">The xml instance number or 1 for gamepad</param> /// <returns>An ID or a dummy ID if the instance is not found</returns> public static string TuneOptionIDfromJsN(string deviceClass, int instance) { // only search for joysticks if (JoystickCls.IsDeviceClass(deviceClass)) { for (int i = 0; i < m_jsMap.Length; i++) { if (m_jsMap[i] == instance) { return(string.Format("{0}{1}{2}", deviceClass, ID_Delimiter, i.ToString( ))); } } // not found return return(string.Format("{0}{1}{2}", deviceClass, ID_Delimiter, -1)); // will not be found in the collection } else { // gamepad return(string.Format("{0}{1}{2}", deviceClass, ID_Delimiter, instance)); } }
// translate a ToID built from JsN into the internal collection key private static string ToIDfromJsToID(string toIDjs) { string deviceClass = ClassFromID(toIDjs); // only search for joysticks if (JoystickCls.IsDeviceClass(deviceClass)) { string[] e = toIDjs.Split(ID_Delimiter); if (e.Length > 1) { int i = int.Parse(e[1]); return(TuneOptionIDfromJsN(e[0], i)); } else { return(""); } } else { // gamepad, mouse return(toIDjs); } }
/// <summary> /// Aquire the DInput joystick devices /// </summary> /// <returns></returns> public bool InitDirectInput( ) { // Enumerate joysticks in the system. int tabs = 0; cbJs1.Items.Clear( ); cbJs2.Items.Clear( ); cbJs3.Items.Clear( ); // JS dropdowns init // scan the Input for attached devices foreach (DeviceInstance instance in Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly)) { // Create the device interface Device jsDevice = new Device(instance.InstanceGuid); JoystickCls js = null; // we have the first tab made as reference so TabPage[0] already exists if (tabs == 0) { // first panel - The Tab content exists already js = new JoystickCls(jsDevice, this, UC_JoyPanel); // does all device related activities for that particular item } else { // setup the further tab contents along the reference one in TabPage[0] (the control named UC_JoyPanel) tc1.TabPages.Add("Joystick " + (tabs + 1).ToString()); UC_JoyPanel uUC_JoyPanelNew = new UC_JoyPanel( ); tc1.TabPages[tabs].Controls.Add(uUC_JoyPanelNew); uUC_JoyPanelNew.Size = UC_JoyPanel.Size; uUC_JoyPanelNew.Location = UC_JoyPanel.Location; js = new JoystickCls(jsDevice, this, uUC_JoyPanelNew); // does all device related activities for that particular item } m_JS.Add(js); // add to joystick list tc1.TabPages[tabs].Tag = js.DevName; // used to find the tab via JS mapping tc1.TabPages[tabs].BackColor = MyColors.JColor[tabs]; // each tab has its own color cbJs1.Items.Add(js.DevName); cbJs2.Items.Add(js.DevName); cbJs3.Items.Add(js.DevName); // populate DropDowns with the JS name // next tab tabs++; if (tabs == 8) { break; // cannot load more JSticks than predefined Tabs } } /* * // TEST CREATE ALL 8 TABS * for ( int i=(tabs+1); i < 9; i++ ) { * tc1.TabPages.Add( "Joystick " + i.ToString( ) ); * } */ if (tabs == 0) { MessageBox.Show("Unable to create a joystick device. Program will exit.", "No joystick found"); return(false); } InitActionTree( ); return(true); }
/// <summary> /// Read an Options from XML - do some sanity check /// </summary> /// <param name="xml">the XML action fragment</param> /// <returns>True if an action was decoded</returns> public bool fromXML(XElement options) { /* * This can be a lot of the following options * try to do our best.... * * <options type="joystick" instance="1"> * .. * </options> * * * <options type="joystick" instance="1"> * .. * </options> * */ string instance = (string)options.Attribute("instance"); // mandadory string type = (string)options.Attribute("type"); // mandadory if (!int.TryParse(instance, out int nInstance)) { nInstance = 0; } // now dispatch to the instance to capture the content if (JoystickCls.IsDeviceClass(type)) { string toID = TuneOptionIDfromJsN(JoystickCls.DeviceClass, nInstance); // now this might not be availabe if devices have been changed if (this.ContainsKey(toID)) { this[toID].fromXML(options); } else { log.InfoFormat("Read XML Options - joystick instance {0} is not available - dropped this content", nInstance); } } else if (GamepadCls.IsDeviceClass(type)) { string toID = TuneOptionID(GamepadCls.DeviceClass, nInstance); if (this.ContainsKey(toID))// 20170513: bugfix if gamepad is in the XML but not connected right now - ignore { this[toID].fromXML(options); } else { log.InfoFormat("Read XML Options - xboxpad instance {0} is not available - dropped this content", nInstance); } } else if (KeyboardCls.IsDeviceClass(type)) // CIG names mouse - keyboard { string toID = TuneOptionID(MouseCls.DeviceClass, nInstance); if (this.ContainsKey(toID)) { this[toID].fromXML(options); } else { log.InfoFormat("Read XML Options - keyboard(mouse) instance {0} is not available - dropped this content", nInstance); } } return(true); }