SB_EventSystem.cs 13 KB

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