From 2a7e796862543394b85e3a5ed48539cc859165de Mon Sep 17 00:00:00 2001 From: SDraw Date: Wed, 23 Mar 2022 11:37:07 +0300 Subject: [PATCH] Consideration of Index controllers Hud notifications --- ml_lme_cvr/LeapIK.cs | 4 +- ml_lme_cvr/LeapTracked.cs | 45 ++++++++++----------- ml_lme_cvr/Main.cs | 57 +++++++++++++++++++++++++-- ml_lme_cvr/Properties/AssemblyInfo.cs | 6 +-- ml_lme_cvr/Settings.cs | 6 +-- 5 files changed, 83 insertions(+), 35 deletions(-) diff --git a/ml_lme_cvr/LeapIK.cs b/ml_lme_cvr/LeapIK.cs index 76d8a20..c15d153 100644 --- a/ml_lme_cvr/LeapIK.cs +++ b/ml_lme_cvr/LeapIK.cs @@ -13,8 +13,8 @@ namespace ml_lme_cvr Transform m_leftHand = null; Transform m_rightHand = null; - float m_leftHandWeight = 1f; - float m_rightHandWeight = 1f; + float m_leftHandWeight = 0f; + float m_rightHandWeight = 0f; bool m_leftHandVisible = false; bool m_rightHandVisible = false; diff --git a/ml_lme_cvr/LeapTracked.cs b/ml_lme_cvr/LeapTracked.cs index dba0f15..ad8c9a9 100644 --- a/ml_lme_cvr/LeapTracked.cs +++ b/ml_lme_cvr/LeapTracked.cs @@ -1,5 +1,6 @@ using ABI_RC.Core.Player; using ABI_RC.Core.Savior; +using System.Linq; using UnityEngine; namespace ml_lme_cvr @@ -19,19 +20,30 @@ namespace ml_lme_cvr Transform m_leftHand = null; Transform m_rightHand = null; + bool m_knucklesInUse = false; + void Start() { - m_indexIK = this.GetComponent(); + m_indexIK = this.GetComponent(); + m_knucklesInUse = PlayerSetup.Instance._trackerManager.trackerNames.Contains("knuckles"); if((m_indexIK != null) && (m_animator != null)) { - m_indexIK.avatarAnimator = m_animator; - m_indexIK.Recalibrate(); - - m_indexIK.activeControl = m_enabled; - CVRInputManager.Instance.individualFingerTracking = m_enabled; - + if(!PlayerSetup.Instance._inVr) + { + // Seems that VR mode always calibrates IndexIK, so let's force it + m_indexIK.avatarAnimator = m_animator; + m_indexIK.Recalibrate(); + } m_calibrated = true; + + m_indexIK.activeControl = (m_enabled || m_knucklesInUse); + CVRInputManager.Instance.individualFingerTracking = (m_enabled || m_knucklesInUse); + + m_leapIK = m_animator.gameObject.AddComponent(); + m_leapIK.SetEnabled(m_enabled); + m_leapIK.SetFingersOnly(m_fingersOnly); + m_leapIK.SetHands(m_leftHand, m_rightHand); } } @@ -43,7 +55,7 @@ namespace ml_lme_cvr if((m_animator != null) && (m_indexIK != null)) { m_indexIK.activeControl = true; - if(!m_calibrated) + if(!m_calibrated && !PlayerSetup.Instance._inVr) { m_indexIK.avatarAnimator = m_animator; m_indexIK.Recalibrate(); @@ -56,8 +68,8 @@ namespace ml_lme_cvr { if((m_indexIK != null) && m_calibrated) { - m_indexIK.activeControl = false; - CVRInputManager.Instance.individualFingerTracking = false; + m_indexIK.activeControl = m_knucklesInUse; + CVRInputManager.Instance.individualFingerTracking = m_knucklesInUse; } } @@ -65,15 +77,7 @@ namespace ml_lme_cvr m_leapIK.SetEnabled(m_enabled); } - public void SetAnimator(Animator p_animator) - { - m_animator = p_animator; - - m_leapIK = m_animator.gameObject.AddComponent(); - m_leapIK.SetEnabled(m_enabled); - m_leapIK.SetFingersOnly(m_fingersOnly); - m_leapIK.SetHands(m_leftHand, m_rightHand); - } + public void SetAnimator(Animator p_animator) => m_animator = p_animator; public void SetFingersOnly(bool p_state) { @@ -87,9 +91,6 @@ namespace ml_lme_cvr { m_leftHand = p_left; m_rightHand = p_right; - - if(m_leapIK != null) - m_leapIK.SetHands(p_left, p_right); } public void UpdateTracking(GestureMatcher.GesturesData p_gesturesData) diff --git a/ml_lme_cvr/Main.cs b/ml_lme_cvr/Main.cs index 721e1c2..e4abe4f 100644 --- a/ml_lme_cvr/Main.cs +++ b/ml_lme_cvr/Main.cs @@ -1,4 +1,5 @@ using ABI_RC.Core.Player; +using ABI_RC.Core.UI; using UnityEngine; namespace ml_lme_cvr @@ -35,8 +36,13 @@ namespace ml_lme_cvr Settings.HeadOffsetChange += this.OnSettingsHeadOffsetChange; m_leapController = new Leap.Controller(); - m_gesturesData = new GestureMatcher.GesturesData(); + m_leapController.Device += this.OnLeapDeviceInitialized; + m_leapController.DeviceFailure += this.OnLeapDeviceFailure; + m_leapController.DeviceLost += this.OnLeapDeviceLost; + m_leapController.Connect += this.OnLeapServiceConnect; + m_leapController.Disconnect += this.OnLeapServiceDisconnect; + m_gesturesData = new GestureMatcher.GesturesData(); m_leapHands = new GameObject[GestureMatcher.GesturesData.ms_handsCount]; // Patches @@ -113,6 +119,11 @@ namespace ml_lme_cvr } } } + else + { + for(int i = 0; i < GestureMatcher.GesturesData.ms_handsCount; i++) + m_gesturesData.m_handsPresenses[i] = false; + } if(m_leapTracked != null) m_leapTracked.UpdateTracking(m_gesturesData); @@ -232,13 +243,51 @@ namespace ml_lme_cvr } } + // Leap events + void OnLeapDeviceInitialized(object p_sender, Leap.DeviceEventArgs p_args) + { + if(Settings.Enabled && (m_leapController != null)) + { + m_leapController.ClearPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_SCREENTOP); + if(Settings.HmdMode) + m_leapController.SetPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_HMD); + else + m_leapController.ClearPolicy(Leap.Controller.PolicyFlag.POLICY_OPTIMIZE_HMD); + } + + if(CohtmlHud.Instance != null) + CohtmlHud.Instance.ViewDropText("Leap Motion Extension", "Device initialized"); + } + + void OnLeapDeviceFailure(object p_sender, Leap.DeviceFailureEventArgs p_args) + { + if(CohtmlHud.Instance != null) + CohtmlHud.Instance.ViewDropText("Leap Motion Extension", "Device failure, code " + p_args.ErrorCode + ": " + p_args.ErrorMessage); + } + + void OnLeapDeviceLost(object p_sender, Leap.DeviceEventArgs p_args) + { + if(CohtmlHud.Instance != null) + CohtmlHud.Instance.ViewDropText("Leap Motion Extension", "Device lost"); + } + + void OnLeapServiceConnect(object p_sender, Leap.ConnectionEventArgs p_args) + { + if(CohtmlHud.Instance != null) + CohtmlHud.Instance.ViewDropText("Leap Motion Extension", "Service connected"); + } + + void OnLeapServiceDisconnect(object p_sender, Leap.ConnectionLostEventArgs p_args) + { + if(CohtmlHud.Instance != null) + CohtmlHud.Instance.ViewDropText("Leap Motion Extension", "Service disconnected"); + } + // Patches static void OnAvatarSetup(ref PlayerSetup __instance) { - if(__instance != null && __instance == PlayerSetup.Instance) - { + if((__instance != null) && (__instance == PlayerSetup.Instance)) ms_instance?.OnLocalPlayerAvatarSetup(__instance._animator, __instance.GetComponent()); - } } void OnLocalPlayerAvatarSetup(Animator p_animator, IndexIK p_indexIK) { diff --git a/ml_lme_cvr/Properties/AssemblyInfo.cs b/ml_lme_cvr/Properties/AssemblyInfo.cs index 2e706d0..d357e8f 100644 --- a/ml_lme_cvr/Properties/AssemblyInfo.cs +++ b/ml_lme_cvr/Properties/AssemblyInfo.cs @@ -1,10 +1,10 @@ using System.Reflection; [assembly: AssemblyTitle("LeapMotionExtension")] -[assembly: AssemblyVersion("1.0.0")] -[assembly: AssemblyFileVersion("1.0.0")] +[assembly: AssemblyVersion("1.0.1")] +[assembly: AssemblyFileVersion("1.0.1")] -[assembly: MelonLoader.MelonInfo(typeof(ml_lme_cvr.LeapMotionExtension), "LeapMotionExtension", "1.0.0", "SDraw", "https://github.com/SDraw")] +[assembly: MelonLoader.MelonInfo(typeof(ml_lme_cvr.LeapMotionExtension), "LeapMotionExtension", "1.0.1", "SDraw", "https://github.com/SDraw")] [assembly: MelonLoader.MelonGame(null, "ChilloutVR")] [assembly: MelonLoader.MelonPlatform(MelonLoader.MelonPlatformAttribute.CompatiblePlatforms.WINDOWS_X64)] [assembly: MelonLoader.MelonPlatformDomain(MelonLoader.MelonPlatformDomainAttribute.CompatibleDomains.MONO)] diff --git a/ml_lme_cvr/Settings.cs b/ml_lme_cvr/Settings.cs index 0033ae6..4a7e064 100644 --- a/ml_lme_cvr/Settings.cs +++ b/ml_lme_cvr/Settings.cs @@ -29,7 +29,7 @@ namespace ml_lme_cvr static bool ms_hmdMode = false; static float ms_rootAngle = 0f; static bool ms_headAttach = false; - static Vector3 ms_headOffset = new Vector3(0f, 0f, 0f); + static Vector3 ms_headOffset = new Vector3(0f, -0.3f, 0.15f); static bool ms_initialized = false; @@ -53,7 +53,7 @@ namespace ml_lme_cvr static void BeforeSettingsLoad(ref CVRSettings __instance) { - if(!ms_initialized && __instance != null) + if(!ms_initialized && (__instance != null)) { var l_settings = HarmonyLib.Traverse.Create(__instance)?.Field("_settings")?.GetValue>(); if(l_settings != null) @@ -72,8 +72,6 @@ namespace ml_lme_cvr l_settings.Add(new CVRSettingsInt(ms_defaultSettings[11], 0)); } - // Changes events - // Enable tracking __instance.settingBoolChanged.AddListener((name, value) => {