using System; using HurricaneVR.Framework.Shared; using UnityEngine; using UnityEngine.XR; namespace HurricaneVR.Framework.ControllerInput { enum ButtonReadType { None = 0, Binary, Axis1D, Axis2DUp, Axis2DDown, Axis2DLeft, Axis2DRight } struct ButtonInfo { public ButtonInfo(string name, ButtonReadType type) { this.name = name; this.type = type; } public string name; public ButtonReadType type; } public class HVRXRInputController : HVRController { static ButtonInfo[] s_ButtonData = new ButtonInfo[] { new ButtonInfo("", ButtonReadType.None), new ButtonInfo("MenuButton", ButtonReadType.Binary), new ButtonInfo("Trigger", ButtonReadType.Axis1D), new ButtonInfo("Grip", ButtonReadType.Axis1D), new ButtonInfo("TriggerPressed", ButtonReadType.Binary), new ButtonInfo("GripPressed", ButtonReadType.Binary), new ButtonInfo("PrimaryButton", ButtonReadType.Binary), new ButtonInfo("PrimaryTouch", ButtonReadType.Binary), new ButtonInfo("SecondaryButton", ButtonReadType.Binary), new ButtonInfo("SecondaryTouch", ButtonReadType.Binary), new ButtonInfo("Primary2DAxisTouch", ButtonReadType.Binary), new ButtonInfo("Primary2DAxisClick", ButtonReadType.Binary), new ButtonInfo("Secondary2DAxisTouch", ButtonReadType.Binary), new ButtonInfo("Secondary2DAxisClick", ButtonReadType.Binary), new ButtonInfo("Primary2DAxis", ButtonReadType.Axis2DUp), new ButtonInfo("Primary2DAxis", ButtonReadType.Axis2DDown), new ButtonInfo("Primary2DAxis", ButtonReadType.Axis2DLeft), new ButtonInfo("Primary2DAxis", ButtonReadType.Axis2DRight), new ButtonInfo("Secondary2DAxis", ButtonReadType.Axis2DUp), new ButtonInfo("Secondary2DAxis", ButtonReadType.Axis2DDown), new ButtonInfo("Secondary2DAxis", ButtonReadType.Axis2DLeft), new ButtonInfo("Secondary2DAxis", ButtonReadType.Axis2DRight), }; protected virtual InputFeatureUsage JoystickAxisFeature => InputMap.JoystickAxis == InputAxes.Primary2DAxis ? CommonUsages.primary2DAxis : CommonUsages.secondary2DAxis; protected virtual InputFeatureUsage TrackPadAxisFeature => InputMap.TrackPadAxis == InputAxes.Primary2DAxis ? CommonUsages.primary2DAxis : CommonUsages.secondary2DAxis; protected override void UpdateInput() { if (InputMap) { Device.TryGetFeatureValue(JoystickAxisFeature, out JoystickAxis); Device.TryGetFeatureValue(TrackPadAxisFeature, out TrackpadAxis); Device.TryGetFeatureValue(CommonUsages.grip, out Grip); Device.TryGetFeatureValue(CommonUsages.trigger, out Trigger); PrimaryButton = IsPressed(Device, InputMap.Primary); PrimaryTouch = IsPressed(Device, InputMap.PrimaryTouch); SecondaryButton = IsPressed(Device, InputMap.Secondary); SecondaryTouch = IsPressed(Device, InputMap.SecondaryTouch); MenuButton = IsPressed(Device, InputMap.Menu); JoystickClicked = IsPressed(Device, InputMap.JoystickButton); TrackPadClicked = IsPressed(Device, InputMap.TrackPadButton); JoystickTouch = IsPressed(Device, InputMap.JoystickTouch); TrackPadTouch = IsPressed(Device, InputMap.TrackPadTouch); #if USING_XR_MANAGEMENT Device.TryGetFeatureValue(indexTouch, out TriggerTouch); #else Device.TryGetFeatureValue(legacyIndexTouch, out var temp); TriggerTouch = temp > 0f; #endif } } private readonly InputFeatureUsage indexTouch = new InputFeatureUsage("IndexTouch"); private static InputFeatureUsage legacyIndexTouch = new InputFeatureUsage("IndexTouch"); protected override void AfterInputUpdate() { SetButtonState(HVRButtons.ThumbNearTouch, ref ThumbNearTouchState, ThumbTouch); SetButtonState(HVRButtons.TriggerNearTouch, ref TriggerNearTouchState, TriggerTouch); SetButtonState(HVRButtons.ThumbTouch, ref ThumbTouchState, PrimaryTouch || SecondaryTouch || TrackPadTouch || JoystickTouch); } public bool CheckAdditionalFeature(HVRXRInputFeatures input) { if (input == HVRXRInputFeatures.SecondaryAxis2DUp || input == HVRXRInputFeatures.SecondaryAxis2DDown || input == HVRXRInputFeatures.SecondaryAxis2DLeft || input == HVRXRInputFeatures.SecondaryAxis2DRight) { return IsPressed(Device, InputMap.TrackPadButton); } if (input == HVRXRInputFeatures.PrimaryAxis2DUp || input == HVRXRInputFeatures.PrimaryAxis2DDown || input == HVRXRInputFeatures.PrimaryAxis2DLeft || input == HVRXRInputFeatures.PrimaryAxis2DRight) { return IsPressed(Device, InputMap.TrackPadButton); } return true; } public bool IsPressed(InputDevice device, HVRXRInputFeatures inputFeature, float threshold = 0f) { if ((int)inputFeature >= s_ButtonData.Length) { throw new ArgumentException("[InputHelpers.IsPressed] The value of