SB_EventSystem.cs 14 KB

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