PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Projects/SharpInputSystem.Xna/XnaJoystick.cs

http://sharpinputsystem.codeplex.com
C# | 175 lines | 92 code | 43 blank | 40 comment | 24 complexity | 3c425e4270c8ef781561fe2667f73f13 MD5 | raw file
Possible License(s): 0BSD, IPL-1.0, LGPL-2.0, GPL-2.0, Apache-2.0
  1. #region MIT/X11 License
  2. /*
  3. Sharp Input System Library
  4. Copyright Š 2007-2011 Michael Cummings
  5. The overall design, and a majority of the core code contained within
  6. this library is a derivative of the open source Open Input System ( OIS ) ,
  7. which can be found at http://www.sourceforge.net/projects/wgois.
  8. Many thanks to the Phillip Castaneda for maintaining such a high quality project.
  9. Permission is hereby granted, free of charge, to any person obtaining a copy
  10. of this software and associated documentation files (the "Software"), to deal
  11. in the Software without restriction, including without limitation the rights
  12. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. copies of the Software, and to permit persons to whom the Software is
  14. furnished to do so, subject to the following conditions:
  15. The above copyright notice and this permission notice shall be included in
  16. all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. THE SOFTWARE.
  24. */
  25. #endregion MIT/X11 License
  26. #region Namespace Declarations
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Text;
  30. //using Common.Logging;
  31. using XInput = Microsoft.Xna.Framework.Input;
  32. using MXF = Microsoft.Xna.Framework;
  33. #endregion Namespace Declarations
  34. namespace SharpInputSystem.Xna
  35. {
  36. class XnaJoystick : Joystick
  37. {
  38. #region Fields and Properties
  39. //private static readonly ILog log = LogManager.GetLogger( typeof( XnaJoystick ) );
  40. private const int _BUFFER_SIZE = 124;
  41. private JoystickInfo _joyInfo;
  42. private Guid _deviceGuid;
  43. private Dictionary<int, int> _axisMapping = new Dictionary<int, int>();
  44. #endregion Fields and Properties
  45. #region Construction and Destruction
  46. public XnaJoystick( InputManager creator, bool buffered )
  47. {
  48. Creator = creator;
  49. IsBuffered = buffered;
  50. Type = InputType.Joystick;
  51. EventListener = null;
  52. _joyInfo = (JoystickInfo)( (XnaInputManager)Creator ).CaptureDevice<Joystick>();
  53. if ( _joyInfo == null )
  54. {
  55. throw new Exception( "No devices match requested type." );
  56. }
  57. _deviceGuid = _joyInfo.DeviceId;
  58. Vendor = _joyInfo.Vendor;
  59. DeviceID = _joyInfo.Id.ToString();
  60. //log.Debug( "XnaJoystick device created." );
  61. }
  62. protected override void Dispose( bool disposeManagedResources )
  63. {
  64. if ( !IsDisposed )
  65. {
  66. if ( disposeManagedResources )
  67. {
  68. // Dispose managed resources.
  69. }
  70. // There are no unmanaged resources to release, but
  71. // if we add them, they need to be released here.
  72. ( (XnaInputManager)Creator ).ReleaseDevice<Joystick>( _joyInfo );
  73. }
  74. // If it is available, make the call to the
  75. // base class's Dispose(Boolean) method
  76. base.Dispose( disposeManagedResources );
  77. }
  78. #endregion Construction and Destruction
  79. #region Methods
  80. protected void _enumerate()
  81. {
  82. //We can check force feedback here too
  83. XInput.GamePadCapabilities joystickCapabilities;
  84. joystickCapabilities = XInput.GamePad.GetCapabilities( (MXF.PlayerIndex)Int32.Parse( this.DeviceID ) );
  85. if ( joystickCapabilities.HasAButton ) this.ButtonCount++;
  86. if ( joystickCapabilities.HasBButton ) this.ButtonCount++;
  87. if ( joystickCapabilities.HasXButton ) this.ButtonCount++;
  88. if ( joystickCapabilities.HasYButton ) this.ButtonCount++;
  89. if ( joystickCapabilities.HasStartButton ) this.ButtonCount++;
  90. if ( joystickCapabilities.HasBackButton ) this.ButtonCount++;
  91. if ( joystickCapabilities.HasLeftStickButton ) this.ButtonCount++;
  92. if ( joystickCapabilities.HasRightStickButton ) this.ButtonCount++;
  93. if ( joystickCapabilities.HasLeftShoulderButton ) this.ButtonCount++;
  94. if ( joystickCapabilities.HasRightShoulderButton ) this.ButtonCount++;
  95. if ( joystickCapabilities.HasDPadUpButton && joystickCapabilities.HasDPadDownButton &&
  96. joystickCapabilities.HasDPadLeftButton && joystickCapabilities.HasDPadRightButton )
  97. this.AxisCount++;
  98. if ( joystickCapabilities.HasLeftTrigger ) this.AxisCount++;
  99. if ( joystickCapabilities.HasRightTrigger ) this.AxisCount++;
  100. if ( joystickCapabilities.HasLeftXThumbStick && joystickCapabilities.HasLeftYThumbStick ) this.AxisCount++;
  101. if ( joystickCapabilities.HasRightXThumbStick && joystickCapabilities.HasRightYThumbStick ) this.AxisCount++;
  102. this.PovCount++;
  103. _axisMapping.Clear();
  104. //Enumerate Force Feedback (if any)
  105. //Enumerate and set axis constraints (and check FF Axes)
  106. }
  107. #endregion Methods
  108. #region InputObject Implementation
  109. public override void Capture()
  110. {
  111. XInput.GamePadState currentState = XInput.GamePad.GetState( (MXF.PlayerIndex)Int32.Parse( DeviceID ) );
  112. }
  113. protected override void Initialize()
  114. {
  115. //Enumerate all axes/buttons/sliders/etc before aquiring
  116. _enumerate();
  117. JoystickState.Axis.Capacity = this.AxisCount;
  118. for ( int i = 0; i < this.AxisCount; i++ )
  119. {
  120. JoystickState.Axis.Add( new Axis() );
  121. }
  122. JoystickState.Clear();
  123. Capture();
  124. }
  125. #endregion InputObject Implementation
  126. }
  127. }