SB_EventSystem.cs 14 KB

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