/// <summary> /// Creates a new gamepad configuration source from the given string /// </summary> /// <param name="item">The string to parse</param> /// <returns>The new gamepad configuration source</returns> private static GamePadConfigurationSource ParseSource(string item) { if (String.IsNullOrEmpty(item)) { return(new GamePadConfigurationSource()); } switch (item[0]) { case 'a': return(new GamePadConfigurationSource(isAxis: true, index: ParseIndex(item))); case 'b': return(new GamePadConfigurationSource(isAxis: false, index: ParseIndex(item))); case 'h': { HatPosition position; JoystickHat hat = ParseHat(item, out position); return(new GamePadConfigurationSource(hat, position)); } default: throw new InvalidOperationException("[Input] Invalid GamePad configuration value"); } }
/// <summary> /// Creates a new gamepad configuration source from a hat /// </summary> /// <param name="hat">The hat</param> /// <param name="pos">The starting hat position</param> public GamePadConfigurationSource(JoystickHat hat, HatPosition pos) : this() { Type = ConfigurationType.Hat; Hat = hat; map_hat_position = pos; }
/// <summary> /// Parses a string in the format "h#.#" where: /// - the 1st # is the zero-based hat id /// - the 2nd # is a bit-flag defining the hat position /// </summary> /// <param name="item">The string to parse</param> /// <param name="position">The hat position assigned via 'out'</param> /// <returns>The new joystick hat</returns> private static JoystickHat ParseHat(string item, out HatPosition position) { JoystickHat hat = JoystickHat.Hat0; int id = Int32.Parse(item.Substring(1, 1)); int pos = Int32.Parse(item.Substring(3)); position = HatPosition.Centered; switch (pos) { case 1: position = HatPosition.Up; break; case 2: position = HatPosition.Right; break; case 3: position = HatPosition.UpRight; break; case 4: position = HatPosition.Down; break; case 6: position = HatPosition.DownRight; break; case 8: position = HatPosition.Left; break; case 9: position = HatPosition.UpLeft; break; case 12: position = HatPosition.DownLeft; break; default: position = HatPosition.Centered; break; } return(hat + id); }
static JoystickHat ParseHat(string item, out HatPosition position) { // item is in the format "h#.#" where: // - the 1st # is the zero-based hat id // - the 2nd # is a bit-flag defining the hat position JoystickHat hat = JoystickHat.Hat0; int id = Int32.Parse(item.Substring(1, 1)); int pos = Int32.Parse(item.Substring(3)); position = HatPosition.Centered; switch (pos) { case 1: position = HatPosition.Up; break; case 2: position = HatPosition.Right; break; case 3: position = HatPosition.UpRight; break; case 4: position = HatPosition.Down; break; case 6: position = HatPosition.DownRight; break; case 8: position = HatPosition.Left; break; case 9: position = HatPosition.UpLeft; break; case 12: position = HatPosition.DownLeft; break; default: position = HatPosition.Centered; break; } return(hat + id); }
public GamePadConfigurationSource(JoystickHat hat, HatPosition pos) : this() { Type = ConfigurationType.Hat; Hat = hat; map_hat_position = pos; }
/// <summary> /// Initializes a new instance of the <see cref="Input"/> struct. /// </summary> /// <param name="actionName">The name of the action.</param> /// <param name="joystickHat">The joystick hat.</param> public Input(string actionName, JoystickHat joystickHat) { ActionName = actionName; key = null; joystickAxis = null; joystickButton = null; this.joystickHat = joystickHat; }
public void JoystickHat(Joystick joystick, int hat, JoystickHat direction) { EventData ed = new EventData(EventType.JoystickHat); ed.joystick = joystick; ed.idx = hat; ed.direction = direction; list.AddLast(ed); }
public JoystickHatPosition this[JoystickHat hat] { get { if (this.HatCount > (int)hat) return GetDualityJoystickHatPosition(this.state.GetHat(GetOpenTKJoystickHat(hat)).Position); else return JoystickHatPosition.Centered; } }
public JoystickHatPosition this[JoystickHat hat] { get { if (this.HatCount > (int)hat) { return(GetDualityJoystickHatPosition(this.state.GetHat(GetOpenTKJoystickHat(hat)).Position)); } else { return(JoystickHatPosition.Centered); } } }
private static OpenTK.Input.JoystickHat GetOpenTKJoystickHat(JoystickHat hat) { switch (hat) { case JoystickHat.Hat0: return(OpenTK.Input.JoystickHat.Hat0); case JoystickHat.Hat1: return(OpenTK.Input.JoystickHat.Hat1); case JoystickHat.Hat2: return(OpenTK.Input.JoystickHat.Hat2); case JoystickHat.Hat3: return(OpenTK.Input.JoystickHat.Hat3); } return(OpenTK.Input.JoystickHat.Last); }
/// <summary> /// Determines whether the specified <see cref="osuTK.Input.JoystickState"/> is equal to the current <see cref="osuTK.Input.JoystickState"/>. /// </summary> /// <param name="other">The <see cref="osuTK.Input.JoystickState"/> to compare with the current <see cref="osuTK.Input.JoystickState"/>.</param> /// <returns><c>true</c> if the specified <see cref="osuTK.Input.JoystickState"/> is equal to the current /// <see cref="osuTK.Input.JoystickState"/>; otherwise, <c>false</c>.</returns> public bool Equals(JoystickState other) { bool equals = buttons == other.buttons && IsConnected == other.IsConnected; for (int i = 0; equals && i < MaxAxes; i++) { equals &= GetAxisUnsafe(i) == other.GetAxisUnsafe(i); } for (int i = 0; equals && i < MaxHats; i++) { JoystickHat hat = JoystickHat.Hat0 + i; equals &= GetHat(hat).Equals(other.GetHat(hat)); } return(equals); }
public void JoyStickHatTest([Values(ButtonState.Pressed, ButtonState.Released)] ButtonState left, [Values(ButtonState.Pressed, ButtonState.Released)] ButtonState right, [Values(ButtonState.Pressed, ButtonState.Released)] ButtonState up, [Values(ButtonState.Pressed, ButtonState.Released)] ButtonState down) { var hat = new JoystickHat { Left = left, Right = right, Up = up, Down = down, }; Assert.AreEqual(left, hat.Left); Assert.AreEqual(right, hat.Right); Assert.AreEqual(up, hat.Up); Assert.AreEqual(down, hat.Down); }
/// <summary> /// Gets the hat. /// </summary> /// <returns>The hat.</returns> /// <param name="hat">Hat.</param> public JoystickHatState GetHat(JoystickHat hat) { switch (hat) { case JoystickHat.Hat0: return(hat0); case JoystickHat.Hat1: return(hat1); case JoystickHat.Hat2: return(hat2); case JoystickHat.Hat3: return(hat3); default: return(new JoystickHatState()); } }
public EventData(EventType t) { type = t; key = KeyConstant.Unknown; scancode = Scancode.Unknow; joystick = null; direction = JoystickHat.Centered; gamepadButton = GamepadButton.A; gamepadAxis = GamepadAxis.LeftX; text = null; flag = false; fx = 0; fy = 0; fz = 0; fw = 0; fp = 0; idx = 0; idy = 0; lid = 0; }
internal void SetHat(JoystickHat hat, JoystickHatState value) { switch (hat) { case JoystickHat.Hat0: hat0 = value; break; case JoystickHat.Hat1: hat1 = value; break; case JoystickHat.Hat2: hat2 = value; break; case JoystickHat.Hat3: hat3 = value; break; default: return; } }
internal void SetHat(JoystickHat hat, JoystickHatState value) { switch (hat) { case JoystickHat.Hat0: hat0 = value; break; case JoystickHat.Hat1: hat1 = value; break; case JoystickHat.Hat2: hat2 = value; break; case JoystickHat.Hat3: hat3 = value; break; default: throw new ArgumentOutOfRangeException("hat"); } }
private static OpenTK.Input.JoystickHat GetOpenTKJoystickHat(JoystickHat hat) { switch (hat) { case JoystickHat.Hat0: return OpenTK.Input.JoystickHat.Hat0; case JoystickHat.Hat1: return OpenTK.Input.JoystickHat.Hat1; case JoystickHat.Hat2: return OpenTK.Input.JoystickHat.Hat2; case JoystickHat.Hat3: return OpenTK.Input.JoystickHat.Hat3; } return OpenTK.Input.JoystickHat.Last; }
public void SetHat(short collection, HIDPage page, short usage, HatPosition pos) { JoystickHat hat = GetHat(collection, page, usage); State.SetHat(hat, new JoystickHatState(pos)); }
public override void JoystickHat(Joystick joystick, int hat, JoystickHat direction) => behc.OnJoystickHat?.Invoke(joystick, hat, direction);
public void Poll() { for (int i = 0; i < 4; i++) { JoystickCapabilities caps = Joystick.GetCapabilities(i); if (caps.IsConnected && joysticks[i].Description == DisconnectedName) { // New joystick connected joysticks[i] = new LegacyJoystickDevice( i, caps.AxisCount + 2 * caps.HatCount, caps.ButtonCount); //device.Description = Joystick.GetName(i); joysticks[i].Description = ConnectedName; } else if (!caps.IsConnected && joysticks[i].Description != DisconnectedName) { // Joystick disconnected joysticks[i] = new LegacyJoystickDevice(i, 0, 0); joysticks[i].Description = DisconnectedName; } JoystickState state = Joystick.GetState(i); for (int axis_index = 0; axis_index < caps.AxisCount; axis_index++) { JoystickAxis axis = JoystickAxis.Axis0 + axis_index; joysticks[i].SetAxis(axis, state.GetAxis(axis)); } for (int button_index = 0; button_index < caps.ButtonCount; button_index++) { JoystickButton button = JoystickButton.Button0 + button_index; joysticks[i].SetButton(button, state.GetButton(button) == ButtonState.Pressed); } for (int hat_index = 0; hat_index < caps.HatCount; hat_index++) { // LegacyJoystickDriver report hats as pairs of axes // Make sure we have enough axes left for this mapping int axis_index = caps.AxisCount + 2 * hat_index; if (axis_index < JoystickState.MaxAxes) { JoystickHat hat = JoystickHat.Hat0 + hat_index; JoystickHatState hat_state = state.GetHat(hat); JoystickAxis axis = JoystickAxis.Axis0 + axis_index; float x = 0; float y = 0; if (hat_state.IsDown) { y--; } if (hat_state.IsUp) { y++; } if (hat_state.IsLeft) { x--; } if (hat_state.IsRight) { x++; } joysticks[i].SetAxis(axis, x); joysticks[i].SetAxis(axis + 1, y); } } } }
public GamePadState GetState(int index) { JoystickState joy = Joystick.GetState(index); GamePadState pad = new GamePadState(); if (joy.IsConnected) { pad.SetConnected(true); pad.SetPacketNumber(joy.PacketNumber); GamePadConfiguration configuration = GetConfiguration(Joystick.GetGuid(index)); foreach (GamePadConfigurationItem map in configuration) { switch (map.Source.Type) { case ConfigurationType.Axis: { // JoystickAxis -> Buttons/GamePadAxes mapping JoystickAxis source_axis = map.Source.Axis; short value = joy.GetAxisRaw(source_axis); switch (map.Target.Type) { case ConfigurationType.Axis: pad.SetAxis(map.Target.Axis, value); break; case ConfigurationType.Button: // Todo: if SDL2 GameController config is ever updated to // distinguish between negative/positive axes, then remove // Math.Abs below. // Button is considered press when the axis is >= 0.5 from center pad.SetButton(map.Target.Button, Math.Abs(value) >= short.MaxValue >> 1); break; } } break; case ConfigurationType.Button: { // JoystickButton -> Buttons/GamePadAxes mapping JoystickButton source_button = map.Source.Button; bool pressed = joy.GetButton(source_button) == ButtonState.Pressed; switch (map.Target.Type) { case ConfigurationType.Axis: // Todo: if SDL2 GameController config is ever updated to // distinguish between negative/positive axes, then update // the following line to support both. short value = pressed ? short.MaxValue : (map.Target.Axis & (GamePadAxes.LeftTrigger | GamePadAxes.RightTrigger)) != 0 ? short.MinValue : (short)0; pad.SetAxis(map.Target.Axis, value); break; case ConfigurationType.Button: pad.SetButton(map.Target.Button, pressed); break; } } break; case ConfigurationType.Hat: { // JoystickHat -> Buttons/GamePadAxes mapping JoystickHat source_hat = map.Source.Hat; JoystickHatState state = joy.GetHat(source_hat); bool pressed = false; switch (map.Source.HatPosition) { case HatPosition.Down: pressed = state.IsDown; break; case HatPosition.Up: pressed = state.IsUp; break; case HatPosition.Left: pressed = state.IsLeft; break; case HatPosition.Right: pressed = state.IsRight; break; } switch (map.Target.Type) { case ConfigurationType.Axis: // Todo: if SDL2 GameController config is ever updated to // distinguish between negative/positive axes, then update // the following line to support both. short value = pressed ? short.MaxValue : (map.Target.Axis & (GamePadAxes.LeftTrigger | GamePadAxes.RightTrigger)) != 0 ? short.MinValue : (short)0; pad.SetAxis(map.Target.Axis, value); break; case ConfigurationType.Button: pad.SetButton(map.Target.Button, pressed); break; } } break; } } } return(pad); }
/// <summary> /// [GET] Returns the current position of the specified joystick hat. /// </summary> /// <param name="hat"></param> /// <returns></returns> public JoystickHatPosition this[JoystickHat hat] { get { return this.currentState.HatPosition[(int)hat]; } }
void PollJoystick(LinuxJoystickDetails js) { unsafe { const int EventCount = 32; InputEvent *events = stackalloc InputEvent[EventCount]; long length = 0; while (true) { length = (long)Libc.read(js.FileDescriptor, (void *)events, (UIntPtr)(sizeof(InputEvent) * EventCount)); if (length <= 0) { break; } // Only mark the joystick as connected when we actually start receiving events. // Otherwise, the Xbox wireless receiver will register 4 joysticks even if no // actual joystick is connected to the receiver. js.Caps.SetIsConnected(true); js.State.SetIsConnected(true); length /= sizeof(InputEvent); for (int i = 0; i < length; i++) { InputEvent *e = events + i; switch (e->Type) { case EvdevType.ABS: { AxisInfo axis; if (js.AxisMap.TryGetValue((EvdevAxis)e->Code, out axis)) { if (axis.Info.Maximum > axis.Info.Minimum) { if (e->Code >= (int)EvdevAxis.HAT0X && e->Code <= (int)EvdevAxis.HAT3Y) { // We currently treat analogue hats as digital hats // to maintain compatibility with SDL2. We can do // better than this, however. JoystickHat hat = JoystickHat.Hat0 + (e->Code - (int)EvdevAxis.HAT0X) / 2; JoystickHatState pos = js.State.GetHat(hat); int xy_axis = (int)axis.Axis & 0x1; switch (xy_axis) { case 0: // X-axis pos = TranslateHat( e->Value.CompareTo(0) + 1, pos.IsUp ? 0 : pos.IsDown ? 2 : 1); break; case 1: // Y-axis pos = TranslateHat( pos.IsLeft ? 0 : pos.IsRight ? 2 : 1, e->Value.CompareTo(0) + 1); break; } js.State.SetHat(hat, pos); } else { // This axis represents a regular axis or trigger js.State.SetAxis( axis.Axis, (short)Common.HidHelper.ScaleValue(e->Value, axis.Info.Minimum, axis.Info.Maximum, short.MinValue, short.MaxValue)); } } } break; } case EvdevType.KEY: { JoystickButton button; if (js.ButtonMap.TryGetValue((EvdevButton)e->Code, out button)) { js.State.SetButton(button, e->Value != 0); } break; } } // Create a serial number (total seconds in 24.8 fixed point format) int sec = (int)((long)e->Time.Seconds & 0xffffffff); int msec = (int)e->Time.MicroSeconds / 1000; int packet = ((sec & 0x00ffffff) << 24) | Common.HidHelper.ScaleValue(msec, 0, 1000, 0, 255); js.State.SetPacketNumber(packet); } } } }
/// <summary> /// Returns all old hat displacement that stopped since last frame. /// </summary> /// <param name="hat"></param> /// <returns></returns> public JoystickHatPosition HatReleased(JoystickHat hat) { return this.lastState.HatPosition[(int)hat] & (~this.currentState.HatPosition[(int)hat]); }
public JoystickHatEventArgs(JoystickInput inputChannel, JoystickHat hat, JoystickHatPosition hatPos, JoystickHatPosition lastHatPos) : base(inputChannel) { this.hat = hat; this.hatPos = hatPos; this.lastHatPos = lastHatPos; }
internal void InvokeJoystickHat(Joystick joystick, int hat, JoystickHat direction) { OnJoystickHat?.Invoke(joystick, hat, direction); JoystickHat(joystick, hat, direction); }
/// <summary> /// Returns the current position of the specified joystick hat. /// </summary> /// <param name="hat"></param> /// <returns></returns> public JoystickHatPosition HatPosition(JoystickHat hat) { return(this.currentState.HatPosition[(int)hat]); }
/// <summary> /// Returns the current position of the specified joystick hat. /// </summary> /// <param name="hat"></param> /// <returns></returns> public JoystickHatPosition HatPosition(JoystickHat hat) { return this.currentState.HatPosition[(int)hat]; }
/// <summary> /// Called when a joystick hat direction changes. /// </summary> /// <param name="joystick">The joystick object.</param> /// <param name="hat">The hat number.</param> /// <param name="direction">The new hat direction.</param> public virtual void JoystickHat(Joystick joystick, int hat, JoystickHat direction) { }
internal static OpenTK.Input.JoystickHat ToOpenTKHat(JoystickHat hat) { return((OpenTK.Input.JoystickHat)((int)hat)); }
/// <summary> /// [GET] Returns the current position of the specified joystick hat. /// </summary> /// <param name="hat"></param> /// <returns></returns> public JoystickHatPosition this[JoystickHat hat] { get { return(this.currentState.HatPosition[(int)hat]); } }
/// <summary> /// Returns all old hat displacement that stopped since last frame. /// </summary> /// <param name="hat"></param> /// <returns></returns> public JoystickHatPosition HatReleased(JoystickHat hat) { return(this.lastState.HatPosition[(int)hat] & (~this.currentState.HatPosition[(int)hat])); }