NxrButtonEvent.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using NibiruTask;
  2. using UnityEngine;
  3. using UnityEngine.Events;
  4. namespace Nxr.Internal
  5. {
  6. public class NxrButtonEvent : MonoBehaviour
  7. {
  8. [System.Serializable]
  9. public class NibiruControllerEvent : UnityEvent<NxrInstantNativeApi.NibiruDeviceType>
  10. {
  11. }
  12. [System.Serializable]
  13. public class NibiruControllerTouchEvent : UnityEvent<NxrInstantNativeApi.NibiruDeviceType, float, float>
  14. {
  15. }
  16. public NibiruControllerEvent onMenuDown;
  17. public NibiruControllerEvent onMenuUp;
  18. public NibiruControllerEvent onSystemDown;
  19. public NibiruControllerEvent onSystemUp;
  20. public NibiruControllerEvent onTriggerDown;
  21. public NibiruControllerEvent onTriggerUp;
  22. public NibiruControllerEvent onGripDown;
  23. public NibiruControllerEvent onGripUp;
  24. public NibiruControllerEvent onTouchpadDown;
  25. public NibiruControllerEvent onTouchpadUp;
  26. public NibiruControllerEvent onTouchpadTouch;
  27. public NibiruControllerEvent onTouchpadRelease;
  28. public NibiruControllerTouchEvent onTouchPosition;
  29. public UnityEvent onDpadLeftDown;
  30. public UnityEvent onDpadLeftUp;
  31. public UnityEvent onDpadRightDown;
  32. public UnityEvent onDpadRightUp;
  33. public UnityEvent onDpadUpLifted;
  34. public UnityEvent onDpadUpDown;
  35. public UnityEvent onDpadDownUp;
  36. public UnityEvent onDpadDownPressed;
  37. public UnityEvent onDpadCenterDown;
  38. public UnityEvent onDpadCenterUp;
  39. NxrInstantNativeApi.Nibiru_ControllerStates _prevStates_hmd;
  40. NxrInstantNativeApi.Nibiru_ControllerStates _currentStates_hmd;
  41. NxrTrackedDevice[] nvrTrackedDevices;
  42. private void Start()
  43. {
  44. nvrTrackedDevices = FindObjectsOfType<NxrTrackedDevice>();
  45. }
  46. public void RefreshTrackedDevices()
  47. {
  48. nvrTrackedDevices = FindObjectsOfType<NxrTrackedDevice>();
  49. }
  50. private int getNoloType(NxrInstantNativeApi.NibiruDeviceType deviceType)
  51. {
  52. int noloType = (int)CDevice.NOLO_TYPE.NONE;
  53. if (deviceType == NxrInstantNativeApi.NibiruDeviceType.LeftController)
  54. {
  55. noloType = (int)CDevice.NOLO_TYPE.LEFT;
  56. }
  57. else if (deviceType == NxrInstantNativeApi.NibiruDeviceType.RightController)
  58. {
  59. noloType = (int)CDevice.NOLO_TYPE.RIGHT;
  60. }
  61. else if (deviceType == NxrInstantNativeApi.NibiruDeviceType.Hmd)
  62. {
  63. noloType = (int)CDevice.NOLO_TYPE.HEAD;
  64. }
  65. return noloType;
  66. }
  67. //-------------------------------------------------
  68. void Update()
  69. {
  70. _prevStates_hmd = _currentStates_hmd;
  71. #if UNITY_STANDALONE_WIN || ANDROID_REMOTE_NRR
  72. _currentStates_hmd = NxrInstantNativeApi.GetControllerStates(NxrInstantNativeApi.NibiruDeviceType.Hmd);
  73. #endif
  74. if (GetHMDButtonDown(NxrTrackedDevice.ButtonID.DPadCenter))
  75. {
  76. NxrViewer.Instance.Triggered = true;
  77. onDpadCenterDown.Invoke();
  78. }
  79. if (GetHMDButtonUp(NxrTrackedDevice.ButtonID.DPadCenter))
  80. {
  81. onDpadCenterUp.Invoke();
  82. }
  83. if (GetHMDButtonDown(NxrTrackedDevice.ButtonID.DPadUp))
  84. {
  85. onDpadUpDown.Invoke();
  86. }
  87. if (GetHMDButtonUp(NxrTrackedDevice.ButtonID.DPadUp))
  88. {
  89. onDpadUpLifted.Invoke();
  90. }
  91. if (GetHMDButtonDown(NxrTrackedDevice.ButtonID.DPadDown))
  92. {
  93. onDpadDownPressed.Invoke();
  94. }
  95. if (GetHMDButtonUp(NxrTrackedDevice.ButtonID.DPadDown))
  96. {
  97. onDpadDownUp.Invoke();
  98. }
  99. if (GetHMDButtonDown(NxrTrackedDevice.ButtonID.DPadLeft))
  100. {
  101. onDpadLeftDown.Invoke();
  102. }
  103. if (GetHMDButtonUp(NxrTrackedDevice.ButtonID.DPadLeft))
  104. {
  105. onDpadLeftUp.Invoke();
  106. }
  107. if (GetHMDButtonDown(NxrTrackedDevice.ButtonID.DPadRight))
  108. {
  109. onDpadRightDown.Invoke();
  110. }
  111. if (GetHMDButtonUp(NxrTrackedDevice.ButtonID.DPadRight))
  112. {
  113. onDpadRightUp.Invoke();
  114. }
  115. if(nvrTrackedDevices != null)
  116. {
  117. foreach(NxrTrackedDevice device in nvrTrackedDevices)
  118. {
  119. if(device.GetButtonDown(NxrTrackedDevice.ButtonID.TouchPad))
  120. {
  121. if (!device.isGamePad)
  122. {
  123. NxrViewer.Instance.Triggered = true;
  124. }
  125. onTouchpadDown.Invoke(device.deviceType);
  126. }
  127. if (device.GetButtonUp(NxrTrackedDevice.ButtonID.TouchPad))
  128. {
  129. onTouchpadUp.Invoke(device.deviceType);
  130. }
  131. if (device.GetButtonDown(NxrTrackedDevice.ButtonID.System))
  132. {
  133. onSystemDown.Invoke(device.deviceType);
  134. }
  135. if (device.GetButtonUp(NxrTrackedDevice.ButtonID.System))
  136. {
  137. onSystemUp.Invoke(device.deviceType);
  138. }
  139. if (device.GetButtonDown(NxrTrackedDevice.ButtonID.Menu))
  140. {
  141. onMenuDown.Invoke(device.deviceType);
  142. }
  143. if (device.GetButtonUp(NxrTrackedDevice.ButtonID.Menu))
  144. {
  145. onMenuUp.Invoke(device.deviceType);
  146. }
  147. if (device.GetButtonDown(NxrTrackedDevice.ButtonID.Grip))
  148. {
  149. onGripDown.Invoke(device.deviceType);
  150. }
  151. if (device.GetButtonUp(NxrTrackedDevice.ButtonID.Grip))
  152. {
  153. onGripUp.Invoke(device.deviceType);
  154. }
  155. if (device.GetButtonDown(NxrTrackedDevice.ButtonID.Trigger))
  156. {
  157. onTriggerDown.Invoke(device.deviceType);
  158. }
  159. if (device.GetButtonUp(NxrTrackedDevice.ButtonID.Trigger))
  160. {
  161. onTriggerUp.Invoke(device.deviceType);
  162. }
  163. if (device.GetTouchDown(NxrTrackedDevice.ButtonID.TrackpadTouch))
  164. {
  165. onTouchpadTouch.Invoke(device.deviceType);
  166. }
  167. if (device.GetTouchUp(NxrTrackedDevice.ButtonID.TrackpadTouch))
  168. {
  169. onTouchpadRelease.Invoke(device.deviceType);
  170. }
  171. Vector2 position = device.GetTouchPosition();
  172. if (position.x != 0 || position.y !=0)
  173. {
  174. onTouchPosition.Invoke(device.deviceType, position.x, position.y);
  175. }
  176. }
  177. }
  178. }
  179. public bool GetHMDButtonDown(NxrTrackedDevice.ButtonID btn)
  180. {
  181. return (_currentStates_hmd.hmdButtons & (1 << (int)btn)) != 0 && (_prevStates_hmd.hmdButtons & (1 << (int)btn)) == 0;
  182. }
  183. public bool GetHMDButtonUp(NxrTrackedDevice.ButtonID btn)
  184. {
  185. return (_currentStates_hmd.hmdButtons & (1 << (int)btn)) == 0 && (_prevStates_hmd.hmdButtons & (1 << (int)btn)) != 0;
  186. }
  187. public bool GetHMDButtonPressed(NxrTrackedDevice.ButtonID btn)
  188. {
  189. return (_currentStates_hmd.hmdButtons & (1 << (int)btn)) != 0;
  190. }
  191. }
  192. }