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.

306 lines
10 KiB

5 years ago
  1. //========= Copyright 2016-2020, HTC Corporation. All rights reserved. ===========
  2. using HTC.UnityPlugin.Utility;
  3. using System;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.Events;
  7. namespace HTC.UnityPlugin.Vive
  8. {
  9. // Use this helper component to combine multiple Vive inputs into one virtual button
  10. public class ViveInputVirtualButton : MonoBehaviour
  11. {
  12. public enum InputsOperatorEnum
  13. {
  14. Or,
  15. And,
  16. }
  17. [Serializable]
  18. public class InputEntry
  19. {
  20. public ViveRoleProperty viveRole = ViveRoleProperty.New(HandRole.RightHand);
  21. [CustomOrderedEnum]
  22. public ControllerButton button = ControllerButton.Trigger;
  23. }
  24. [Serializable]
  25. public struct OutputEventArgs
  26. {
  27. public ViveInputVirtualButton senderObj;
  28. public ButtonEventType eventType;
  29. }
  30. [Serializable]
  31. public class OutputEvent : UnityEvent<OutputEventArgs> { }
  32. [SerializeField]
  33. private InputsOperatorEnum m_combineInputsOperator = InputsOperatorEnum.Or;
  34. [SerializeField]
  35. private List<InputEntry> m_inputs = new List<InputEntry>();
  36. [SerializeField]
  37. private OutputEvent m_onVirtualPress = new OutputEvent();
  38. [SerializeField]
  39. private OutputEvent m_onVirtualClick = new OutputEvent();
  40. [SerializeField]
  41. private OutputEvent m_onVirtualPressDown = new OutputEvent();
  42. [SerializeField]
  43. private OutputEvent m_onVirtualPressUp = new OutputEvent();
  44. [SerializeField]
  45. private List<GameObject> m_toggleGameObjectOnVirtualClick = new List<GameObject>();
  46. [SerializeField]
  47. private List<Behaviour> m_toggleComponentOnVirtualClick = new List<Behaviour>();
  48. private bool m_isUpdating;
  49. private int m_updatedFrameCount;
  50. private bool m_prevPressState = false;
  51. private bool m_currPressState = false;
  52. private float m_lastPressDownTime = 0f;
  53. private int m_clickCount = 0;
  54. [Obsolete("Use Behaviour.enable instead.")]
  55. public bool active { get { return enabled; } set { enabled = value; } }
  56. public InputsOperatorEnum combineInputsOperator { get { return m_combineInputsOperator; } }
  57. public List<InputEntry> inputs { get { return m_inputs; } }
  58. public List<GameObject> toggleGameObjectOnVirtualClick { get { return m_toggleGameObjectOnVirtualClick; } }
  59. public List<Behaviour> toggleComponentOnVirtualClick { get { return m_toggleComponentOnVirtualClick; } }
  60. public OutputEvent onPress { get { return m_onVirtualPress; } }
  61. public OutputEvent onClick { get { return m_onVirtualClick; } }
  62. public OutputEvent onPressDown { get { return m_onVirtualPressDown; } }
  63. public OutputEvent onPressUp { get { return m_onVirtualPressUp; } }
  64. private bool isPress { get { return m_currPressState; } }
  65. private bool isDown { get { return !m_prevPressState && m_currPressState; } }
  66. private bool isUp { get { return m_prevPressState && !m_currPressState; } }
  67. #if UNITY_EDITOR
  68. private void Reset()
  69. {
  70. m_inputs.Add(new InputEntry()
  71. {
  72. viveRole = ViveRoleProperty.New(HandRole.RightHand),
  73. button = ControllerButton.Trigger,
  74. });
  75. }
  76. #endif
  77. private void UpdateState()
  78. {
  79. if (!ChangeProp.Set(ref m_updatedFrameCount, Time.frameCount)) { return; }
  80. m_prevPressState = m_currPressState;
  81. m_currPressState = false;
  82. if (m_inputs.Count == 0) { return; }
  83. switch (m_combineInputsOperator)
  84. {
  85. case InputsOperatorEnum.Or:
  86. m_currPressState = false;
  87. for (int i = 0, imax = m_inputs.Count; i < imax; ++i)
  88. {
  89. if (ViveInput.GetPress(m_inputs[i].viveRole, m_inputs[i].button))
  90. {
  91. m_currPressState = true;
  92. break;
  93. }
  94. }
  95. break;
  96. case InputsOperatorEnum.And:
  97. m_currPressState = true;
  98. for (int i = 0, imax = m_inputs.Count; i < imax; ++i)
  99. {
  100. if (!ViveInput.GetPress(m_inputs[i].viveRole, m_inputs[i].button))
  101. {
  102. m_currPressState = false;
  103. break;
  104. }
  105. }
  106. break;
  107. }
  108. }
  109. private void Update()
  110. {
  111. m_isUpdating = true;
  112. UpdateState();
  113. var timeNow = Time.unscaledTime;
  114. // handle events
  115. if (isPress)
  116. {
  117. if (isDown)
  118. {
  119. // record click count
  120. if (timeNow - m_lastPressDownTime < ViveInput.clickInterval)
  121. {
  122. ++m_clickCount;
  123. }
  124. else
  125. {
  126. m_clickCount = 1;
  127. }
  128. // record press down time
  129. m_lastPressDownTime = timeNow;
  130. // PressDown event
  131. if (m_onVirtualPressDown != null)
  132. {
  133. m_onVirtualPressDown.Invoke(new OutputEventArgs()
  134. {
  135. senderObj = this,
  136. eventType = ButtonEventType.Down,
  137. });
  138. }
  139. }
  140. // Press event
  141. if (m_onVirtualPress != null)
  142. {
  143. m_onVirtualPress.Invoke(new OutputEventArgs()
  144. {
  145. senderObj = this,
  146. eventType = ButtonEventType.Press,
  147. });
  148. }
  149. }
  150. else if (isUp)
  151. {
  152. // PressUp event
  153. if (m_onVirtualPressUp != null)
  154. {
  155. m_onVirtualPressUp.Invoke(new OutputEventArgs()
  156. {
  157. senderObj = this,
  158. eventType = ButtonEventType.Up,
  159. });
  160. }
  161. if (timeNow - m_lastPressDownTime < ViveInput.clickInterval)
  162. {
  163. for (int i = m_toggleGameObjectOnVirtualClick.Count - 1; i >= 0; --i)
  164. {
  165. if (m_toggleGameObjectOnVirtualClick[i] != null) { m_toggleGameObjectOnVirtualClick[i].SetActive(!m_toggleGameObjectOnVirtualClick[i].activeSelf); }
  166. }
  167. for (int i = m_toggleComponentOnVirtualClick.Count - 1; i >= 0; --i)
  168. {
  169. if (m_toggleComponentOnVirtualClick[i] != null) { m_toggleComponentOnVirtualClick[i].enabled = !m_toggleComponentOnVirtualClick[i].enabled; }
  170. }
  171. // Click event
  172. if (m_onVirtualClick != null)
  173. {
  174. m_onVirtualClick.Invoke(new OutputEventArgs()
  175. {
  176. senderObj = this,
  177. eventType = ButtonEventType.Click,
  178. });
  179. }
  180. }
  181. }
  182. if (!isActiveAndEnabled)
  183. {
  184. InternalDisable();
  185. }
  186. m_isUpdating = false;
  187. }
  188. private void OnDisable()
  189. {
  190. if (!m_isUpdating)
  191. {
  192. InternalDisable();
  193. }
  194. }
  195. private void InternalDisable()
  196. {
  197. var timeNow = Time.unscaledTime;
  198. // clean up
  199. m_prevPressState = m_currPressState;
  200. m_currPressState = false;
  201. if (isUp)
  202. {
  203. // PressUp event
  204. if (m_onVirtualPressUp != null)
  205. {
  206. m_onVirtualPressUp.Invoke(new OutputEventArgs()
  207. {
  208. senderObj = this,
  209. eventType = ButtonEventType.Up,
  210. });
  211. }
  212. if (timeNow - m_lastPressDownTime < ViveInput.clickInterval)
  213. {
  214. for (int i = m_toggleGameObjectOnVirtualClick.Count - 1; i >= 0; --i)
  215. {
  216. if (m_toggleGameObjectOnVirtualClick[i] != null) { m_toggleGameObjectOnVirtualClick[i].SetActive(!m_toggleGameObjectOnVirtualClick[i].activeSelf); }
  217. }
  218. for (int i = m_toggleComponentOnVirtualClick.Count - 1; i >= 0; --i)
  219. {
  220. if (m_toggleComponentOnVirtualClick[i] != null) { m_toggleComponentOnVirtualClick[i].enabled = !m_toggleComponentOnVirtualClick[i].enabled; }
  221. }
  222. // Click event
  223. if (m_onVirtualClick != null)
  224. {
  225. m_onVirtualClick.Invoke(new OutputEventArgs()
  226. {
  227. senderObj = this,
  228. eventType = ButtonEventType.Click,
  229. });
  230. }
  231. }
  232. }
  233. m_prevPressState = false;
  234. }
  235. public bool GetVirtualPress()
  236. {
  237. UpdateState();
  238. return isPress;
  239. }
  240. public bool GetVirtualPressDown()
  241. {
  242. UpdateState();
  243. return isDown;
  244. }
  245. public bool GetVirtualPressUp()
  246. {
  247. UpdateState();
  248. return isUp;
  249. }
  250. public int GetVirtualClickCount()
  251. {
  252. UpdateState();
  253. return m_clickCount;
  254. }
  255. public float GetLastVirtualPressDownTime()
  256. {
  257. UpdateState();
  258. return m_lastPressDownTime;
  259. }
  260. }
  261. }