/Assets/HoloToolkit/Input/Scripts/InputSources/GamePadInputSource.cs

https://bitbucket.org/kmsuzanna/extensiusmanagerdashboard · C# · 158 lines · 126 code · 30 blank · 2 comment · 5 complexity · 0afe9330b4a7cc68fa93befab3065356 MD5 · raw file

  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License. See LICENSE in the project root for license information.
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. namespace HoloToolkit.Unity.InputModule
  6. {
  7. public class GamePadInputSource : BaseInputSource
  8. {
  9. protected const string XboxController = "Xbox Controller";
  10. protected const string XboxOneForWindows = "Xbox One For Windows";
  11. protected const string XboxBluetoothGamePad = "Xbox Bluetooth Gamepad";
  12. protected const string XboxWirelessController = "Xbox Wireless Controller";
  13. protected const string MotionControllerLeft = "Spatial Controller - Left";
  14. protected const string MotionControllerRight = "Spatial Controller - Right";
  15. protected uint SourceId;
  16. [SerializeField]
  17. [Tooltip("Time in seconds to determine if an Input Device has been connected or disconnected")]
  18. protected float DeviceRefreshInterval = 3.0f;
  19. protected int LastDeviceUpdateCount;
  20. protected string[] LastDeviceList;
  21. protected StandaloneInputModule InputModule;
  22. protected const string DefaultHorizontalAxis = "Horizontal";
  23. protected const string DefaultVerticalAxis = "Vertical";
  24. protected const string DefaultSubmitButton = "Submit";
  25. protected const string DefaultCancelButton = "Cancel";
  26. protected const bool DefaultForceActiveState = false;
  27. protected string PreviousHorizontalAxis;
  28. protected string PreviousVerticalAxis;
  29. protected string PreviousSubmitButton;
  30. protected string PreviousCancelButton;
  31. protected bool PreviousForceActiveState;
  32. private float deviceRefreshTimer;
  33. #region Unity methods
  34. protected virtual void Awake()
  35. {
  36. InputModule = FindObjectOfType<StandaloneInputModule>();
  37. if (InputModule == null)
  38. {
  39. Debug.LogError("Missing the Standalone Input Module for GamePad Input Source!\n" +
  40. "Ensure you have an Event System in your scene.");
  41. }
  42. }
  43. protected virtual void Update()
  44. {
  45. deviceRefreshTimer += Time.unscaledDeltaTime;
  46. if (deviceRefreshTimer >= DeviceRefreshInterval)
  47. {
  48. deviceRefreshTimer = 0.0f;
  49. RefreshDevices();
  50. }
  51. }
  52. #endregion // Unity methods
  53. protected virtual void RefreshDevices()
  54. {
  55. var joystickNames = Input.GetJoystickNames();
  56. if (joystickNames.Length <= 0) { return; }
  57. for (var i = 0; i < joystickNames.Length; i++)
  58. {
  59. Debug.LogWarningFormat("Joystick \"{0}\" has not been setup with the input manager. Create a new class that inherits from \"GamePadInputSource\" and implement it.", joystickNames[i]);
  60. }
  61. }
  62. #region Base Input Source Methods
  63. public override bool TryGetSourceKind(uint sourceId, out InteractionSourceInfo sourceKind)
  64. {
  65. sourceKind = InteractionSourceInfo.Controller;
  66. return true;
  67. }
  68. public override bool TryGetPointerPosition(uint sourceId, out Vector3 position)
  69. {
  70. position = Vector3.zero;
  71. return false;
  72. }
  73. public override bool TryGetPointerRotation(uint sourceId, out Quaternion rotation)
  74. {
  75. rotation = Quaternion.identity;
  76. return false;
  77. }
  78. public override bool TryGetPointingRay(uint sourceId, out Ray pointingRay)
  79. {
  80. pointingRay = default(Ray);
  81. return false;
  82. }
  83. public override bool TryGetGripPosition(uint sourceId, out Vector3 position)
  84. {
  85. position = Vector3.zero;
  86. return false;
  87. }
  88. public override bool TryGetGripRotation(uint sourceId, out Quaternion rotation)
  89. {
  90. rotation = Quaternion.identity;
  91. return false;
  92. }
  93. public override SupportedInputInfo GetSupportedInputInfo(uint sourceId)
  94. {
  95. return SupportedInputInfo.None;
  96. }
  97. public override bool TryGetThumbstick(uint sourceId, out bool isPressed, out Vector2 position)
  98. {
  99. isPressed = false;
  100. position = Vector2.zero;
  101. return false;
  102. }
  103. public override bool TryGetTouchpad(uint sourceId, out bool isPressed, out bool isTouched, out Vector2 position)
  104. {
  105. isPressed = false;
  106. isTouched = false;
  107. position = Vector2.zero;
  108. return false;
  109. }
  110. public override bool TryGetSelect(uint sourceId, out bool isPressed, out double pressedAmount)
  111. {
  112. isPressed = false;
  113. pressedAmount = 0.0;
  114. return false;
  115. }
  116. public override bool TryGetGrasp(uint sourceId, out bool isPressed)
  117. {
  118. isPressed = false;
  119. return false;
  120. }
  121. public override bool TryGetMenu(uint sourceId, out bool isPressed)
  122. {
  123. isPressed = false;
  124. return false;
  125. }
  126. #endregion // Base Input Source Methods
  127. }
  128. }