namespace ChivaXR.VR { using System.Collections; using System.Collections.Generic; using UnityEngine; using VRTK; [RequireComponent(typeof(CVR_ToolTypeFlags))] public class CVR_InteractableVRTK : VRTK_InteractableObject, IVR_Interactable { private GameObject interactableCurrentHand; #region 接口IVR_Interactable /// /// 当前工具类型 /// public CVR_ToolType toolType { get { if (toolTypeFlags == null) { toolTypeFlags = this.GetComponent(); } return toolTypeFlags.CVR_ToolType; } } private CVR_ToolTypeFlags toolTypeFlags; /// /// 获取当前工具类型 /// /// public CVR_ToolType GetToolType() { return toolType; } public event CVR_InteractableObjectEventHandler CVR_InteractableObjectGrabbed; public event CVR_InteractableObjectEventHandler CVR_InteractableObjectPreUnGrabbed; public event CVR_InteractableObjectEventHandler CVR_InteractableObjectUsed; public event CVR_InteractableObjectEventHandler CVR_InteractableObjectUnused; protected override void Awake() { base.Awake(); CVR_ToolManager.Instance.RegisterTool(toolType, this); } /// /// 检测按键状态 /// /// /// /// /// public bool CVR_CheckButtonState(CVR_ButtonTypes btnType, CVR_ButtonPressTypes pressType) { return ChivaVRToVRTK.CheckButtonState(btnType, pressType, CVR_GetCurrentHand()); } public Vector2 CVR_GetControllerAxis(CVR_ButtonTypes btnType) { return ChivaVRToVRTK.GetControllerAxis(btnType, CVR_GetCurrentHand()); } /// /// 获取当前手部类型 /// /// public CVR_ControllerHand CVR_GetCurrentHand() { return ChivaVRToVRTK.GetChivaVR_ControllerHand(VRTK_DeviceFinder.GetControllerHand(interactableCurrentHand)); } public GameObject CVR_GetHeadsetCamera() { return VRTK_SDK_Bridge.GetHeadsetCamera().gameObject; } public GameObject CVR_GetInteractableObject() { return interactableCurrentHand; } public GameObject CVR_GetCurrentObject() { return this.gameObject; } public bool CVR_IsGrabbed() { return IsGrabbed(); } public bool CVR_IsUsing() { return IsUsing(); } public void ForceStopUsing() { GetUsingScript().ForceStopUsing(); } #endregion #region VRTK public override void Grabbed(VRTK_InteractGrab currentGrabbingObject = null) { base.Grabbed(currentGrabbingObject); interactableCurrentHand = currentGrabbingObject.gameObject; OnChivaVRInteractableObjectGrabbde(SetChivaVRInteractableObjectEvent(interactableCurrentHand)); } public override void Ungrabbed(VRTK_InteractGrab previousGrabbingObject = null) { GameObject previousGrabbingGameObject = (previousGrabbingObject != null ? previousGrabbingObject.gameObject : null); OnChivaVRInteractableObjectPreUnGrabbde(SetChivaVRInteractableObjectEvent(previousGrabbingGameObject)); base.Ungrabbed(previousGrabbingObject); interactableCurrentHand = null; } public override void StartUsing(VRTK_InteractUse currentUsingObject = null) { OnChivaVRInteractableObjectUsed(SetChivaVRInteractableObjectEvent(interactableCurrentHand)); base.StartUsing(currentUsingObject); } public override void StopUsing(VRTK_InteractUse previousUsingObject = null, bool resetUsingObjectState = true) { OnChivaVRInteractableObjectUnUsed(SetChivaVRInteractableObjectEvent(interactableCurrentHand)); base.StopUsing(previousUsingObject, resetUsingObjectState); } public virtual void OnChivaVRInteractableObjectPreUnGrabbde(CVR_InteractableObjectEventArgs e) { CVR_InteractableObjectPreUnGrabbed?.Invoke(this, e); } public virtual void OnChivaVRInteractableObjectGrabbde(CVR_InteractableObjectEventArgs e) { CVR_InteractableObjectGrabbed?.Invoke(this, e); } public virtual void OnChivaVRInteractableObjectUsed(CVR_InteractableObjectEventArgs e) { CVR_InteractableObjectUsed?.Invoke(this, e); } public virtual void OnChivaVRInteractableObjectUnUsed(CVR_InteractableObjectEventArgs e) { CVR_InteractableObjectUnused?.Invoke(this, e); } public CVR_InteractableObjectEventArgs SetChivaVRInteractableObjectEvent(GameObject interactingObject) { CVR_InteractableObjectEventArgs e; e.interactingObject = interactingObject; return e; } #endregion } /// /// ChivaVR跟VRTK的转换工具 /// public class ChivaVRToVRTK { /// /// 检测按钮状态 /// /// 按键类型 /// 按下类型 /// 手部类型 /// public static bool CheckButtonState(CVR_ButtonTypes btnType, CVR_ButtonPressTypes pressType, CVR_ControllerHand controllerHand) { return VRTK_SDK_Bridge.GetControllerButtonState(GetButtonTypes(btnType), GetPressTypes(pressType) , VRTK_ControllerReference.GetControllerReference(GetControllerHand(controllerHand))); } /// /// 检测按钮状态 /// /// 按键类型 /// 按下类型 /// 手部类型 /// public static Vector2 GetControllerAxis(CVR_ButtonTypes btnType, CVR_ControllerHand controllerHand) { return VRTK_SDK_Bridge.GetControllerAxis(GetButtonTypes(btnType), VRTK_ControllerReference.GetControllerReference(GetControllerHand(controllerHand))); } /// /// 转化按键类型 /// /// /// public static SDK_BaseController.ButtonTypes GetButtonTypes(CVR_ButtonTypes types) { switch (types) { case CVR_ButtonTypes.ButtonOne: return SDK_BaseController.ButtonTypes.ButtonOne; case CVR_ButtonTypes.ButtonTwo: return SDK_BaseController.ButtonTypes.ButtonTwo; case CVR_ButtonTypes.Grip: return SDK_BaseController.ButtonTypes.Grip; case CVR_ButtonTypes.Trigger: return SDK_BaseController.ButtonTypes.Trigger; case CVR_ButtonTypes.Touchpad: return SDK_BaseController.ButtonTypes.Touchpad; case CVR_ButtonTypes.TouchpadTwo: return SDK_BaseController.ButtonTypes.TouchpadTwo; } return SDK_BaseController.ButtonTypes.GripHairline; } public static SDK_BaseController.ButtonPressTypes GetPressTypes(CVR_ButtonPressTypes pressTypes) { switch (pressTypes) { case CVR_ButtonPressTypes.Press: return SDK_BaseController.ButtonPressTypes.Press; case CVR_ButtonPressTypes.PressDown: return SDK_BaseController.ButtonPressTypes.PressDown; case CVR_ButtonPressTypes.PressUp: return SDK_BaseController.ButtonPressTypes.PressUp; case CVR_ButtonPressTypes.Touch: return SDK_BaseController.ButtonPressTypes.Touch; case CVR_ButtonPressTypes.TouchDown: return SDK_BaseController.ButtonPressTypes.TouchDown; case CVR_ButtonPressTypes.TouchUp: return SDK_BaseController.ButtonPressTypes.TouchUp; default: return SDK_BaseController.ButtonPressTypes.Touch; } } public static SDK_BaseController.ControllerHand GetControllerHand(CVR_ControllerHand controllerHand) { switch (controllerHand) { case CVR_ControllerHand.None: return SDK_BaseController.ControllerHand.None; case CVR_ControllerHand.Left: return SDK_BaseController.ControllerHand.Left; case CVR_ControllerHand.Right: return SDK_BaseController.ControllerHand.Right; default: return SDK_BaseController.ControllerHand.None; } } public static CVR_ControllerHand GetChivaVR_ControllerHand(SDK_BaseController.ControllerHand controllerHand) { switch (controllerHand) { case SDK_BaseController.ControllerHand.None: return CVR_ControllerHand.None; case SDK_BaseController.ControllerHand.Left: return CVR_ControllerHand.Left; case SDK_BaseController.ControllerHand.Right: return CVR_ControllerHand.Right; default: return CVR_ControllerHand.None; } } } }