public GamepadCollectData(JoystickState state) { { XL = state.GetAxis(JoystickAxis.Axis0); XR = state.GetAxis(JoystickAxis.Axis3); LT = state.GetAxis(JoystickAxis.Axis2); RT = state.GetAxis(JoystickAxis.Axis5); B = state.GetButton(JoystickButton.Button1); A = state.GetButton(JoystickButton.Button0); } }
public void UpdateState(JoystickState state) { this.IsConnected = state.IsConnected; for (var i = 0; i < this.axes_.Length; ++i) { var axis = this.axes_[i]; axis.Value = state.GetAxis(i); } for (var i = 0; i < this.buttons_.Length; ++i) { var button = this.buttons_[i]; var buttonState = state.GetButton(i); if (buttonState == OpenTK.Input.ButtonState.Pressed) { button.Down(); } else { button.Up(); } } }
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, 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 < (int)caps.AxisCount; axis_index++) { JoystickAxis axis = JoystickAxis.Axis0 + axis_index; joysticks[i].SetAxis(axis, state.GetAxis(axis)); } for (int button_index = 0; button_index < (int)caps.ButtonCount; button_index++) { JoystickButton button = JoystickButton.Button0 + button_index; joysticks[i].SetButton(button, state.GetButton(button) == ButtonState.Pressed); } } }
public bool GetButton(int joystick, ref JoystickButton button, params JoystickButton[] excludeButtons) { for (int i = 0; i < numJoysticks; ++i) { if (IsConnected(i)) { JoystickState state = Joystick.GetState(i); foreach (JoystickButton enumVal in Enum.GetValues(typeof(JoystickButton))) { if (state.GetButton(enumVal) == ButtonState.Pressed) { bool cont = false; foreach (JoystickButton exclude in excludeButtons) { if (exclude == enumVal) { cont = true; break; } } if (cont) { continue; } button = enumVal; return(true); } } } } return(false); }
void InitializeCallbacks() { const int dzp = 400; const int dzn = -400; names.Clear(); actions.Clear(); NumButtons = 0; AddItem("X+", () => state.GetAxis(0) >= dzp); AddItem("X-", () => state.GetAxis(0) <= dzn); AddItem("Y+", () => state.GetAxis(1) >= dzp); AddItem("Y-", () => state.GetAxis(1) <= dzn); AddItem("Z+", () => state.GetAxis(2) >= dzp); AddItem("Z-", () => state.GetAxis(2) <= dzn); // Enjoy our delicious sliders. They're smaller than regular burgers but cost more. int jb = 1; for (int i = 0; i < 64; i++) { AddItem($"B{jb}", () => state.GetButton(i) == ButtonState.Pressed); jb++; } jb = 1; foreach (JoystickHat enval in Enum.GetValues(typeof(JoystickHat))) { AddItem($"POV{jb}U", () => state.GetHat(enval).IsUp); AddItem($"POV{jb}D", () => state.GetHat(enval).IsDown); AddItem($"POV{jb}L", () => state.GetHat(enval).IsLeft); AddItem($"POV{jb}R", () => state.GetHat(enval).IsRight); jb++; } }
void InitializeJoystickControls() { // OpenTK GamePad axis return float values (as opposed to the shorts of SlimDX) const float ConversionFactor = 1.0f / short.MaxValue; const float dzp = (short)4000 * ConversionFactor; const float dzn = (short)-4000 * ConversionFactor; //const float dzt = 0.6f; // axis AddItem("X+", () => jState.GetAxis(0) >= dzp); AddItem("X-", () => jState.GetAxis(0) <= dzn); AddItem("Y+", () => jState.GetAxis(1) >= dzp); AddItem("Y-", () => jState.GetAxis(1) <= dzn); AddItem("Z+", () => jState.GetAxis(2) >= dzp); AddItem("Z-", () => jState.GetAxis(2) <= dzn); AddItem("W+", () => jState.GetAxis(3) >= dzp); AddItem("W-", () => jState.GetAxis(3) <= dzn); AddItem("V+", () => jState.GetAxis(4) >= dzp); AddItem("V-", () => jState.GetAxis(4) <= dzn); AddItem("S+", () => jState.GetAxis(5) >= dzp); AddItem("S-", () => jState.GetAxis(5) <= dzn); AddItem("Q+", () => jState.GetAxis(6) >= dzp); AddItem("Q-", () => jState.GetAxis(6) <= dzn); AddItem("P+", () => jState.GetAxis(7) >= dzp); AddItem("P-", () => jState.GetAxis(7) <= dzn); AddItem("N+", () => jState.GetAxis(8) >= dzp); AddItem("N-", () => jState.GetAxis(8) <= dzn); // should be enough axis, but just in case: for (int i = 9; i < 64; i++) { int j = i; AddItem(string.Format("Axis{0}+", j.ToString()), () => jState.GetAxis(j) >= dzp); AddItem(string.Format("Axis{0}-", j.ToString()), () => jState.GetAxis(j) <= dzn); } // buttons for (int i = 0; i < _joystickCapabilities.Value.ButtonCount; i++) { int j = i; AddItem(string.Format("B{0}", i + 1), () => jState.GetButton(j) == ButtonState.Pressed); } // hats AddItem("POV1U", () => jState.GetHat(JoystickHat.Hat0).IsUp); AddItem("POV1D", () => jState.GetHat(JoystickHat.Hat0).IsDown); AddItem("POV1L", () => jState.GetHat(JoystickHat.Hat0).IsLeft); AddItem("POV1R", () => jState.GetHat(JoystickHat.Hat0).IsRight); AddItem("POV2U", () => jState.GetHat(JoystickHat.Hat1).IsUp); AddItem("POV2D", () => jState.GetHat(JoystickHat.Hat1).IsDown); AddItem("POV2L", () => jState.GetHat(JoystickHat.Hat1).IsLeft); AddItem("POV2R", () => jState.GetHat(JoystickHat.Hat1).IsRight); AddItem("POV3U", () => jState.GetHat(JoystickHat.Hat2).IsUp); AddItem("POV3D", () => jState.GetHat(JoystickHat.Hat2).IsDown); AddItem("POV3L", () => jState.GetHat(JoystickHat.Hat2).IsLeft); AddItem("POV3R", () => jState.GetHat(JoystickHat.Hat2).IsRight); AddItem("POV4U", () => jState.GetHat(JoystickHat.Hat3).IsUp); AddItem("POV4D", () => jState.GetHat(JoystickHat.Hat3).IsDown); AddItem("POV4L", () => jState.GetHat(JoystickHat.Hat3).IsLeft); AddItem("POV4R", () => jState.GetHat(JoystickHat.Hat3).IsRight); }
public void UpdateInputState() { JoystickState state = Joystick.GetState(0); if (state.IsConnected) { if (false) { Console.WriteLine(@"axis0={0}", state.GetAxis(JoystickAxis.Axis0)); // side to side Console.WriteLine(@"axis1={0}", state.GetAxis(JoystickAxis.Axis1)); // forward back Console.WriteLine(@"axis2={0}", state.GetAxis(JoystickAxis.Axis2)); // knob Console.WriteLine(@"axis3={0}", state.GetAxis(JoystickAxis.Axis3)); // z-rotation Console.WriteLine(@"axis4={0}", state.GetAxis(JoystickAxis.Axis4)); Console.WriteLine(@"axis5={0}", state.GetAxis(JoystickAxis.Axis5)); Console.WriteLine(@"axis6={0}", state.GetAxis(JoystickAxis.Axis6)); Console.WriteLine(@"axis7={0}", state.GetAxis(JoystickAxis.Axis7)); Console.WriteLine(@"button0={0}", state.GetButton(JoystickButton.Button0)); Console.WriteLine(@"button1={0}", state.GetButton(JoystickButton.Button1)); Console.WriteLine(@"button2={0}", state.GetButton(JoystickButton.Button2)); Console.WriteLine(@"button3={0}", state.GetButton(JoystickButton.Button3)); Console.WriteLine(@"button4={0}", state.GetButton(JoystickButton.Button4)); Console.WriteLine(@"button5={0}", state.GetButton(JoystickButton.Button5)); Console.WriteLine(@"button6={0}", state.GetButton(JoystickButton.Button6)); Console.WriteLine(@"button7={0}", state.GetButton(JoystickButton.Button7)); Console.WriteLine(@"button8={0}", state.GetButton(JoystickButton.Button8)); Console.WriteLine(@"button9={0}", state.GetButton(JoystickButton.Button9)); Console.WriteLine(@"button10={0}", state.GetButton(JoystickButton.Button10)); // hat up Console.WriteLine(@"button11={0}", state.GetButton(JoystickButton.Button11)); // hat left Console.WriteLine(@"button12={0}", state.GetButton(JoystickButton.Button12)); // hat right Console.WriteLine(@"button13={0}", state.GetButton(JoystickButton.Button13)); // hat down Console.WriteLine(@"button14={0}", state.GetButton(JoystickButton.Button14)); Console.WriteLine(@"button15={0}", state.GetButton(JoystickButton.Button15)); Console.ReadKey(); } var x = state.GetAxis(JoystickAxis.Axis0); // used for rotating up var y = state.GetAxis(JoystickAxis.Axis1); var z = state.GetAxis(JoystickAxis.Axis3); var forward = state.GetAxis(JoystickAxis.Axis2); var strafeUp = state.GetButton(JoystickButton.Button10) == OpenTK.Input.ButtonState.Pressed; var strafeLeft = state.GetButton(JoystickButton.Button11) == OpenTK.Input.ButtonState.Pressed; var strafeRight = state.GetButton(JoystickButton.Button12) == OpenTK.Input.ButtonState.Pressed; var strafeDown = state.GetButton(JoystickButton.Button13) == OpenTK.Input.ButtonState.Pressed; var reset = state.GetButton(JoystickButton.Button8) == OpenTK.Input.ButtonState.Pressed; var goSlow = state.GetButton(JoystickButton.Button1) == OpenTK.Input.ButtonState.Pressed; var goFast = state.GetButton(JoystickButton.Button3) == OpenTK.Input.ButtonState.Pressed; if (Math.Abs(forward) > 0.1f) { MoveForward(-forward * WalkSpeed); } if (Math.Abs(x) > 0.1f) { RotateUp(x); } if (strafeUp || strafeDown || strafeRight || strafeLeft) { var strafeX = strafeLeft ? StrafeSpeed : strafeRight ? -StrafeSpeed : 0f; var strafeY = strafeUp ? StrafeSpeed : strafeDown ? -StrafeSpeed : 0f; Strafe(strafeX, strafeY); } if (z > 0.05f || z < -0.05f || y > 0.05f || y < -0.05f) { //Console.Out.WriteLine("x="+z+" y="+z); Rotate(z * RotateSpeed, y * RotateSpeed); } if (reset) { Reset(); } if (!_lastGoSlow && goSlow) { SlowDown(); } if (!_lastGoFast && goFast) { SpeedUp(); } _lastGoSlow = goSlow; _lastGoFast = goFast; } if (false) // this.Focused || GraphicsWindow.Focused) { //DirectInput.KeyboardState ks = keyboardDevice.GetCurrentKeyboardState(); //float m = 1f; //if (ks[DirectInput.Key.LeftShift]) m = 10f; //if (ks[DirectInput.Key.W]) camera.MoveForward(m); //if (ks[DirectInput.Key.S]) camera.MoveForward(-m); //if (ks[DirectInput.Key.A]) camera.Strafe(-m, 0f); //if (ks[DirectInput.Key.D]) camera.Strafe(m, 0f); //if (ks[DirectInput.Key.E]) camera.Strafe(0f, m); //if (ks[DirectInput.Key.C]) camera.Strafe(0f, -m); //if (ks[DirectInput.Key.P]) camera.describe(); } }
/// <summary> /// Reads the input from the controller. /// </summary> /// <param name="joystick">The state of the joystick to read input from.</param> internal static void ReadInput(JoystickState joystick) { double brakeAxis = joystick.GetAxis(0); double powerAxis = joystick.GetAxis(1); foreach (var notch in BrakeNotchMap) { if (brakeAxis >= GetRangeMin((byte)notch.Key) && brakeAxis <= GetRangeMax((byte)notch.Key)) { InputTranslator.BrakeNotch = notch.Value; } } foreach (var notch in PowerNotchMap) { if (powerAxis >= GetRangeMin((byte)notch.Key) && powerAxis <= GetRangeMax((byte)notch.Key)) { InputTranslator.PowerNotch = notch.Value; } } InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Select] = joystick.GetButton(ButtonIndex.Select); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Start] = joystick.GetButton(ButtonIndex.Start); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.A] = joystick.GetButton(ButtonIndex.A); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.B] = joystick.GetButton(ButtonIndex.B); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.C] = joystick.GetButton(ButtonIndex.C); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.D] = joystick.GetButton(ButtonIndex.D); if (hasDirectionButtons) { InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Up] = (ButtonState)(joystick.GetHat(JoystickHat.Hat0).IsUp ? 1 : 0); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Down] = (ButtonState)(joystick.GetHat(JoystickHat.Hat0).IsDown ? 1 : 0); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Left] = (ButtonState)(joystick.GetHat(JoystickHat.Hat0).IsLeft ? 1 : 0); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Right] = (ButtonState)(joystick.GetHat(JoystickHat.Hat0).IsRight ? 1 : 0); } }
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); } } } }
internal override ButtonState GetButton(int button) { return(state.GetButton(button)); }
/// <summary> /// Reads the input from the controller. /// </summary> internal override void ReadInput() { JoystickState joystick = Joystick.GetState(joystickIndex); PowerNotchesEnum powerNotch = PowerNotchesEnum.None; BrakeNotchesEnum brakeNotch = BrakeNotchesEnum.None; powerNotch = joystick.IsButtonDown(ButtonIndex.Power1) ? powerNotch | PowerNotchesEnum.Power1 : powerNotch & ~PowerNotchesEnum.Power1; brakeNotch = joystick.IsButtonDown(ButtonIndex.Brake1) ? brakeNotch | BrakeNotchesEnum.Brake1 : brakeNotch & ~BrakeNotchesEnum.Brake1; brakeNotch = joystick.IsButtonDown(ButtonIndex.Brake2) ? brakeNotch | BrakeNotchesEnum.Brake2 : brakeNotch & ~BrakeNotchesEnum.Brake2; brakeNotch = joystick.IsButtonDown(ButtonIndex.Brake3) ? brakeNotch | BrakeNotchesEnum.Brake3 : brakeNotch & ~BrakeNotchesEnum.Brake3; brakeNotch = joystick.IsButtonDown(ButtonIndex.Brake4) ? brakeNotch | BrakeNotchesEnum.Brake4 : brakeNotch & ~BrakeNotchesEnum.Brake4; if (UsesHat) { // The adapter uses the hat to map the direction buttons. // This is the case of some PlayStation adapters. powerNotch = joystick.GetHat((JoystickHat)HatIndex).IsLeft ? powerNotch | PowerNotchesEnum.Power2 : powerNotch & ~PowerNotchesEnum.Power2; powerNotch = joystick.GetHat((JoystickHat)HatIndex).IsRight ? powerNotch | PowerNotchesEnum.Power3 : powerNotch & ~PowerNotchesEnum.Power3; } else { // The adapter maps the direction buttons to independent buttons. powerNotch = joystick.IsButtonDown(ButtonIndex.Power2) ? powerNotch | PowerNotchesEnum.Power2 : powerNotch & ~PowerNotchesEnum.Power2; powerNotch = joystick.IsButtonDown(ButtonIndex.Power3) ? powerNotch | PowerNotchesEnum.Power3 : powerNotch & ~PowerNotchesEnum.Power3; } if (UsesHat && powerNotch == PowerNotchesEnum.P4) { // Hack for adapters using a hat where pressing left and right simultaneously reports only left being pressed if (InputTranslator.PreviousPowerNotch < InputTranslator.PowerNotches.P3) { InputTranslator.PowerNotch = InputTranslator.PowerNotches.N; } else { InputTranslator.PowerNotch = InputTranslator.PowerNotches.P4; } } else if (UsesHat && powerNotch == PowerNotchesEnum.Transition) { // Hack for adapters using a hat where pressing left and right simultaneously reports nothing being pressed, the same as the transition state // Has the side effect of the power notch jumping P1>N>P2, but it is barely noticeable unless moving the handle very slowly if (InputTranslator.PreviousPowerNotch < InputTranslator.PowerNotches.P2) { InputTranslator.PowerNotch = InputTranslator.PowerNotches.N; } } else if (powerNotch != PowerNotchesEnum.Transition) { // Set notch only if it is not a transition InputTranslator.PowerNotch = PowerNotchMap[powerNotch]; } if (brakeNotch != BrakeNotchesEnum.Transition && (brakeNotch == BrakeNotchesEnum.Emergency || brakeNotch >= BrakeNotchesEnum.B8)) { // Set notch only if it is not a transition nor an unmarked notch InputTranslator.BrakeNotch = BrakeNotchMap[brakeNotch]; } InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Select] = joystick.GetButton(ButtonIndex.Select); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Start] = joystick.GetButton(ButtonIndex.Start); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.A] = joystick.GetButton(ButtonIndex.A); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.B] = joystick.GetButton(ButtonIndex.B); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.C] = joystick.GetButton(ButtonIndex.C); }
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> /// Reads the input from the controller. /// </summary> internal override void ReadInput() { JoystickState joystick = Joystick.GetState(joystickIndex); double handleAxis = Math.Round(joystick.GetAxis(1), 4); for (int i = 0; i < brakeBytes.Length; i += 2) { // Each notch uses two bytes, minimum value and maximum value if (handleAxis >= GetAxisValue(brakeBytes[i]) && handleAxis <= GetAxisValue(brakeBytes[i + 1])) { if (brakeBytes.Length == i + 2) { // Last notch should be Emergency InputTranslator.BrakeNotch = InputTranslator.BrakeNotches.Emergency; } else { // Regular brake notch InputTranslator.BrakeNotch = (InputTranslator.BrakeNotches)(i / 2); } break; } InputTranslator.BrakeNotch = InputTranslator.BrakeNotches.Released; } for (int i = 0; i < powerBytes.Length; i += 2) { // Each notch uses two bytes, minimum value and maximum value if (handleAxis >= GetAxisValue(powerBytes[i]) && handleAxis <= GetAxisValue(powerBytes[i + 1])) { InputTranslator.PowerNotch = (InputTranslator.PowerNotches)(i / 2); break; } InputTranslator.PowerNotch = InputTranslator.PowerNotches.N; } InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Select] = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.Select]); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Start] = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.Start]); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.A] = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.A]); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.B] = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.B]); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.C] = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.C]); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.D] = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.D]); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.LDoor] = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.LDoor]); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.RDoor] = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.RDoor]); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Up] = (ButtonState)(joystick.GetHat(JoystickHat.Hat0).IsUp ? 1 : 0); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Down] = (ButtonState)(joystick.GetHat(JoystickHat.Hat0).IsDown ? 1 : 0); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Left] = (ButtonState)(joystick.GetHat(JoystickHat.Hat0).IsLeft ? 1 : 0); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Right] = (ButtonState)(joystick.GetHat(JoystickHat.Hat0).IsRight ? 1 : 0); }
public void Update() { for (int i = 0; i < curKeysDown.Length; ++i) { prevKeysDown[i] = curKeysDown[i]; curKeysDown[i] = game.Keyboard[(Key)i]; } for (int i = 0; i < curMouseDown.Length; ++i) { prevMouseDown[i] = curMouseDown[i]; curMouseDown[i] = game.Mouse[(MouseButton)i]; } mousePosition.X = game.Mouse.X; mousePosition.Y = game.Mouse.Y; mouseDeltaPos.X = ((float)game.Mouse.XDelta) / ((float)game.ClientSize.Width); mouseDeltaPos.Y = ((float)game.Mouse.YDelta) / ((float)game.ClientSize.Height); for (int i = 0; i < numJoysticks; ++i) { if (IsConnected(i)) { JoystickState state = Joystick.GetState(i); for (int j = 0; j < curJoyDown[i].Buttons.Length; ++j) { prevJoyDown[i].Buttons[j] = curJoyDown[i].Buttons[j]; curJoyDown[i].Buttons[j] = joyMapping[i].HasButtons[j] ? state.GetButton(joyMapping[i].Buttons[j]) == ButtonState.Pressed : false; } prevJoyDown[i].LeftAxis.X = curJoyDown[i].LeftAxis.X; prevJoyDown[i].LeftAxis.Y = curJoyDown[i].LeftAxis.Y; prevJoyDown[i].RightAxis.X = curJoyDown[i].RightAxis.X; prevJoyDown[i].RightAxis.Y = curJoyDown[i].RightAxis.Y; curJoyDown[i].LeftAxis.X = joyMapping[i].HasLeftAxisX ? state.GetAxis(joyMapping[i].LeftAxisX) : 0.0f; curJoyDown[i].LeftAxis.Y = joyMapping[i].HasLeftAxisY ? state.GetAxis(joyMapping[i].LeftAxisY) : 0.0f; curJoyDown[i].RightAxis.X = joyMapping[i].HasRightAxisX ? state.GetAxis(joyMapping[i].RightAxisX) : 0.0f; curJoyDown[i].RightAxis.Y = joyMapping[i].HasRightAxisY ? state.GetAxis(joyMapping[i].RightAxisY) : 0.0f; if (curJoyDown[i].LeftAxis.X > 0.0f && curJoyDown[i].LeftAxis.X < joyDeadZone[i]) { curJoyDown[i].LeftAxis.X = 0.0f; } else if (curJoyDown[i].LeftAxis.X <0.0f && curJoyDown[i].LeftAxis.X> -joyDeadZone[i]) { curJoyDown[i].LeftAxis.X = 0.0f; } if (curJoyDown[i].LeftAxis.Y > 0.0f && curJoyDown[i].LeftAxis.Y < joyDeadZone[i]) { curJoyDown[i].LeftAxis.Y = 0.0f; } else if (curJoyDown[i].LeftAxis.Y <0.0f && curJoyDown[i].LeftAxis.Y> -joyDeadZone[i]) { curJoyDown[i].LeftAxis.Y = 0.0f; } if (curJoyDown[i].RightAxis.X > 0.0f && curJoyDown[i].RightAxis.X < joyDeadZone[i]) { curJoyDown[i].RightAxis.X = 0.0f; } else if (curJoyDown[i].RightAxis.X <0.0f && curJoyDown[i].RightAxis.X> -joyDeadZone[i]) { curJoyDown[i].RightAxis.X = 0.0f; } if (curJoyDown[i].RightAxis.Y > 0.0f && curJoyDown[i].RightAxis.Y < joyDeadZone[i]) { curJoyDown[i].RightAxis.Y = 0.0f; } else if (curJoyDown[i].RightAxis.Y <0.0f && curJoyDown[i].RightAxis.Y> -joyDeadZone[i]) { curJoyDown[i].RightAxis.Y = 0.0f; } } } }
/// <summary> /// Reads the input from the controller. /// </summary> /// <param name="joystick">The state of the joystick to read input from.</param> internal static void ReadInput(JoystickState joystick) { PowerNotches powerNotch = PowerNotches.None; BrakeNotches brakeNotch = BrakeNotches.None; powerNotch = joystick.IsButtonDown(ButtonIndex.Power1) ? powerNotch | PowerNotches.Power1 : powerNotch & ~PowerNotches.Power1; brakeNotch = joystick.IsButtonDown(ButtonIndex.Brake1) ? brakeNotch | BrakeNotches.Brake1 : brakeNotch & ~BrakeNotches.Brake1; brakeNotch = joystick.IsButtonDown(ButtonIndex.Brake2) ? brakeNotch | BrakeNotches.Brake2 : brakeNotch & ~BrakeNotches.Brake2; brakeNotch = joystick.IsButtonDown(ButtonIndex.Brake3) ? brakeNotch | BrakeNotches.Brake3 : brakeNotch & ~BrakeNotches.Brake3; brakeNotch = joystick.IsButtonDown(ButtonIndex.Brake4) ? brakeNotch | BrakeNotches.Brake4 : brakeNotch & ~BrakeNotches.Brake4; if (usesHat) { // The adapter uses the hat to map the direction buttons. // This is the case of some PlayStation adapters. powerNotch = joystick.GetHat((JoystickHat)hatIndex).IsLeft ? powerNotch | PowerNotches.Power2 : powerNotch & ~PowerNotches.Power2; powerNotch = joystick.GetHat((JoystickHat)hatIndex).IsRight ? powerNotch | PowerNotches.Power3 : powerNotch & ~PowerNotches.Power3; } else { // The adapter maps the direction buttons to independent buttons. powerNotch = joystick.IsButtonDown(ButtonIndex.Power2) ? powerNotch | PowerNotches.Power2 : powerNotch & ~PowerNotches.Power2; powerNotch = joystick.IsButtonDown(ButtonIndex.Power3) ? powerNotch | PowerNotches.Power3 : powerNotch & ~PowerNotches.Power3; } if (usesHat && powerNotch == PowerNotches.P4) { if (InputTranslator.PreviousPowerNotch < InputTranslator.PowerNotches.P3) { // Hack for adapters which map the direction buttons to a hat and confuse N with P4 InputTranslator.PowerNotch = InputTranslator.PowerNotches.N; } else { InputTranslator.PowerNotch = InputTranslator.PowerNotches.P4; } } else if (powerNotch != PowerNotches.Transition) { // Set notch only if it is not a transition InputTranslator.PowerNotch = PowerNotchMap[powerNotch]; } if (brakeNotch != BrakeNotches.Transition && (brakeNotch == BrakeNotches.Emergency || brakeNotch >= BrakeNotches.B8)) { // Set notch only if it is not a transition nor an unmarked notch InputTranslator.BrakeNotch = BrakeNotchMap[brakeNotch]; } InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Select] = joystick.GetButton(ButtonIndex.Select); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Start] = joystick.GetButton(ButtonIndex.Start); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.A] = joystick.GetButton(ButtonIndex.A); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.B] = joystick.GetButton(ButtonIndex.B); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.C] = joystick.GetButton(ButtonIndex.C); }
/// <summary> /// Reads the input from the controller. /// </summary> internal override void ReadInput() { JoystickState joystick = Joystick.GetState(joystickIndex); double brakeAxis = Math.Round(joystick.GetAxis(0), 4); double powerAxis = Math.Round(joystick.GetAxis(1), 4); for (int i = 0; i < brakeBytes.Length; i += 2) { // Each notch uses two bytes, minimum value and maximum value if (brakeAxis >= GetAxisValue(brakeBytes[i]) && brakeAxis <= GetAxisValue(brakeBytes[i + 1])) { if (brakeBytes.Length == i + 2) { // Last notch should be Emergency InputTranslator.BrakeNotch = InputTranslator.BrakeNotches.Emergency; } else { // Regular brake notch InputTranslator.BrakeNotch = (InputTranslator.BrakeNotches)(i / 2); } break; } } for (int i = 0; i < powerBytes.Length; i += 2) { // Each notch uses two bytes, minimum value and maximum value if (powerAxis >= GetAxisValue(powerBytes[i]) && powerAxis <= GetAxisValue(powerBytes[i + 1])) { InputTranslator.PowerNotch = (InputTranslator.PowerNotches)(i / 2); break; } } InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Select] = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.Select]); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Start] = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.Start]); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.A] = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.A]); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.B] = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.B]); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.C] = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.C]); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.D] = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.D]); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.LDoor] = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.LDoor]); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.RDoor] = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.RDoor]); if (Buttons.HasFlag(ControllerButtons.DPad)) { if (comboDpad) { // On some controllers, check for Select+A/B/C/D combo bool dPadUp = Convert.ToBoolean(joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.Select])) && Convert.ToBoolean(joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.D])); bool dPadDown = Convert.ToBoolean(joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.Select])) && Convert.ToBoolean(joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.B])); bool dPadLeft = Convert.ToBoolean(joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.Select])) && Convert.ToBoolean(joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.A])); bool dPadRight = Convert.ToBoolean(joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.Select])) && Convert.ToBoolean(joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.C])); bool dPadAny = dPadUp || dPadDown || dPadLeft || dPadRight; InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Up] = (ButtonState)(dPadUp ? 1 : 0); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Down] = (ButtonState)(dPadDown ? 1 : 0); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Left] = (ButtonState)(dPadLeft ? 1 : 0); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Right] = (ButtonState)(dPadRight ? 1 : 0); // Disable original buttons if necessary InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Select] = InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Select] ^ (ButtonState)(dPadAny ? 1 : 0); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.A] = InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.A] ^ (ButtonState)(dPadLeft ? 1 : 0); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.B] = InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.B] ^ (ButtonState)(dPadDown ? 1 : 0); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.C] = InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.C] ^ (ButtonState)(dPadRight ? 1 : 0); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.D] = InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.D] ^ (ButtonState)(dPadUp ? 1 : 0); } else { // On other controllers, read the first hat InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Up] = (ButtonState)(joystick.GetHat(JoystickHat.Hat0).IsUp ? 1 : 0); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Down] = (ButtonState)(joystick.GetHat(JoystickHat.Hat0).IsDown ? 1 : 0); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Left] = (ButtonState)(joystick.GetHat(JoystickHat.Hat0).IsLeft ? 1 : 0); InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Right] = (ButtonState)(joystick.GetHat(JoystickHat.Hat0).IsRight ? 1 : 0); } } }
private string GetButtonText(JoystickButton button) { var buttonValue = _currentState.GetButton(button); return(string.Format("{0}={1}", button, buttonValue)); }
public ControllerHandler(Dictionary <string, ButtonState> oldPsButtons, int freq, string mode, byte controllmode = 0) { _freq = freq; JoystickState joyStick = Joystick.GetState(0); if (joyStick.IsConnected) { _xl = Convert.ToInt16(Math.Floor(joyStick.GetAxis(JoystickAxis.Axis0) * 100)); // Left stick sideways _yl = Convert.ToInt16(Math.Floor(joyStick.GetAxis(JoystickAxis.Axis1) * 100 * -1)); // Left stick updown _xr = Convert.ToInt16(Math.Floor(joyStick.GetAxis(JoystickAxis.Axis3) * 100 * -1)); // Right stick sideways _yr = Convert.ToInt16(Math.Floor(joyStick.GetAxis(JoystickAxis.Axis4) * 100)); // Right stick updown _cross = joyStick.GetButton(JoystickButton.Button0); // Cross _circle = joyStick.GetButton(JoystickButton.Button1); // Circle _triangle = joyStick.GetButton(JoystickButton.Button3); // Triangle _square = joyStick.GetButton(JoystickButton.Button2); // Square _start = joyStick.GetButton(JoystickButton.Button7); // Start _selectB = joyStick.GetButton(JoystickButton.Button6); // Select _l1 = joyStick.GetButton(JoystickButton.Button4); // L1 _r1 = joyStick.GetButton(JoystickButton.Button5); // R1 _l3 = joyStick.GetButton(JoystickButton.Button8); // L3 _hatSwitch = joyStick.GetHat(JoystickHat.Hat0); // hatSwitch // Console.WriteLine("" + // "x left: {0}\n" + // "y left: {1}\n" + // "x right: {2}\n" + // "y right: {3}\n" + // "Speed: {4}\n" + // "Wheels: {5}\n" + // "ControllMode: {6}\n" // , _xl, _yl, _xr, _yr, _speed * 2.55, _wheelPos, _controllMode); _psButtons = new Dictionary <string, ButtonState> { { "cross", _cross }, { "circle", _circle }, { "triangle", _triangle }, { "square", _square }, { "start", _start }, { "select", _selectB }, { "l1", _l1 }, { "r1", _r1 }, { "l3", _l3 } }; // foreach (var button in _psButtons) // { // Console.WriteLine(button.Key + ": " + button.Value); // } // Console.WriteLine(); if (oldPsButtons != null) { #region Speed controll code #region Left stick code if (_controllMode == 1) { // Backwards if (_yl > 5) { _speed = _yl; } else if (_yl < 5 && _speed < 1) { _speed += 5; } // Forwards if (_yl < -5) { _speed = _yl; } else if (_yl > -5 && _speed > 1) { _speed -= 5; } if (_speed >= -5 && _speed <= 5) { _speed = 0; // Fix for absolute stop of engines } } #endregion #region cross+circle if (_controllMode == 2) { if (_cross == ButtonState.Pressed && _hatSwitch.Position == HatPosition.Centered && !(_speed == 70 || _speed > 70)) { _speed += 10; } else if (_cross == ButtonState.Pressed && _hatSwitch.IsUp && _speed != 100) { _speed += 10; } else if (_cross == ButtonState.Pressed && _hatSwitch.Position == HatPosition.Centered && _speed > 70) { _speed -= 10; } else if (_cross == ButtonState.Released && _speed > 0) { _speed = 0; } else if (_circle == ButtonState.Pressed && !(_speed == -70 || _speed < -70)) { _speed -= 10; } else if (_circle == ButtonState.Pressed && _hatSwitch.IsDown && _speed != -100) { _speed -= 10; } else if (_circle == ButtonState.Pressed && _hatSwitch.Position == HatPosition.Centered && _speed < -70) { _speed += 10; } else if (_circle == ButtonState.Released && _speed < 0) { _speed = 0; } } #endregion #endregion #region steering controll code #region leftstick if (_controllMode == 1) { // Backwards if (_xl > 5) { _wheelPos1 = _xl; } else if (_xl < 5 && _wheelPos1 > 1) { _wheelPos1 = 0; } // Forwards if (_xl < -5) { _wheelPos2 = _xl; } else if (_xl > -5 && _wheelPos2 < -1) { _wheelPos2 = 0; } if (_xl == 0) { _wheelPos1 = 0; _wheelPos2 = 0; } } #endregion #region cross+circle if (_controllMode == 2) { if (_hatSwitch.IsRight && _wheelPos1 < 50) { _wheelPos1 += 10; } else if (_hatSwitch.Position == HatPosition.Centered && _wheelPos1 > 0) { _wheelPos1 = 0; } if (_hatSwitch.IsLeft && _wheelPos2 < 50) { _wheelPos2 += 10; } else if (_hatSwitch.Position == HatPosition.Centered && _wheelPos2 > 0) { _wheelPos2 = 0; } } #endregion #endregion #region pan_tilt #region servoPosY if (_yr > 5) { servoPosY = _yr; } else if (_yr < 5 && servoPosY > 1) { servoPosY = 0; } if (_yr < -5) { servoPosY = _yr; } else if (_yr > -5 && servoPosY < -1) { servoPosY = 0; } if (_yr == 0) { servoPosY = 0; } #endregion #region servoPosX if (_xr > 5) { servoPosX = _xr; } else if (_xr < 5 && servoPosX > 1) { servoPosX = 0; } if (_xr < -5) { servoPosX = _xr; } else if (_xr > -5 && servoPosX < -1) { servoPosX = 0; } if (_xr == 0) { servoPosX = 0; } #endregion #endregion #region Button checks // Gets all the buttons out of the oldpsbuttons to see if it was pressed ButtonState selectOld, startOld, triangleOld, squareOld, l1Old, r1Old, l3Old; oldPsButtons.TryGetValue("select", out selectOld); oldPsButtons.TryGetValue("start", out startOld); oldPsButtons.TryGetValue("triangle", out triangleOld); oldPsButtons.TryGetValue("square", out squareOld); oldPsButtons.TryGetValue("l1", out l1Old); oldPsButtons.TryGetValue("r1", out r1Old); oldPsButtons.TryGetValue("l1", out l3Old); if (_start == ButtonState.Released && startOld == ButtonState.Pressed) { _resetmh = true; _speed = 0; _wheelPos1 = 0; _wheelPos2 = 0; } #endregion } } }