public bool CheckTrigger(JoystickTrigger trigger) { Guid deviceGuid = trigger.GetDeviceInstanceAsGuid(); var cache = this.joysticks.FirstOrDefault <JoyCache>(x => x.JoyStick.Information.InstanceGuid == deviceGuid); if (cache == null) { return(false); } if (trigger.Type == JoystickTriggerType.Button) { return(cache.LastState.GetButtons()[trigger.ButtonNumber]); } else if (trigger.Type == JoystickTriggerType.Axis) { int value = JoyInput.GetAxisValue(cache.LastState, trigger.Axis); if (trigger.AxisPositive) { return(value > 49152); } else { return(value < 16384); } } else if (trigger.Type == JoystickTriggerType.Pov) { int[] povValues = cache.LastState.GetPointOfViewControllers(); if (trigger.PovNumber >= povValues.Length) { return(false); } JoystickTriggerPovDirection direction = this.GetPovDirection(povValues[trigger.PovNumber]); return(((int)trigger.PovDirection & (int)direction) > 0); } return(false); }
private JoystickTrigger SpotTrigger(Joystick joystick, JoystickState oldState, JoystickState newState) { /////////////////////// // Check the buttons bool[] oldButtons = oldState.GetButtons(); bool[] newButtons = newState.GetButtons(); for (int button = 0; button < joystick.Capabilities.ButtonCount; button++) { // it may be pressed, but it only counts if it's newly pressed. if (newButtons[button] && !oldButtons[button]) { var newTrigger = new JoystickTrigger(); newTrigger.DeviceInstance = joystick.Information.InstanceGuid.ToString(); newTrigger.Type = JoystickTriggerType.Button; newTrigger.ButtonNumber = button; return(newTrigger); } } /////////////////////// // Check each axis var axi = (JoystickTriggerAxis[])Enum.GetValues(typeof(JoystickTriggerAxis)); foreach (JoystickTriggerAxis axis in axi) { int oldAxisValue = JoyInput.GetAxisValue(oldState, axis); int newAxisValue = JoyInput.GetAxisValue(newState, axis); if (oldAxisValue != newAxisValue) { bool positive; if (newAxisValue < 16384) { positive = false; } else if (newAxisValue > 49152) { positive = true; } else { continue; // the axis isn't far enough. Screw em! } var newTrigger = new JoystickTrigger(); newTrigger.DeviceInstance = joystick.Information.InstanceGuid.ToString(); newTrigger.Type = JoystickTriggerType.Axis; newTrigger.Axis = axis; newTrigger.AxisPositive = positive; return(newTrigger); } } /////////////////////// // Check each Point-Of-View (POV) thingie. int[] oldPovs = oldState.GetPointOfViewControllers(); int[] newPovs = newState.GetPointOfViewControllers(); for (int pov = 0; pov < joystick.Capabilities.PovCount; pov++) { int newPov = newPovs[pov]; if ((newPov != -1) && (newPov != oldPovs[pov])) { JoystickTriggerPovDirection direction; if (newPov == 0) { direction = JoystickTriggerPovDirection.Up; } else if (newPov == 9000) { direction = JoystickTriggerPovDirection.Right; } else if (newPov == 18000) { direction = JoystickTriggerPovDirection.Down; } else if (newPov == 27000) { direction = JoystickTriggerPovDirection.Left; } else { continue; // we don't accept diagnols here. } var newTrigger = new JoystickTrigger(); newTrigger.DeviceInstance = joystick.Information.InstanceGuid.ToString(); newTrigger.Type = JoystickTriggerType.Pov; newTrigger.PovNumber = pov; newTrigger.PovDirection = direction; return(newTrigger); } } // We checked everything. There's no new trigger to be found. return(null); }