2021년 4학년 1학기 기업연계프로젝트2 컴퓨터소프트웨어공학과 <원광투어팀> 팀장 : 송유진 팀원 : 김나영, 이경희, 한유진
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
8.3 KiB

5 years ago
  1. //========= Copyright 2016-2020, HTC Corporation. All rights reserved. ===========
  2. using HTC.UnityPlugin.Utility;
  3. using HTC.UnityPlugin.VRModuleManagement;
  4. using System;
  5. using UnityEngine;
  6. using UnityEngine.Events;
  7. namespace HTC.UnityPlugin.Vive
  8. {
  9. public enum ButtonEventType
  10. {
  11. /// <summary>
  12. /// Button unpressed at last frame, pressed at this frame
  13. /// </summary>
  14. Down,
  15. /// <summary>
  16. /// Button pressed at this frame
  17. /// </summary>
  18. Press,
  19. /// <summary>
  20. /// Button pressed at last frame, unpressed at the frame
  21. /// </summary>
  22. Up,
  23. /// <summary>
  24. /// Button up at this frame, and last button down time is in certain interval
  25. /// </summary>
  26. Click,
  27. }
  28. /// <summary>
  29. /// Defines virtual buttons for Vive controller
  30. /// </summary>
  31. public enum ControllerButton
  32. {
  33. None = -1,
  34. // classic buttons
  35. System = 14,
  36. Menu = 4, // Cosmos(RightHandB, LeftHandY), Index(B)
  37. MenuTouch = 7, // Cosmos(RightHandB, LeftHandY), Index(B)
  38. Trigger = 0, // on:0.55 off:0.45
  39. TriggerTouch = 8, // on:0.25 off:0.20
  40. Pad = 1,
  41. PadTouch = 3,
  42. Grip = 2,
  43. GripTouch = 9,
  44. CapSenseGrip = 10, // on:1.00 off:0.90 // Knuckles, Oculus Touch only
  45. CapSenseGripTouch = 11, // on:0.25 off:0.20 // Knuckles, Oculus Touch only
  46. ProximitySensor = 15,
  47. Bumper = 16,
  48. BumperTouch = 17,
  49. AKey = 12, // Knuckles(InnerFaceButton), Oculus Touch(RightHandA or LeftHandX pressed), Cosmos(RightHandA, LeftHandX), Index(A)
  50. AKeyTouch = 13, // Knuckles(InnerFaceButton), Oculus Touch(RightHandA or LeftHandX touched), Cosmos(RightHandA, LeftHandX), Index(A)
  51. // button alias
  52. BKey = Menu,
  53. BkeyTouch = MenuTouch,
  54. OuterFaceButton = Menu, // 7
  55. OuterFaceButtonTouch = MenuTouch, // 9
  56. InnerFaceButton = AKey, // 12
  57. InnerFaceButtonTouch = AKeyTouch, // 13
  58. [HideInInspector]
  59. Axis0 = Pad,
  60. [HideInInspector]
  61. Axis1 = Trigger,
  62. [HideInInspector]
  63. Axis2 = CapSenseGrip,
  64. [HideInInspector]
  65. Axis3 = Bumper,
  66. [HideInInspector]
  67. Axis4 = 18,
  68. [HideInInspector]
  69. Axis0Touch = PadTouch,
  70. [HideInInspector]
  71. Axis1Touch = TriggerTouch,
  72. [HideInInspector]
  73. Axis2Touch = CapSenseGripTouch,
  74. [HideInInspector]
  75. Axis3Touch = BumperTouch,
  76. [HideInInspector]
  77. Axis4Touch = 19,
  78. // virtual buttons
  79. HairTrigger = 5, // Pressed if trigger button is pressing, unpressed if trigger button is releasing
  80. FullTrigger = 6, // on:1.00 off:1.00
  81. DPadLeft = 20,
  82. DPadUp = 21,
  83. DPadRight = 22,
  84. DPadDown = 23,
  85. DPadLeftTouch = 24,
  86. DPadUpTouch = 25,
  87. DPadRightTouch = 26,
  88. DPadDownTouch = 27,
  89. DPadUpperLeft = 28,
  90. DPadUpperRight = 29,
  91. DPadLowerRight = 30,
  92. DPadLowerLeft = 31,
  93. DPadUpperLeftTouch = 32,
  94. DPadUpperRightTouch = 33,
  95. DPadLowerRightTouch = 34,
  96. DPadLowerLeftTouch = 35,
  97. DPadCenter = 36,
  98. DPadCenterTouch = 37,
  99. }
  100. public enum ControllerAxis
  101. {
  102. None = -1,
  103. PadX,
  104. PadY,
  105. Trigger,
  106. CapSenseGrip, // Knuckles, Oculus Touch only
  107. IndexCurl, // Knuckles only
  108. MiddleCurl, // Knuckles only
  109. RingCurl, // Knuckles only
  110. PinkyCurl, // Knuckles only
  111. JoystickCap = RingCurl, // Cosmos only
  112. TriggerCap = PinkyCurl, // Cosmos only
  113. JoystickX,
  114. JoystickY,
  115. }
  116. public enum ScrollType
  117. {
  118. None = -1,
  119. Auto,
  120. Trackpad,
  121. Thumbstick,
  122. }
  123. public class RawControllerState
  124. {
  125. public readonly bool[] buttonPress = new bool[ViveInput.CONTROLLER_BUTTON_COUNT];
  126. public readonly float[] axisValue = new float[ViveInput.CONTROLLER_AXIS_COUNT];
  127. }
  128. /// <summary>
  129. /// Singleton that manage and update controllers input
  130. /// </summary>
  131. public partial class ViveInput : SingletonBehaviour<ViveInput>
  132. {
  133. public static readonly int CONTROLLER_BUTTON_COUNT = EnumUtils.GetMaxValue(typeof(ControllerButton)) + 1;
  134. public static readonly int CONTROLLER_AXIS_COUNT = EnumUtils.GetMaxValue(typeof(ControllerAxis)) + 1;
  135. public static readonly int BUTTON_EVENT_COUNT = EnumUtils.GetMaxValue(typeof(ButtonEventType)) + 1;
  136. private static readonly CtrlState s_defaultState = new CtrlState();
  137. private static readonly IndexedTable<Type, ICtrlState[]> s_roleStateTable = new IndexedTable<Type, ICtrlState[]>();
  138. private static UnityAction s_onUpdate;
  139. [SerializeField]
  140. private float m_clickInterval = 0.3f;
  141. [SerializeField]
  142. private bool m_dontDestroyOnLoad = false;
  143. [SerializeField]
  144. private UnityEvent m_onUpdate = new UnityEvent();
  145. public static float clickInterval
  146. {
  147. get { return Instance.m_clickInterval; }
  148. set { Instance.m_clickInterval = Mathf.Max(0f, value); }
  149. }
  150. public static event UnityAction onUpdate { add { s_onUpdate += value; } remove { s_onUpdate -= value; } }
  151. static ViveInput()
  152. {
  153. SetDefaultInitGameObjectGetter(VRModule.GetInstanceGameObject);
  154. }
  155. #if UNITY_EDITOR
  156. private void OnValidate()
  157. {
  158. m_clickInterval = Mathf.Max(m_clickInterval, 0f);
  159. }
  160. #endif
  161. protected override void OnSingletonBehaviourInitialized()
  162. {
  163. if (m_dontDestroyOnLoad && transform.parent == null)
  164. {
  165. DontDestroyOnLoad(gameObject);
  166. }
  167. }
  168. private void Update()
  169. {
  170. if (!IsInstance) { return; }
  171. for (int i = 0, imax = s_roleStateTable.Count; i < imax; ++i)
  172. {
  173. var states = s_roleStateTable.GetValueByIndex(i);
  174. if (states == null) { continue; }
  175. foreach (var state in states)
  176. {
  177. if (state == null) { continue; }
  178. state.Update();
  179. }
  180. }
  181. if (s_onUpdate != null) { s_onUpdate(); }
  182. if (m_onUpdate != null) { m_onUpdate.Invoke(); }
  183. }
  184. private static bool IsValidButton(ControllerButton button) { return button >= 0 && (int)button < CONTROLLER_BUTTON_COUNT; }
  185. private static bool IsValidAxis(ControllerAxis axis) { return axis >= 0 && (int)axis < CONTROLLER_BUTTON_COUNT; }
  186. private static ICtrlState GetState(Type roleType, int roleValue)
  187. {
  188. Initialize();
  189. var info = ViveRoleEnum.GetInfo(roleType);
  190. if (!info.IsValidRoleValue(roleValue)) { return s_defaultState; }
  191. ICtrlState[] stateList;
  192. if (!s_roleStateTable.TryGetValue(roleType, out stateList) || stateList == null)
  193. {
  194. s_roleStateTable[roleType] = stateList = new ICtrlState[info.ValidRoleLength];
  195. }
  196. var roleOffset = info.RoleValueToRoleOffset(roleValue);
  197. if (stateList[roleOffset] == null)
  198. {
  199. stateList[roleOffset] = new RCtrlState(roleType, roleValue);
  200. }
  201. stateList[roleOffset].Update();
  202. return stateList[roleOffset];
  203. }
  204. private static ICtrlState<TRole> GetState<TRole>(TRole role)
  205. {
  206. Initialize();
  207. var info = ViveRoleEnum.GetInfo<TRole>();
  208. if (!info.IsValidRole(role)) { return RGCtrolState<TRole>.s_defaultState; }
  209. if (RGCtrolState<TRole>.s_roleStates == null)
  210. {
  211. RGCtrolState<TRole>.s_roleStates = new RGCtrolState<TRole>[info.ValidRoleLength];
  212. }
  213. var roleOffset = info.RoleToRoleOffset(role);
  214. if (RGCtrolState<TRole>.s_roleStates[roleOffset] == null)
  215. {
  216. RGCtrolState<TRole>.s_roleStates[roleOffset] = new RGCtrolState<TRole>(role);
  217. s_roleStateTable[typeof(TRole)][roleOffset] = RGCtrolState<TRole>.s_roleStates[roleOffset];
  218. }
  219. RGCtrolState<TRole>.s_roleStates[roleOffset].Update();
  220. return RGCtrolState<TRole>.s_roleStates[roleOffset];
  221. }
  222. }
  223. }