SB_EventSystem.cs 14 KB

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