PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/MonoGame.Framework/Input/GamePadState.cs

https://github.com/notJUSTguitar/MonoGame
C# | 168 lines | 117 code | 14 blank | 37 comment | 23 complexity | 4392badfcd5f1a0babca5f9d1f5ee0e2 MD5 | raw file
  1. #region License
  2. /*
  3. Microsoft Public License (Ms-PL)
  4. MonoGame - Copyright © 2009 The MonoGame Team
  5. All rights reserved.
  6. This license governs use of the accompanying software. If you use the software, you accept this license. If you do not
  7. accept the license, do not use the software.
  8. 1. Definitions
  9. The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under
  10. U.S. copyright law.
  11. A "contribution" is the original software, or any additions or changes to the software.
  12. A "contributor" is any person that distributes its contribution under this license.
  13. "Licensed patents" are a contributor's patent claims that read directly on its contribution.
  14. 2. Grant of Rights
  15. (A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3,
  16. each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.
  17. (B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3,
  18. each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.
  19. 3. Conditions and Limitations
  20. (A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
  21. (B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software,
  22. your patent license from such contributor to the software ends automatically.
  23. (C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution
  24. notices that are present in the software.
  25. (D) If you distribute any portion of the software in source code form, you may do so only under this license by including
  26. a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object
  27. code form, you may only do so under a license that complies with this license.
  28. (E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees
  29. or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent
  30. permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular
  31. purpose and non-infringement.
  32. */
  33. #endregion License
  34. using Microsoft.Xna.Framework;
  35. using System;
  36. namespace Microsoft.Xna.Framework.Input
  37. {
  38. public struct GamePadState
  39. {
  40. private GamePadThumbSticks _thumbs;
  41. private Buttons _buttons;
  42. private GamePadDPad _dPad;
  43. private GamePadTriggers _triggers;
  44. internal GamePadState(Buttons buttons, Vector2 LeftStick, Vector2 RightStick)
  45. {
  46. _buttons = buttons;
  47. _thumbs = new GamePadThumbSticks(LeftStick,RightStick);
  48. }
  49. public GamePadState(GamePadThumbSticks thumbs, GamePadTriggers triggers, GamePadButtons gamePadButtons, GamePadDPad dPad)
  50. {
  51. _thumbs = thumbs;
  52. _triggers = triggers;
  53. ConvertGamePadButtonsToButtons(ref gamePadButtons, out _buttons);
  54. _dPad = dPad;
  55. }
  56. public GamePadButtons Buttons
  57. {
  58. get
  59. {
  60. return new GamePadButtons(_buttons);
  61. }
  62. }
  63. public bool IsConnected
  64. {
  65. get
  66. {
  67. return true;
  68. }
  69. }
  70. public GamePadThumbSticks ThumbSticks
  71. {
  72. get
  73. {
  74. return this._thumbs;
  75. }
  76. }
  77. public bool IsButtonDown(Microsoft.Xna.Framework.Input.Buttons button)
  78. {
  79. return ((_buttons & button) == button);
  80. }
  81. public bool IsButtonUp(Microsoft.Xna.Framework.Input.Buttons button)
  82. {
  83. return !this.IsButtonDown(button);
  84. }
  85. public GamePadDPad DPad
  86. {
  87. get
  88. {
  89. return _dPad;
  90. }
  91. }
  92. public GamePadTriggers Triggers
  93. {
  94. get
  95. {
  96. return _triggers;
  97. }
  98. }
  99. internal static void ConvertGamePadButtonsToButtons(ref GamePadButtons gamePadButtons, out Buttons buttons)
  100. {
  101. buttons = new Buttons();
  102. if (gamePadButtons.A == ButtonState.Pressed)
  103. {
  104. buttons |= Microsoft.Xna.Framework.Input.Buttons.A;
  105. }
  106. if (gamePadButtons.B == ButtonState.Pressed)
  107. {
  108. buttons |= Microsoft.Xna.Framework.Input.Buttons.B;
  109. }
  110. if (gamePadButtons.X == ButtonState.Pressed)
  111. {
  112. buttons |= Microsoft.Xna.Framework.Input.Buttons.X;
  113. }
  114. if (gamePadButtons.Y == ButtonState.Pressed)
  115. {
  116. buttons |= Microsoft.Xna.Framework.Input.Buttons.Y;
  117. }
  118. if (gamePadButtons.Back == ButtonState.Pressed)
  119. {
  120. buttons |= Microsoft.Xna.Framework.Input.Buttons.Back;
  121. }
  122. if (gamePadButtons.Start == ButtonState.Pressed)
  123. {
  124. buttons |= Microsoft.Xna.Framework.Input.Buttons.Start;
  125. }
  126. if (gamePadButtons.BigButton == ButtonState.Pressed)
  127. {
  128. buttons |= Microsoft.Xna.Framework.Input.Buttons.BigButton;
  129. }
  130. if (gamePadButtons.LeftShoulder == ButtonState.Pressed)
  131. {
  132. buttons |= Microsoft.Xna.Framework.Input.Buttons.LeftShoulder;
  133. }
  134. if (gamePadButtons.RightShoulder == ButtonState.Pressed)
  135. {
  136. buttons |= Microsoft.Xna.Framework.Input.Buttons.RightShoulder;
  137. }
  138. if (gamePadButtons.LeftStick == ButtonState.Pressed)
  139. {
  140. buttons |= Microsoft.Xna.Framework.Input.Buttons.LeftStick;
  141. }
  142. if (gamePadButtons.RightStick == ButtonState.Pressed)
  143. {
  144. buttons |= Microsoft.Xna.Framework.Input.Buttons.RightStick;
  145. }
  146. }
  147. }
  148. }