// Base Controller|SDK_Base|006 namespace VRTK { using UnityEngine; using System.Collections.Generic; public struct VRTKSDKBaseControllerEventArgs { public VRTK_ControllerReference controllerReference; } public delegate void VRTKSDKBaseControllerEventHandler(object sender, VRTKSDKBaseControllerEventArgs e); /// /// The Base Controller SDK script provides a bridge to SDK methods that deal with the input devices. /// /// /// This is an abstract class to implement the interface required by all implemented SDKs. /// public abstract class SDK_BaseController : SDK_Base { /// /// Types of buttons on a controller /// public enum ButtonTypes { /// /// Button One on the controller. /// ButtonOne, /// /// Button Two on the controller. /// ButtonTwo, /// /// Grip on the controller. /// Grip, /// /// Grip Hairline on the controller. /// GripHairline, /// /// Start Menu on the controller. /// StartMenu, /// /// Trigger on the controller. /// Trigger, /// /// Trigger Hairline on the controller. /// TriggerHairline, /// /// Touchpad on the controller. /// Touchpad, /// /// Touchpad Two on the controller. /// TouchpadTwo, /// /// Middle Finger on the controller. /// MiddleFinger, /// /// Ring Finger on the controller. /// RingFinger, /// /// Pinky Finger on the controller. /// PinkyFinger } /// /// Concepts of controller button press /// public enum ButtonPressTypes { /// /// The button is currently being pressed. /// Press, /// /// The button has just been pressed down. /// PressDown, /// /// The button has just been released. /// PressUp, /// /// The button is currently being touched. /// Touch, /// /// The button has just been touched. /// TouchDown, /// /// The button is no longer being touched. /// TouchUp } /// /// The elements of a generic controller /// public enum ControllerElements { /// /// The default point on the controller to attach grabbed objects to. /// AttachPoint, /// /// The trigger button. /// Trigger, /// /// The left part of the grip button collection. /// GripLeft, /// /// The right part of the grip button collection. /// GripRight, /// /// The touch pad/stick. /// Touchpad, /// /// The first generic button. /// ButtonOne, /// /// The second generic button. /// ButtonTwo, /// /// The system menu button. /// SystemMenu, /// /// The encompassing mesh of the controller body. /// Body, /// /// The start menu button. /// StartMenu, /// /// The touch pad/stick two. /// TouchpadTwo } /// /// Controller hand reference. /// public enum ControllerHand { /// /// No hand is assigned. /// None, /// /// The left hand is assigned. /// Left, /// /// The right hand is assigned. /// Right } /// /// SDK Controller types. /// public enum ControllerType { /// /// No controller type. /// Undefined, /// /// A custom controller type. /// Custom, /// /// The Simulator default hand controller. /// Simulator_Hand, /// /// The HTC Vive wand controller for SteamVR. /// SteamVR_ViveWand, /// /// The Oculus Touch controller for SteamVR. /// SteamVR_OculusTouch, /// /// The Oculus Touch controller for Oculus Utilities. /// Oculus_OculusTouch, /// /// The Daydream controller for Google Daydream SDK. /// Daydream_Controller, /// /// The Flip controller for Ximmerse SDK. /// Ximmerse_Flip, /// /// The Valve Knuckles controller for SteamVR. /// SteamVR_ValveKnuckles, /// /// The Oculus Gamepad for Oculus Utilities. /// Oculus_OculusGamepad, /// /// The Oculus Remote for Oculus Utilities. /// Oculus_OculusRemote, /// /// The Oculus GearVR HMD controls for Oculus Utilities. /// Oculus_GearVRHMD, /// /// The Oculus GearVR controller for Oculus Utilities. /// Oculus_GearVRController, /// /// The Windows Mixed Reality Motion Controller for Windows Mixed Reality. /// WindowsMR_MotionController, /// /// The Windows Mixed Reality Motion Controller for SteamVR. /// SteamVR_WindowsMRController } public event VRTKSDKBaseControllerEventHandler LeftControllerReady; public event VRTKSDKBaseControllerEventHandler RightControllerReady; public event VRTKSDKBaseControllerEventHandler LeftControllerModelReady; public event VRTKSDKBaseControllerEventHandler RightControllerModelReady; protected Transform defaultSDKLeftControllerModel = null; protected Transform defaultSDKRightControllerModel = null; public virtual void OnControllerReady(ControllerHand hand) { VRTKSDKBaseControllerEventArgs e; e.controllerReference = VRTK_ControllerReference.GetControllerReference(hand); switch (hand) { case ControllerHand.Left: if (LeftControllerReady != null) { LeftControllerReady(this, e); } break; case ControllerHand.Right: if (RightControllerReady != null) { RightControllerReady(this, e); } break; } } /// /// The ProcessUpdate method enables an SDK to run logic for every Unity Update /// /// The reference for the controller. /// A dictionary of generic options that can be used to within the update. public abstract void ProcessUpdate(VRTK_ControllerReference controllerReference, Dictionary options); /// /// The ProcessFixedUpdate method enables an SDK to run logic for every Unity FixedUpdate /// /// The reference for the controller. /// A dictionary of generic options that can be used to within the fixed update. public abstract void ProcessFixedUpdate(VRTK_ControllerReference controllerReference, Dictionary options); /// /// The GetCurrentControllerType method returns the current used ControllerType based on the SDK and headset being used. /// /// The reference to the controller to get type of. /// The ControllerType based on the SDK and headset being used. public abstract ControllerType GetCurrentControllerType(VRTK_ControllerReference controllerReference = null); /// /// The GetControllerDefaultColliderPath returns the path to the prefab that contains the collider objects for the default controller of this SDK. /// /// The controller hand to check for /// A path to the resource that contains the collider GameObject. public abstract string GetControllerDefaultColliderPath(ControllerHand hand); /// /// The GetControllerElementPath returns the path to the game object that the given controller element for the given hand resides in. /// /// The controller element to look up. /// The controller hand to look up. /// Whether to get the initial path or the full path to the element. /// A string containing the path to the game object that the controller element resides in. public abstract string GetControllerElementPath(ControllerElements element, ControllerHand hand, bool fullPath = false); /// /// The GetControllerIndex method returns the index of the given controller. /// /// The GameObject containing the controller. /// The index of the given controller. public abstract uint GetControllerIndex(GameObject controller); /// /// The GetControllerByIndex method returns the GameObject of a controller with a specific index. /// /// The index of the controller to find. /// If true it will return the actual controller, if false it will return the script alias controller GameObject. /// The GameObject of the controller public abstract GameObject GetControllerByIndex(uint index, bool actual = false); /// /// The GetControllerOrigin method returns the origin of the given controller. /// /// The reference to the controller to retrieve the origin from. /// A Transform containing the origin of the controller. public abstract Transform GetControllerOrigin(VRTK_ControllerReference controllerReference); /// /// The GenerateControllerPointerOrigin method can create a custom pointer origin Transform to represent the pointer position and forward. /// /// The GameObject that the origin will become parent of. If it is a controller then it will also be used to determine the hand if required. /// A generated Transform that contains the custom pointer origin. [System.Obsolete("GenerateControllerPointerOrigin has been deprecated and will be removed in a future version of VRTK.")] public abstract Transform GenerateControllerPointerOrigin(GameObject parent); /// /// The GetControllerLeftHand method returns the GameObject containing the representation of the left hand controller. /// /// If true it will return the actual controller, if false it will return the script alias controller GameObject. /// The GameObject containing the left hand controller. public abstract GameObject GetControllerLeftHand(bool actual = false); /// /// The GetControllerRightHand method returns the GameObject containing the representation of the right hand controller. /// /// If true it will return the actual controller, if false it will return the script alias controller GameObject. /// The GameObject containing the right hand controller. public abstract GameObject GetControllerRightHand(bool actual = false); /// /// The IsControllerLeftHand/1 method is used to check if the given controller is the the left hand controller. /// /// The GameObject to check. /// Returns true if the given controller is the left hand controller. public abstract bool IsControllerLeftHand(GameObject controller); /// /// The IsControllerRightHand/1 method is used to check if the given controller is the the right hand controller. /// /// The GameObject to check. /// Returns true if the given controller is the right hand controller. public abstract bool IsControllerRightHand(GameObject controller); /// /// The IsControllerLeftHand/2 method is used to check if the given controller is the the left hand controller. /// /// The GameObject to check. /// If true it will check the actual controller, if false it will check the script alias controller. /// Returns true if the given controller is the left hand controller. public abstract bool IsControllerLeftHand(GameObject controller, bool actual); /// /// The IsControllerRightHand/2 method is used to check if the given controller is the the right hand controller. /// /// The GameObject to check. /// If true it will check the actual controller, if false it will check the script alias controller. /// Returns true if the given controller is the right hand controller. public abstract bool IsControllerRightHand(GameObject controller, bool actual); /// /// The WaitForControllerModel method determines whether the controller model for the given hand requires waiting to load in on scene start. /// /// The hand to determine if the controller model will be ready for. /// Returns true if the controller model requires loading in at runtime and therefore needs waiting for. Returns false if the controller model will be available at start. public abstract bool WaitForControllerModel(ControllerHand hand); /// /// The GetControllerModel method returns the model alias for the given GameObject. /// /// The GameObject to get the model alias for. /// The GameObject that has the model alias within it. public abstract GameObject GetControllerModel(GameObject controller); /// /// The GetControllerModel method returns the model alias for the given controller hand. /// /// The hand enum of which controller model to retrieve. /// The GameObject that has the model alias within it. public abstract GameObject GetControllerModel(ControllerHand hand); /// /// The GetControllerModelHand method returns the hand for the given controller model GameObject. /// /// The controller model GameObject to get the hand for. /// The hand enum for which the given controller model is for. public virtual ControllerHand GetControllerModelHand(GameObject controllerModel) { VRTK_SDKManager sdkManager = VRTK_SDKManager.instance; if (sdkManager != null && sdkManager.loadedSetup != null) { if (controllerModel == sdkManager.loadedSetup.modelAliasLeftController) { return ControllerHand.Left; } else if (controllerModel == sdkManager.loadedSetup.modelAliasRightController) { return ControllerHand.Right; } } return ControllerHand.None; } /// /// The GetControllerRenderModel method gets the game object that contains the given controller's render model. /// /// The reference to the controller to check. /// A GameObject containing the object that has a render model for the controller. public abstract GameObject GetControllerRenderModel(VRTK_ControllerReference controllerReference); /// /// The SetControllerRenderModelWheel method sets the state of the scroll wheel on the controller render model. /// /// The GameObject containing the controller render model. /// If true and the render model has a scroll wheen then it will be displayed, if false then the scroll wheel will be hidden. public abstract void SetControllerRenderModelWheel(GameObject renderModel, bool state); /// /// The HapticPulse/2 method is used to initiate a simple haptic pulse on the tracked object of the given controller reference. /// /// The reference to the tracked object to initiate the haptic pulse on. /// The intensity of the rumble of the controller motor. `0` to `1`. public abstract void HapticPulse(VRTK_ControllerReference controllerReference, float strength = 0.5f); /// /// The HapticPulse/2 method is used to initiate a haptic pulse based on an audio clip on the tracked object of the given controller reference. /// /// The reference to the tracked object to initiate the haptic pulse on. /// The audio clip to use for the haptic pattern. public abstract bool HapticPulse(VRTK_ControllerReference controllerReference, AudioClip clip); /// /// The GetHapticModifiers method is used to return modifiers for the duration and interval if the SDK handles it slightly differently. /// /// An SDK_ControllerHapticModifiers object with a given `durationModifier` and an `intervalModifier`. public abstract SDK_ControllerHapticModifiers GetHapticModifiers(); /// /// The GetVelocity method is used to determine the current velocity of the tracked object on the given controller reference. /// /// The reference to the tracked object to check for. /// A Vector3 containing the current velocity of the tracked object. public abstract Vector3 GetVelocity(VRTK_ControllerReference controllerReference); /// /// The GetAngularVelocity method is used to determine the current angular velocity of the tracked object on the given controller reference. /// /// The reference to the tracked object to check for. /// A Vector3 containing the current angular velocity of the tracked object. public abstract Vector3 GetAngularVelocity(VRTK_ControllerReference controllerReference); /// /// The IsTouchpadStatic method is used to determine if the touchpad is currently not being moved. /// /// /// /// /// Returns true if the touchpad is not currently being touched or moved. public abstract bool IsTouchpadStatic(bool isTouched, Vector2 currentAxisValues, Vector2 previousAxisValues, int compareFidelity); /// /// The GetButtonAxis method retrieves the current X/Y axis values for the given button type on the given controller reference. /// /// The type of button to check for the axis on. /// The reference to the controller to check the button axis on. /// A Vector2 of the X/Y values of the button axis. If no axis values exist for the given button, then a Vector2.Zero is returned. public abstract Vector2 GetButtonAxis(ButtonTypes buttonType, VRTK_ControllerReference controllerReference); /// /// The GetButtonSenseAxis method retrieves the current sense axis value for the given button type on the given controller reference. /// /// The type of button to check for the sense axis on. /// The reference to the controller to check the sense axis on. /// The current sense axis value. public abstract float GetButtonSenseAxis(ButtonTypes buttonType, VRTK_ControllerReference controllerReference); /// /// The GetButtonHairlineDelta method is used to get the difference between the current button press and the previous frame button press. /// /// The type of button to get the hairline delta for. /// The reference to the controller to get the hairline delta for. /// The delta between the button presses. public abstract float GetButtonHairlineDelta(ButtonTypes buttonType, VRTK_ControllerReference controllerReference); /// /// The GetControllerButtonState method is used to determine if the given controller button for the given press type on the given controller reference is currently taking place. /// /// The type of button to check for the state of. /// The button state to check for. /// The reference to the controller to check the button state on. /// Returns true if the given button is in the state of the given press type on the given controller reference. public abstract bool GetControllerButtonState(ButtonTypes buttonType, ButtonPressTypes pressType, VRTK_ControllerReference controllerReference); protected virtual GameObject GetSDKManagerControllerLeftHand(bool actual = false) { VRTK_SDKManager sdkManager = VRTK_SDKManager.instance; if (sdkManager != null && sdkManager.loadedSetup != null) { return (actual ? sdkManager.loadedSetup.actualLeftController : sdkManager.scriptAliasLeftController); } return null; } protected virtual GameObject GetSDKManagerControllerRightHand(bool actual = false) { VRTK_SDKManager sdkManager = VRTK_SDKManager.instance; if (sdkManager != null && sdkManager.loadedSetup != null) { return (actual ? sdkManager.loadedSetup.actualRightController : sdkManager.scriptAliasRightController); } return null; } protected virtual bool CheckActualOrScriptAliasControllerIsLeftHand(GameObject controller) { return (IsControllerLeftHand(controller, true) || IsControllerLeftHand(controller, false)); } protected virtual bool CheckActualOrScriptAliasControllerIsRightHand(GameObject controller) { return (IsControllerRightHand(controller, true) || IsControllerRightHand(controller, false)); } protected virtual bool CheckControllerLeftHand(GameObject controller, bool actual) { VRTK_SDKManager sdkManager = VRTK_SDKManager.instance; if (sdkManager != null && sdkManager.loadedSetup != null && controller != null) { return (actual ? controller == sdkManager.loadedSetup.actualLeftController : controller == sdkManager.scriptAliasLeftController); } return false; } protected virtual bool CheckControllerRightHand(GameObject controller, bool actual) { VRTK_SDKManager sdkManager = VRTK_SDKManager.instance; if (sdkManager != null && sdkManager.loadedSetup != null && controller != null) { return (actual ? controller == sdkManager.loadedSetup.actualRightController : controller == sdkManager.scriptAliasRightController); } return false; } protected virtual GameObject GetControllerModelFromController(GameObject controller) { return GetControllerModel(VRTK_DeviceFinder.GetControllerHand(controller)); } protected virtual GameObject GetSDKManagerControllerModelForHand(ControllerHand hand) { VRTK_SDKManager sdkManager = VRTK_SDKManager.instance; if (sdkManager != null && sdkManager.loadedSetup != null) { switch (hand) { case ControllerHand.Left: return sdkManager.loadedSetup.modelAliasLeftController; case ControllerHand.Right: return sdkManager.loadedSetup.modelAliasRightController; } } return null; } protected virtual GameObject GetActualController(GameObject controller) { GameObject returnController = null; VRTK_SDKManager sdkManager = VRTK_SDKManager.instance; if (sdkManager != null && sdkManager.loadedSetup != null) { if (IsControllerLeftHand(controller)) { returnController = sdkManager.loadedSetup.actualLeftController; } else if (IsControllerRightHand(controller)) { returnController = sdkManager.loadedSetup.actualRightController; } } return returnController; } protected virtual void OnControllerModelReady(ControllerHand hand, VRTK_ControllerReference controllerReference) { VRTKSDKBaseControllerEventArgs e; e.controllerReference = controllerReference; switch (hand) { case ControllerHand.Left: if (LeftControllerModelReady != null) { LeftControllerModelReady(this, e); } break; case ControllerHand.Right: if (RightControllerModelReady != null) { RightControllerModelReady(this, e); } break; } } protected virtual bool ShouldWaitForControllerModel(ControllerHand hand, bool ignoreChildCount) { //If the default model isn't set or the current controller model isn't the default controller model, then don't bother waiting for the model to stream in. switch (hand) { case ControllerHand.Left: return IsDefaultControllerModel(defaultSDKLeftControllerModel, GetControllerModel(ControllerHand.Left), ignoreChildCount); case ControllerHand.Right: return IsDefaultControllerModel(defaultSDKRightControllerModel, GetControllerModel(ControllerHand.Right), ignoreChildCount); } return false; } protected virtual bool IsDefaultControllerModel(Transform givenDefault, GameObject givenActual, bool ignoreChildCount) { return (givenDefault != null && givenActual == givenDefault.gameObject && givenActual != null && (ignoreChildCount || givenActual.transform.childCount == 0)); } } public class SDK_ControllerHapticModifiers { public float durationModifier = 1f; public float intervalModifier = 1f; public ushort maxHapticVibration = 1; public int hapticsBufferSize = 8192; } }