SB_EventSystem.cs 12 KB

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