ButtonMouseEventInput.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. using System.Diagnostics;
  2. using System;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using Rokid.UXR.Module;
  6. namespace Rokid.UXR.Interaction
  7. {
  8. public class ButtonMouseEventInput : MonoSingleton<ButtonMouseEventInput>, IEventInput
  9. {
  10. public static Action<Vector2, bool> OnMouseMove;
  11. public static Action OnActiveButtonMouseModule;
  12. public static Action OnReleaseButtonMouseModule;
  13. public static Action OnEnterLongPressState;
  14. public static Action OnExitLongPressState;
  15. public static Action OnReHoverState;
  16. private Vector2 mouseDelta = Vector2.zero;
  17. private bool mouseChange;
  18. private float elapsedTime = 0;
  19. private Vector2 scrollDelta = Vector2.zero;
  20. private bool scrollChange;
  21. private long xDownTime = 0;
  22. private long yDownTime = 0;
  23. public float DefaultScrollSpeed = 0.5f;
  24. public float DefaultSpeedScale = 0.002f;
  25. private long GetCurrentTime()
  26. {
  27. TimeSpan ts = DateTime.UtcNow - new DateTime(1970,
  28. 1, 1, 0, 0, 0, 0);
  29. return Convert.ToInt64(ts.TotalMilliseconds);
  30. }
  31. private bool isEnterLongPressState = false;
  32. private bool isReHoverState = false;
  33. public Transform Interactor { get; set; }
  34. public Transform CursorVisual { get; private set; }
  35. public Transform TurnPageIndicator { get; set; }
  36. private bool initialize = false;
  37. public void Initialize(Transform parent)
  38. {
  39. if (Interactor == null)
  40. {
  41. GameObject go = GameObject.Find("ButtonMouseRayInteractor");
  42. if (go == null)
  43. {
  44. go = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Interactor/ButtonMouseRayInteractor"));
  45. }
  46. Interactor = go.transform;
  47. Interactor.name = "ButtonMouseRayInteractor";
  48. Interactor.SetParent(transform);
  49. CursorVisual = Interactor.Find("CursorVisual");
  50. }
  51. Interactor.SetParent(transform);
  52. this.transform.SetParent(parent);
  53. if (TurnPageIndicator == null)
  54. {
  55. GameObject indicator = GameObject.Find("RKTurnPageIndicator");
  56. if (indicator == null)
  57. {
  58. indicator = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Interactor/RKTurnPageIndicator"));
  59. }
  60. indicator.gameObject.SetActive(false);
  61. TurnPageIndicator = indicator.transform;
  62. TurnPageIndicator.name = "RKTurnPageIndicator";
  63. TurnPageIndicator.SetParent(transform);
  64. }
  65. initialize = true;
  66. }
  67. public void Release()
  68. {
  69. OnReleaseButtonMouseModule?.Invoke();
  70. Destroy(this.gameObject);
  71. }
  72. private void Update()
  73. {
  74. if (!initialize)
  75. return;
  76. bool hasKeyEvent = false;
  77. mouseDelta = Vector2.zero;
  78. scrollDelta = Vector2.zero;
  79. // 判断是否长按,以显示方向图标
  80. if (isEnterLongPressState)
  81. {
  82. #if UNITY_EDITOR
  83. if (Input.GetKeyDown(KeyCode.KeypadEnter))
  84. #else
  85. if (Input.GetKeyDown(KeyCode.JoystickButton0) || Input.GetKeyDown(KeyCode.Backspace))
  86. #endif
  87. {
  88. if (!isReHoverState)
  89. {
  90. OnReHoverState();
  91. isReHoverState = true;
  92. }
  93. TurnPageIndicator.gameObject.SetActive(false);
  94. isEnterLongPressState = false;
  95. OnExitLongPressState();
  96. }
  97. if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.DownArrow)
  98. || RKNativeInput.Instance.GetKeyDown(RKKeyEvent.KEY_LEFT) || RKNativeInput.Instance.GetKeyDown(RKKeyEvent.KEY_RIGHT) || RKNativeInput.Instance.GetKeyDown(RKKeyEvent.KEY_UP) || RKNativeInput.Instance.GetKeyDown(RKKeyEvent.KEY_DOWN)
  99. )
  100. {
  101. if (!isReHoverState)
  102. {
  103. OnReHoverState();
  104. isReHoverState = true;
  105. }
  106. }
  107. }
  108. else
  109. {
  110. if (IsLongDown())
  111. {
  112. TurnPageIndicator.position = CursorVisual.position;
  113. TurnPageIndicator.rotation = CursorVisual.rotation;
  114. TurnPageIndicator.localScale = CursorVisual.localScale;
  115. TurnPageIndicator.gameObject.SetActive(true);
  116. isReHoverState = false;
  117. isEnterLongPressState = true;
  118. OnEnterLongPressState();
  119. }
  120. }
  121. // 按键就进行自动切换
  122. if (InputModuleManager.Instance.GetButtonMouseActive() == false)
  123. {
  124. if (Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.RightArrow) || Input.GetKeyUp(KeyCode.UpArrow) || Input.GetKeyUp(KeyCode.DownArrow)
  125. || RKNativeInput.Instance.GetKeyUp(RKKeyEvent.KEY_LEFT) || RKNativeInput.Instance.GetKeyUp(RKKeyEvent.KEY_RIGHT) || RKNativeInput.Instance.GetKeyUp(RKKeyEvent.KEY_UP) || RKNativeInput.Instance.GetKeyUp(RKKeyEvent.KEY_DOWN)
  126. || Input.GetKeyUp(KeyCode.JoystickButton0))
  127. {
  128. ActiveModule();
  129. return;
  130. }
  131. else
  132. {
  133. if (isEnterLongPressState)
  134. {
  135. TurnPageIndicator.gameObject.SetActive(false);
  136. isEnterLongPressState = false;
  137. OnExitLongPressState();
  138. }
  139. return;
  140. }
  141. }
  142. else
  143. {
  144. if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.RightArrow)
  145. || RKNativeInput.Instance.GetKeyDown(RKKeyEvent.KEY_LEFT) || RKNativeInput.Instance.GetKeyDown(RKKeyEvent.KEY_RIGHT)
  146. )
  147. {
  148. xDownTime = GetCurrentTime();
  149. RKLog.Info("====ButtonMouseEventInput==== : GetKeyDown: LeftArrow or RightArrow");
  150. hasKeyEvent = true;
  151. }
  152. if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.DownArrow)
  153. || RKNativeInput.Instance.GetKeyDown(RKKeyEvent.KEY_UP) || RKNativeInput.Instance.GetKeyDown(RKKeyEvent.KEY_DOWN)
  154. )
  155. {
  156. yDownTime = GetCurrentTime();
  157. RKLog.Info("====ButtonMouseEventInput==== : GetKeyDown: UpArrow or DownArrow");
  158. hasKeyEvent = true;
  159. }
  160. }
  161. if (Input.GetKey(KeyCode.UpArrow) || RKNativeInput.Instance.GetKey(RKKeyEvent.KEY_UP))
  162. {
  163. if (isEnterLongPressState)
  164. {
  165. scrollDelta[1] = -DefaultScrollSpeed;
  166. }
  167. else
  168. {
  169. long value = GetCurrentTime() - yDownTime;
  170. mouseDelta.y = -DefaultSpeedScale * Mathf.Clamp(value, 0, value);
  171. #if UNITY_EDITOR
  172. mouseDelta.y = -mouseDelta.y;
  173. #endif
  174. }
  175. hasKeyEvent = true;
  176. }
  177. if (Input.GetKey(KeyCode.DownArrow) || RKNativeInput.Instance.GetKey(RKKeyEvent.KEY_DOWN))
  178. {
  179. if (isEnterLongPressState)
  180. {
  181. scrollDelta[1] = DefaultScrollSpeed;
  182. }
  183. else
  184. {
  185. long value = GetCurrentTime() - yDownTime;
  186. mouseDelta.y = DefaultSpeedScale * Mathf.Clamp(value, 0, value);
  187. #if UNITY_EDITOR
  188. mouseDelta.y = -mouseDelta.y;
  189. #endif
  190. }
  191. hasKeyEvent = true;
  192. }
  193. if (Input.GetKey(KeyCode.LeftArrow) || RKNativeInput.Instance.GetKey(RKKeyEvent.KEY_LEFT))
  194. {
  195. if (isEnterLongPressState)
  196. {
  197. scrollDelta[0] = -DefaultScrollSpeed;
  198. }
  199. else
  200. {
  201. long value = GetCurrentTime() - xDownTime;
  202. mouseDelta.x = -DefaultSpeedScale * Mathf.Clamp(value, 0, value);
  203. }
  204. hasKeyEvent = true;
  205. }
  206. if (Input.GetKey(KeyCode.RightArrow) || RKNativeInput.Instance.GetKey(RKKeyEvent.KEY_RIGHT))
  207. {
  208. if (isEnterLongPressState)
  209. {
  210. scrollDelta[0] = DefaultScrollSpeed;
  211. }
  212. else
  213. {
  214. long value = GetCurrentTime() - xDownTime;
  215. mouseDelta.x = DefaultSpeedScale * Mathf.Clamp(value, 0, value);
  216. }
  217. hasKeyEvent = true;
  218. }
  219. // 进入锁定状态,不移动指针
  220. if (!isEnterLongPressState && hasKeyEvent)
  221. {
  222. OnMouseMove?.Invoke(mouseDelta, isEnterLongPressState);
  223. }
  224. }
  225. public Vector2 GetMouseScrollDelta()
  226. {
  227. return scrollDelta;
  228. }
  229. #region IsLongDown
  230. float longDownTime = 1.1f;
  231. float downTime = 0;
  232. bool triggerLongDown;
  233. private bool IsLongDown()
  234. {
  235. #if UNITY_EDITOR
  236. KeyCode pressKeyCode = KeyCode.KeypadEnter;
  237. #else
  238. KeyCode pressKeyCode = KeyCode.JoystickButton0;
  239. #endif
  240. if (Input.GetKeyDown(pressKeyCode))
  241. {
  242. triggerLongDown = false;
  243. downTime = 0;
  244. }
  245. if (Input.GetKey(pressKeyCode) && !triggerLongDown)
  246. {
  247. downTime += Time.deltaTime;
  248. if (downTime >= longDownTime)
  249. {
  250. triggerLongDown = true;
  251. return true;
  252. }
  253. }
  254. return false;
  255. }
  256. #endregion
  257. public void ActiveModule()
  258. {
  259. RKLog.KeyInfo("====ButtonMouseEventInput==== : ActiveModule");
  260. OnActiveButtonMouseModule?.Invoke();
  261. RKVirtualController.Instance.Change(ControllerType.Mouse);
  262. EventSystem.current.pixelDragThreshold = 5;
  263. }
  264. protected override void OnDestroy()
  265. {
  266. base.OnDestroy();
  267. OnMouseMove = null;
  268. initialize = false;
  269. }
  270. /// <summary>
  271. /// 是否进入长按锁定状态
  272. /// </summary>
  273. /// <returns></returns>
  274. public bool IsInLockState()
  275. {
  276. return InputModuleManager.Instance.GetButtonMouseActive() && isEnterLongPressState;
  277. }
  278. }
  279. }