Pārlūkot izejas kodu

姿态角鼠标

lvjincheng 3 gadi atpakaļ
vecāks
revīzija
55aa226377

+ 0 - 22
Assets/BowArrow/Fonts/MySDF/TTC/msyhbd.ttc.meta

@@ -1,22 +0,0 @@
-fileFormatVersion: 2
-guid: 87db0319e99df824d846d9d64cc6b3dc
-TrueTypeFontImporter:
-  externalObjects: {}
-  serializedVersion: 4
-  fontSize: 16
-  forceTextureCase: -2
-  characterSpacing: 0
-  characterPadding: 1
-  includeFontData: 1
-  fontNames:
-  - Microsoft YaHei
-  fallbackFontReferences:
-  - {fileID: 12800000, guid: 60dc4c0d16e7d9b409b09053503d801a, type: 3}
-  customCharacters: 
-  fontRenderingMode: 0
-  ascentCalculationMode: 1
-  useLegacyBoundsCalculation: 0
-  shouldRoundAdvanceValue: 1
-  userData: 
-  assetBundleName: 
-  assetBundleVariant: 

+ 0 - 23
Assets/BowArrow/Fonts/MySDF/TTC/msyhl.ttc.meta

@@ -1,23 +0,0 @@
-fileFormatVersion: 2
-guid: 1fe2c66e9931e654684916f22c725988
-TrueTypeFontImporter:
-  externalObjects: {}
-  serializedVersion: 4
-  fontSize: 16
-  forceTextureCase: -2
-  characterSpacing: 0
-  characterPadding: 1
-  includeFontData: 1
-  fontNames:
-  - Microsoft YaHei
-  fallbackFontReferences:
-  - {fileID: 12800000, guid: 60dc4c0d16e7d9b409b09053503d801a, type: 3}
-  - {fileID: 12800000, guid: 87db0319e99df824d846d9d64cc6b3dc, type: 3}
-  customCharacters: 
-  fontRenderingMode: 0
-  ascentCalculationMode: 1
-  useLegacyBoundsCalculation: 0
-  shouldRoundAdvanceValue: 1
-  userData: 
-  assetBundleName: 
-  assetBundleVariant: 

+ 3 - 3
Assets/BowArrow/Scripts/Bluetooth/AimHandler.cs

@@ -251,10 +251,10 @@ public class AimHandler : MonoBehaviour
             if (bytes.Length == 2) {
                 if (bytes[0] == 0x66 && bytes[1] == 0x31) {
                     if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) {
-                        //鼠标居中
-                        SB_EventSystem.ins.MakeMouseToScreenCenter(); 
                         //视角回正
                         DoIdentity();
+                        //鼠标居中
+                        SB_EventSystem.ins.MakeMouseToScreenCenter(newRotation);
                     } else {
                         AutoResetView.DoIdentity();
                     }
@@ -491,7 +491,7 @@ public class AimHandler : MonoBehaviour
         }
     }
 
-    Quaternion newRotation = Quaternion.identity;
+    [NonSerialized] public Quaternion newRotation = Quaternion.identity;
 
     public void DoIdentity()
     {

+ 227 - 116
Assets/BowArrow/Scripts/Expand/SB_EventSystem.cs

@@ -5,13 +5,13 @@ using UnityEngine.UI;
 using JC.Unity;
 
 /*
+* 姿态角鼠标
 * SmartBow_事件系统
 * 可进入硬件控制虚拟鼠标模式 
 */
 public class SB_EventSystem : MonoBehaviour
 {   
     public static SB_EventSystem ins;
-    MouseTest mouseTest;
 
     void Awake()
     {
@@ -25,38 +25,16 @@ public class SB_EventSystem : MonoBehaviour
     }
 
     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) => {
@@ -75,8 +53,10 @@ public class SB_EventSystem : MonoBehaviour
         [System.NonSerialized] public bool simulateMouseIsAwaked;
         public void AwakenSimulateMouse() {
             simulateMouseIsAwaked = !simulateMouse.gameObject.activeSelf;
+            if (simulateMouseIsAwaked && AimHandler.ins) {
+                MakeMouseToScreenCenter(AimHandler.ins.newRotation);
+            }
             simulateMouse.gameObject.SetActive(simulateMouseIsAwaked);
-            hasAxisQuat = false;
              try { 
                 GlobalEventCenter.ins.onSimulateMouseAwakeChanged?.Invoke(simulateMouseIsAwaked); 
             } catch (System.Exception e) { Debug.LogError(e.Message); }
@@ -84,68 +64,44 @@ public class SB_EventSystem : MonoBehaviour
     #endregion 鼠标激活/关闭
 
     #region 模拟鼠标移动
-        Vector3 targetNormalVector;
-        Quaternion targetAxisQuat;
+        Vector3 identityEulerAngles;
         Quaternion nowAxisQuat;
-        bool hasAxisQuat;
+        Quaternion newAxisQuat;
+        Vector2 mousePointerPosition;
+        bool hasDoIdentity = false; //用于一开始会自当校准一次
         public void MoveSimulateMouse(Quaternion axisQuat) {
+            if (!hasDoIdentity) {
+                hasDoIdentity = true;
+                AimHandler.ins.DoIdentity();
+                MakeMouseToScreenCenter(AimHandler.ins.newRotation);
+            }
             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;
-            }
+            newAxisQuat = axisQuat;
         }
-        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);
-            }
+            nowAxisQuat = Quaternion.Lerp(nowAxisQuat, newAxisQuat, 0.033f * 8);
+            Vector3 resEulerAngles = FormatQuat(nowAxisQuat).eulerAngles - identityEulerAngles;
+            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);
         }
-        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;
+        Quaternion FormatQuat(Quaternion quat) { 
+            Vector3 normalVector = quat * Vector3.forward; //得出对应的法向量
+            return Quaternion.LookRotation(normalVector); //变回四元组,主要作用是规范,去除z轴影响
         }
     #endregion 模拟鼠标移动
 
+    //鼠标居中
+    public void MakeMouseToScreenCenter(Quaternion identityQuaternion) {
+        identityEulerAngles = FormatQuat(identityQuaternion).eulerAngles;
+        nowAxisQuat = identityQuaternion;
+    }
+
     //点击鼠标
     float _lastClickMouseTime = -100;
     void RecordClickMouseTime() {
@@ -161,48 +117,203 @@ public class SB_EventSystem : MonoBehaviour
         if (isCooling) return;
         simulateMouse.ClickMousePointer();
     }
+}
+/**传统鼠标 */
+// public class SB_EventSystem : MonoBehaviour
+// {   
+//     public static SB_EventSystem ins;
+//     MouseTest mouseTest;
 
-    //鼠标居中
-    public void MakeMouseToScreenCenter() {
-        hasAxisQuat = false;
-        simulateMouse.MakeMouseToScreenCenter();
-    }
+//     void Awake()
+//     {
+//         if (ins) {
+//             Destroy(this.gameObject);
+//         } else {
+//             ins = this;
+//             DontDestroyOnLoad(this.gameObject);
+//             AwakenSimulateMouse();
+//         }
+//     }
 
-    /** 鼠标测试类 */
-    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();
-                }
-            }
-        }
-    }
-}
+//     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();
+//                 }
+//             }
+//         }
+//     }
+// }
 

+ 5 - 0
Assets/JC/Untiy/SimulateMouse/SimulateMouse.cs

@@ -92,6 +92,11 @@ namespace JC.Unity {
                 val += deltaPos;
                 mousePointer.anchoredPosition = ClampValue(val);
             }
+            public void SetMousePointerPosition(Vector2 pos)
+            {
+                if (mousePointer == null) return;
+                mousePointer.position = pos;
+            }
             public void ClickMousePointer()
             {
                 mScreenRayRaycasterUIWrapper.Click();

BIN
备用资源/Fonts/SDF/TTC/msyh.ttc


+ 0 - 0
Assets/BowArrow/Fonts/MySDF/TTC/msyhbd.ttc → 备用资源/Fonts/SDF/TTC/msyhbd.ttc


+ 0 - 0
Assets/BowArrow/Fonts/MySDF/TTC/msyhl.ttc → 备用资源/Fonts/SDF/TTC/msyhl.ttc