SB_EventSystem.cs 14 KB

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