SB_EventSystem.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using JC.Unity;
  6. /*
  7. * 姿态角鼠标
  8. * SmartBow_事件系统
  9. * 可进入硬件控制虚拟鼠标模式
  10. */
  11. public class SB_EventSystem : MonoBehaviour
  12. {
  13. public static SB_EventSystem ins;
  14. void Awake()
  15. {
  16. if (ins) {
  17. Destroy(this.gameObject);
  18. } else {
  19. ins = this;
  20. DontDestroyOnLoad(this.gameObject);
  21. AwakenSimulateMouse();
  22. }
  23. }
  24. void Start() {
  25. InitListenerForMouseHoverHightColor();
  26. if (CommonConfig.StandaloneMode) gameObject.AddComponent<KeyBoardSelector>();
  27. }
  28. void Update() {
  29. //mouseTest.Update();
  30. UpdateMoveSimulateMouse();
  31. }
  32. [SerializeField] public MouseConfirm mouseConfirm;
  33. [SerializeField] public SimulateMouse simulateMouse;
  34. #region 客户要求鼠标点到按钮时,按钮高亮
  35. // Color pointerHoverColor = new Color(233f/255, 233f/255, 233f/255, 128f/255);
  36. void InitListenerForMouseHoverHightColor() {
  37. simulateMouse.OnPointerEnter += (Selectable target) => {
  38. mouseConfirm.SetSelectable(target);
  39. // Button btn = target.GetComponent<Button>();
  40. // if (!btn) return;
  41. // if (btn.transition != Selectable.Transition.ColorTint) return;
  42. // if (!btn.interactable) return;
  43. // ColorBlock colorBlock = btn.colors;
  44. // colorBlock.highlightedColor = pointerHoverColor;
  45. // btn.colors = colorBlock;
  46. };
  47. //mouseConfirm .OnResumeTarget += (Selectable target) => {
  48. // simulateMouse.onSetNullToSelectable(target);
  49. //};
  50. }
  51. #endregion
  52. #region 鼠标激活/关闭
  53. [System.NonSerialized] public bool simulateMouseIsAwaked;
  54. public void AwakenSimulateMouse() {
  55. simulateMouseIsAwaked = !simulateMouse.gameObject.activeSelf;
  56. simulateMouse.gameObject.SetActive(simulateMouseIsAwaked);
  57. try {
  58. GlobalEventCenter.ins.onSimulateMouseAwakeChanged?.Invoke(simulateMouseIsAwaked);
  59. } catch (System.Exception e) { Debug.LogError(e.Message); }
  60. }
  61. #endregion 鼠标激活/关闭
  62. #region 模拟鼠标移动
  63. Quaternion nowAxisQuat = Quaternion.identity;
  64. Quaternion newAxisQuat;
  65. Vector2 mousePointerPosition;
  66. public void MoveSimulateMouse(Quaternion axisQuat) {
  67. if (IsClickMouseCooling()) { //点击就是射出,射出会产生抖动,防止接收抖动后的数据
  68. return;
  69. }
  70. newAxisQuat = axisQuat;
  71. }
  72. void UpdateMoveSimulateMouse() {
  73. if (!simulateMouseIsAwaked) return;
  74. if (InfraredDemo.running) return;
  75. nowAxisQuat = Quaternion.Lerp(nowAxisQuat, newAxisQuat, 0.033f * 8);
  76. Vector3 resEulerAngles = FormatQuat(nowAxisQuat).eulerAngles;
  77. resEulerAngles.x = Mathf.Clamp(resEulerAngles.x > 180 ? resEulerAngles.x - 360 : resEulerAngles.x, -12.5f, 12.5f);
  78. resEulerAngles.y = Mathf.Clamp(resEulerAngles.y > 180 ? resEulerAngles.y - 360 : resEulerAngles.y, -22.2f, 22.2f);
  79. mousePointerPosition.x = (resEulerAngles.y + 22.2f) / 44.4f * Screen.width;
  80. mousePointerPosition.y = (12.5f - resEulerAngles.x) / 25f * Screen.height;
  81. simulateMouse.SetMousePointerPosition(mousePointerPosition);
  82. }
  83. Quaternion FormatQuat(Quaternion quat) {
  84. Vector3 normalVector = quat * Vector3.forward;
  85. return Quaternion.LookRotation(normalVector);
  86. }
  87. #endregion 模拟鼠标移动
  88. #region 模拟鼠标移动-红外
  89. public void MoveSimulateMouseByInfrared(Vector2 point)
  90. {
  91. if (!simulateMouseIsAwaked) return;
  92. if (IsClickMouseCooling())
  93. { //点击就是射出,射出会产生抖动,防止接收抖动后的数据
  94. return;
  95. }
  96. simulateMouse.SetMousePointerPosition(point);
  97. }
  98. #endregion
  99. //点击鼠标
  100. float _lastClickMouseTime = -100;
  101. void RecordClickMouseTime() {
  102. _lastClickMouseTime = Time.realtimeSinceStartup;
  103. }
  104. bool IsClickMouseCooling() { //防止高频连续多次点击
  105. return Time.realtimeSinceStartup - _lastClickMouseTime < 1;
  106. }
  107. public void ClickMouse() {
  108. bool isCooling = IsClickMouseCooling();
  109. RecordClickMouseTime();
  110. if (isCooling) return;
  111. // simulateMouse.ClickMousePointer();
  112. mouseConfirm.OnClikc_Confirm();
  113. }
  114. /** 鼠标测试类 */
  115. MouseTest mouseTest = new MouseTest();
  116. class MouseTest {
  117. Quaternion quat = Quaternion.identity;
  118. float moveSensitivity = 30;
  119. public void Update() {
  120. if (Input.GetKey(KeyCode.A)) {
  121. quat = Quaternion.AngleAxis(-moveSensitivity * Time.deltaTime, Vector3.up) * quat;
  122. }
  123. else if (Input.GetKey(KeyCode.D)) {
  124. quat = Quaternion.AngleAxis(moveSensitivity * Time.deltaTime, Vector3.up) * quat;
  125. }
  126. else if (Input.GetKey(KeyCode.W)) {
  127. quat = Quaternion.AngleAxis(-moveSensitivity * Time.deltaTime, Vector3.right) * quat;
  128. }
  129. else if (Input.GetKey(KeyCode.S)) {
  130. quat = Quaternion.AngleAxis(moveSensitivity * Time.deltaTime, Vector3.right) * quat;
  131. }
  132. SB_EventSystem.ins.MoveSimulateMouse(quat);
  133. if (Input.GetKeyDown(KeyCode.E)) {
  134. SB_EventSystem.ins.AwakenSimulateMouse();
  135. }
  136. if (Input.GetKeyDown(KeyCode.R)) {
  137. SB_EventSystem.ins.ClickMouse();
  138. }
  139. }
  140. }
  141. }
  142. /**传统鼠标 */
  143. // public class SB_EventSystem : MonoBehaviour
  144. // {
  145. // public static SB_EventSystem ins;
  146. // MouseTest mouseTest;
  147. // void Awake()
  148. // {
  149. // if (ins) {
  150. // Destroy(this.gameObject);
  151. // } else {
  152. // ins = this;
  153. // DontDestroyOnLoad(this.gameObject);
  154. // AwakenSimulateMouse();
  155. // }
  156. // }
  157. // void Start() {
  158. // mouseTest = new MouseTest(this);
  159. // // InitListenerForMouseClickHightColor();
  160. // InitListenerForMouseHoverHightColor();
  161. // }
  162. // void Update() {
  163. // UpdateMoveSimulateMouse();
  164. // mouseTest.Update();
  165. // }
  166. // [SerializeField] SimulateMouse simulateMouse;
  167. // #region 客户要求鼠标点到按钮时,按钮高亮
  168. // // Graphic lockGraphic = null;
  169. // // Color pointerClickColor = Color.yellow;
  170. // // void InitListenerForMouseClickHightColor() {
  171. // // simulateMouse.OnPointerClick += (Selectable target) => {
  172. // // if (lockGraphic) return;
  173. // // Button btn = target.GetComponent<Button>();
  174. // // if (btn.transition != Selectable.Transition.ColorTint) return;
  175. // // if (btn.targetGraphic) {
  176. // // Graphic graphic = btn.targetGraphic;
  177. // // lockGraphic = graphic;
  178. // // Color oldColor = graphic.color;
  179. // // graphic.color = pointerClickColor;
  180. // // DoTweenUtil.CallDelay(0.3f, () => {
  181. // // lockGraphic = null;
  182. // // graphic.color = oldColor;
  183. // // });
  184. // // }
  185. // // };
  186. // // }
  187. // Color pointerHoverColor = new Color(233f/255, 233f/255, 233f/255, 128f/255);
  188. // void InitListenerForMouseHoverHightColor() {
  189. // simulateMouse.OnPointerEnter += (Selectable target) => {
  190. // Button btn = target.GetComponent<Button>();
  191. // if (!btn) return;
  192. // if (btn.transition != Selectable.Transition.ColorTint) return;
  193. // if (!btn.interactable) return;
  194. // ColorBlock colorBlock = btn.colors;
  195. // colorBlock.highlightedColor = pointerHoverColor;
  196. // btn.colors = colorBlock;
  197. // };
  198. // }
  199. // #endregion
  200. // #region 鼠标激活/关闭
  201. // [System.NonSerialized] public bool simulateMouseIsAwaked;
  202. // public void AwakenSimulateMouse() {
  203. // simulateMouseIsAwaked = !simulateMouse.gameObject.activeSelf;
  204. // simulateMouse.gameObject.SetActive(simulateMouseIsAwaked);
  205. // hasAxisQuat = false;
  206. // try {
  207. // GlobalEventCenter.ins.onSimulateMouseAwakeChanged?.Invoke(simulateMouseIsAwaked);
  208. // } catch (System.Exception e) { Debug.LogError(e.Message); }
  209. // }
  210. // #endregion 鼠标激活/关闭
  211. // #region 模拟鼠标移动
  212. // Vector3 targetNormalVector;
  213. // Quaternion targetAxisQuat;
  214. // Quaternion nowAxisQuat;
  215. // bool hasAxisQuat;
  216. // public void MoveSimulateMouse(Quaternion axisQuat) {
  217. // if (IsClickMouseCooling()) { //点击就是射出,射出会产生抖动,防止接收抖动后的数据
  218. // hasAxisQuat = false;
  219. // return;
  220. // }
  221. // //格式化
  222. // Vector3 normalVector = axisQuat * Vector3.forward; //得出对应的法向量
  223. // axisQuat = Quaternion.LookRotation(normalVector); //变回四元组,主要作用是规范,去除z轴影响
  224. // if (hasAxisQuat) {
  225. // //法向量的z从+到-或从-到+,都会导致y轴转180度,也就是x轴旋转从<90跨越>90时触发的问题,这种情况要避免
  226. // if (
  227. // normalVector.z <= 0 && targetNormalVector.z >= 0 ||
  228. // normalVector.z >= 0 && targetNormalVector.z <= 0
  229. // ) {
  230. // hasAxisQuat = false; //重置
  231. // return;
  232. // }
  233. // //ok
  234. // targetAxisQuat = axisQuat;
  235. // targetNormalVector = normalVector;
  236. // } else {
  237. // nowAxisQuat = targetAxisQuat = axisQuat;
  238. // targetNormalVector = normalVector;
  239. // hasAxisQuat = true;
  240. // }
  241. // }
  242. // Vector2 deltaVectorForMouse;
  243. // void UpdateMoveSimulateMouse() {
  244. // if (!simulateMouseIsAwaked) return;
  245. // if (hasAxisQuat) {
  246. // Vector3 lastAngle = nowAxisQuat.eulerAngles;
  247. // nowAxisQuat = Quaternion.Lerp(nowAxisQuat, targetAxisQuat, 0.033f * 8);
  248. // Vector3 curAngle = nowAxisQuat.eulerAngles;
  249. // float dx = FormatDeltaAngleY(curAngle.y - lastAngle.y) / 30f * simulateMouse.GetScaleScreenWidth();
  250. // float dy = -FormatDeltaAngleX(curAngle.x - lastAngle.x) / 21f * simulateMouse.GetScaleScreenHeight();
  251. // deltaVectorForMouse.x = dx;
  252. // deltaVectorForMouse.y = dy;
  253. // simulateMouse.MoveMousePointer(deltaVectorForMouse);
  254. // }
  255. // }
  256. // float FormatDeltaAngleX(float value)
  257. // {
  258. // return FormatDeltaAngleY(value);
  259. // }
  260. // float FormatDeltaAngleY(float value)
  261. // {
  262. // if (Mathf.Abs(value) > 180) {
  263. // if (value < 0) {
  264. // return 360f + value;
  265. // }
  266. // if (value > 0) {
  267. // return value - 360f;
  268. // }
  269. // }
  270. // return value;
  271. // }
  272. // #endregion 模拟鼠标移动
  273. // //点击鼠标
  274. // float _lastClickMouseTime = -100;
  275. // void RecordClickMouseTime() {
  276. // _lastClickMouseTime = Time.realtimeSinceStartup;
  277. // }
  278. // bool IsClickMouseCooling() { //防止高频连续多次点击
  279. // return Time.realtimeSinceStartup - _lastClickMouseTime < 1;
  280. // }
  281. // public void ClickMouse() {
  282. // AimHandler.ins._9Axis.axisCSBridge.o09AxisCS.OnShot(100, 100, 100000);
  283. // bool isCooling = IsClickMouseCooling();
  284. // RecordClickMouseTime();
  285. // if (isCooling) return;
  286. // simulateMouse.ClickMousePointer();
  287. // }
  288. // //鼠标居中
  289. // public void MakeMouseToScreenCenter() {
  290. // hasAxisQuat = false;
  291. // simulateMouse.MakeMouseToScreenCenter();
  292. // }
  293. // /** 鼠标测试类 */
  294. // class MouseTest {
  295. // SB_EventSystem es;
  296. // float mouseTestSensitivity = 3f;
  297. // bool mouseTest = false; //开启测试-开关
  298. // public MouseTest(SB_EventSystem es) {
  299. // this.es = es;
  300. // }
  301. // public void Update() {
  302. // if (mouseTest) {
  303. // if (Input.GetKey(KeyCode.A)) {
  304. // es.deltaVectorForMouse.x = -mouseTestSensitivity;
  305. // }
  306. // else if (Input.GetKey(KeyCode.D)) {
  307. // es.deltaVectorForMouse.x = +mouseTestSensitivity;
  308. // } else {
  309. // es.deltaVectorForMouse.x = 0;
  310. // }
  311. // if (Input.GetKey(KeyCode.W)) {
  312. // es.deltaVectorForMouse.y = +mouseTestSensitivity;
  313. // }
  314. // else if (Input.GetKey(KeyCode.S)) {
  315. // es.deltaVectorForMouse.y = -mouseTestSensitivity;
  316. // } else {
  317. // es.deltaVectorForMouse.y = 0;
  318. // }
  319. // es.simulateMouse.MoveMousePointer(es.deltaVectorForMouse);
  320. // if (Input.GetKeyDown(KeyCode.E)) {
  321. // es.AwakenSimulateMouse();
  322. // }
  323. // if (Input.GetKeyDown(KeyCode.R)) {
  324. // es.simulateMouse.ClickMousePointer();
  325. // }
  326. // }
  327. // }
  328. // }
  329. // }