EditorControllerProvider.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. /// <summary>
  13. /// The class is to emulate controller with mouse and keyboard input in unity editor mode. </summary>
  14. public class EditorControllerProvider : ControllerProviderBase
  15. {
  16. /// <summary> Gets the number of controllers. </summary>
  17. /// <value> The number of controllers. </value>
  18. public override int ControllerCount
  19. {
  20. get
  21. {
  22. return 1;
  23. }
  24. }
  25. /// <summary> Constructor. </summary>
  26. /// <param name="states"> The states.</param>
  27. public EditorControllerProvider(ControllerState[] states) : base(states)
  28. {
  29. Inited = true;
  30. }
  31. /// <summary> Executes the 'pause' action. </summary>
  32. public override void OnPause()
  33. {
  34. }
  35. /// <summary> Executes the 'resume' action. </summary>
  36. public override void OnResume()
  37. {
  38. }
  39. /// <summary> Updates this object. </summary>
  40. public override void Update()
  41. {
  42. if (!Inited)
  43. return;
  44. UpdateControllerState(0);
  45. }
  46. public override void Recenter()
  47. {
  48. base.Recenter();
  49. needRecenter = true;
  50. }
  51. /// <summary> Executes the 'destroy' action. </summary>
  52. public override void OnDestroy()
  53. {
  54. }
  55. /// <summary> Updates the controller state described by index. </summary>
  56. /// <param name="index"> Zero-based index of the.</param>
  57. private void UpdateControllerState(int index)
  58. {
  59. states[index].controllerType = ControllerType.CONTROLLER_TYPE_EDITOR;
  60. states[index].availableFeature = ControllerAvailableFeature.CONTROLLER_AVAILABLE_FEATURE_ROTATION;
  61. states[index].connectionState = ControllerConnectionState.CONTROLLER_CONNECTION_STATE_CONNECTED;
  62. states[index].rotation = rotation;
  63. states[index].touchPos = touchPos;
  64. states[index].isTouching = isTouching;
  65. states[index].recentered = false;
  66. states[index].isCharging = false;
  67. states[index].batteryLevel = 100;
  68. if (NRInput.EmulateVirtualDisplayInEditor)
  69. {
  70. IControllerStateParser stateParser = ControllerStateParseUtility.GetControllerStateParser(states[index].controllerType, index);
  71. if (stateParser != null)
  72. {
  73. stateParser.ParserControllerState(states[index]);
  74. }
  75. }
  76. else
  77. {
  78. var trigger_button_state = (states[index].buttonsState) & (ControllerButton.TRIGGER);
  79. var app_button_state = (states[index].buttonsState) & (ControllerButton.APP);
  80. var home_button_state = (states[index].buttonsState) & (ControllerButton.HOME);
  81. states[index].buttonsState =
  82. ((buttonState[0] == 1) ? ControllerButton.TRIGGER : 0)
  83. | ((buttonState[1] == 1) ? ControllerButton.APP : 0)
  84. | ((buttonState[2] == 1) ? ControllerButton.HOME : 0);
  85. states[index].buttonsDown =
  86. (trigger_button_state != ControllerButton.TRIGGER && buttonState[0] == 1 ? ControllerButton.TRIGGER : 0)
  87. | (app_button_state != ControllerButton.APP && buttonState[1] == 1 ? ControllerButton.APP : 0)
  88. | (home_button_state != ControllerButton.HOME && buttonState[2] == 1 ? ControllerButton.HOME : 0);
  89. states[index].buttonsUp =
  90. (trigger_button_state == ControllerButton.TRIGGER && buttonState[0] == 0 ? ControllerButton.TRIGGER : 0)
  91. | (app_button_state == ControllerButton.APP && buttonState[1] == 0 ? ControllerButton.APP : 0)
  92. | (home_button_state == ControllerButton.HOME && buttonState[2] == 0 ? ControllerButton.HOME : 0);
  93. }
  94. CheckRecenter(index);
  95. if (needRecenter)
  96. {
  97. states[0].recentered = true;
  98. resetRotation = Quaternion.Inverse(originRotation);
  99. needRecenter = false;
  100. }
  101. }
  102. bool needRecenter;
  103. float timeLast = 0;
  104. private void CheckRecenter(int index)
  105. {
  106. if (states[index].GetButton(ControllerButton.HOME))
  107. {
  108. timeLast += Time.deltaTime;
  109. if (timeLast > 2f)
  110. {
  111. needRecenter = true;
  112. timeLast = 0f;
  113. }
  114. }
  115. else
  116. {
  117. timeLast = 0f;
  118. }
  119. }
  120. // Receive input data.
  121. private static Vector2 touchPos;
  122. private static bool isTouching = false;
  123. private static int[] buttonState = new int[3] { 0, 0, 0 };
  124. private static Quaternion rotation = Quaternion.identity;
  125. private static Quaternion originRotation = Quaternion.identity;
  126. private static Quaternion resetRotation = Quaternion.identity;
  127. public static void SetControllerTouchPoint(float x, float y)
  128. {
  129. touchPos.x = x;
  130. touchPos.y = y;
  131. }
  132. public static void SetControllerIsTouching(bool istouching)
  133. {
  134. isTouching = istouching;
  135. }
  136. public static void SetControllerButtonState(ControllerButton button, int state)
  137. {
  138. switch (button)
  139. {
  140. case ControllerButton.TRIGGER:
  141. buttonState[0] = state;
  142. buttonState[1] = 0;
  143. buttonState[2] = 0;
  144. break;
  145. case ControllerButton.APP:
  146. buttonState[0] = 0;
  147. buttonState[1] = state;
  148. buttonState[2] = 0;
  149. break;
  150. case ControllerButton.HOME:
  151. buttonState[0] = 0;
  152. buttonState[1] = 0;
  153. buttonState[2] = state;
  154. break;
  155. default:
  156. break;
  157. }
  158. }
  159. public static void SetControllerRotation(Quaternion qua)
  160. {
  161. originRotation = qua;
  162. rotation = resetRotation * originRotation;
  163. }
  164. }
  165. }