SB_EventSystem.cs 14 KB

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