public bool IsButtonPressed(uint port, RETRO_DEVICE_ID_JOYPAD button) { HidState state = _currentState; if (state == null) { return(false); } ushort hidButton; if (_buttonToButtonMappings.TryGetValue(button, out hidButton)) { return(IsButtonPressed(hidButton, state)); } DirectionPadState directionPadState; if (_directionPadToButtonMappings.TryGetValue(button, out directionPadState)) { return(IsDirectionPadPressed(directionPadState, state)); } HidAxis axis; if (_analogToButtonMappings.TryGetValue(button, out axis)) { return(IsAxisPressed(axis, state, _axisDeadZone)); } return(false); }
public DeviceInput GetPressedInput() { HidState state = _currentState; if (state == null) { return(null); } if (state.Buttons.Count > 0) { string buttonId = state.Buttons.First().ToString(); return(new DeviceInput(buttonId, buttonId, InputType.Button)); } if (IsDirectionPadStateValid(state.DirectionPadState)) { string direction = state.DirectionPadState.ToString(); return(new DeviceInput(direction, direction, InputType.Button)); } foreach (HidAxisState axis in state.AxisStates.Values) { short value = NumericUtils.UIntToShort(axis.Value); if (value > HidGameControl.DEFAULT_DEADZONE || value < -HidGameControl.DEFAULT_DEADZONE) { bool isPositive = value > 0; return(new DeviceInput(string.Format("{0}({1}){2}", axis.Index, axis.Name, isPositive ? "+" : "-"), axis.Index.ToString(), InputType.Axis, isPositive)); } } return(null); }
protected void HidListener_StateChanged(object sender, HidStateEventArgs e) { HidState state = e.State; if (state.VendorId == _vendorId && state.ProductId == _productId) { _currentState = e.State; } }
public short GetAnalog(uint port, RETRO_DEVICE_INDEX_ANALOG index, RETRO_DEVICE_ID_ANALOG direction) { HidState state = _currentState; if (state == null) { return(0); } RetroAnalogDevice positive; RetroAnalogDevice negative; RetroPadMapping.GetAnalogEnums(index, direction, out positive, out negative); short positivePosition = 0; short negativePosition = 0; HidAxis axis; ushort button; DirectionPadState directionPadState; if (_analogToAnalogMappings.TryGetValue(positive, out axis)) { positivePosition = GetAxisPositionMapped(axis, state, true); } else if (_directionPadToAnalogMappings.TryGetValue(positive, out directionPadState) && IsDirectionPadPressed(directionPadState, state)) { positivePosition = short.MaxValue; } else if (_buttonToAnalogMappings.TryGetValue(positive, out button) && IsButtonPressed(button, state)) { positivePosition = short.MaxValue; } if (_analogToAnalogMappings.TryGetValue(negative, out axis)) { negativePosition = GetAxisPositionMapped(axis, state, false); } else if (_directionPadToAnalogMappings.TryGetValue(negative, out directionPadState) && IsDirectionPadPressed(directionPadState, state)) { positivePosition = short.MinValue; } else if (_buttonToAnalogMappings.TryGetValue(negative, out button) && IsButtonPressed(button, state)) { negativePosition = short.MinValue; } if (positivePosition != 0 && negativePosition == 0) { return(positivePosition); } if (positivePosition == 0 && negativePosition != 0) { return(negativePosition); } return(0); }
public static short GetAxisPosition(HidAxis axis, HidState state) { HidAxisState axisState; if (!state.AxisStates.TryGetValue(axis.Axis, out axisState)) { return(0); } return(NumericUtils.UIntToShort(axisState.Value)); }
void OnHidEvent(object aSender, Event aHidEvent) { if (!aHidEvent.Device.IsGamePad) { return; } #if DEBUG if (aHidEvent.IsRepeat) { Logger.Debug("HID: Repeat"); } #endif HashSet <ushort> buttons = new HashSet <ushort>(); foreach (ushort usage in aHidEvent.Usages) { buttons.Add(usage); } //For each axis Dictionary <ushort, HidAxisState> axisStates = new Dictionary <ushort, HidAxisState>(); foreach (KeyValuePair <HIDP_VALUE_CAPS, uint> entry in aHidEvent.UsageValues) { //HatSwitch is handled separately as direction pad state if (entry.Key.IsRange || entry.Key.NotRange.Usage == (ushort)GenericDesktop.HatSwitch) { continue; } //Get our usage type Type usageType = HidUtils.UsageType((UsagePage)entry.Key.UsagePage); if (usageType == null) { continue; } //Get the name of our axis string name = Enum.GetName(usageType, entry.Key.NotRange.Usage); ushort index = entry.Key.NotRange.DataIndex; axisStates[index] = new HidAxisState(name, index, entry.Value, entry.Key.BitSize); } DirectionPadState directionPadState = aHidEvent.GetDirectionPadState(); HidState state = new HidState { VendorId = aHidEvent.Device.VendorId, ProductId = aHidEvent.Device.ProductId, Name = aHidEvent.Device.Name, FriendlyName = aHidEvent.Device.FriendlyName, Buttons = buttons, AxisStates = axisStates, DirectionPadState = directionPadState }; OnStateChanged(new HidStateEventArgs(state)); }
public static bool IsAxisPressed(HidAxis axis, HidState state, short deadZone) { HidAxisState axisState; if (!state.AxisStates.TryGetValue(axis.Axis, out axisState)) { return(false); } short value = NumericUtils.UIntToShort(axisState.Value); return(axis.PositiveValues ? value > deadZone : value < -deadZone); }
public static short GetAxisPositionMapped(HidAxis axis, HidState state, bool isMappedToPositive) { short position = GetAxisPosition(axis, state); if (position == 0 || (axis.PositiveValues && position <= 0) || (!axis.PositiveValues && position >= 0)) { return(0); } bool shouldInvert = (axis.PositiveValues && !isMappedToPositive) || (!axis.PositiveValues && isMappedToPositive); if (shouldInvert) { position = (short)(-position - 1); } return(position); }
public bool UpdateState(HidState state) { if (_name != null) { if (state.Name != _name) { return(false); } } else { if (state.ProductId != _productId || state.VendorId != _vendorId) { return(false); } _name = state.Name; ServiceRegistration.Get <ILogger>().Debug("HidGameControl: Mapped Hid controller configuration to device {0}, {1}, {2}", state.Name, state.ProductId, state.VendorId); } _currentState = state; return(true); }
public static bool IsDirectionPadPressed(DirectionPadState directionPadState, HidState state) { return(state.DirectionPadState == directionPadState); }
public static bool IsButtonPressed(ushort button, HidState state) { return(state.Buttons.Contains(button)); }
public HidStateEventArgs(HidState state) { State = state; }