[TrackedControllerFix] Needed changes for SmoothRay Support

This commit is contained in:
NotAKidoS 2023-05-21 23:04:17 -05:00
parent ea9ebf374b
commit b5df421bec
2 changed files with 17 additions and 13 deletions

View file

@ -4,16 +4,18 @@ using Valve.VR;
namespace NAK.TrackedControllerFix.HarmonyPatches; namespace NAK.TrackedControllerFix.HarmonyPatches;
internal class PlayerSetupPatches class PlayerSetupPatches
{ {
[HarmonyPostfix] [HarmonyPostfix]
[HarmonyPatch(typeof(PlayerSetup), "Start")] [HarmonyPatch(typeof(PlayerSetup), nameof(PlayerSetup.Start))]
private static void Post_PlayerSetup_Start(ref PlayerSetup __instance) static void Post_PlayerSetup_Start(ref PlayerSetup __instance)
{ {
// Add TrackedControllerFix // Add TrackedControllerFix
var vrLeftHandTracker = __instance.vrLeftHandTracker.AddComponent<TrackedControllerFixer>(); var vrLeftHandTracker = __instance.vrLeftHandTracker.AddComponent<TrackedControllerFixer>();
vrLeftHandTracker.inputSource = SteamVR_Input_Sources.LeftHand; vrLeftHandTracker.inputSource = SteamVR_Input_Sources.LeftHand;
var vrRightHandTracker = __instance.vrRightHandTracker.AddComponent<TrackedControllerFixer>(); var vrRightHandTracker = __instance.vrRightHandTracker.AddComponent<TrackedControllerFixer>();
vrRightHandTracker.inputSource = SteamVR_Input_Sources.RightHand; vrRightHandTracker.inputSource = SteamVR_Input_Sources.RightHand;
vrLeftHandTracker.Initialize();
vrRightHandTracker.Initialize();
} }
} }

View file

@ -8,11 +8,11 @@ public class TrackedControllerFixer : MonoBehaviour
public SteamVR_Input_Sources inputSource; public SteamVR_Input_Sources inputSource;
public int deviceIndex; public int deviceIndex;
private SteamVR_TrackedObject trackedObject; SteamVR_TrackedObject trackedObject;
private SteamVR_Behaviour_Pose oldBehaviourPose; SteamVR_Behaviour_Pose oldBehaviourPose;
private SteamVR_Action_Pose actionPose = SteamVR_Input.GetAction<SteamVR_Action_Pose>("Pose", false); SteamVR_Action_Pose actionPose = SteamVR_Input.GetAction<SteamVR_Action_Pose>("Pose", false);
private void Start() public void Initialize()
{ {
trackedObject = gameObject.AddComponent<SteamVR_TrackedObject>(); trackedObject = gameObject.AddComponent<SteamVR_TrackedObject>();
oldBehaviourPose = gameObject.GetComponent<SteamVR_Behaviour_Pose>(); oldBehaviourPose = gameObject.GetComponent<SteamVR_Behaviour_Pose>();
@ -20,26 +20,28 @@ public class TrackedControllerFixer : MonoBehaviour
if (actionPose != null) CheckDeviceIndex(); if (actionPose != null) CheckDeviceIndex();
} }
private void OnEnable() void OnEnable()
{ {
if (actionPose != null) actionPose[inputSource].onDeviceConnectedChanged += OnDeviceConnectedChanged; if (actionPose != null) actionPose[inputSource].onDeviceConnectedChanged += OnDeviceConnectedChanged;
oldBehaviourPose.enabled = false; if (oldBehaviourPose != null)
oldBehaviourPose.enabled = false;
} }
private void OnDisable() void OnDisable()
{ {
if (actionPose != null) actionPose[inputSource].onDeviceConnectedChanged -= OnDeviceConnectedChanged; if (actionPose != null) actionPose[inputSource].onDeviceConnectedChanged -= OnDeviceConnectedChanged;
oldBehaviourPose.enabled = true; if (oldBehaviourPose != null)
oldBehaviourPose.enabled = true;
} }
private void OnDeviceConnectedChanged(SteamVR_Action_Pose changedAction, SteamVR_Input_Sources changedSource, bool connected) void OnDeviceConnectedChanged(SteamVR_Action_Pose changedAction, SteamVR_Input_Sources changedSource, bool connected)
{ {
if (actionPose != changedAction) actionPose = changedAction; if (actionPose != changedAction) actionPose = changedAction;
if (changedSource != inputSource) return; if (changedSource != inputSource) return;
CheckDeviceIndex(); CheckDeviceIndex();
} }
private void CheckDeviceIndex() void CheckDeviceIndex()
{ {
if (actionPose[inputSource].active && actionPose[inputSource].deviceIsConnected) if (actionPose[inputSource].active && actionPose[inputSource].deviceIsConnected)
{ {