using System; using System.Timers; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Input; using Input = Microsoft.Xna.Framework.Input;// to provide shorthand to clear up ambiguities namespace LegoControl { public class XInput { Timer updateTimer; GamePadState gamePadState; GamePadState previousState; PlayerIndex playerIndex = PlayerIndex.One; int vibrationCountdown = 0; public bool Connected { get { return gamePadState.IsConnected;} } public double UpdateInterval { get; set; } public bool A { get; set; } public bool B { get; set; } public bool X { get; set; } public bool Y { get; set; } public bool LeftShoulder { get; set; } public bool RightShoulder { get; set; } public bool Start { get; set; } public bool Back { get; set; } public bool LeftStickPressed { get; set; } public bool RightStickPressed { get; set; } public bool Up { get; set; } public bool Down { get; set; } public bool Left { get; set; } public bool Right { get; set; } public int LeftThumbX { get; set; } public int LeftThumbY { get; set; } public int RightThumbX { get; set; } public int RightThumbY { get; set; } public int LeftTrigger { get; set; } public int RightTrigger { get; set; } public XInput(){ gamePadState = GamePad.GetState(playerIndex); UpdateInterval = 50; updateTimer = new Timer(UpdateInterval); updateTimer.Elapsed += new ElapsedEventHandler(updateTimer_Elapsed); } private void SelectedIndexChanged(int index) { switch (index) { case 0: playerIndex = PlayerIndex.One; break; case 1: playerIndex = PlayerIndex.Two; break; case 2: playerIndex = PlayerIndex.Three; break; case 3: playerIndex = PlayerIndex.Four; break; default: playerIndex = PlayerIndex.One; break; } this.StopAllVibration(); } private void StopAllVibration() { GamePad.SetVibration(PlayerIndex.One, 0.0f, 0.0f); GamePad.SetVibration(PlayerIndex.Two, 0.0f, 0.0f); GamePad.SetVibration(PlayerIndex.Three, 0.0f, 0.0f); GamePad.SetVibration(PlayerIndex.Four, 0.0f, 0.0f); } private void Vibration(float amount) { GamePad.SetVibration(playerIndex, amount, amount); vibrationCountdown = 30; } private void CheckVibrationTimeout() { if (vibrationCountdown > 0) { --vibrationCountdown; if (vibrationCountdown == 0.0f) { GamePad.SetVibration(playerIndex, 0.0f, 0.0f); } } } private void UpdateControllerState() { this.previousState = this.gamePadState; this.gamePadState = GamePad.GetState(this.playerIndex); if (!this.gamePadState.Buttons.Equals(this.previousState.Buttons)) { this.A = (this.gamePadState.Buttons.A == Input.ButtonState.Pressed); this.B = (this.gamePadState.Buttons.B == Input.ButtonState.Pressed); this.X = (this.gamePadState.Buttons.X == Input.ButtonState.Pressed); this.Y = (this.gamePadState.Buttons.Y == Input.ButtonState.Pressed); this.LeftShoulder = (this.gamePadState.Buttons.LeftShoulder == Input.ButtonState.Pressed); this.RightShoulder = (this.gamePadState.Buttons.RightShoulder == Input.ButtonState.Pressed); this.Start = (this.gamePadState.Buttons.Start == Input.ButtonState.Pressed); this.Back = (this.gamePadState.Buttons.Back == Input.ButtonState.Pressed); this.LeftStickPressed = (this.gamePadState.Buttons.LeftStick == Input.ButtonState.Pressed); this.RightStickPressed = (this.gamePadState.Buttons.RightStick == Input.ButtonState.Pressed); } if (!this.gamePadState.DPad.Equals(this.previousState.DPad)) { this.Up = (this.gamePadState.DPad.Up == Input.ButtonState.Pressed); this.Down = (this.gamePadState.DPad.Down == Input.ButtonState.Pressed); this.Left = (this.gamePadState.DPad.Left == Input.ButtonState.Pressed); this.Right = (this.gamePadState.DPad.Right == Input.ButtonState.Pressed); } this.LeftThumbX = (int)((this.gamePadState.ThumbSticks.Left.X) * 100.0f); this.LeftThumbY = (int)((this.gamePadState.ThumbSticks.Left.Y) * 100.0f); this.RightThumbX = (int)((this.gamePadState.ThumbSticks.Right.X) * 100.0f); this.RightThumbY = (int)((this.gamePadState.ThumbSticks.Right.Y) * 100.0f); //The triggers return a value between 0.0 and 1.0. this.LeftTrigger = (int)((this.gamePadState.Triggers.Left * 100f)); this.RightTrigger = (int)(this.gamePadState.Triggers.Right * 100f); } private void updateTimer_Elapsed(object sender, EventArgs e) { this.CheckVibrationTimeout(); this.UpdateControllerState(); } public void StartController() { this.updateTimer.Start(); } public void StopController() { this.StopAllVibration(); } } }