| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using JC.Unity;
- /*
- * 姿态角鼠标
- * SmartBow_事件系统
- * 可进入硬件控制虚拟鼠标模式
- */
- public class SB_EventSystem : MonoBehaviour
- {
- public static SB_EventSystem ins;
- void Awake()
- {
- if (ins) {
- Destroy(this.gameObject);
- } else {
- ins = this;
- DontDestroyOnLoad(this.gameObject);
- AwakenSimulateMouse();
- }
- }
- void Start() {
- InitListenerForMouseHoverHightColor();
- if (CommonConfig.StandaloneMode) gameObject.AddComponent<KeyBoardSelector>();
- }
- void Update() {
- //mouseTest.Update();
- if (!(ScreenLocate.Main != null && ScreenLocate.Main.DebugOnZIMDemo))
- {
- UpdateMoveSimulateMouse();
- }
- }
- [SerializeField] public MouseConfirm mouseConfirm;
- [SerializeField] public SimulateMouse simulateMouse;
- #region 客户要求鼠标点到按钮时,按钮高亮
- // Color pointerHoverColor = new Color(233f/255, 233f/255, 233f/255, 128f/255);
- void InitListenerForMouseHoverHightColor() {
- simulateMouse.OnPointerEnter += (Selectable target) => {
- mouseConfirm.SetSelectable(target);
- // Button btn = target.GetComponent<Button>();
- // if (!btn) return;
- // if (btn.transition != Selectable.Transition.ColorTint) return;
- // if (!btn.interactable) return;
- // ColorBlock colorBlock = btn.colors;
- // colorBlock.highlightedColor = pointerHoverColor;
- // btn.colors = colorBlock;
- };
- //mouseConfirm .OnResumeTarget += (Selectable target) => {
- // simulateMouse.onSetNullToSelectable(target);
- //};
- }
- #endregion
- #region 鼠标激活/关闭
- [System.NonSerialized] public bool simulateMouseIsAwaked;
- public void AwakenSimulateMouse() {
- simulateMouseIsAwaked = !simulateMouse.gameObject.activeSelf;
- simulateMouse.gameObject.SetActive(simulateMouseIsAwaked);
- try {
- GlobalEventCenter.ins.onSimulateMouseAwakeChanged?.Invoke(simulateMouseIsAwaked);
- } catch (System.Exception e) { Debug.LogError(e.Message); }
- }
- #endregion 鼠标激活/关闭
- #region 模拟鼠标移动
- Quaternion nowAxisQuat = Quaternion.identity;
- Quaternion newAxisQuat;
- Vector2 mousePointerPosition;
- public void MoveSimulateMouse(Quaternion axisQuat) {
- if (IsClickMouseCooling()) { //点击就是射出,射出会产生抖动,防止接收抖动后的数据
- return;
- }
- newAxisQuat = axisQuat;
- }
- void UpdateMoveSimulateMouse() {
- if (!simulateMouseIsAwaked) return;
- //if (InfraredDemo.running) return;
- if (!AimHandler.ins.bRuning9Axis())
- {
- return;
- }
- nowAxisQuat = Quaternion.Lerp(nowAxisQuat, newAxisQuat, 0.033f * 8);
- Vector3 resEulerAngles = FormatQuat(nowAxisQuat).eulerAngles;
- resEulerAngles.x = Mathf.Clamp(resEulerAngles.x > 180 ? resEulerAngles.x - 360 : resEulerAngles.x, -12.5f, 12.5f);
- resEulerAngles.y = Mathf.Clamp(resEulerAngles.y > 180 ? resEulerAngles.y - 360 : resEulerAngles.y, -22.2f, 22.2f);
- mousePointerPosition.x = (resEulerAngles.y + 22.2f) / 44.4f * Screen.width;
- mousePointerPosition.y = (12.5f - resEulerAngles.x) / 25f * Screen.height;
- simulateMouse.SetMousePointerPosition(mousePointerPosition);
- }
- Quaternion FormatQuat(Quaternion quat) {
- Vector3 normalVector = quat * Vector3.forward;
- return Quaternion.LookRotation(normalVector);
- }
- #endregion 模拟鼠标移动
- #region 模拟鼠标移动-红外
- public void MoveSimulateMouseByInfrared(Vector2 point)
- {
- if (!simulateMouseIsAwaked) return;
- if (IsClickMouseCooling())
- { //点击就是射出,射出会产生抖动,防止接收抖动后的数据
- return;
- }
- simulateMouse.SetMousePointerPosition(point);
- }
- #endregion
- //点击鼠标
- float _lastClickMouseTime = -100;
- void RecordClickMouseTime() {
- _lastClickMouseTime = Time.realtimeSinceStartup;
- }
- bool IsClickMouseCooling() { //防止高频连续多次点击
- return Time.realtimeSinceStartup - _lastClickMouseTime < 1;
- }
- public void ClickMouse() {
- bool isCooling = IsClickMouseCooling();
- RecordClickMouseTime();
- if (isCooling) return;
- // simulateMouse.ClickMousePointer();
- mouseConfirm.OnClikc_Confirm();
- Debug.Log("射击触发按钮:" + Time.realtimeSinceStartup);
- }
- /** 鼠标测试类 */
- MouseTest mouseTest = new MouseTest();
- class MouseTest {
- Quaternion quat = Quaternion.identity;
- float moveSensitivity = 30;
- public void Update() {
- if (Input.GetKey(KeyCode.A)) {
- quat = Quaternion.AngleAxis(-moveSensitivity * Time.deltaTime, Vector3.up) * quat;
- }
- else if (Input.GetKey(KeyCode.D)) {
- quat = Quaternion.AngleAxis(moveSensitivity * Time.deltaTime, Vector3.up) * quat;
- }
- else if (Input.GetKey(KeyCode.W)) {
- quat = Quaternion.AngleAxis(-moveSensitivity * Time.deltaTime, Vector3.right) * quat;
- }
- else if (Input.GetKey(KeyCode.S)) {
- quat = Quaternion.AngleAxis(moveSensitivity * Time.deltaTime, Vector3.right) * quat;
- }
- SB_EventSystem.ins.MoveSimulateMouse(quat);
- if (Input.GetKeyDown(KeyCode.E)) {
- SB_EventSystem.ins.AwakenSimulateMouse();
- }
- if (Input.GetKeyDown(KeyCode.R)) {
- SB_EventSystem.ins.ClickMouse();
- }
- }
- }
- }
- /**传统鼠标 */
- // public class SB_EventSystem : MonoBehaviour
- // {
- // public static SB_EventSystem ins;
- // MouseTest mouseTest;
- // void Awake()
- // {
- // if (ins) {
- // Destroy(this.gameObject);
- // } else {
- // ins = this;
- // DontDestroyOnLoad(this.gameObject);
- // AwakenSimulateMouse();
- // }
- // }
- // void Start() {
- // mouseTest = new MouseTest(this);
- // // InitListenerForMouseClickHightColor();
- // InitListenerForMouseHoverHightColor();
- // }
- // void Update() {
- // UpdateMoveSimulateMouse();
- // mouseTest.Update();
- // }
-
- // [SerializeField] SimulateMouse simulateMouse;
- // #region 客户要求鼠标点到按钮时,按钮高亮
- // // Graphic lockGraphic = null;
- // // Color pointerClickColor = Color.yellow;
- // // void InitListenerForMouseClickHightColor() {
- // // simulateMouse.OnPointerClick += (Selectable target) => {
- // // if (lockGraphic) return;
- // // Button btn = target.GetComponent<Button>();
- // // if (btn.transition != Selectable.Transition.ColorTint) return;
- // // if (btn.targetGraphic) {
- // // Graphic graphic = btn.targetGraphic;
- // // lockGraphic = graphic;
- // // Color oldColor = graphic.color;
- // // graphic.color = pointerClickColor;
- // // DoTweenUtil.CallDelay(0.3f, () => {
- // // lockGraphic = null;
- // // graphic.color = oldColor;
- // // });
- // // }
- // // };
- // // }
- // Color pointerHoverColor = new Color(233f/255, 233f/255, 233f/255, 128f/255);
- // void InitListenerForMouseHoverHightColor() {
- // simulateMouse.OnPointerEnter += (Selectable target) => {
- // Button btn = target.GetComponent<Button>();
- // if (!btn) return;
- // if (btn.transition != Selectable.Transition.ColorTint) return;
- // if (!btn.interactable) return;
- // ColorBlock colorBlock = btn.colors;
- // colorBlock.highlightedColor = pointerHoverColor;
- // btn.colors = colorBlock;
- // };
- // }
- // #endregion
- // #region 鼠标激活/关闭
- // [System.NonSerialized] public bool simulateMouseIsAwaked;
- // public void AwakenSimulateMouse() {
- // simulateMouseIsAwaked = !simulateMouse.gameObject.activeSelf;
- // simulateMouse.gameObject.SetActive(simulateMouseIsAwaked);
- // hasAxisQuat = false;
- // try {
- // GlobalEventCenter.ins.onSimulateMouseAwakeChanged?.Invoke(simulateMouseIsAwaked);
- // } catch (System.Exception e) { Debug.LogError(e.Message); }
- // }
- // #endregion 鼠标激活/关闭
- // #region 模拟鼠标移动
- // Vector3 targetNormalVector;
- // Quaternion targetAxisQuat;
- // Quaternion nowAxisQuat;
- // bool hasAxisQuat;
- // public void MoveSimulateMouse(Quaternion axisQuat) {
- // if (IsClickMouseCooling()) { //点击就是射出,射出会产生抖动,防止接收抖动后的数据
- // hasAxisQuat = false;
- // return;
- // }
- // //格式化
- // Vector3 normalVector = axisQuat * Vector3.forward; //得出对应的法向量
- // axisQuat = Quaternion.LookRotation(normalVector); //变回四元组,主要作用是规范,去除z轴影响
- // if (hasAxisQuat) {
- // //法向量的z从+到-或从-到+,都会导致y轴转180度,也就是x轴旋转从<90跨越>90时触发的问题,这种情况要避免
- // if (
- // normalVector.z <= 0 && targetNormalVector.z >= 0 ||
- // normalVector.z >= 0 && targetNormalVector.z <= 0
- // ) {
- // hasAxisQuat = false; //重置
- // return;
- // }
- // //ok
- // targetAxisQuat = axisQuat;
- // targetNormalVector = normalVector;
- // } else {
- // nowAxisQuat = targetAxisQuat = axisQuat;
- // targetNormalVector = normalVector;
- // hasAxisQuat = true;
- // }
- // }
- // Vector2 deltaVectorForMouse;
- // void UpdateMoveSimulateMouse() {
- // if (!simulateMouseIsAwaked) return;
- // if (hasAxisQuat) {
- // Vector3 lastAngle = nowAxisQuat.eulerAngles;
- // nowAxisQuat = Quaternion.Lerp(nowAxisQuat, targetAxisQuat, 0.033f * 8);
- // Vector3 curAngle = nowAxisQuat.eulerAngles;
- // float dx = FormatDeltaAngleY(curAngle.y - lastAngle.y) / 30f * simulateMouse.GetScaleScreenWidth();
- // float dy = -FormatDeltaAngleX(curAngle.x - lastAngle.x) / 21f * simulateMouse.GetScaleScreenHeight();
- // deltaVectorForMouse.x = dx;
- // deltaVectorForMouse.y = dy;
- // simulateMouse.MoveMousePointer(deltaVectorForMouse);
- // }
- // }
- // float FormatDeltaAngleX(float value)
- // {
- // return FormatDeltaAngleY(value);
- // }
- // float FormatDeltaAngleY(float value)
- // {
- // if (Mathf.Abs(value) > 180) {
- // if (value < 0) {
- // return 360f + value;
- // }
- // if (value > 0) {
- // return value - 360f;
- // }
- // }
- // return value;
- // }
- // #endregion 模拟鼠标移动
- // //点击鼠标
- // float _lastClickMouseTime = -100;
- // void RecordClickMouseTime() {
- // _lastClickMouseTime = Time.realtimeSinceStartup;
- // }
- // bool IsClickMouseCooling() { //防止高频连续多次点击
- // return Time.realtimeSinceStartup - _lastClickMouseTime < 1;
- // }
- // public void ClickMouse() {
- // AimHandler.ins._9Axis.axisCSBridge.o09AxisCS.OnShot(100, 100, 100000);
- // bool isCooling = IsClickMouseCooling();
- // RecordClickMouseTime();
- // if (isCooling) return;
- // simulateMouse.ClickMousePointer();
- // }
- // //鼠标居中
- // public void MakeMouseToScreenCenter() {
- // hasAxisQuat = false;
- // simulateMouse.MakeMouseToScreenCenter();
- // }
- // /** 鼠标测试类 */
- // class MouseTest {
- // SB_EventSystem es;
- // float mouseTestSensitivity = 3f;
- // bool mouseTest = false; //开启测试-开关
- // public MouseTest(SB_EventSystem es) {
- // this.es = es;
- // }
- // public void Update() {
- // if (mouseTest) {
- // if (Input.GetKey(KeyCode.A)) {
- // es.deltaVectorForMouse.x = -mouseTestSensitivity;
- // }
- // else if (Input.GetKey(KeyCode.D)) {
- // es.deltaVectorForMouse.x = +mouseTestSensitivity;
- // } else {
- // es.deltaVectorForMouse.x = 0;
- // }
- // if (Input.GetKey(KeyCode.W)) {
- // es.deltaVectorForMouse.y = +mouseTestSensitivity;
- // }
- // else if (Input.GetKey(KeyCode.S)) {
- // es.deltaVectorForMouse.y = -mouseTestSensitivity;
- // } else {
- // es.deltaVectorForMouse.y = 0;
- // }
- // es.simulateMouse.MoveMousePointer(es.deltaVectorForMouse);
- // if (Input.GetKeyDown(KeyCode.E)) {
- // es.AwakenSimulateMouse();
- // }
- // if (Input.GetKeyDown(KeyCode.R)) {
- // es.simulateMouse.ClickMousePointer();
- // }
- // }
- // }
- // }
- // }
|