/// <summary> /// This function will be called from UpdateTimer on main form. /// </summary> /// <param name="device">Device responsible for activity.</param> public void UpdateFromDirectInput(Joystick device) { // Update direct input form and return actions (pressed buttons/dpads, turned axis/sliders). JoystickState state; //List<string> actions = diControl.UpdateFrom(device, out state); DirectInputState diState = null; if (state != null) { diState = new DirectInputState(state); } StopRecording(diState); var contains = PadTabControl.TabPages.Contains(DirectInputTabPage); var enable = device != null; if (!enable && contains) { PadTabControl.TabPages.Remove(DirectInputTabPage); } if (enable && !contains) { PadTabControl.TabPages.Add(DirectInputTabPage); } ForceFeedbackGroupBox.Enabled = enable; TriggersGroupBox.Enabled = enable; AxisToDPadGroupBox.Enabled = enable; DeviceGroupBox.Enabled = enable; LeftThumbXUserControl.Enabled = enable; LeftThumbYUserControl.Enabled = enable; RightThumbXUserControl.Enabled = enable; RightThumbYUserControl.Enabled = enable; if (enable) { UpdateControl(DirectInputTabPage, device.Information.InstanceName); } // If this is different device. if (!AppHelper.IsSameDevice(device, instanceGuid)) { Guid iGuid = Guid.Empty; if (enable) { try { iGuid = device.Information.InstanceGuid; } catch (Exception) { if (SettingManager.Current.IsDebugMode) { throw; } } } instanceGuid = !enable ? Guid.Empty : iGuid; ResetDiMenuStrip(device); } }
/// <summary> /// Acquires the currently connected DirectInput devies /// </summary> public static void AcquireJoysticks() { Devices.Clear(); if (DirectInput != null) { foreach (var device in DirectInput.GetDevices(DeviceType.Gamepad, DeviceEnumerationFlags.AllDevices)) { var joystick = new Joystick(DirectInput, device.ProductGuid); Console.WriteLine(string.Format("Device Acquired! {0}", device.ProductGuid.ToString())); joystick.Acquire(); Devices.Add(joystick); DirectInputState.AddDirectInputDevice(device.ProductGuid); } foreach (var device in DirectInput.GetDevices(DeviceType.Joystick, DeviceEnumerationFlags.AllDevices)) { var joystick = new Joystick(DirectInput, device.ProductGuid); Console.WriteLine(string.Format("Device Acquired! {0}", device.ProductGuid.ToString())); joystick.Acquire(); Devices.Add(joystick); DirectInputState.AddDirectInputDevice(device.ProductGuid); } foreach (var device in DirectInput.GetDevices(DeviceType.Driving, DeviceEnumerationFlags.AllDevices)) { var joystick = new Joystick(DirectInput, device.ProductGuid); Console.WriteLine(string.Format("Device Acquired! {0}", device.ProductGuid.ToString())); joystick.Acquire(); Devices.Add(joystick); DirectInputState.AddDirectInputDevice(device.ProductGuid); } foreach (var device in DirectInput.GetDevices(DeviceType.Flight, DeviceEnumerationFlags.AllDevices)) { var joystick = new Joystick(DirectInput, device.ProductGuid); Console.WriteLine(string.Format("Device Acquired! {0}", device.ProductGuid.ToString())); joystick.Acquire(); Devices.Add(joystick); DirectInputState.AddDirectInputDevice(device.ProductGuid); } } }
/// <summary> /// Called whhen recording is in progress. /// </summary> /// <param name="state">Current direct input activity.</param> /// <returns>True if recording stopped, otherwise false.</returns> public bool StopRecording(DirectInputState state = null) { lock (recordingLock) { // If recording is not in progress then return false. if (!Recording) { recordingSnapshot = null; return(false); } // If recording snapshot was not created yet then... else if (recordingSnapshot == null) { // Make snapshot out of the first state during recordining. recordingSnapshot = state; return(false); } // Get actions by comparing intial snapshot with current state. var actions = recordingSnapshot.CompareTo(state); string action = null; // Must stop recording if null passed. var stop = actions == null; // if at least one action was recorded then... if (!stop && actions.Length > 0) { // If this is DPad ComboBox then... if (CurrentCbx == DPadComboBox) { // Get first action suitable for DPad var dPadAction = actions.FirstOrDefault(x => dPadRx.IsMatch(x)); if (dPadAction != null) { action = dPadRx.Match(dPadAction).Groups[0].Value; stop = true; } } else { // Get first recorded action. action = actions[0]; stop = true; } } // If recording must stop then... if (stop) { Recording = false; RecordingTimer.Stop(); // If stop was initiaded before action was recorded then... if (string.IsNullOrEmpty(action)) { CurrentCbx.Items.Clear(); } else { // If suitable action was recorded then... SettingManager.Current.SetComboBoxValue(CurrentCbx, action); // Save setting and notify if vaue changed. if (SettingManager.Current.SaveSetting(CurrentCbx)) { MainForm.Current.NotifySettingsChange(); } } CurrentCbx.ForeColor = SystemColors.WindowText; CurrentCbx = null; } return(stop); } }
/// <summary> /// Updates the state of DirectInput devices /// </summary> /// <param name="state"></param> private static void DirectInputTimerProc(object state) { // for each joystick connected to the system try { foreach (var device in Devices) { try { var jState = device.GetCurrentState(); DirectInputState.UpdatedPressedElements(device.Information.ProductGuid, jState.Buttons); DirectInputState.UpdatePressedDpads(device.Information.ProductGuid, jState.PointOfViewControllers); DirectInputState.UpdateAxis(device.Information.ProductGuid, jState.X, jState.Y, jState.Z, jState.RotationX, jState.RotationY, jState.RotationZ, jState.Sliders[0], jState.Sliders[1]); if (DirectInputButtonInsert != null && device.Information.ProductGuid == CurrentDevideGuid) { for (int j = 0; j < device.Capabilities.ButtonCount; j++) { if (jState.Buttons[j]) { DirectInputButtonInsert.Invoke(j); return; } } } if (DirectInputAxisInsert != null && device.Information.ProductGuid == CurrentDevideGuid) { string axisChanged = ""; var axis = device.GetObjects(SharpDX.DirectInput.DeviceObjectTypeFlags.Axis); if (jState.X != 0 && jState.X < 30000 || jState.X > 35000) { axisChanged = "X"; } else if (jState.Y != 0 && jState.Y < 30000 || jState.Y > 35000) { axisChanged = "Y"; } else if (jState.Z != 0 && jState.Z < 30000 || jState.Z > 35000) { axisChanged = "Z"; } else if (jState.RotationX != 0 && jState.RotationX < 30000 || jState.RotationX > 35000) { axisChanged = "RotationX"; } else if (jState.RotationY != 0 && jState.RotationY < 30000 || jState.RotationY > 35000) { axisChanged = "RotationY"; } else if (jState.RotationZ != 0 && jState.RotationZ < 30000 || jState.RotationZ > 35000) { axisChanged = "RotationZ"; } else if (jState.Sliders[0] > 5000 && jState.Sliders[0] < 30000 || jState.Sliders[0] > 35000 && jState.Sliders[0] < 60000) { axisChanged = "Slider0"; } else if (jState.Sliders[1] > 5000 && jState.Sliders[1] < 30000 || jState.Sliders[1] > 35000 && jState.Sliders[1] < 60000) { axisChanged = "Slider1"; } if (!string.IsNullOrEmpty(axisChanged)) { DirectInputAxisInsert.Invoke(axisChanged); DirectInputAxisInsert = null; return; } } if (DirectInputDpadInsert != null && device.Information.ProductGuid == CurrentDevideGuid) { for (int j = 0; j < device.Capabilities.PovCount; j++) { if (jState.PointOfViewControllers[j] != -1) { DirectInputDpadInsert.Invoke(j); return; } } } } catch (Exception) { string text = "lol"; } } } catch (Exception) { string text = "lol"; } }
void StartRecording() { lock (recordingLock) { // If recording is not in progress then return. if (Recording) return; Recording = true; recordingSnapshot = null; drawRecordingImage = true; RecordingTimer.Start(); CurrentCbx.ForeColor = SystemColors.GrayText; MainForm.Current.StatusTimerLabel.Text = (CurrentCbx == DPadComboBox) ? "Recording - press any D-Pad button on your direct input device. Press ESC to cancel..." : "Recording - press button, move axis or slider on your direct input device. Press ESC to cancel..."; } }
/// <summary> /// This function will be called from UpdateTimer on main form. /// </summary> /// <param name="device">Device responsible for activity.</param> public void UpdateFromDirectInput(Joystick device) { // Update direct input form and return actions (pressed buttons/dpads, turned axis/sliders). JoystickState state; //List<string> actions = diControl.UpdateFrom(device, out state); DirectInputState diState = null; if (state != null) diState = new DirectInputState(state); StopRecording(diState); var contains = PadTabControl.TabPages.Contains(DirectInputTabPage); var enable = device != null; if (!enable && contains) { PadTabControl.TabPages.Remove(DirectInputTabPage); } if (enable && !contains) { PadTabControl.TabPages.Add(DirectInputTabPage); } ForceFeedbackGroupBox.Enabled = enable; TriggersGroupBox.Enabled = enable; AxisToDPadGroupBox.Enabled = enable; DeviceGroupBox.Enabled = enable; LeftThumbXUserControl.Enabled = enable; LeftThumbYUserControl.Enabled = enable; RightThumbXUserControl.Enabled = enable; RightThumbYUserControl.Enabled = enable; if (enable) { UpdateControl(DirectInputTabPage, device.Information.InstanceName); } // If this is different device. if (!AppHelper.IsSameDevice(device, instanceGuid)) { Guid iGuid = Guid.Empty; if (enable) { try { iGuid = device.Information.InstanceGuid; } catch (Exception) { if (SettingManager.Current.IsDebugMode) throw; } } instanceGuid = !enable ? Guid.Empty : iGuid; ResetDiMenuStrip(device); } }
/// <summary> /// Called whhen recording is in progress. /// </summary> /// <param name="state">Current direct input activity.</param> /// <returns>True if recording stopped, otherwise false.</returns> public bool StopRecording(DirectInputState state = null) { lock (recordingLock) { // If recording is not in progress then return false. if (!Recording) { recordingSnapshot = null; return false; } // If recording snapshot was not created yet then... else if (recordingSnapshot == null) { // Make snapshot out of the first state during recordining. recordingSnapshot = state; return false; } // Get actions by comparing intial snapshot with current state. var actions = recordingSnapshot.CompareTo(state); string action = null; // Must stop recording if null passed. var stop = actions == null; // if at least one action was recorded then... if (!stop && actions.Length > 0){ // If this is DPad ComboBox then... if (CurrentCbx == DPadComboBox){ // Get first action suitable for DPad var dPadAction = actions.FirstOrDefault(x => dPadRx.IsMatch(x)); if (dPadAction != null){ action = dPadRx.Match(dPadAction).Groups[0].Value; stop = true; } } else { // Get first recorded action. action = actions[0]; stop = true; } } // If recording must stop then... if (stop) { Recording = false; RecordingTimer.Stop(); // If stop was initiaded before action was recorded then... if (string.IsNullOrEmpty(action)) { CurrentCbx.Items.Clear(); } else { // If suitable action was recorded then... SettingManager.Current.SetComboBoxValue(CurrentCbx, action); // Save setting and notify if vaue changed. if (SettingManager.Current.SaveSetting(CurrentCbx)) MainForm.Current.NotifySettingsChange(); } CurrentCbx.ForeColor = SystemColors.WindowText; CurrentCbx = null; } return stop; } }