SB_EventSystem.cs 13 KB

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