/FITClub.ExternalClasses/Controls/PCGamePad.cs
C# | 258 lines | 222 code | 22 blank | 14 comment | 90 complexity | add57c142b29508e75b664458ce3254b MD5 | raw file
- #region File Description
- // --------------------------------
- // <copyright file="PCGamePad.cs" company="Faculty of Information Technology, Czech Technical University in Prague">
- // This document is shared by Microsoft Public License (MS-PL)
- // For further details please see http://msdn.microsoft.com/en-us/library/ff648068.aspx or license.txt.
- // </copyright>
- // <author>Vladislav Richter</author>
-
- // <email>richtvl2@fit.cvut.cz</email>
-
- // <date>9. 11. 2011 22:22:37</date>
-
- // <summary>
- //
- // </summary>
- // ------------------------------------------------------------------------------------------------------
- #endregion
- #if WINDOWS
- using System.Collections.Generic;
- using Microsoft.Xna.Framework.Input;
- using Nuclex.Input;
-
- namespace ExternalClasses.Controls
- {
- public enum PCGamePadButtons { Up, Down, Left, Right, Attack, Block }
- public struct PCGamePadState
- {
- public bool Up, Down, Left, Right, Attack, Block, Reset;
-
- public bool IsButtonDown(PCGamePadButtons button)
- {
- switch (button)
- {
- case PCGamePadButtons.Up: return Up;
- case PCGamePadButtons.Down: return Down;
- case PCGamePadButtons.Left: return Left;
- case PCGamePadButtons.Right: return Right;
- case PCGamePadButtons.Attack: return Attack;
- case PCGamePadButtons.Block: return Block;
- default: return false;
- }
- }
- }
-
- public struct PCGamePadsInfo
- {
- public ExtendedPlayerIndex index;
- public string Name;
- }
-
- public class PCGamePad
- {
-
- public static InputManager input{set; get;}
-
- public static int GetNumberOfPCGamePadsConnected()
- {
- int count = 0;
- if (input.GetGamePad(ExtendedPlayerIndex.Five).IsAttached) count++;
- if (input.GetGamePad(ExtendedPlayerIndex.Six).IsAttached) count++;
- if (input.GetGamePad(ExtendedPlayerIndex.Seven).IsAttached) count++;
- if (input.GetGamePad(ExtendedPlayerIndex.Eight).IsAttached) count++;
- return count;
- }
-
- public static List<PCGamePadsInfo> GetPCGamePadsInfos()
- {
- List<PCGamePadsInfo> Result = new List<PCGamePadsInfo>();
- if (input.GetGamePad(ExtendedPlayerIndex.Five).IsAttached)
- {
- PCGamePadsInfo info = new PCGamePadsInfo();
- info.index = ExtendedPlayerIndex.Five;
- info.Name = input.GetGamePad(ExtendedPlayerIndex.Five).Name;
- Result.Add(info);
- }
- if (input.GetGamePad(ExtendedPlayerIndex.Six).IsAttached)
- {
- PCGamePadsInfo info = new PCGamePadsInfo();
- info.index = ExtendedPlayerIndex.Six;
- info.Name = input.GetGamePad(ExtendedPlayerIndex.Six).Name;
- Result.Add(info);
- }
- if (input.GetGamePad(ExtendedPlayerIndex.Seven).IsAttached)
- {
- PCGamePadsInfo info = new PCGamePadsInfo();
- info.index = ExtendedPlayerIndex.Seven;
- info.Name = input.GetGamePad(ExtendedPlayerIndex.Seven).Name;
- Result.Add(info);
- }
- if (input.GetGamePad(ExtendedPlayerIndex.Eight).IsAttached)
- {
- PCGamePadsInfo info = new PCGamePadsInfo();
- info.index = ExtendedPlayerIndex.Eight;
- info.Name = input.GetGamePad(ExtendedPlayerIndex.Eight).Name;
- Result.Add(info);
- }
- return Result;
- }
-
- public static bool IsAnyButtonPressed(ExtendedPlayerIndex index)
- {
- if (input.GetGamePad(index).GetExtendedState().GetButton(11) == ButtonState.Pressed) return true;
- if (input.GetGamePad(index).GetExtendedState().GetButton(10) == ButtonState.Pressed) return true;
- if (input.GetGamePad(index).GetExtendedState().GetButton(9) == ButtonState.Pressed) return true;
- if (input.GetGamePad(index).GetExtendedState().GetButton(8) == ButtonState.Pressed) return true;
- if (input.GetGamePad(index).GetExtendedState().GetButton(7) == ButtonState.Pressed) return true;
- if (input.GetGamePad(index).GetExtendedState().GetButton(6) == ButtonState.Pressed) return true;
- if (input.GetGamePad(index).GetExtendedState().GetButton(5) == ButtonState.Pressed) return true;
- if (input.GetGamePad(index).GetExtendedState().GetButton(4) == ButtonState.Pressed) return true;
- if (input.GetGamePad(index).GetExtendedState().GetButton(3) == ButtonState.Pressed) return true;
- if (input.GetGamePad(index).GetExtendedState().GetButton(2) == ButtonState.Pressed) return true;
- if (input.GetGamePad(index).GetExtendedState().GetButton(1) == ButtonState.Pressed) return true;
- if (input.GetGamePad(index).GetExtendedState().GetButton(0) == ButtonState.Pressed) return true;
- return false;
- }
-
- public static bool IsAnyButtonAtAnyGamePadPressed()
- {
- if (IsAnyButtonPressed(ExtendedPlayerIndex.Five)) return true;
- if (IsAnyButtonPressed(ExtendedPlayerIndex.Six)) return true;
- if (IsAnyButtonPressed(ExtendedPlayerIndex.Seven)) return true;
- if (IsAnyButtonPressed(ExtendedPlayerIndex.Eight)) return true;
- return false;
- }
-
- public static ExtendedPlayerIndex GetNextPCGamepad()
- {
- if (IsAnyButtonPressed(ExtendedPlayerIndex.Five)) return ExtendedPlayerIndex.Five;
- if (IsAnyButtonPressed(ExtendedPlayerIndex.Six)) return ExtendedPlayerIndex.Six;
- if (IsAnyButtonPressed(ExtendedPlayerIndex.Seven)) return ExtendedPlayerIndex.Seven;
- if (IsAnyButtonPressed(ExtendedPlayerIndex.Eight)) return ExtendedPlayerIndex.Eight;
- return ExtendedPlayerIndex.Five;
- }
-
- public static bool AnyPCGamePadUpPressed()
- {
- if (GetState(ExtendedPlayerIndex.Five).Up) return true;
- if (GetState(ExtendedPlayerIndex.Six).Up) return true;
- if (GetState(ExtendedPlayerIndex.Seven).Up) return true;
- if (GetState(ExtendedPlayerIndex.Eight).Up) return true;
- return false;
- }
-
- public static bool AnyPCGamePadDownPressed()
- {
- if (GetState(ExtendedPlayerIndex.Five).Down) return true;
- if (GetState(ExtendedPlayerIndex.Six).Down) return true;
- if (GetState(ExtendedPlayerIndex.Seven).Down) return true;
- if (GetState(ExtendedPlayerIndex.Eight).Down) return true;
- return false;
- }
-
- public static bool AnyPCGamePadLeftPressed()
- {
- if (GetState(ExtendedPlayerIndex.Five).Left) return true;
- if (GetState(ExtendedPlayerIndex.Six).Left) return true;
- if (GetState(ExtendedPlayerIndex.Seven).Left) return true;
- if (GetState(ExtendedPlayerIndex.Eight).Left) return true;
- return false;
- }
-
- public static bool AnyPCGamePadRightPressed()
- {
- if (GetState(ExtendedPlayerIndex.Five).Right) return true;
- if (GetState(ExtendedPlayerIndex.Six).Right) return true;
- if (GetState(ExtendedPlayerIndex.Seven).Right) return true;
- if (GetState(ExtendedPlayerIndex.Eight).Right) return true;
- return false;
- }
-
- public static bool AnyPCGamePadAttackPressed()
- {
- if (GetState(ExtendedPlayerIndex.Five).Attack) return true;
- if (GetState(ExtendedPlayerIndex.Six).Attack) return true;
- if (GetState(ExtendedPlayerIndex.Seven).Attack) return true;
- if (GetState(ExtendedPlayerIndex.Eight).Attack) return true;
- return false;
- }
-
- public static bool AnyPCGamePadBlockPressed()
- {
- if (GetState(ExtendedPlayerIndex.Five).Block) return true;
- if (GetState(ExtendedPlayerIndex.Six).Block) return true;
- if (GetState(ExtendedPlayerIndex.Seven).Block) return true;
- if (GetState(ExtendedPlayerIndex.Eight).Block) return true;
- return false;
- }
-
- public static PCGamePadState AnyPCGamePadGetState()
- {
- PCGamePadState result = new PCGamePadState();
- if (AnyPCGamePadUpPressed()) result.Up = true;
- if (AnyPCGamePadDownPressed()) result.Down = true;
- if (AnyPCGamePadLeftPressed()) result.Left = true;
- if (AnyPCGamePadRightPressed()) result.Right = true;
- if (AnyPCGamePadAttackPressed()) result.Attack = true;
- if (AnyPCGamePadBlockPressed()) result.Block = true;
- return result;
- }
-
- public static PCGamePadState GetState(ExtendedPlayerIndex Index)
- {
- PCGamePadState result = new PCGamePadState();
- if (input.GetGamePad(Index).GetExtendedState().GetButton(8) == ButtonState.Pressed) result.Reset = true;
- if (input.GetGamePad(Index).GetState().Buttons.RightShoulder == ButtonState.Pressed) result.Attack = true;
- //if (input.GetGamePad(Index).GetState().Buttons.RightStick == ButtonState.Pressed) result.Attack = true;
- if (input.GetGamePad(Index).GetExtendedState().GetButton(2) == ButtonState.Pressed) result.Attack = true;
- if (input.GetGamePad(Index).GetState().Buttons.LeftShoulder == ButtonState.Pressed) result.Block = true;
- //if (input.GetGamePad(Index).GetState().Buttons.LeftStick == ButtonState.Pressed) result.Block = true;
- if (input.GetGamePad(Index).GetExtendedState().GetButton(3) == ButtonState.Pressed) result.Block = true;
- int direction = input.GetGamePad(Index).GetExtendedState().Pov1;
- switch (direction)
- {
- case 00:
- result.Up = true;
- break;
- case 4500:
- result.Up = true;
- result.Right = true;
- break;
- case 9000:
- result.Right = true;
- break;
- case 13500:
- result.Right = true;
- result.Down = true;
- break;
- case 18000:
- result.Down = true;
- break;
- case 22500:
- result.Left = true;
- result.Down = true;
- break;
- case 27000:
- result.Left = true;
- break;
- case 31500:
- result.Left = true;
- result.Up = true;
- break;
- default:
- break;
- }
- if (input.GetGamePad(Index).GetState().ThumbSticks.Left.X < -0.5f) result.Left = true;
- if (input.GetGamePad(Index).GetState().ThumbSticks.Left.X > +0.5f) result.Right = true;
- if (input.GetGamePad(Index).GetState().ThumbSticks.Left.Y < -0.5f) result.Down = true;
- if (input.GetGamePad(Index).GetState().ThumbSticks.Left.Y > +0.5f) result.Up = true;
- if (input.GetGamePad(Index).GetState().ThumbSticks.Right.X < -0.5f) result.Left = true;
- if (input.GetGamePad(Index).GetState().ThumbSticks.Right.X > +0.5f) result.Right = true;
- if (input.GetGamePad(Index).GetState().ThumbSticks.Right.Y < -0.5f) result.Down = true;
- if (input.GetGamePad(Index).GetState().ThumbSticks.Right.Y > +0.5f) result.Up = true;
- return result;
- }
- }
- }
-
- #endif