SB_EventSystem.cs 13 KB

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