NREmulatorController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using UnityEngine;
  12. #if UNITY_EDITOR
  13. /// <summary> A controller for handling nr emulators. </summary>
  14. public class NREmulatorController : MonoBehaviour
  15. {
  16. /// <summary> regular speed. </summary>
  17. public float HeadMoveSpeed = 1.0f;
  18. /// <summary> How sensitive it with mouse. </summary>
  19. public float HeadRotateSpeed = 1.0f;
  20. /// <summary> The default controller panel. </summary>
  21. public GameObject DefaultControllerPanel;
  22. /// <summary> The image default. </summary>
  23. public GameObject ImageDefault;
  24. /// <summary> The image application. </summary>
  25. public GameObject ImageApp;
  26. /// <summary> The image confirm. </summary>
  27. public GameObject ImageConfirm;
  28. /// <summary> The image home. </summary>
  29. public GameObject ImageHome;
  30. /// <summary> The image left. </summary>
  31. public GameObject ImageLeft;
  32. /// <summary> The image right. </summary>
  33. public GameObject ImageRight;
  34. /// <summary> The image up. </summary>
  35. public GameObject ImageUp;
  36. /// <summary> The image down. </summary>
  37. public GameObject ImageDown;
  38. /// <summary> The width. </summary>
  39. private const int kWidth = 2;
  40. /// <summary> The height. </summary>
  41. private const int kHeight = 2;
  42. /// <summary> The touch action. </summary>
  43. private TouchActionState m_TouchAction;
  44. /// <summary> The touch action current frame. </summary>
  45. private int m_TouchActionCurFrame;
  46. /// <summary> Target for the. </summary>
  47. private GameObject m_Target;
  48. /// <summary> Values that represent touch action states. </summary>
  49. enum TouchActionState
  50. {
  51. /// <summary> An enum constant representing the idle option. </summary>
  52. Idle,
  53. /// <summary> An enum constant representing the left option. </summary>
  54. Left,
  55. /// <summary> An enum constant representing the right option. </summary>
  56. Right,
  57. /// <summary> An enum constant representing the up option. </summary>
  58. Up,
  59. /// <summary> An enum constant representing the down option. </summary>
  60. Down,
  61. };
  62. void Start()
  63. {
  64. DontDestroyOnLoad(this);
  65. m_Target = new GameObject("NREmulatorControllerTarget");
  66. m_Target.transform.rotation = Quaternion.identity;
  67. DontDestroyOnLoad(m_Target);
  68. }
  69. void LateUpdate()
  70. {
  71. UpdateControllerRotateByInput();
  72. if (NRInput.EmulateVirtualDisplayInEditor)
  73. {
  74. DefaultControllerPanel.SetActive(false);
  75. UpdateVirtualControllerButtons();
  76. }
  77. else
  78. {
  79. DefaultControllerPanel.SetActive(true);
  80. UpdateDefaultControllerButtons();
  81. }
  82. }
  83. private void UpdateDefaultControllerButtons()
  84. {
  85. if (Input.GetMouseButtonDown(0))
  86. {
  87. SetConfirmButton(true);
  88. ImageConfirm.SetActive(true);
  89. }
  90. if (Input.GetMouseButtonUp(0))
  91. {
  92. SetConfirmButton(false);
  93. ImageConfirm.SetActive(false);
  94. }
  95. if (Input.GetMouseButtonDown(1))
  96. {
  97. SetAppButton(true);
  98. ImageApp.SetActive(true);
  99. }
  100. if (Input.GetMouseButtonUp(1))
  101. {
  102. SetAppButton(false);
  103. ImageApp.SetActive(false);
  104. }
  105. if (Input.GetMouseButtonDown(2))
  106. {
  107. SetHomeButton(true);
  108. ImageHome.SetActive(true);
  109. }
  110. if (Input.GetMouseButtonUp(2))
  111. {
  112. SetHomeButton(false);
  113. ImageHome.SetActive(false);
  114. }
  115. if (m_TouchAction != TouchActionState.Idle)
  116. {
  117. UpdateTouchAction();
  118. }
  119. else
  120. {
  121. if (Input.GetKeyDown(KeyCode.LeftArrow))
  122. {
  123. ImageLeft.SetActive(true);
  124. m_TouchAction = TouchActionState.Left;
  125. }
  126. else if (Input.GetKeyDown(KeyCode.RightArrow))
  127. {
  128. ImageRight.SetActive(true);
  129. m_TouchAction = TouchActionState.Right;
  130. }
  131. else if (Input.GetKeyDown(KeyCode.UpArrow))
  132. {
  133. ImageUp.SetActive(true);
  134. m_TouchAction = TouchActionState.Up;
  135. }
  136. else if (Input.GetKeyDown(KeyCode.DownArrow))
  137. {
  138. ImageDown.SetActive(true);
  139. m_TouchAction = TouchActionState.Down;
  140. }
  141. else if (Input.GetKeyUp(KeyCode.DownArrow)
  142. | Input.GetKeyUp(KeyCode.UpArrow)
  143. | Input.GetKeyUp(KeyCode.RightArrow)
  144. | Input.GetKeyUp(KeyCode.LeftArrow))
  145. {
  146. EditorControllerProvider.SetControllerIsTouching(false);
  147. }
  148. }
  149. }
  150. private void UpdateVirtualControllerButtons()
  151. {
  152. EditorControllerProvider.SetControllerButtonState(ControllerButton.APP, 0);
  153. EditorControllerProvider.SetControllerButtonState(ControllerButton.HOME, 0);
  154. EditorControllerProvider.SetControllerButtonState(ControllerButton.TRIGGER, 0);
  155. EditorControllerProvider.SetControllerIsTouching(true);
  156. EditorControllerProvider.SetControllerTouchPoint(NRVirtualDisplayer.GetEmulatorScreenTouch().x, NRVirtualDisplayer.GetEmulatorScreenTouch().y);
  157. }
  158. private void UpdateTouchAction()
  159. {
  160. EditorControllerProvider.SetControllerIsTouching(true);
  161. const int kActionMaxFrame = 20;
  162. float touchx = 0;
  163. float touchy = 0;
  164. if (m_TouchAction == TouchActionState.Left)
  165. {
  166. touchy = kHeight / 2;
  167. touchx = 0.1f * kWidth + ((float)(kActionMaxFrame - m_TouchActionCurFrame) / kActionMaxFrame) * (0.8f * kWidth);
  168. }
  169. else if (m_TouchAction == TouchActionState.Right)
  170. {
  171. touchy = kHeight / 2;
  172. touchx = 0.1f * kWidth + ((float)m_TouchActionCurFrame / kActionMaxFrame) * (0.8f * kWidth);
  173. }
  174. else if (m_TouchAction == TouchActionState.Up)
  175. {
  176. touchx = kWidth / 2;
  177. touchy = 0.1f * kHeight + ((float)(kActionMaxFrame - m_TouchActionCurFrame) / kActionMaxFrame) * (0.8f * kHeight);
  178. }
  179. else if (m_TouchAction == TouchActionState.Down)
  180. {
  181. touchx = kWidth / 2;
  182. touchy = 0.1f * kHeight + ((float)m_TouchActionCurFrame / kActionMaxFrame) * (0.8f * kHeight);
  183. }
  184. EditorControllerProvider.SetControllerTouchPoint(touchx - 1f, touchy - 1f);
  185. if (m_TouchActionCurFrame == kActionMaxFrame)
  186. {
  187. m_TouchActionCurFrame = 0;
  188. m_TouchAction = TouchActionState.Idle;
  189. ImageLeft.SetActive(false);
  190. ImageRight.SetActive(false);
  191. ImageUp.SetActive(false);
  192. ImageDown.SetActive(false);
  193. }
  194. m_TouchActionCurFrame++;
  195. }
  196. private void UpdateControllerRotateByInput()
  197. {
  198. if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
  199. {
  200. float mouse_x = Input.GetAxis("Mouse X") * HeadRotateSpeed;
  201. float mouse_y = Input.GetAxis("Mouse Y") * HeadRotateSpeed;
  202. Quaternion q = Quaternion.Euler(-mouse_y, mouse_x, 0);
  203. m_Target.transform.rotation = m_Target.transform.rotation * q;
  204. }
  205. EditorControllerProvider.SetControllerRotation(m_Target.transform.rotation);
  206. }
  207. /// <summary> Sets application button. </summary>
  208. /// <param name="touch"> True to touch.</param>
  209. public void SetAppButton(bool touch)
  210. {
  211. if (touch)
  212. {
  213. EditorControllerProvider.SetControllerTouchPoint(0f, 0f);
  214. EditorControllerProvider.SetControllerIsTouching(true);
  215. EditorControllerProvider.SetControllerButtonState(ControllerButton.APP, 1);
  216. }
  217. else
  218. {
  219. EditorControllerProvider.SetControllerButtonState(ControllerButton.APP, 0);
  220. EditorControllerProvider.SetControllerIsTouching(false);
  221. }
  222. }
  223. /// <summary> Sets home button. </summary>
  224. /// <param name="touch"> True to touch.</param>
  225. public void SetHomeButton(bool touch)
  226. {
  227. if (touch)
  228. {
  229. EditorControllerProvider.SetControllerTouchPoint(0f, 0f);
  230. EditorControllerProvider.SetControllerIsTouching(true);
  231. EditorControllerProvider.SetControllerButtonState(ControllerButton.HOME, 1);
  232. }
  233. else
  234. {
  235. EditorControllerProvider.SetControllerButtonState(ControllerButton.HOME, 0);
  236. EditorControllerProvider.SetControllerIsTouching(false);
  237. }
  238. }
  239. /// <summary> Sets confirm button. </summary>
  240. /// <param name="touch"> True to touch.</param>
  241. public void SetConfirmButton(bool touch)
  242. {
  243. if (touch)
  244. {
  245. EditorControllerProvider.SetControllerTouchPoint(0f, 0f);
  246. EditorControllerProvider.SetControllerIsTouching(true);
  247. EditorControllerProvider.SetControllerButtonState(ControllerButton.TRIGGER, 1);
  248. }
  249. else
  250. {
  251. EditorControllerProvider.SetControllerButtonState(ControllerButton.TRIGGER, 0);
  252. EditorControllerProvider.SetControllerIsTouching(false);
  253. }
  254. }
  255. }
  256. #endif
  257. }