123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- 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
- /// <summary>
- /// 当前工具类型
- /// </summary>
- public CVR_ToolType toolType
- {
- get
- {
- if (toolTypeFlags == null)
- {
- toolTypeFlags = this.GetComponent<CVR_ToolTypeFlags>();
- }
- return toolTypeFlags.CVR_ToolType;
- }
- }
- private CVR_ToolTypeFlags toolTypeFlags;
- /// <summary>
- /// 获取当前工具类型
- /// </summary>
- /// <returns></returns>
- 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);
- }
- /// <summary>
- /// 检测按键状态
- /// </summary>
- /// <param name="btnType"></param>
- /// <param name="pressType"></param>
- /// <param name="controllerHand"></param>
- /// <returns></returns>
- 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());
- }
- /// <summary>
- /// 获取当前手部类型
- /// </summary>
- /// <returns></returns>
- 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
- }
- /// <summary>
- /// ChivaVR跟VRTK的转换工具
- /// </summary>
- public class ChivaVRToVRTK
- {
- /// <summary>
- /// 检测按钮状态
- /// </summary>
- /// <param name="btnType">按键类型</param>
- /// <param name="pressType">按下类型</param>
- /// <param name="controllerHand">手部类型</param>
- /// <returns></returns>
- 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)));
- }
- /// <summary>
- /// 检测按钮状态
- /// </summary>
- /// <param name="btnType">按键类型</param>
- /// <param name="pressType">按下类型</param>
- /// <param name="controllerHand">手部类型</param>
- /// <returns></returns>
- public static Vector2 GetControllerAxis(CVR_ButtonTypes btnType, CVR_ControllerHand controllerHand)
- {
- return VRTK_SDK_Bridge.GetControllerAxis(GetButtonTypes(btnType),
- VRTK_ControllerReference.GetControllerReference(GetControllerHand(controllerHand)));
- }
- /// <summary>
- /// 转化按键类型
- /// </summary>
- /// <param name="types"></param>
- /// <returns></returns>
- 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;
- }
- }
- }
- }
|