SB_EventSystem.cs 13 KB

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