| 1 | /************************************************************************************ |
| 2 | Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved. |
| 3 | |
| 4 | Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use |
| 5 | the Utilities SDK except in compliance with the License, which is provided at the time of installation |
| 6 | or download, or which otherwise accompanies this software in either electronic or hard copy form. |
| 7 | |
| 8 | You may obtain a copy of the License at |
| 9 | https://developer.oculus.com/licenses/utilities-1.31 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, the Utilities SDK distributed |
| 12 | under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 13 | ANY KIND, either express or implied. See the License for the specific language governing |
| 14 | permissions and limitations under the License. |
| 15 | ************************************************************************************/ |
| 16 | |
| 17 | using System; |
| 18 | using System.Collections; |
| 19 | using System.Collections.Generic; |
| 20 | using System.Runtime.InteropServices; |
| 21 | using UnityEngine; |
| 22 | |
| 23 | #if UNITY_2017_2_OR_NEWER |
| 24 | using InputTracking = UnityEngine.XR.InputTracking; |
| 25 | using Node = UnityEngine.XR.XRNode; |
| 26 | using Settings = UnityEngine.XR.XRSettings; |
| 27 | #else |
| 28 | using InputTracking = UnityEngine.VR.InputTracking; |
| 29 | using Node = UnityEngine.VR.VRNode; |
| 30 | using Settings = UnityEngine.VR.VRSettings; |
| 31 | #endif |
| 32 | |
| 33 | /// <summary> |
| 34 | /// Provides a unified input system for Oculus controllers and gamepads. |
| 35 | /// </summary> |
| 36 | public static class OVRInput |
| 37 | { |
| 38 | [Flags] |
| 39 | /// Virtual button mappings that allow the same input bindings to work across different controllers. |
| 40 | public enum Button |
| 41 | { |
| 42 | None = 0, ///< Maps to RawButton: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 43 | One = 0x00000001, ///< Maps to RawButton: [Gamepad, Touch, RTouch: A], [LTouch: X], [LTrackedRemote: LTouchpad], [RTrackedRemote: RTouchpad], [Touchpad, Remote: Start] |
| 44 | Two = 0x00000002, ///< Maps to RawButton: [Gamepad, Touch, RTouch: B], [LTouch: Y], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: Back] |
| 45 | Three = 0x00000004, ///< Maps to RawButton: [Gamepad, Touch: X], [LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 46 | Four = 0x00000008, ///< Maps to RawButton: [Gamepad, Touch: Y], [LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 47 | Start = 0x00000100, ///< Maps to RawButton: [Gamepad: Start], [Touch, LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: Start], [RTouch: None] |
| 48 | Back = 0x00000200, ///< Maps to RawButton: [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: Back], [Touch, LTouch, RTouch: None] |
| 49 | PrimaryShoulder = 0x00001000, ///< Maps to RawButton: [Gamepad: LShoulder], [Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 50 | PrimaryIndexTrigger = 0x00002000, ///< Maps to RawButton: [Gamepad, Touch, LTouch, LTrackedRemote: LIndexTrigger], [RTouch, RTrackedRemote: RIndexTrigger], [Touchpad, Remote: None] |
| 51 | PrimaryHandTrigger = 0x00004000, ///< Maps to RawButton: [Touch, LTouch: LHandTrigger], [RTouch: RHandTrigger], [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 52 | PrimaryThumbstick = 0x00008000, ///< Maps to RawButton: [Gamepad, Touch, LTouch: LThumbstick], [RTouch: RThumbstick], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 53 | PrimaryThumbstickUp = 0x00010000, ///< Maps to RawButton: [Gamepad, Touch, LTouch: LThumbstickUp], [RTouch: RThumbstickUp], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 54 | PrimaryThumbstickDown = 0x00020000, ///< Maps to RawButton: [Gamepad, Touch, LTouch: LThumbstickDown], [RTouch: RThumbstickDown], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 55 | PrimaryThumbstickLeft = 0x00040000, ///< Maps to RawButton: [Gamepad, Touch, LTouch: LThumbstickLeft], [RTouch: RThumbstickLeft], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 56 | PrimaryThumbstickRight = 0x00080000, ///< Maps to RawButton: [Gamepad, Touch, LTouch: LThumbstickRight], [RTouch: RThumbstickRight], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 57 | PrimaryTouchpad = 0x00000400, ///< Maps to RawButton: [LTrackedRemote, Touchpad: LTouchpad], [RTrackedRemote: RTouchpad], [Gamepad, Touch, LTouch, RTouch, Remote: None] |
| 58 | SecondaryShoulder = 0x00100000, ///< Maps to RawButton: [Gamepad: RShoulder], [Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 59 | SecondaryIndexTrigger = 0x00200000, ///< Maps to RawButton: [Gamepad, Touch: RIndexTrigger], [LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 60 | SecondaryHandTrigger = 0x00400000, ///< Maps to RawButton: [Touch: RHandTrigger], [Gamepad, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 61 | SecondaryThumbstick = 0x00800000, ///< Maps to RawButton: [Gamepad, Touch: RThumbstick], [LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 62 | SecondaryThumbstickUp = 0x01000000, ///< Maps to RawButton: [Gamepad, Touch: RThumbstickUp], [LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 63 | SecondaryThumbstickDown = 0x02000000, ///< Maps to RawButton: [Gamepad, Touch: RThumbstickDown], [LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 64 | SecondaryThumbstickLeft = 0x04000000, ///< Maps to RawButton: [Gamepad, Touch: RThumbstickLeft], [LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 65 | SecondaryThumbstickRight = 0x08000000, ///< Maps to RawButton: [Gamepad, Touch: RThumbstickRight], [LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 66 | SecondaryTouchpad = 0x00000800, ///< Maps to RawButton: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 67 | DpadUp = 0x00000010, ///< Maps to RawButton: [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: DpadUp], [Touch, LTouch, RTouch: None] |
| 68 | DpadDown = 0x00000020, ///< Maps to RawButton: [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: DpadDown], [Touch, LTouch, RTouch: None] |
| 69 | DpadLeft = 0x00000040, ///< Maps to RawButton: [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: DpadLeft], [Touch, LTouch, RTouch: None] |
| 70 | DpadRight = 0x00000080, ///< Maps to RawButton: [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: DpadRight], [Touch, LTouch, RTouch: None] |
| 71 | Up = 0x10000000, ///< Maps to RawButton: [Gamepad, Touch, LTouch: LThumbstickUp], [RTouch: RThumbstickUp], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: DpadUp] |
| 72 | Down = 0x20000000, ///< Maps to RawButton: [Gamepad, Touch, LTouch: LThumbstickDown], [RTouch: RThumbstickDown], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: DpadDown] |
| 73 | Left = 0x40000000, ///< Maps to RawButton: [Gamepad, Touch, LTouch: LThumbstickLeft], [RTouch: RThumbstickLeft], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: DpadLeft] |
| 74 | Right = unchecked((int)0x80000000),///< Maps to RawButton: [Gamepad, Touch, LTouch: LThumbstickRight], [RTouch: RThumbstickRight], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: DpadRight] |
| 75 | Any = ~None, ///< Maps to RawButton: [Gamepad, Touch, LTouch, RTouch: Any] |
| 76 | } |
| 77 | |
| 78 | [Flags] |
| 79 | /// Raw button mappings that can be used to directly query the state of a controller. |
| 80 | public enum RawButton |
| 81 | { |
| 82 | None = 0, ///< Maps to Physical Button: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 83 | A = 0x00000001, ///< Maps to Physical Button: [Gamepad, Touch, RTouch: A], [LTrackedRemote: LIndexTrigger], [RTrackedRemote: RIndexTrigger], [LTouch, Touchpad, Remote: None] |
| 84 | B = 0x00000002, ///< Maps to Physical Button: [Gamepad, Touch, RTouch: B], [LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 85 | X = 0x00000100, ///< Maps to Physical Button: [Gamepad, Touch, LTouch: X], [RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 86 | Y = 0x00000200, ///< Maps to Physical Button: [Gamepad, Touch, LTouch: Y], [RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 87 | Start = 0x00100000, ///< Maps to Physical Button: [Gamepad, Touch, LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: Start], [RTouch: None] |
| 88 | Back = 0x00200000, ///< Maps to Physical Button: [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: Back], [Touch, LTouch, RTouch: None] |
| 89 | LShoulder = 0x00000800, ///< Maps to Physical Button: [Gamepad: LShoulder], [Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 90 | LIndexTrigger = 0x10000000, ///< Maps to Physical Button: [Gamepad, Touch, LTouch, LTrackedRemote: LIndexTrigger], [RTouch, RTrackedRemote, Touchpad, Remote: None] |
| 91 | LHandTrigger = 0x20000000, ///< Maps to Physical Button: [Touch, LTouch: LHandTrigger], [Gamepad, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 92 | LThumbstick = 0x00000400, ///< Maps to Physical Button: [Gamepad, Touch, LTouch: LThumbstick], [RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 93 | LThumbstickUp = 0x00000010, ///< Maps to Physical Button: [Gamepad, Touch, LTouch: LThumbstickUp], [RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 94 | LThumbstickDown = 0x00000020, ///< Maps to Physical Button: [Gamepad, Touch, LTouch: LThumbstickDown], [RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 95 | LThumbstickLeft = 0x00000040, ///< Maps to Physical Button: [Gamepad, Touch, LTouch: LThumbstickLeft], [RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 96 | LThumbstickRight = 0x00000080, ///< Maps to Physical Button: [Gamepad, Touch, LTouch: LThumbstickRight], [RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 97 | LTouchpad = 0x40000000, ///< Maps to Physical Button: [LTrackedRemote: LTouchpad], [Gamepad, Touch, LTouch, RTouch, RTrackedRemote, Touchpad, Remote: None] |
| 98 | RShoulder = 0x00000008, ///< Maps to Physical Button: [Gamepad: RShoulder], [Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 99 | RIndexTrigger = 0x04000000, ///< Maps to Physical Button: [Gamepad, Touch, RTouch, RTrackedRemote: RIndexTrigger], [LTouch, LTrackedRemote, Touchpad, Remote: None] |
| 100 | RHandTrigger = 0x08000000, ///< Maps to Physical Button: [Touch, RTouch: RHandTrigger], [Gamepad, LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 101 | RThumbstick = 0x00000004, ///< Maps to Physical Button: [Gamepad, Touch, RTouch: RThumbstick], [LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 102 | RThumbstickUp = 0x00001000, ///< Maps to Physical Button: [Gamepad, Touch, RTouch: RThumbstickUp], [LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 103 | RThumbstickDown = 0x00002000, ///< Maps to Physical Button: [Gamepad, Touch, RTouch: RThumbstickDown], [LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 104 | RThumbstickLeft = 0x00004000, ///< Maps to Physical Button: [Gamepad, Touch, RTouch: RThumbstickLeft], [LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 105 | RThumbstickRight = 0x00008000, ///< Maps to Physical Button: [Gamepad, Touch, RTouch: RThumbstickRight], [LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 106 | RTouchpad = unchecked((int)0x80000000),///< Maps to Physical Button: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 107 | DpadUp = 0x00010000, ///< Maps to Physical Button: [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: DpadUp], [Touch, LTouch, RTouch: None] |
| 108 | DpadDown = 0x00020000, ///< Maps to Physical Button: [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: DpadDown], [Touch, LTouch, RTouch: None] |
| 109 | DpadLeft = 0x00040000, ///< Maps to Physical Button: [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: DpadLeft], [Touch, LTouch, RTouch: None] |
| 110 | DpadRight = 0x00080000, ///< Maps to Physical Button: [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: DpadRight], [Touch, LTouch, RTouch: None] |
| 111 | Any = ~None, ///< Maps to Physical Button: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: Any] |
| 112 | } |
| 113 | |
| 114 | [Flags] |
| 115 | /// Virtual capacitive touch mappings that allow the same input bindings to work across different controllers with capacitive touch support. |
| 116 | public enum Touch |
| 117 | { |
| 118 | None = 0, ///< Maps to RawTouch: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 119 | One = Button.One, ///< Maps to RawTouch: [Touch, RTouch: A], [LTouch: X], [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 120 | Two = Button.Two, ///< Maps to RawTouch: [Touch, RTouch: B], [LTouch: Y], [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 121 | Three = Button.Three, ///< Maps to RawTouch: [Touch: X], [Gamepad, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 122 | Four = Button.Four, ///< Maps to RawTouch: [Touch: Y], [Gamepad, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 123 | PrimaryIndexTrigger = Button.PrimaryIndexTrigger, ///< Maps to RawTouch: [Touch, LTouch: LIndexTrigger], [RTouch: RIndexTrigger], [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 124 | PrimaryThumbstick = Button.PrimaryThumbstick, ///< Maps to RawTouch: [Touch, LTouch: LThumbstick], [RTouch: RThumbstick], [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 125 | PrimaryThumbRest = 0x00001000, ///< Maps to RawTouch: [Touch, LTouch: LThumbRest], [RTouch: RThumbRest], [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 126 | PrimaryTouchpad = Button.PrimaryTouchpad, ///< Maps to RawTouch: [LTrackedRemote, Touchpad: LTouchpad], [RTrackedRemote: RTouchpad], [Gamepad, Touch, LTouch, RTouch, Remote: None] |
| 127 | SecondaryIndexTrigger = Button.SecondaryIndexTrigger, ///< Maps to RawTouch: [Touch: RIndexTrigger], [Gamepad, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 128 | SecondaryThumbstick = Button.SecondaryThumbstick, ///< Maps to RawTouch: [Touch: RThumbstick], [Gamepad, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 129 | SecondaryThumbRest = 0x00100000, ///< Maps to RawTouch: [Touch: RThumbRest], [Gamepad, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 130 | SecondaryTouchpad = Button.SecondaryTouchpad, ///< Maps to RawTouch: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 131 | Any = ~None, ///< Maps to RawTouch: [Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad: Any], [Gamepad, Remote: None] |
| 132 | } |
| 133 | |
| 134 | [Flags] |
| 135 | /// Raw capacitive touch mappings that can be used to directly query the state of a controller. |
| 136 | public enum RawTouch |
| 137 | { |
| 138 | None = 0, ///< Maps to Physical Touch: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 139 | A = RawButton.A, ///< Maps to Physical Touch: [Touch, RTouch: A], [Gamepad, LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 140 | B = RawButton.B, ///< Maps to Physical Touch: [Touch, RTouch: B], [Gamepad, LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 141 | X = RawButton.X, ///< Maps to Physical Touch: [Touch, LTouch: X], [Gamepad, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 142 | Y = RawButton.Y, ///< Maps to Physical Touch: [Touch, LTouch: Y], [Gamepad, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 143 | LIndexTrigger = 0x00001000, ///< Maps to Physical Touch: [Touch, LTouch: LIndexTrigger], [Gamepad, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 144 | LThumbstick = RawButton.LThumbstick, ///< Maps to Physical Touch: [Touch, LTouch: LThumbstick], [Gamepad, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 145 | LThumbRest = 0x00000800, ///< Maps to Physical Touch: [Touch, LTouch: LThumbRest], [Gamepad, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 146 | LTouchpad = RawButton.LTouchpad, ///< Maps to Physical Touch: [LTrackedRemote, Touchpad: LTouchpad], [Gamepad, Touch, LTouch, RTouch, RTrackedRemote, Remote: None] |
| 147 | RIndexTrigger = 0x00000010, ///< Maps to Physical Touch: [Touch, RTouch: RIndexTrigger], [Gamepad, LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 148 | RThumbstick = RawButton.RThumbstick, ///< Maps to Physical Touch: [Touch, RTouch: RThumbstick], [Gamepad, LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 149 | RThumbRest = 0x00000008, ///< Maps to Physical Touch: [Touch, RTouch: RThumbRest], [Gamepad, LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 150 | RTouchpad = RawButton.RTouchpad, ///< Maps to Physical Touch: [RTrackedRemote: RTouchpad], [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, Touchpad, Remote: None] |
| 151 | Any = ~None, ///< Maps to Physical Touch: [Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad: Any], [Gamepad, Remote: None] |
| 152 | } |
| 153 | |
| 154 | [Flags] |
| 155 | /// Virtual near touch mappings that allow the same input bindings to work across different controllers with near touch support. |
| 156 | /// A near touch uses the capacitive touch sensors of a controller to detect approximate finger proximity prior to a full touch being reported. |
| 157 | public enum NearTouch |
| 158 | { |
| 159 | None = 0, ///< Maps to RawNearTouch: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 160 | PrimaryIndexTrigger = 0x00000001, ///< Maps to RawNearTouch: [Touch, LTouch: LIndexTrigger], [RTouch: RIndexTrigger], [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 161 | PrimaryThumbButtons = 0x00000002, ///< Maps to RawNearTouch: [Touch, LTouch: LThumbButtons], [RTouch: RThumbButtons], [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 162 | SecondaryIndexTrigger = 0x00000004, ///< Maps to RawNearTouch: [Touch: RIndexTrigger], [Gamepad, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 163 | SecondaryThumbButtons = 0x00000008, ///< Maps to RawNearTouch: [Touch: RThumbButtons], [Gamepad, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 164 | Any = ~None, ///< Maps to RawNearTouch: [Touch, LTouch, RTouch: Any], [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 165 | } |
| 166 | |
| 167 | [Flags] |
| 168 | /// Raw near touch mappings that can be used to directly query the state of a controller. |
| 169 | public enum RawNearTouch |
| 170 | { |
| 171 | None = 0, ///< Maps to Physical NearTouch: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 172 | LIndexTrigger = 0x00000001, ///< Maps to Physical NearTouch: [Touch, LTouch: Implies finger is in close proximity to LIndexTrigger.], [Gamepad, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 173 | LThumbButtons = 0x00000002, ///< Maps to Physical NearTouch: [Touch, LTouch: Implies thumb is in close proximity to LThumbstick OR X/Y buttons.], [Gamepad, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 174 | RIndexTrigger = 0x00000004, ///< Maps to Physical NearTouch: [Touch, RTouch: Implies finger is in close proximity to RIndexTrigger.], [Gamepad, LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 175 | RThumbButtons = 0x00000008, ///< Maps to Physical NearTouch: [Touch, RTouch: Implies thumb is in close proximity to RThumbstick OR A/B buttons.], [Gamepad, LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 176 | Any = ~None, ///< Maps to Physical NearTouch: [Touch, LTouch, RTouch: Any], [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 177 | } |
| 178 | |
| 179 | [Flags] |
| 180 | /// Virtual 1-dimensional axis (float) mappings that allow the same input bindings to work across different controllers. |
| 181 | public enum Axis1D |
| 182 | { |
| 183 | None = 0, ///< Maps to RawAxis1D: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 184 | PrimaryIndexTrigger = 0x01, ///< Maps to RawAxis1D: [Gamepad, Touch, LTouch: LIndexTrigger], [RTouch: RIndexTrigger], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 185 | PrimaryHandTrigger = 0x04, ///< Maps to RawAxis1D: [Touch, LTouch: LHandTrigger], [RTouch: RHandTrigger], [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 186 | SecondaryIndexTrigger = 0x02, ///< Maps to RawAxis1D: [Gamepad, Touch: RIndexTrigger], [LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 187 | SecondaryHandTrigger = 0x08, ///< Maps to RawAxis1D: [Touch: RHandTrigger], [Gamepad, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 188 | Any = ~None, ///< Maps to RawAxis1D: [Gamepad, Touch, LTouch, RTouch: Any], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 189 | } |
| 190 | |
| 191 | [Flags] |
| 192 | /// Raw 1-dimensional axis (float) mappings that can be used to directly query the state of a controller. |
| 193 | public enum RawAxis1D |
| 194 | { |
| 195 | None = 0, ///< Maps to Physical Axis1D: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 196 | LIndexTrigger = 0x01, ///< Maps to Physical Axis1D: [Gamepad, Touch, LTouch: LIndexTrigger], [RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 197 | LHandTrigger = 0x04, ///< Maps to Physical Axis1D: [Touch, LTouch: LHandTrigger], [Gamepad, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 198 | RIndexTrigger = 0x02, ///< Maps to Physical Axis1D: [Gamepad, Touch, RTouch: RIndexTrigger], [LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 199 | RHandTrigger = 0x08, ///< Maps to Physical Axis1D: [Touch, RTouch: RHandTrigger], [Gamepad, LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 200 | Any = ~None, ///< Maps to Physical Axis1D: [Gamepad, Touch, LTouch, RTouch: Any], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 201 | } |
| 202 | |
| 203 | [Flags] |
| 204 | /// Virtual 2-dimensional axis (Vector2) mappings that allow the same input bindings to work across different controllers. |
| 205 | public enum Axis2D |
| 206 | { |
| 207 | None = 0, ///< Maps to RawAxis2D: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 208 | PrimaryThumbstick = 0x01, ///< Maps to RawAxis2D: [Gamepad, Touch, LTouch: LThumbstick], [RTouch: RThumbstick], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 209 | PrimaryTouchpad = 0x04, ///< Maps to RawAxis2D: [LTrackedRemote, Touchpad: LTouchpad], RTrackedRemote: RTouchpad], [Gamepad, Touch, LTouch, RTouch, Remote: None] |
| 210 | SecondaryThumbstick = 0x02, ///< Maps to RawAxis2D: [Gamepad, Touch: RThumbstick], [LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 211 | SecondaryTouchpad = 0x08, ///< Maps to RawAxis2D: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 212 | Any = ~None, ///< Maps to RawAxis2D: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad: Any], [Remote: None] |
| 213 | } |
| 214 | |
| 215 | [Flags] |
| 216 | /// Raw 2-dimensional axis (Vector2) mappings that can be used to directly query the state of a controller. |
| 217 | public enum RawAxis2D |
| 218 | { |
| 219 | None = 0, ///< Maps to Physical Axis2D: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 220 | LThumbstick = 0x01, ///< Maps to Physical Axis2D: [Gamepad, Touch, LTouch: LThumbstick], [RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 221 | LTouchpad = 0x04, ///< Maps to Physical Axis2D: [LTrackedRemote, Touchpad: LTouchpad], [Gamepad, Touch, LTouch, RTouch, RTrackedRemote, Remote: None] |
| 222 | RThumbstick = 0x02, ///< Maps to Physical Axis2D: [Gamepad, Touch, RTouch: RThumbstick], [LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None] |
| 223 | RTouchpad = 0x08, ///< Maps to Physical Axis2D: [RTrackedRemote: RTouchpad], [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, Touchpad, Remote: None] |
| 224 | Any = ~None, ///< Maps to Physical Axis2D: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote: Any], [Touchpad, Remote: None] |
| 225 | } |
| 226 | |
| 227 | [Flags] |
| 228 | /// OpenVR Controller State Enum |
| 229 | public enum OpenVRButton : ulong |
| 230 | { |
| 231 | None = 0, |
| 232 | Two = 0x0002, |
| 233 | Thumbstick = 0x100000000, |
| 234 | Grip = 0x0004, |
| 235 | } |
| 236 | |
| 237 | [Flags] |
| 238 | /// Identifies a controller which can be used to query the virtual or raw input state. |
| 239 | public enum Controller |
| 240 | { |
| 241 | None = OVRPlugin.Controller.None, ///< Null controller. |
| 242 | LTouch = OVRPlugin.Controller.LTouch, ///< Left Oculus Touch controller. Virtual input mapping differs from the combined L/R Touch mapping. |
| 243 | RTouch = OVRPlugin.Controller.RTouch, ///< Right Oculus Touch controller. Virtual input mapping differs from the combined L/R Touch mapping. |
| 244 | Touch = OVRPlugin.Controller.Touch, ///< Combined Left/Right pair of Oculus Touch controllers. |
| 245 | Remote = OVRPlugin.Controller.Remote, ///< Oculus Remote controller. |
| 246 | Gamepad = OVRPlugin.Controller.Gamepad, ///< Xbox 360 or Xbox One gamepad on PC. Generic gamepad on Android. |
| 247 | Touchpad = OVRPlugin.Controller.Touchpad, ///< GearVR touchpad on Android. |
| 248 | LTrackedRemote = OVRPlugin.Controller.LTrackedRemote, ///< Left GearVR tracked remote on Android. |
| 249 | RTrackedRemote = OVRPlugin.Controller.RTrackedRemote, ///< Right GearVR tracked remote on Android. |
| 250 | Active = OVRPlugin.Controller.Active, ///< Default controller. Represents the controller that most recently registered a button press from the user. |
| 251 | All = OVRPlugin.Controller.All, ///< Represents the logical OR of all controllers. |
| 252 | } |
| 253 | |
| 254 | public enum Handedness |
| 255 | { |
| 256 | Unsupported = OVRPlugin.Handedness.Unsupported, |
| 257 | LeftHanded = OVRPlugin.Handedness.LeftHanded, |
| 258 | RightHanded = OVRPlugin.Handedness.RightHanded, |
| 259 | } |
| 260 | |
| 261 | private static readonly float AXIS_AS_BUTTON_THRESHOLD = 0.5f; |
| 262 | private static readonly float AXIS_DEADZONE_THRESHOLD = 0.2f; |
| 263 | private static List<OVRControllerBase> controllers; |
| 264 | private static Controller activeControllerType = Controller.None; |
| 265 | private static Controller connectedControllerTypes = Controller.None; |
| 266 | private static OVRPlugin.Step stepType = OVRPlugin.Step.Render; |
| 267 | private static int fixedUpdateCount = 0; |
| 268 | |
| 269 | |
| 270 | private static bool _pluginSupportsActiveController = false; |
| 271 | private static bool _pluginSupportsActiveControllerCached = false; |
| 272 | private static System.Version _pluginSupportsActiveControllerMinVersion = new System.Version(1, 9, 0); |
| 273 | private static bool pluginSupportsActiveController |
| 274 | { |
| 275 | get |
| 276 | { |
| 277 | if (!_pluginSupportsActiveControllerCached) |
| 278 | { |
| 279 | bool isSupportedPlatform = true; |
| 280 | #if (UNITY_ANDROID && !UNITY_EDITOR) || UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX |
| 281 | isSupportedPlatform = false; |
| 282 | #endif |
| 283 | _pluginSupportsActiveController = isSupportedPlatform && (OVRPlugin.version >= _pluginSupportsActiveControllerMinVersion); |
| 284 | _pluginSupportsActiveControllerCached = true; |
| 285 | } |
| 286 | |
| 287 | return _pluginSupportsActiveController; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /// <summary> |
| 292 | /// Creates an instance of OVRInput. |
| 293 | /// </summary> |
| 294 | static OVRInput() |
| 295 | { |
| 296 | controllers = new List<OVRControllerBase> |
| 297 | { |
| 298 | #if UNITY_ANDROID && !UNITY_EDITOR |
| 299 | new OVRControllerGamepadAndroid(), |
| 300 | new OVRControllerTouchpad(), |
| 301 | new OVRControllerLTrackedRemote(), |
| 302 | new OVRControllerRTrackedRemote(), |
| 303 | new OVRControllerTouch(), |
| 304 | new OVRControllerLTouch(), |
| 305 | new OVRControllerRTouch(), |
| 306 | #elif UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX |
| 307 | new OVRControllerGamepadMac(), |
| 308 | #else |
| 309 | new OVRControllerGamepadPC(), |
| 310 | new OVRControllerTouch(), |
| 311 | new OVRControllerLTouch(), |
| 312 | new OVRControllerRTouch(), |
| 313 | new OVRControllerRemote(), |
| 314 | #endif |
| 315 | }; |
| 316 | |
| 317 | InitHapticInfo(); |
| 318 | } |
| 319 | |
| 320 | /// <summary> |
| 321 | /// Updates the internal state of OVRInput. Must be called manually if used independently from OVRManager. |
| 322 | /// </summary> |
| 323 | public static void Update() |
| 324 | { |
| 325 | connectedControllerTypes = Controller.None; |
| 326 | stepType = OVRPlugin.Step.Render; |
| 327 | fixedUpdateCount = 0; |
| 328 | |
| 329 | if (OVRManager.loadedXRDevice == OVRManager.XRDevice.OpenVR) |
| 330 | { |
| 331 | UpdateXRControllerNodeIds(); |
| 332 | UpdateXRControllerHaptics(); |
| 333 | } |
| 334 | |
| 335 | for (int i = 0; i < controllers.Count; i++) |
| 336 | { |
| 337 | OVRControllerBase controller = controllers[i]; |
| 338 | |
| 339 | connectedControllerTypes |= controller.Update(); |
| 340 | |
| 341 | if ((connectedControllerTypes & controller.controllerType) != 0) |
| 342 | { |
| 343 | RawButton rawButtonMask = RawButton.Any; |
| 344 | RawTouch rawTouchMask = RawTouch.Any; |
| 345 | |
| 346 | if (Get(rawButtonMask, controller.controllerType) |
| 347 | || Get(rawTouchMask, controller.controllerType)) |
| 348 | { |
| 349 | activeControllerType = controller.controllerType; |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | if ((activeControllerType == Controller.LTouch) || (activeControllerType == Controller.RTouch)) |
| 355 | { |
| 356 | if ((connectedControllerTypes & Controller.Touch) == Controller.Touch) |
| 357 | { |
| 358 | // If either Touch controller is Active and both Touch controllers are connected, set both to Active. |
| 359 | activeControllerType = Controller.Touch; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | if ((connectedControllerTypes & activeControllerType) == 0) |
| 364 | { |
| 365 | activeControllerType = Controller.None; |
| 366 | } |
| 367 | |
| 368 | // Promote TrackedRemote to Active if one is connected and no other controller is active |
| 369 | if (activeControllerType == Controller.None) |
| 370 | { |
| 371 | if ((connectedControllerTypes & Controller.RTrackedRemote) != 0) |
| 372 | { |
| 373 | activeControllerType = Controller.RTrackedRemote; |
| 374 | } |
| 375 | else if ((connectedControllerTypes & Controller.LTrackedRemote) != 0) |
| 376 | { |
| 377 | activeControllerType = Controller.LTrackedRemote; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | if ( OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus && pluginSupportsActiveController) |
| 382 | { |
| 383 | // override locally derived active and connected controllers if plugin provides more accurate data |
| 384 | connectedControllerTypes = (OVRInput.Controller)OVRPlugin.GetConnectedControllers(); |
| 385 | activeControllerType = (OVRInput.Controller)OVRPlugin.GetActiveController(); |
| 386 | } |
| 387 | else if (OVRManager.loadedXRDevice == OVRManager.XRDevice.OpenVR) |
| 388 | { |
| 389 | activeControllerType = connectedControllerTypes; |
| 390 | } |
| 391 | |
| 392 | } |
| 393 | |
| 394 | /// <summary> |
| 395 | /// Updates the internal physics state of OVRInput. Must be called manually if used independently from OVRManager. |
| 396 | /// </summary> |
| 397 | public static void FixedUpdate() |
| 398 | { |
| 399 | stepType = OVRPlugin.Step.Physics; |
| 400 | |
| 401 | double predictionSeconds = (double)fixedUpdateCount * Time.fixedDeltaTime / Mathf.Max(Time.timeScale, 1e-6f); |
| 402 | fixedUpdateCount++; |
| 403 | |
| 404 | OVRPlugin.UpdateNodePhysicsPoses(0, predictionSeconds); |
| 405 | } |
| 406 | |
| 407 | /// <summary> |
| 408 | /// Returns true if the given Controller's orientation is currently tracked. |
| 409 | /// Only supported for Oculus LTouch and RTouch controllers. Non-tracked controllers will return false. |
| 410 | /// </summary> |
| 411 | public static bool GetControllerOrientationTracked(OVRInput.Controller controllerType) |
| 412 | { |
| 413 | switch (controllerType) |
| 414 | { |
| 415 | case Controller.LTouch: |
| 416 | case Controller.LTrackedRemote: |
| 417 | return OVRPlugin.GetNodeOrientationTracked(OVRPlugin.Node.HandLeft); |
| 418 | case Controller.RTouch: |
| 419 | case Controller.RTrackedRemote: |
| 420 | return OVRPlugin.GetNodeOrientationTracked(OVRPlugin.Node.HandRight); |
| 421 | default: |
| 422 | return false; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | /// <summary> |
| 427 | /// Returns true if the given Controller's orientation is currently valid. |
| 428 | /// Only supported for Oculus LTouch and RTouch controllers. Non-tracked controllers will return false. |
| 429 | /// </summary> |
| 430 | public static bool GetControllerOrientationValid(OVRInput.Controller controllerType) |
| 431 | { |
| 432 | switch (controllerType) |
| 433 | { |
| 434 | case Controller.LTouch: |
| 435 | case Controller.LTrackedRemote: |
| 436 | return OVRPlugin.GetNodeOrientationValid(OVRPlugin.Node.HandLeft); |
| 437 | case Controller.RTouch: |
| 438 | case Controller.RTrackedRemote: |
| 439 | return OVRPlugin.GetNodeOrientationValid(OVRPlugin.Node.HandRight); |
| 440 | default: |
| 441 | return false; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | |
| 446 | /// <summary> |
| 447 | /// Returns true if the given Controller's position is currently tracked. |
| 448 | /// Only supported for Oculus LTouch and RTouch controllers. Non-tracked controllers will return false. |
| 449 | /// </summary> |
| 450 | public static bool GetControllerPositionTracked(OVRInput.Controller controllerType) |
| 451 | { |
| 452 | switch (controllerType) |
| 453 | { |
| 454 | case Controller.LTouch: |
| 455 | case Controller.LTrackedRemote: |
| 456 | return OVRPlugin.GetNodePositionTracked(OVRPlugin.Node.HandLeft); |
| 457 | case Controller.RTouch: |
| 458 | case Controller.RTrackedRemote: |
| 459 | return OVRPlugin.GetNodePositionTracked(OVRPlugin.Node.HandRight); |
| 460 | default: |
| 461 | return false; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | /// <summary> |
| 466 | /// Returns true if the given Controller's position is currently valid. |
| 467 | /// Only supported for Oculus LTouch and RTouch controllers. Non-tracked controllers will return false. |
| 468 | /// </summary> |
| 469 | public static bool GetControllerPositionValid(OVRInput.Controller controllerType) |
| 470 | { |
| 471 | switch (controllerType) |
| 472 | { |
| 473 | case Controller.LTouch: |
| 474 | case Controller.LTrackedRemote: |
| 475 | return OVRPlugin.GetNodePositionValid(OVRPlugin.Node.HandLeft); |
| 476 | case Controller.RTouch: |
| 477 | case Controller.RTrackedRemote: |
| 478 | return OVRPlugin.GetNodePositionValid(OVRPlugin.Node.HandRight); |
| 479 | default: |
| 480 | return false; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | /// <summary> |
| 485 | /// Gets the position of the given Controller local to its tracking space. |
| 486 | /// Only supported for Oculus LTouch and RTouch controllers. Non-tracked controllers will return Vector3.zero. |
| 487 | /// </summary> |
| 488 | public static Vector3 GetLocalControllerPosition(OVRInput.Controller controllerType) |
| 489 | { |
| 490 | switch (controllerType) |
| 491 | { |
| 492 | case Controller.LTouch: |
| 493 | case Controller.LTrackedRemote: |
| 494 | if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus) |
| 495 | return OVRPlugin.GetNodePose(OVRPlugin.Node.HandLeft, stepType).ToOVRPose().position; |
| 496 | else if (OVRManager.loadedXRDevice == OVRManager.XRDevice.OpenVR) |
| 497 | return openVRControllerDetails[0].localPosition; |
| 498 | else |
| 499 | { |
| 500 | Vector3 retVec; |
| 501 | if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.LeftHand, NodeStatePropertyType.Position, OVRPlugin.Node.HandLeft, stepType, out retVec)) |
| 502 | return retVec; |
| 503 | return Vector3.zero; //Will never be hit, but is a final fallback. |
| 504 | } |
| 505 | case Controller.RTouch: |
| 506 | case Controller.RTrackedRemote: |
| 507 | if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus) |
| 508 | return OVRPlugin.GetNodePose(OVRPlugin.Node.HandRight, stepType).ToOVRPose().position; |
| 509 | else if (OVRManager.loadedXRDevice == OVRManager.XRDevice.OpenVR) |
| 510 | return openVRControllerDetails[1].localPosition; |
| 511 | else |
| 512 | { |
| 513 | Vector3 retVec; |
| 514 | if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.RightHand, NodeStatePropertyType.Position, OVRPlugin.Node.HandRight, stepType, out retVec)) |
| 515 | return retVec; |
| 516 | return Vector3.zero; |
| 517 | } |
| 518 | default: |
| 519 | return Vector3.zero; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | /// <summary> |
| 524 | /// Gets the linear velocity of the given Controller local to its tracking space. |
| 525 | /// Only supported for Oculus LTouch and RTouch controllers. Non-tracked controllers will return Vector3.zero. |
| 526 | /// </summary> |
| 527 | public static Vector3 GetLocalControllerVelocity(OVRInput.Controller controllerType) |
| 528 | { |
| 529 | Vector3 velocity = Vector3.zero; |
| 530 | |
| 531 | switch (controllerType) |
| 532 | { |
| 533 | case Controller.LTouch: |
| 534 | case Controller.LTrackedRemote: |
| 535 | if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.LeftHand, NodeStatePropertyType.Velocity, OVRPlugin.Node.HandLeft, stepType, out velocity)) |
| 536 | { |
| 537 | return velocity; |
| 538 | } |
| 539 | else |
| 540 | { |
| 541 | return Vector3.zero; |
| 542 | } |
| 543 | case Controller.RTouch: |
| 544 | case Controller.RTrackedRemote: |
| 545 | if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.RightHand, NodeStatePropertyType.Velocity, OVRPlugin.Node.HandRight, stepType, out velocity)) |
| 546 | { |
| 547 | return velocity; |
| 548 | } |
| 549 | else |
| 550 | { |
| 551 | return Vector3.zero; |
| 552 | } |
| 553 | default: |
| 554 | return Vector3.zero; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | /// <summary> |
| 559 | /// Gets the linear acceleration of the given Controller local to its tracking space. |
| 560 | /// Only supported for Oculus LTouch and RTouch controllers. Non-tracked controllers will return Vector3.zero. |
| 561 | /// </summary> |
| 562 | public static Vector3 GetLocalControllerAcceleration(OVRInput.Controller controllerType) |
| 563 | { |
| 564 | Vector3 accel = Vector3.zero; |
| 565 | |
| 566 | switch (controllerType) |
| 567 | { |
| 568 | case Controller.LTouch: |
| 569 | case Controller.LTrackedRemote: |
| 570 | if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.LeftHand, NodeStatePropertyType.Acceleration, OVRPlugin.Node.HandLeft, stepType, out accel)) |
| 571 | { |
| 572 | return accel; |
| 573 | } |
| 574 | else |
| 575 | { |
| 576 | return Vector3.zero; |
| 577 | } |
| 578 | case Controller.RTouch: |
| 579 | case Controller.RTrackedRemote: |
| 580 | if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.RightHand, NodeStatePropertyType.Acceleration, OVRPlugin.Node.HandRight, stepType, out accel)) |
| 581 | { |
| 582 | return accel; |
| 583 | } |
| 584 | else |
| 585 | { |
| 586 | return Vector3.zero; |
| 587 | } |
| 588 | default: |
| 589 | return Vector3.zero; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | /// <summary> |
| 594 | /// Gets the rotation of the given Controller local to its tracking space. |
| 595 | /// Only supported for Oculus LTouch and RTouch controllers. Non-tracked controllers will return Quaternion.identity. |
| 596 | /// </summary> |
| 597 | public static Quaternion GetLocalControllerRotation(OVRInput.Controller controllerType) |
| 598 | { |
| 599 | switch (controllerType) |
| 600 | { |
| 601 | case Controller.LTouch: |
| 602 | case Controller.LTrackedRemote: |
| 603 | if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus) |
| 604 | return OVRPlugin.GetNodePose(OVRPlugin.Node.HandLeft, stepType).ToOVRPose().orientation; |
| 605 | else if (OVRManager.loadedXRDevice == OVRManager.XRDevice.OpenVR) |
| 606 | return openVRControllerDetails[0].localOrientation; |
| 607 | else |
| 608 | { |
| 609 | Quaternion retQuat; |
| 610 | if (OVRNodeStateProperties.GetNodeStatePropertyQuaternion(Node.LeftHand, NodeStatePropertyType.Orientation, OVRPlugin.Node.HandLeft, stepType, out retQuat)) |
| 611 | return retQuat; |
| 612 | return Quaternion.identity; |
| 613 | } |
| 614 | case Controller.RTouch: |
| 615 | case Controller.RTrackedRemote: |
| 616 | if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus) |
| 617 | return OVRPlugin.GetNodePose(OVRPlugin.Node.HandRight, stepType).ToOVRPose().orientation; |
| 618 | else if (OVRManager.loadedXRDevice == OVRManager.XRDevice.OpenVR) |
| 619 | return openVRControllerDetails[1].localOrientation; |
| 620 | else |
| 621 | { |
| 622 | Quaternion retQuat; |
| 623 | if (OVRNodeStateProperties.GetNodeStatePropertyQuaternion(Node.RightHand, NodeStatePropertyType.Orientation, OVRPlugin.Node.HandRight, stepType, out retQuat)) |
| 624 | return retQuat; |
| 625 | return Quaternion.identity; |
| 626 | } |
| 627 | default: |
| 628 | return Quaternion.identity; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | /// <summary> |
| 633 | /// Gets the angular velocity of the given Controller local to its tracking space in radians per second around each axis. |
| 634 | /// Only supported for Oculus LTouch and RTouch controllers. Non-tracked controllers will return Vector3.zero. |
| 635 | /// </summary> |
| 636 | public static Vector3 GetLocalControllerAngularVelocity(OVRInput.Controller controllerType) |
| 637 | { |
| 638 | Vector3 velocity = Vector3.zero; |
| 639 | |
| 640 | switch (controllerType) |
| 641 | { |
| 642 | case Controller.LTouch: |
| 643 | case Controller.LTrackedRemote: |
| 644 | if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.LeftHand, NodeStatePropertyType.AngularVelocity, OVRPlugin.Node.HandLeft, stepType, out velocity)) |
| 645 | { |
| 646 | return velocity; |
| 647 | } |
| 648 | else |
| 649 | { |
| 650 | return Vector3.zero; |
| 651 | } |
| 652 | case Controller.RTouch: |
| 653 | case Controller.RTrackedRemote: |
| 654 | if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.RightHand, NodeStatePropertyType.AngularVelocity, OVRPlugin.Node.HandRight, stepType, out velocity)) |
| 655 | { |
| 656 | return velocity; |
| 657 | } |
| 658 | else |
| 659 | { |
| 660 | return Vector3.zero; |
| 661 | } |
| 662 | default: |
| 663 | return Vector3.zero; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | /// <summary> |
| 668 | /// Gets the angular acceleration of the given Controller local to its tracking space in radians per second per second around each axis. |
| 669 | /// Only supported for Oculus LTouch and RTouch controllers. Non-tracked controllers will return Vector3.zero. |
| 670 | /// </summary> |
| 671 | public static Vector3 GetLocalControllerAngularAcceleration(OVRInput.Controller controllerType) |
| 672 | { |
| 673 | Vector3 accel = Vector3.zero; |
| 674 | |
| 675 | switch (controllerType) |
| 676 | { |
| 677 | case Controller.LTouch: |
| 678 | case Controller.LTrackedRemote: |
| 679 | if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.LeftHand, NodeStatePropertyType.AngularAcceleration, OVRPlugin.Node.HandLeft, stepType, out accel)) |
| 680 | { |
| 681 | return accel; |
| 682 | } |
| 683 | else |
| 684 | { |
| 685 | return Vector3.zero; |
| 686 | } |
| 687 | case Controller.RTouch: |
| 688 | case Controller.RTrackedRemote: |
| 689 | if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.RightHand, NodeStatePropertyType.AngularAcceleration, OVRPlugin.Node.HandRight, stepType, out accel)) |
| 690 | { |
| 691 | return accel; |
| 692 | } |
| 693 | else |
| 694 | { |
| 695 | return Vector3.zero; |
| 696 | } |
| 697 | default: |
| 698 | return Vector3.zero; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | /// <summary> |
| 703 | /// Gets the dominant hand that the user has specified in settings, for mobile devices. |
| 704 | /// </summary> |
| 705 | public static Handedness GetDominantHand() |
| 706 | { |
| 707 | return (Handedness) OVRPlugin.GetDominantHand(); |
| 708 | } |
| 709 | |
| 710 | /// <summary> |
| 711 | /// Gets the current state of the given virtual button mask with the given controller mask. |
| 712 | /// Returns true if any masked button is down on any masked controller. |
| 713 | /// </summary> |
| 714 | public static bool Get(Button virtualMask, Controller controllerMask = Controller.Active) |
| 715 | { |
| 716 | return GetResolvedButton(virtualMask, RawButton.None, controllerMask); |
| 717 | } |
| 718 | |
| 719 | /// <summary> |
| 720 | /// Gets the current state of the given raw button mask with the given controller mask. |
| 721 | /// Returns true if any masked button is down on any masked controllers. |
| 722 | /// </summary> |
| 723 | public static bool Get(RawButton rawMask, Controller controllerMask = Controller.Active) |
| 724 | { |
| 725 | return GetResolvedButton(Button.None, rawMask, controllerMask); |
| 726 | } |
| 727 | |
| 728 | private static bool GetResolvedButton(Button virtualMask, RawButton rawMask, Controller controllerMask) |
| 729 | { |
| 730 | if ((controllerMask & Controller.Active) != 0) |
| 731 | controllerMask |= activeControllerType; |
| 732 | |
| 733 | for (int i = 0; i < controllers.Count; i++) |
| 734 | { |
| 735 | OVRControllerBase controller = controllers[i]; |
| 736 | |
| 737 | if (ShouldResolveController(controller.controllerType, controllerMask)) |
| 738 | { |
| 739 | RawButton resolvedMask = rawMask | controller.ResolveToRawMask(virtualMask); |
| 740 | |
| 741 | if (((RawButton)controller.currentState.Buttons & resolvedMask) != 0) |
| 742 | { |
| 743 | return true; |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | return false; |
| 749 | } |
| 750 | |
| 751 | /// <summary> |
| 752 | /// Gets the current down state of the given virtual button mask with the given controller mask. |
| 753 | /// Returns true if any masked button was pressed this frame on any masked controller and no masked button was previously down last frame. |
| 754 | /// </summary> |
| 755 | public static bool GetDown(Button virtualMask, Controller controllerMask = Controller.Active) |
| 756 | { |
| 757 | return GetResolvedButtonDown(virtualMask, RawButton.None, controllerMask); |
| 758 | } |
| 759 | |
| 760 | /// <summary> |
| 761 | /// Gets the current down state of the given raw button mask with the given controller mask. |
| 762 | /// Returns true if any masked button was pressed this frame on any masked controller and no masked button was previously down last frame. |
| 763 | /// </summary> |
| 764 | public static bool GetDown(RawButton rawMask, Controller controllerMask = Controller.Active) |
| 765 | { |
| 766 | return GetResolvedButtonDown(Button.None, rawMask, controllerMask); |
| 767 | } |
| 768 | |
| 769 | private static bool GetResolvedButtonDown(Button virtualMask, RawButton rawMask, Controller controllerMask) |
| 770 | { |
| 771 | bool down = false; |
| 772 | |
| 773 | if ((controllerMask & Controller.Active) != 0) |
| 774 | controllerMask |= activeControllerType; |
| 775 | |
| 776 | for (int i = 0; i < controllers.Count; i++) |
| 777 | { |
| 778 | OVRControllerBase controller = controllers[i]; |
| 779 | |
| 780 | if (ShouldResolveController(controller.controllerType, controllerMask)) |
| 781 | { |
| 782 | RawButton resolvedMask = rawMask | controller.ResolveToRawMask(virtualMask); |
| 783 | |
| 784 | if (((RawButton)controller.previousState.Buttons & resolvedMask) != 0) |
| 785 | { |
| 786 | return false; |
| 787 | } |
| 788 | |
| 789 | if ((((RawButton)controller.currentState.Buttons & resolvedMask) != 0) |
| 790 | && (((RawButton)controller.previousState.Buttons & resolvedMask) == 0)) |
| 791 | { |
| 792 | down = true; |
| 793 | } |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | return down; |
| 798 | } |
| 799 | |
| 800 | /// <summary> |
| 801 | /// Gets the current up state of the given virtual button mask with the given controller mask. |
| 802 | /// Returns true if any masked button was released this frame on any masked controller and no other masked button is still down this frame. |
| 803 | /// </summary> |
| 804 | public static bool GetUp(Button virtualMask, Controller controllerMask = Controller.Active) |
| 805 | { |
| 806 | return GetResolvedButtonUp(virtualMask, RawButton.None, controllerMask); |
| 807 | } |
| 808 | |
| 809 | /// <summary> |
| 810 | /// Gets the current up state of the given raw button mask with the given controller mask. |
| 811 | /// Returns true if any masked button was released this frame on any masked controller and no other masked button is still down this frame. |
| 812 | /// </summary> |
| 813 | public static bool GetUp(RawButton rawMask, Controller controllerMask = Controller.Active) |
| 814 | { |
| 815 | return GetResolvedButtonUp(Button.None, rawMask, controllerMask); |
| 816 | } |
| 817 | |
| 818 | private static bool GetResolvedButtonUp(Button virtualMask, RawButton rawMask, Controller controllerMask) |
| 819 | { |
| 820 | bool up = false; |
| 821 | |
| 822 | if ((controllerMask & Controller.Active) != 0) |
| 823 | controllerMask |= activeControllerType; |
| 824 | |
| 825 | for (int i = 0; i < controllers.Count; i++) |
| 826 | { |
| 827 | OVRControllerBase controller = controllers[i]; |
| 828 | |
| 829 | if (ShouldResolveController(controller.controllerType, controllerMask)) |
| 830 | { |
| 831 | RawButton resolvedMask = rawMask | controller.ResolveToRawMask(virtualMask); |
| 832 | |
| 833 | if (((RawButton)controller.currentState.Buttons & resolvedMask) != 0) |
| 834 | { |
| 835 | return false; |
| 836 | } |
| 837 | |
| 838 | if ((((RawButton)controller.currentState.Buttons & resolvedMask) == 0) |
| 839 | && (((RawButton)controller.previousState.Buttons & resolvedMask) != 0)) |
| 840 | { |
| 841 | up = true; |
| 842 | } |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | return up; |
| 847 | } |
| 848 | |
| 849 | /// <summary> |
| 850 | /// Gets the current state of the given virtual touch mask with the given controller mask. |
| 851 | /// Returns true if any masked touch is down on any masked controller. |
| 852 | /// </summary> |
| 853 | public static bool Get(Touch virtualMask, Controller controllerMask = Controller.Active) |
| 854 | { |
| 855 | return GetResolvedTouch(virtualMask, RawTouch.None, controllerMask); |
| 856 | } |
| 857 | |
| 858 | /// <summary> |
| 859 | /// Gets the current state of the given raw touch mask with the given controller mask. |
| 860 | /// Returns true if any masked touch is down on any masked controllers. |
| 861 | /// </summary> |
| 862 | public static bool Get(RawTouch rawMask, Controller controllerMask = Controller.Active) |
| 863 | { |
| 864 | return GetResolvedTouch(Touch.None, rawMask, controllerMask); |
| 865 | } |
| 866 | |
| 867 | private static bool GetResolvedTouch(Touch virtualMask, RawTouch rawMask, Controller controllerMask) |
| 868 | { |
| 869 | if ((controllerMask & Controller.Active) != 0) |
| 870 | controllerMask |= activeControllerType; |
| 871 | |
| 872 | for (int i = 0; i < controllers.Count; i++) |
| 873 | { |
| 874 | OVRControllerBase controller = controllers[i]; |
| 875 | |
| 876 | if (ShouldResolveController(controller.controllerType, controllerMask)) |
| 877 | { |
| 878 | RawTouch resolvedMask = rawMask | controller.ResolveToRawMask(virtualMask); |
| 879 | |
| 880 | if (((RawTouch)controller.currentState.Touches & resolvedMask) != 0) |
| 881 | { |
| 882 | return true; |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | return false; |
| 888 | } |
| 889 | |
| 890 | /// <summary> |
| 891 | /// Gets the current down state of the given virtual touch mask with the given controller mask. |
| 892 | /// Returns true if any masked touch was pressed this frame on any masked controller and no masked touch was previously down last frame. |
| 893 | /// </summary> |
| 894 | public static bool GetDown(Touch virtualMask, Controller controllerMask = Controller.Active) |
| 895 | { |
| 896 | return GetResolvedTouchDown(virtualMask, RawTouch.None, controllerMask); |
| 897 | } |
| 898 | |
| 899 | /// <summary> |
| 900 | /// Gets the current down state of the given raw touch mask with the given controller mask. |
| 901 | /// Returns true if any masked touch was pressed this frame on any masked controller and no masked touch was previously down last frame. |
| 902 | /// </summary> |
| 903 | public static bool GetDown(RawTouch rawMask, Controller controllerMask = Controller.Active) |
| 904 | { |
| 905 | return GetResolvedTouchDown(Touch.None, rawMask, controllerMask); |
| 906 | } |
| 907 | |
| 908 | private static bool GetResolvedTouchDown(Touch virtualMask, RawTouch rawMask, Controller controllerMask) |
| 909 | { |
| 910 | bool down = false; |
| 911 | |
| 912 | if ((controllerMask & Controller.Active) != 0) |
| 913 | controllerMask |= activeControllerType; |
| 914 | |
| 915 | for (int i = 0; i < controllers.Count; i++) |
| 916 | { |
| 917 | OVRControllerBase controller = controllers[i]; |
| 918 | |
| 919 | if (ShouldResolveController(controller.controllerType, controllerMask)) |
| 920 | { |
| 921 | RawTouch resolvedMask = rawMask | controller.ResolveToRawMask(virtualMask); |
| 922 | |
| 923 | if (((RawTouch)controller.previousState.Touches & resolvedMask) != 0) |
| 924 | { |
| 925 | return false; |
| 926 | } |
| 927 | |
| 928 | if ((((RawTouch)controller.currentState.Touches & resolvedMask) != 0) |
| 929 | && (((RawTouch)controller.previousState.Touches & resolvedMask) == 0)) |
| 930 | { |
| 931 | down = true; |
| 932 | } |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | return down; |
| 937 | } |
| 938 | |
| 939 | /// <summary> |
| 940 | /// Gets the current up state of the given virtual touch mask with the given controller mask. |
| 941 | /// Returns true if any masked touch was released this frame on any masked controller and no other masked touch is still down this frame. |
| 942 | /// </summary> |
| 943 | public static bool GetUp(Touch virtualMask, Controller controllerMask = Controller.Active) |
| 944 | { |
| 945 | return GetResolvedTouchUp(virtualMask, RawTouch.None, controllerMask); |
| 946 | } |
| 947 | |
| 948 | /// <summary> |
| 949 | /// Gets the current up state of the given raw touch mask with the given controller mask. |
| 950 | /// Returns true if any masked touch was released this frame on any masked controller and no other masked touch is still down this frame. |
| 951 | /// </summary> |
| 952 | public static bool GetUp(RawTouch rawMask, Controller controllerMask = Controller.Active) |
| 953 | { |
| 954 | return GetResolvedTouchUp(Touch.None, rawMask, controllerMask); |
| 955 | } |
| 956 | |
| 957 | private static bool GetResolvedTouchUp(Touch virtualMask, RawTouch rawMask, Controller controllerMask) |
| 958 | { |
| 959 | bool up = false; |
| 960 | |
| 961 | if ((controllerMask & Controller.Active) != 0) |
| 962 | controllerMask |= activeControllerType; |
| 963 | |
| 964 | for (int i = 0; i < controllers.Count; i++) |
| 965 | { |
| 966 | OVRControllerBase controller = controllers[i]; |
| 967 | |
| 968 | if (ShouldResolveController(controller.controllerType, controllerMask)) |
| 969 | { |
| 970 | RawTouch resolvedMask = rawMask | controller.ResolveToRawMask(virtualMask); |
| 971 | |
| 972 | if (((RawTouch)controller.currentState.Touches & resolvedMask) != 0) |
| 973 | { |
| 974 | return false; |
| 975 | } |
| 976 | |
| 977 | if ((((RawTouch)controller.currentState.Touches & resolvedMask) == 0) |
| 978 | && (((RawTouch)controller.previousState.Touches & resolvedMask) != 0)) |
| 979 | { |
| 980 | up = true; |
| 981 | } |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | return up; |
| 986 | } |
| 987 | |
| 988 | /// <summary> |
| 989 | /// Gets the current state of the given virtual near touch mask with the given controller mask. |
| 990 | /// Returns true if any masked near touch is down on any masked controller. |
| 991 | /// </summary> |
| 992 | public static bool Get(NearTouch virtualMask, Controller controllerMask = Controller.Active) |
| 993 | { |
| 994 | return GetResolvedNearTouch(virtualMask, RawNearTouch.None, controllerMask); |
| 995 | } |
| 996 | |
| 997 | /// <summary> |
| 998 | /// Gets the current state of the given raw near touch mask with the given controller mask. |
| 999 | /// Returns true if any masked near touch is down on any masked controllers. |
| 1000 | /// </summary> |
| 1001 | public static bool Get(RawNearTouch rawMask, Controller controllerMask = Controller.Active) |
| 1002 | { |
| 1003 | return GetResolvedNearTouch(NearTouch.None, rawMask, controllerMask); |
| 1004 | } |
| 1005 | |
| 1006 | private static bool GetResolvedNearTouch(NearTouch virtualMask, RawNearTouch rawMask, Controller controllerMask) |
| 1007 | { |
| 1008 | if ((controllerMask & Controller.Active) != 0) |
| 1009 | controllerMask |= activeControllerType; |
| 1010 | |
| 1011 | for (int i = 0; i < controllers.Count; i++) |
| 1012 | { |
| 1013 | OVRControllerBase controller = controllers[i]; |
| 1014 | |
| 1015 | if (ShouldResolveController(controller.controllerType, controllerMask)) |
| 1016 | { |
| 1017 | RawNearTouch resolvedMask = rawMask | controller.ResolveToRawMask(virtualMask); |
| 1018 | |
| 1019 | if (((RawNearTouch)controller.currentState.NearTouches & resolvedMask) != 0) |
| 1020 | { |
| 1021 | return true; |
| 1022 | } |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | return false; |
| 1027 | } |
| 1028 | |
| 1029 | /// <summary> |
| 1030 | /// Gets the current down state of the given virtual near touch mask with the given controller mask. |
| 1031 | /// Returns true if any masked near touch was pressed this frame on any masked controller and no masked near touch was previously down last frame. |
| 1032 | /// </summary> |
| 1033 | public static bool GetDown(NearTouch virtualMask, Controller controllerMask = Controller.Active) |
| 1034 | { |
| 1035 | return GetResolvedNearTouchDown(virtualMask, RawNearTouch.None, controllerMask); |
| 1036 | } |
| 1037 | |
| 1038 | /// <summary> |
| 1039 | /// Gets the current down state of the given raw near touch mask with the given controller mask. |
| 1040 | /// Returns true if any masked near touch was pressed this frame on any masked controller and no masked near touch was previously down last frame. |
| 1041 | /// </summary> |
| 1042 | public static bool GetDown(RawNearTouch rawMask, Controller controllerMask = Controller.Active) |
| 1043 | { |
| 1044 | return GetResolvedNearTouchDown(NearTouch.None, rawMask, controllerMask); |
| 1045 | } |
| 1046 | |
| 1047 | private static bool GetResolvedNearTouchDown(NearTouch virtualMask, RawNearTouch rawMask, Controller controllerMask) |
| 1048 | { |
| 1049 | bool down = false; |
| 1050 | |
| 1051 | if ((controllerMask & Controller.Active) != 0) |
| 1052 | controllerMask |= activeControllerType; |
| 1053 | |
| 1054 | for (int i = 0; i < controllers.Count; i++) |
| 1055 | { |
| 1056 | OVRControllerBase controller = controllers[i]; |
| 1057 | |
| 1058 | if (ShouldResolveController(controller.controllerType, controllerMask)) |
| 1059 | { |
| 1060 | RawNearTouch resolvedMask = rawMask | controller.ResolveToRawMask(virtualMask); |
| 1061 | |
| 1062 | if (((RawNearTouch)controller.previousState.NearTouches & resolvedMask) != 0) |
| 1063 | { |
| 1064 | return false; |
| 1065 | } |
| 1066 | |
| 1067 | if ((((RawNearTouch)controller.currentState.NearTouches & resolvedMask) != 0) |
| 1068 | && (((RawNearTouch)controller.previousState.NearTouches & resolvedMask) == 0)) |
| 1069 | { |
| 1070 | down = true; |
| 1071 | } |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | return down; |
| 1076 | } |
| 1077 | |
| 1078 | /// <summary> |
| 1079 | /// Gets the current up state of the given virtual near touch mask with the given controller mask. |
| 1080 | /// Returns true if any masked near touch was released this frame on any masked controller and no other masked near touch is still down this frame. |
| 1081 | /// </summary> |
| 1082 | public static bool GetUp(NearTouch virtualMask, Controller controllerMask = Controller.Active) |
| 1083 | { |
| 1084 | return GetResolvedNearTouchUp(virtualMask, RawNearTouch.None, controllerMask); |
| 1085 | } |
| 1086 | |
| 1087 | /// <summary> |
| 1088 | /// Gets the current up state of the given raw near touch mask with the given controller mask. |
| 1089 | /// Returns true if any masked near touch was released this frame on any masked controller and no other masked near touch is still down this frame. |
| 1090 | /// </summary> |
| 1091 | public static bool GetUp(RawNearTouch rawMask, Controller controllerMask = Controller.Active) |
| 1092 | { |
| 1093 | return GetResolvedNearTouchUp(NearTouch.None, rawMask, controllerMask); |
| 1094 | } |
| 1095 | |
| 1096 | private static bool GetResolvedNearTouchUp(NearTouch virtualMask, RawNearTouch rawMask, Controller controllerMask) |
| 1097 | { |
| 1098 | bool up = false; |
| 1099 | |
| 1100 | if ((controllerMask & Controller.Active) != 0) |
| 1101 | controllerMask |= activeControllerType; |
| 1102 | |
| 1103 | for (int i = 0; i < controllers.Count; i++) |
| 1104 | { |
| 1105 | OVRControllerBase controller = controllers[i]; |
| 1106 | |
| 1107 | if (ShouldResolveController(controller.controllerType, controllerMask)) |
| 1108 | { |
| 1109 | RawNearTouch resolvedMask = rawMask | controller.ResolveToRawMask(virtualMask); |
| 1110 | |
| 1111 | if (((RawNearTouch)controller.currentState.NearTouches & resolvedMask) != 0) |
| 1112 | { |
| 1113 | return false; |
| 1114 | } |
| 1115 | |
| 1116 | if ((((RawNearTouch)controller.currentState.NearTouches & resolvedMask) == 0) |
| 1117 | && (((RawNearTouch)controller.previousState.NearTouches & resolvedMask) != 0)) |
| 1118 | { |
| 1119 | up = true; |
| 1120 | } |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | return up; |
| 1125 | } |
| 1126 | |
| 1127 | /// <summary> |
| 1128 | /// Gets the current state of the given virtual 1-dimensional axis mask on the given controller mask. |
| 1129 | /// Returns the value of the largest masked axis across all masked controllers. Values range from 0 to 1. |
| 1130 | /// </summary> |
| 1131 | public static float Get(Axis1D virtualMask, Controller controllerMask = Controller.Active) |
| 1132 | { |
| 1133 | return GetResolvedAxis1D(virtualMask, RawAxis1D.None, controllerMask); |
| 1134 | } |
| 1135 | |
| 1136 | /// <summary> |
| 1137 | /// Gets the current state of the given raw 1-dimensional axis mask on the given controller mask. |
| 1138 | /// Returns the value of the largest masked axis across all masked controllers. Values range from 0 to 1. |
| 1139 | /// </summary> |
| 1140 | public static float Get(RawAxis1D rawMask, Controller controllerMask = Controller.Active) |
| 1141 | { |
| 1142 | return GetResolvedAxis1D(Axis1D.None, rawMask, controllerMask); |
| 1143 | } |
| 1144 | |
| 1145 | private static float GetResolvedAxis1D(Axis1D virtualMask, RawAxis1D rawMask, Controller controllerMask) |
| 1146 | { |
| 1147 | float maxAxis = 0.0f; |
| 1148 | |
| 1149 | if ((controllerMask & Controller.Active) != 0) |
| 1150 | controllerMask |= activeControllerType; |
| 1151 | |
| 1152 | for (int i = 0; i < controllers.Count; i++) |
| 1153 | { |
| 1154 | OVRControllerBase controller = controllers[i]; |
| 1155 | |
| 1156 | if (OVRManager.loadedXRDevice != OVRManager.XRDevice.Oculus) |
| 1157 | controller.shouldApplyDeadzone = false; |
| 1158 | |
| 1159 | if (ShouldResolveController(controller.controllerType, controllerMask)) |
| 1160 | { |
| 1161 | RawAxis1D resolvedMask = rawMask | controller.ResolveToRawMask(virtualMask); |
| 1162 | |
| 1163 | if ((RawAxis1D.LIndexTrigger & resolvedMask) != 0) |
| 1164 | { |
| 1165 | float axis = controller.currentState.LIndexTrigger; |
| 1166 | |
| 1167 | if (controller.shouldApplyDeadzone) |
| 1168 | axis = CalculateDeadzone(axis, AXIS_DEADZONE_THRESHOLD); |
| 1169 | |
| 1170 | maxAxis = CalculateAbsMax(maxAxis, axis); |
| 1171 | } |
| 1172 | if ((RawAxis1D.RIndexTrigger & resolvedMask) != 0) |
| 1173 | { |
| 1174 | float axis = controller.currentState.RIndexTrigger; |
| 1175 | |
| 1176 | if (controller.shouldApplyDeadzone) |
| 1177 | axis = CalculateDeadzone(axis, AXIS_DEADZONE_THRESHOLD); |
| 1178 | |
| 1179 | maxAxis = CalculateAbsMax(maxAxis, axis); |
| 1180 | } |
| 1181 | if ((RawAxis1D.LHandTrigger & resolvedMask) != 0) |
| 1182 | { |
| 1183 | float axis = controller.currentState.LHandTrigger; |
| 1184 | |
| 1185 | if (controller.shouldApplyDeadzone) |
| 1186 | axis = CalculateDeadzone(axis, AXIS_DEADZONE_THRESHOLD); |
| 1187 | |
| 1188 | maxAxis = CalculateAbsMax(maxAxis, axis); |
| 1189 | } |
| 1190 | if ((RawAxis1D.RHandTrigger & resolvedMask) != 0) |
| 1191 | { |
| 1192 | float axis = controller.currentState.RHandTrigger; |
| 1193 | |
| 1194 | if (controller.shouldApplyDeadzone) |
| 1195 | axis = CalculateDeadzone(axis, AXIS_DEADZONE_THRESHOLD); |
| 1196 | |
| 1197 | maxAxis = CalculateAbsMax(maxAxis, axis); |
| 1198 | } |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | return maxAxis; |
| 1203 | } |
| 1204 | |
| 1205 | /// <summary> |
| 1206 | /// Gets the current state of the given virtual 2-dimensional axis mask on the given controller mask. |
| 1207 | /// Returns the vector of the largest masked axis across all masked controllers. Values range from -1 to 1. |
| 1208 | /// </summary> |
| 1209 | public static Vector2 Get(Axis2D virtualMask, Controller controllerMask = Controller.Active) |
| 1210 | { |
| 1211 | return GetResolvedAxis2D(virtualMask, RawAxis2D.None, controllerMask); |
| 1212 | } |
| 1213 | |
| 1214 | /// <summary> |
| 1215 | /// Gets the current state of the given raw 2-dimensional axis mask on the given controller mask. |
| 1216 | /// Returns the vector of the largest masked axis across all masked controllers. Values range from -1 to 1. |
| 1217 | /// </summary> |
| 1218 | public static Vector2 Get(RawAxis2D rawMask, Controller controllerMask = Controller.Active) |
| 1219 | { |
| 1220 | return GetResolvedAxis2D(Axis2D.None, rawMask, controllerMask); |
| 1221 | } |
| 1222 | |
| 1223 | private static Vector2 GetResolvedAxis2D(Axis2D virtualMask, RawAxis2D rawMask, Controller controllerMask) |
| 1224 | { |
| 1225 | Vector2 maxAxis = Vector2.zero; |
| 1226 | |
| 1227 | if ((controllerMask & Controller.Active) != 0) |
| 1228 | controllerMask |= activeControllerType; |
| 1229 | |
| 1230 | for (int i = 0; i < controllers.Count; i++) |
| 1231 | { |
| 1232 | OVRControllerBase controller = controllers[i]; |
| 1233 | |
| 1234 | if (OVRManager.loadedXRDevice != OVRManager.XRDevice.Oculus) |
| 1235 | controller.shouldApplyDeadzone = false; |
| 1236 | |
| 1237 | if (ShouldResolveController(controller.controllerType, controllerMask)) |
| 1238 | { |
| 1239 | RawAxis2D resolvedMask = rawMask | controller.ResolveToRawMask(virtualMask); |
| 1240 | |
| 1241 | if ((RawAxis2D.LThumbstick & resolvedMask) != 0) |
| 1242 | { |
| 1243 | Vector2 axis = new Vector2( |
| 1244 | controller.currentState.LThumbstick.x, |
| 1245 | controller.currentState.LThumbstick.y); |
| 1246 | |
| 1247 | if (controller.shouldApplyDeadzone) |
| 1248 | axis = CalculateDeadzone(axis, AXIS_DEADZONE_THRESHOLD); |
| 1249 | |
| 1250 | maxAxis = CalculateAbsMax(maxAxis, axis); |
| 1251 | } |
| 1252 | if ((RawAxis2D.LTouchpad & resolvedMask) != 0) |
| 1253 | { |
| 1254 | Vector2 axis = new Vector2( |
| 1255 | controller.currentState.LTouchpad.x, |
| 1256 | controller.currentState.LTouchpad.y); |
| 1257 | |
| 1258 | //if (controller.shouldApplyDeadzone) |
| 1259 | // axis = CalculateDeadzone(axis, AXIS_DEADZONE_THRESHOLD); |
| 1260 | |
| 1261 | maxAxis = CalculateAbsMax(maxAxis, axis); |
| 1262 | } |
| 1263 | if ((RawAxis2D.RThumbstick & resolvedMask) != 0) |
| 1264 | { |
| 1265 | Vector2 axis = new Vector2( |
| 1266 | controller.currentState.RThumbstick.x, |
| 1267 | controller.currentState.RThumbstick.y); |
| 1268 | |
| 1269 | if (controller.shouldApplyDeadzone) |
| 1270 | axis = CalculateDeadzone(axis, AXIS_DEADZONE_THRESHOLD); |
| 1271 | |
| 1272 | maxAxis = CalculateAbsMax(maxAxis, axis); |
| 1273 | } |
| 1274 | if ((RawAxis2D.RTouchpad & resolvedMask) != 0) |
| 1275 | { |
| 1276 | Vector2 axis = new Vector2( |
| 1277 | controller.currentState.RTouchpad.x, |
| 1278 | controller.currentState.RTouchpad.y); |
| 1279 | |
| 1280 | //if (controller.shouldApplyDeadzone) |
| 1281 | // axis = CalculateDeadzone(axis, AXIS_DEADZONE_THRESHOLD); |
| 1282 | |
| 1283 | maxAxis = CalculateAbsMax(maxAxis, axis); |
| 1284 | } |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | return maxAxis; |
| 1289 | } |
| 1290 | |
| 1291 | /// <summary> |
| 1292 | /// Returns a mask of all currently connected controller types. |
| 1293 | /// </summary> |
| 1294 | public static Controller GetConnectedControllers() |
| 1295 | { |
| 1296 | return connectedControllerTypes; |
| 1297 | } |
| 1298 | |
| 1299 | /// <summary> |
| 1300 | /// Returns true if the specified controller type is currently connected. |
| 1301 | /// </summary> |
| 1302 | public static bool IsControllerConnected(Controller controller) |
| 1303 | { |
| 1304 | return (connectedControllerTypes & controller) == controller; |
| 1305 | } |
| 1306 | |
| 1307 | /// <summary> |
| 1308 | /// Returns the current active controller type. |
| 1309 | /// </summary> |
| 1310 | public static Controller GetActiveController() |
| 1311 | { |
| 1312 | return activeControllerType; |
| 1313 | } |
| 1314 | |
| 1315 | private static void StartVibration(float amplitude, float duration, Node controllerNode) |
| 1316 | { |
| 1317 | int index = (controllerNode == Node.LeftHand) ? 0 : 1; |
| 1318 | hapticInfos[index].hapticsDurationPlayed = 0.0f; |
| 1319 | hapticInfos[index].hapticAmplitude = amplitude; |
| 1320 | hapticInfos[index].hapticsDuration = duration; |
| 1321 | hapticInfos[index].playingHaptics = (amplitude != 0.0f); |
| 1322 | hapticInfos[index].node = controllerNode; |
| 1323 | if (amplitude <= 0.0f || duration <= 0.0f) |
| 1324 | { |
| 1325 | hapticInfos[index].playingHaptics = false; |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | private static int NUM_HAPTIC_CHANNELS = 2; |
| 1330 | private static HapticInfo[] hapticInfos; |
| 1331 | |
| 1332 | private static float OPENVR_MAX_HAPTIC_AMPLITUDE = 4000.0f; |
| 1333 | private static float HAPTIC_VIBRATION_DURATION_SECONDS = 2.0f; |
| 1334 | private static String OPENVR_TOUCH_NAME = "oculus_touch"; |
| 1335 | private static String OPENVR_VIVE_CONTROLLER_NAME = "vive_controller"; |
| 1336 | private static String OPENVR_WINDOWSMR_CONTROLLER_NAME = "holographic_controller"; |
| 1337 | |
| 1338 | [Flags] |
| 1339 | /// OpenVR Controller Enum |
| 1340 | public enum OpenVRController : ulong |
| 1341 | { |
| 1342 | Unknown = 0, |
| 1343 | OculusTouch = 1, |
| 1344 | ViveController = 2, |
| 1345 | WindowsMRController = 3 |
| 1346 | } |
| 1347 | |
| 1348 | [StructLayout(LayoutKind.Sequential)] |
| 1349 | public struct OpenVRControllerDetails |
| 1350 | { |
| 1351 | public OVR.OpenVR.VRControllerState_t state; |
| 1352 | public OpenVRController controllerType; |
| 1353 | public uint deviceID; |
| 1354 | public Vector3 localPosition; //Position relative to Tracking Space |
| 1355 | public Quaternion localOrientation; //Orientation relative to Tracking Space |
| 1356 | } |
| 1357 | |
| 1358 | public static OpenVRControllerDetails[] openVRControllerDetails = new OpenVRControllerDetails[2]; |
| 1359 | |
| 1360 | private class HapticInfo |
| 1361 | { |
| 1362 | public bool playingHaptics; |
| 1363 | public float hapticsDurationPlayed; |
| 1364 | public float hapticsDuration; |
| 1365 | public float hapticAmplitude; |
| 1366 | public Node node; |
| 1367 | } |
| 1368 | |
| 1369 | /// <summary> |
| 1370 | /// Sets OpenVR left and right controller poses local to tracking space |
| 1371 | /// </summary> |
| 1372 | public static void SetOpenVRLocalPose(Vector3 leftPos, Vector3 rightPos, Quaternion leftRot, Quaternion rightRot) |
| 1373 | { |
| 1374 | openVRControllerDetails[0].localPosition = leftPos; |
| 1375 | openVRControllerDetails[0].localOrientation = leftRot; |
| 1376 | openVRControllerDetails[1].localPosition = rightPos; |
| 1377 | openVRControllerDetails[1].localOrientation = rightRot; |
| 1378 | } |
| 1379 | |
| 1380 | /// <summary> |
| 1381 | /// Accesses OpenVR properties about a given deviceID. Especially useful for differentiating per type of OpenVR device (i.e. Oculus, Vive) |
| 1382 | /// </summary> |
| 1383 | public static string GetOpenVRStringProperty(OVR.OpenVR.ETrackedDeviceProperty prop, uint deviceId = OVR.OpenVR.OpenVR.k_unTrackedDeviceIndex_Hmd) |
| 1384 | { |
| 1385 | |
| 1386 | OVR.OpenVR.ETrackedPropertyError error = OVR.OpenVR.ETrackedPropertyError.TrackedProp_Success; |
| 1387 | OVR.OpenVR.CVRSystem system = OVR.OpenVR.OpenVR.System; |
| 1388 | if (system != null) |
| 1389 | { |
| 1390 | uint capacity = system.GetStringTrackedDeviceProperty(deviceId, prop, null, 0, ref error); |
| 1391 | if (capacity > 1) |
| 1392 | { |
| 1393 | var result = new System.Text.StringBuilder((int)capacity); |
| 1394 | system.GetStringTrackedDeviceProperty(deviceId, prop, result, capacity, ref error); |
| 1395 | return result.ToString(); |
| 1396 | } |
| 1397 | return (error != OVR.OpenVR.ETrackedPropertyError.TrackedProp_Success) ? error.ToString() : "<unknown>"; |
| 1398 | } |
| 1399 | return ""; |
| 1400 | } |
| 1401 | |
| 1402 | /// <summary> |
| 1403 | /// Associates OpenVR device IDs with left and right motion controllers, for later haptic playback. |
| 1404 | /// </summary> |
| 1405 | private static void UpdateXRControllerNodeIds() |
| 1406 | { |
| 1407 | if (OVRManager.loadedXRDevice == OVRManager.XRDevice.OpenVR) |
| 1408 | { |
| 1409 | openVRControllerDetails[0].deviceID = OVR.OpenVR.OpenVR.k_unMaxTrackedDeviceCount; |
| 1410 | openVRControllerDetails[1].deviceID = OVR.OpenVR.OpenVR.k_unMaxTrackedDeviceCount; |
| 1411 | |
| 1412 | OVR.OpenVR.CVRSystem system = OVR.OpenVR.OpenVR.System; |
| 1413 | |
| 1414 | if (system != null) |
| 1415 | { |
| 1416 | for (uint id = 0; id < OVR.OpenVR.OpenVR.k_unMaxTrackedDeviceCount; id++) |
| 1417 | { |
| 1418 | OVR.OpenVR.ETrackedDeviceClass deviceClass = system.GetTrackedDeviceClass(id); |
| 1419 | if (deviceClass == OVR.OpenVR.ETrackedDeviceClass.Controller && system.IsTrackedDeviceConnected(id)) |
| 1420 | { |
| 1421 | OpenVRController controllerType; |
| 1422 | String controllerName = GetOpenVRStringProperty(OVR.OpenVR.ETrackedDeviceProperty.Prop_ControllerType_String, id); |
| 1423 | if (controllerName == OPENVR_TOUCH_NAME) |
| 1424 | controllerType = OpenVRController.OculusTouch; |
| 1425 | else if (controllerName == OPENVR_VIVE_CONTROLLER_NAME) |
| 1426 | controllerType = OpenVRController.ViveController; |
| 1427 | else if (controllerName == OPENVR_WINDOWSMR_CONTROLLER_NAME) |
| 1428 | controllerType = OpenVRController.WindowsMRController; |
| 1429 | else |
| 1430 | controllerType = OpenVRController.Unknown; |
| 1431 | |
| 1432 | OVR.OpenVR.ETrackedControllerRole role = system.GetControllerRoleForTrackedDeviceIndex(id); |
| 1433 | if (role == OVR.OpenVR.ETrackedControllerRole.LeftHand) |
| 1434 | { |
| 1435 | system.GetControllerState(id, ref openVRControllerDetails[0].state, (uint)Marshal.SizeOf(typeof(OVR.OpenVR.VRControllerState_t))); |
| 1436 | openVRControllerDetails[0].deviceID = id; |
| 1437 | openVRControllerDetails[0].controllerType = controllerType; |
| 1438 | connectedControllerTypes |= Controller.LTouch; |
| 1439 | } |
| 1440 | else if (role == OVR.OpenVR.ETrackedControllerRole.RightHand) |
| 1441 | { |
| 1442 | system.GetControllerState(id, ref openVRControllerDetails[1].state, (uint)Marshal.SizeOf(typeof(OVR.OpenVR.VRControllerState_t))); |
| 1443 | openVRControllerDetails[1].deviceID = id; |
| 1444 | openVRControllerDetails[1].controllerType = controllerType; |
| 1445 | connectedControllerTypes |= Controller.RTouch; |
| 1446 | } |
| 1447 | } |
| 1448 | } |
| 1449 | } |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | /// <summary> |
| 1454 | /// Runs once a frame to update cross-platform haptic playback |
| 1455 | /// </summary> |
| 1456 | private static void UpdateXRControllerHaptics() |
| 1457 | { |
| 1458 | if (OVRManager.loadedXRDevice == OVRManager.XRDevice.OpenVR) |
| 1459 | { |
| 1460 | for (int i = 0; i < NUM_HAPTIC_CHANNELS; i++) |
| 1461 | { |
| 1462 | if (hapticInfos[i].playingHaptics) |
| 1463 | { |
| 1464 | hapticInfos[i].hapticsDurationPlayed += Time.deltaTime; |
| 1465 | |
| 1466 | PlayHapticImpulse(hapticInfos[i].hapticAmplitude, hapticInfos[i].node); |
| 1467 | |
| 1468 | if (hapticInfos[i].hapticsDurationPlayed >= hapticInfos[i].hapticsDuration) |
| 1469 | { |
| 1470 | hapticInfos[i].playingHaptics = false; |
| 1471 | } |
| 1472 | } |
| 1473 | } |
| 1474 | |
| 1475 | } |
| 1476 | } |
| 1477 | |
| 1478 | private static void InitHapticInfo() |
| 1479 | { |
| 1480 | hapticInfos = new HapticInfo[NUM_HAPTIC_CHANNELS]; |
| 1481 | for (int i = 0; i < NUM_HAPTIC_CHANNELS; i++) |
| 1482 | { |
| 1483 | hapticInfos[i] = new HapticInfo(); |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | private static void PlayHapticImpulse(float amplitude, Node deviceNode) |
| 1488 | { |
| 1489 | OVR.OpenVR.CVRSystem system = OVR.OpenVR.OpenVR.System; |
| 1490 | if (system != null && amplitude != 0.0f) |
| 1491 | { |
| 1492 | uint controllerId = (deviceNode == Node.LeftHand) ? openVRControllerDetails[0].deviceID : openVRControllerDetails[1].deviceID; |
| 1493 | |
| 1494 | if (IsValidOpenVRDevice(controllerId)) |
| 1495 | system.TriggerHapticPulse(controllerId, 0, (char)(OPENVR_MAX_HAPTIC_AMPLITUDE * amplitude)); |
| 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | private static bool IsValidOpenVRDevice(uint deviceId) |
| 1500 | { |
| 1501 | return (deviceId >= 0 && deviceId < OVR.OpenVR.OpenVR.k_unMaxTrackedDeviceCount); |
| 1502 | } |
| 1503 | |
| 1504 | /// <summary> |
| 1505 | /// Activates vibration with the given frequency and amplitude with the given controller mask. |
| 1506 | /// Ignored on controllers that do not support vibration. Expected values range from 0 to 1. |
| 1507 | /// </summary> |
| 1508 | public static void SetControllerVibration(float frequency, float amplitude, Controller controllerMask = Controller.Active) |
| 1509 | { |
| 1510 | if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus) |
| 1511 | { |
| 1512 | if ((controllerMask & Controller.Active) != 0) |
| 1513 | controllerMask |= activeControllerType; |
| 1514 | |
| 1515 | for (int i = 0; i < controllers.Count; i++) |
| 1516 | { |
| 1517 | OVRControllerBase controller = controllers[i]; |
| 1518 | |
| 1519 | if (ShouldResolveController(controller.controllerType, controllerMask)) |
| 1520 | { |
| 1521 | controller.SetControllerVibration(frequency, amplitude); |
| 1522 | } |
| 1523 | } |
| 1524 | } |
| 1525 | else if (OVRManager.loadedXRDevice == OVRManager.XRDevice.OpenVR) |
| 1526 | { |
| 1527 | if (controllerMask == Controller.LTouch || controllerMask == Controller.RTouch) |
| 1528 | { |
| 1529 | Node controllerNode = (controllerMask == Controller.LTouch) ? Node.LeftHand : Node.RightHand; |
| 1530 | StartVibration(amplitude, HAPTIC_VIBRATION_DURATION_SECONDS, controllerNode); |
| 1531 | } |
| 1532 | } |
| 1533 | } |
| 1534 | |
| 1535 | /// <summary> |
| 1536 | /// Triggers a recenter to realign the specified controller's virtual pose with the user's real-world pose. |
| 1537 | /// Only applicable to controllers that require recentering, such as the GearVR Controller. |
| 1538 | /// Ignored for controllers that do not require recentering. |
| 1539 | /// </summary> |
| 1540 | public static void RecenterController(Controller controllerMask = Controller.Active) |
| 1541 | { |
| 1542 | if ((controllerMask & Controller.Active) != 0) |
| 1543 | controllerMask |= activeControllerType; |
| 1544 | |
| 1545 | for (int i = 0; i < controllers.Count; i++) |
| 1546 | { |
| 1547 | OVRControllerBase controller = controllers[i]; |
| 1548 | |
| 1549 | if (ShouldResolveController(controller.controllerType, controllerMask)) |
| 1550 | { |
| 1551 | controller.RecenterController(); |
| 1552 | } |
| 1553 | } |
| 1554 | } |
| 1555 | |
| 1556 | /// <summary> |
| 1557 | /// Returns true if the specified controller was recentered this frame. |
| 1558 | /// Only applicable to controllers that require recentering, such as the GearVR Controller. |
| 1559 | /// Returns false for controllers that do not require recentering. |
| 1560 | /// </summary> |
| 1561 | public static bool GetControllerWasRecentered(Controller controllerMask = Controller.Active) |
| 1562 | { |
| 1563 | if ((controllerMask & Controller.Active) != 0) |
| 1564 | controllerMask |= activeControllerType; |
| 1565 | |
| 1566 | bool wasRecentered = false; |
| 1567 | |
| 1568 | for (int i = 0; i < controllers.Count; i++) |
| 1569 | { |
| 1570 | OVRControllerBase controller = controllers[i]; |
| 1571 | |
| 1572 | if (ShouldResolveController(controller.controllerType, controllerMask)) |
| 1573 | { |
| 1574 | wasRecentered |= controller.WasRecentered(); |
| 1575 | } |
| 1576 | } |
| 1577 | |
| 1578 | return wasRecentered; |
| 1579 | } |
| 1580 | |
| 1581 | /// <summary> |
| 1582 | /// Returns the number of times the controller has been recentered this session. |
| 1583 | /// Useful for detecting recenter events and resetting state such as arm model simulations, etc. |
| 1584 | /// Wraps around to 0 after 255. |
| 1585 | /// Only applicable to controllers that require recentering, such as the GearVR Controller. |
| 1586 | /// Returns 0 for controllers that do not require recentering. |
| 1587 | /// </summary> |
| 1588 | public static byte GetControllerRecenterCount(Controller controllerMask = Controller.Active) |
| 1589 | { |
| 1590 | if ((controllerMask & Controller.Active) != 0) |
| 1591 | controllerMask |= activeControllerType; |
| 1592 | |
| 1593 | byte recenterCount = 0; |
| 1594 | |
| 1595 | for (int i = 0; i < controllers.Count; i++) |
| 1596 | { |
| 1597 | OVRControllerBase controller = controllers[i]; |
| 1598 | |
| 1599 | if (ShouldResolveController(controller.controllerType, controllerMask)) |
| 1600 | { |
| 1601 | recenterCount = controller.GetRecenterCount(); |
| 1602 | break; |
| 1603 | } |
| 1604 | } |
| 1605 | |
| 1606 | return recenterCount; |
| 1607 | } |
| 1608 | |
| 1609 | /// <summary> |
| 1610 | /// Returns the battery percentage remaining for the specified controller. Values range from 0 to 100. |
| 1611 | /// Only applicable to controllers that report battery level, such as the GearVR Controller. |
| 1612 | /// Returns 0 for controllers that do not report battery level. |
| 1613 | /// </summary> |
| 1614 | public static byte GetControllerBatteryPercentRemaining(Controller controllerMask = Controller.Active) |
| 1615 | { |
| 1616 | if ((controllerMask & Controller.Active) != 0) |
| 1617 | controllerMask |= activeControllerType; |
| 1618 | |
| 1619 | byte battery = 0; |
| 1620 | |
| 1621 | for (int i = 0; i < controllers.Count; i++) |
| 1622 | { |
| 1623 | OVRControllerBase controller = controllers[i]; |
| 1624 | |
| 1625 | if (ShouldResolveController(controller.controllerType, controllerMask)) |
| 1626 | { |
| 1627 | battery = controller.GetBatteryPercentRemaining(); |
| 1628 | break; |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | return battery; |
| 1633 | } |
| 1634 | |
| 1635 | private static Vector2 CalculateAbsMax(Vector2 a, Vector2 b) |
| 1636 | { |
| 1637 | float absA = a.sqrMagnitude; |
| 1638 | float absB = b.sqrMagnitude; |
| 1639 | |
| 1640 | if (absA >= absB) |
| 1641 | return a; |
| 1642 | return b; |
| 1643 | } |
| 1644 | |
| 1645 | private static float CalculateAbsMax(float a, float b) |
| 1646 | { |
| 1647 | float absA = (a >= 0) ? a : -a; |
| 1648 | float absB = (b >= 0) ? b : -b; |
| 1649 | |
| 1650 | if (absA >= absB) |
| 1651 | return a; |
| 1652 | return b; |
| 1653 | } |
| 1654 | |
| 1655 | private static Vector2 CalculateDeadzone(Vector2 a, float deadzone) |
| 1656 | { |
| 1657 | if (a.sqrMagnitude <= (deadzone * deadzone)) |
| 1658 | return Vector2.zero; |
| 1659 | |
| 1660 | a *= ((a.magnitude - deadzone) / (1.0f - deadzone)); |
| 1661 | |
| 1662 | if (a.sqrMagnitude > 1.0f) |
| 1663 | return a.normalized; |
| 1664 | return a; |
| 1665 | } |
| 1666 | |
| 1667 | private static float CalculateDeadzone(float a, float deadzone) |
| 1668 | { |
| 1669 | float mag = (a >= 0) ? a : -a; |
| 1670 | |
| 1671 | if (mag <= deadzone) |
| 1672 | return 0.0f; |
| 1673 | |
| 1674 | a *= (mag - deadzone) / (1.0f - deadzone); |
| 1675 | |
| 1676 | if ((a * a) > 1.0f) |
| 1677 | return (a >= 0) ? 1.0f : -1.0f; |
| 1678 | return a; |
| 1679 | } |
| 1680 | |
| 1681 | private static bool ShouldResolveController(Controller controllerType, Controller controllerMask) |
| 1682 | { |
| 1683 | bool isValid = false; |
| 1684 | |
| 1685 | if ((controllerType & controllerMask) == controllerType) |
| 1686 | { |
| 1687 | isValid = true; |
| 1688 | } |
| 1689 | |
| 1690 | // If the mask requests both Touch controllers, reject the individual touch controllers. |
| 1691 | if (((controllerMask & Controller.Touch) == Controller.Touch) |
| 1692 | && ((controllerType & Controller.Touch) != 0) |
| 1693 | && ((controllerType & Controller.Touch) != Controller.Touch)) |
| 1694 | { |
| 1695 | isValid = false; |
| 1696 | } |
| 1697 | |
| 1698 | return isValid; |
| 1699 | } |
| 1700 | |
| 1701 | private abstract class OVRControllerBase |
| 1702 | { |
| 1703 | public class VirtualButtonMap |
| 1704 | { |
| 1705 | public RawButton None = RawButton.None; |
| 1706 | public RawButton One = RawButton.None; |
| 1707 | public RawButton Two = RawButton.None; |
| 1708 | public RawButton Three = RawButton.None; |
| 1709 | public RawButton Four = RawButton.None; |
| 1710 | public RawButton Start = RawButton.None; |
| 1711 | public RawButton Back = RawButton.None; |
| 1712 | public RawButton PrimaryShoulder = RawButton.None; |
| 1713 | public RawButton PrimaryIndexTrigger = RawButton.None; |
| 1714 | public RawButton PrimaryHandTrigger = RawButton.None; |
| 1715 | public RawButton PrimaryThumbstick = RawButton.None; |
| 1716 | public RawButton PrimaryThumbstickUp = RawButton.None; |
| 1717 | public RawButton PrimaryThumbstickDown = RawButton.None; |
| 1718 | public RawButton PrimaryThumbstickLeft = RawButton.None; |
| 1719 | public RawButton PrimaryThumbstickRight = RawButton.None; |
| 1720 | public RawButton PrimaryTouchpad = RawButton.None; |
| 1721 | public RawButton SecondaryShoulder = RawButton.None; |
| 1722 | public RawButton SecondaryIndexTrigger = RawButton.None; |
| 1723 | public RawButton SecondaryHandTrigger = RawButton.None; |
| 1724 | public RawButton SecondaryThumbstick = RawButton.None; |
| 1725 | public RawButton SecondaryThumbstickUp = RawButton.None; |
| 1726 | public RawButton SecondaryThumbstickDown = RawButton.None; |
| 1727 | public RawButton SecondaryThumbstickLeft = RawButton.None; |
| 1728 | public RawButton SecondaryThumbstickRight = RawButton.None; |
| 1729 | public RawButton SecondaryTouchpad = RawButton.None; |
| 1730 | public RawButton DpadUp = RawButton.None; |
| 1731 | public RawButton DpadDown = RawButton.None; |
| 1732 | public RawButton DpadLeft = RawButton.None; |
| 1733 | public RawButton DpadRight = RawButton.None; |
| 1734 | public RawButton Up = RawButton.None; |
| 1735 | public RawButton Down = RawButton.None; |
| 1736 | public RawButton Left = RawButton.None; |
| 1737 | public RawButton Right = RawButton.None; |
| 1738 | |
| 1739 | public RawButton ToRawMask(Button virtualMask) |
| 1740 | { |
| 1741 | RawButton rawMask = 0; |
| 1742 | |
| 1743 | if (virtualMask == Button.None) |
| 1744 | return RawButton.None; |
| 1745 | |
| 1746 | if ((virtualMask & Button.One) != 0) |
| 1747 | rawMask |= One; |
| 1748 | if ((virtualMask & Button.Two) != 0) |
| 1749 | rawMask |= Two; |
| 1750 | if ((virtualMask & Button.Three) != 0) |
| 1751 | rawMask |= Three; |
| 1752 | if ((virtualMask & Button.Four) != 0) |
| 1753 | rawMask |= Four; |
| 1754 | if ((virtualMask & Button.Start) != 0) |
| 1755 | rawMask |= Start; |
| 1756 | if ((virtualMask & Button.Back) != 0) |
| 1757 | rawMask |= Back; |
| 1758 | if ((virtualMask & Button.PrimaryShoulder) != 0) |
| 1759 | rawMask |= PrimaryShoulder; |
| 1760 | if ((virtualMask & Button.PrimaryIndexTrigger) != 0) |
| 1761 | rawMask |= PrimaryIndexTrigger; |
| 1762 | if ((virtualMask & Button.PrimaryHandTrigger) != 0) |
| 1763 | rawMask |= PrimaryHandTrigger; |
| 1764 | if ((virtualMask & Button.PrimaryThumbstick) != 0) |
| 1765 | rawMask |= PrimaryThumbstick; |
| 1766 | if ((virtualMask & Button.PrimaryThumbstickUp) != 0) |
| 1767 | rawMask |= PrimaryThumbstickUp; |
| 1768 | if ((virtualMask & Button.PrimaryThumbstickDown) != 0) |
| 1769 | rawMask |= PrimaryThumbstickDown; |
| 1770 | if ((virtualMask & Button.PrimaryThumbstickLeft) != 0) |
| 1771 | rawMask |= PrimaryThumbstickLeft; |
| 1772 | if ((virtualMask & Button.PrimaryThumbstickRight) != 0) |
| 1773 | rawMask |= PrimaryThumbstickRight; |
| 1774 | if ((virtualMask & Button.PrimaryTouchpad) != 0) |
| 1775 | rawMask |= PrimaryTouchpad; |
| 1776 | if ((virtualMask & Button.SecondaryShoulder) != 0) |
| 1777 | rawMask |= SecondaryShoulder; |
| 1778 | if ((virtualMask & Button.SecondaryIndexTrigger) != 0) |
| 1779 | rawMask |= SecondaryIndexTrigger; |
| 1780 | if ((virtualMask & Button.SecondaryHandTrigger) != 0) |
| 1781 | rawMask |= SecondaryHandTrigger; |
| 1782 | if ((virtualMask & Button.SecondaryThumbstick) != 0) |
| 1783 | rawMask |= SecondaryThumbstick; |
| 1784 | if ((virtualMask & Button.SecondaryThumbstickUp) != 0) |
| 1785 | rawMask |= SecondaryThumbstickUp; |
| 1786 | if ((virtualMask & Button.SecondaryThumbstickDown) != 0) |
| 1787 | rawMask |= SecondaryThumbstickDown; |
| 1788 | if ((virtualMask & Button.SecondaryThumbstickLeft) != 0) |
| 1789 | rawMask |= SecondaryThumbstickLeft; |
| 1790 | if ((virtualMask & Button.SecondaryThumbstickRight) != 0) |
| 1791 | rawMask |= SecondaryThumbstickRight; |
| 1792 | if ((virtualMask & Button.SecondaryTouchpad) != 0) |
| 1793 | rawMask |= SecondaryTouchpad; |
| 1794 | if ((virtualMask & Button.DpadUp) != 0) |
| 1795 | rawMask |= DpadUp; |
| 1796 | if ((virtualMask & Button.DpadDown) != 0) |
| 1797 | rawMask |= DpadDown; |
| 1798 | if ((virtualMask & Button.DpadLeft) != 0) |
| 1799 | rawMask |= DpadLeft; |
| 1800 | if ((virtualMask & Button.DpadRight) != 0) |
| 1801 | rawMask |= DpadRight; |
| 1802 | if ((virtualMask & Button.Up) != 0) |
| 1803 | rawMask |= Up; |
| 1804 | if ((virtualMask & Button.Down) != 0) |
| 1805 | rawMask |= Down; |
| 1806 | if ((virtualMask & Button.Left) != 0) |
| 1807 | rawMask |= Left; |
| 1808 | if ((virtualMask & Button.Right) != 0) |
| 1809 | rawMask |= Right; |
| 1810 | |
| 1811 | return rawMask; |
| 1812 | } |
| 1813 | } |
| 1814 | |
| 1815 | public class VirtualTouchMap |
| 1816 | { |
| 1817 | public RawTouch None = RawTouch.None; |
| 1818 | public RawTouch One = RawTouch.None; |
| 1819 | public RawTouch Two = RawTouch.None; |
| 1820 | public RawTouch Three = RawTouch.None; |
| 1821 | public RawTouch Four = RawTouch.None; |
| 1822 | public RawTouch PrimaryIndexTrigger = RawTouch.None; |
| 1823 | public RawTouch PrimaryThumbstick = RawTouch.None; |
| 1824 | public RawTouch PrimaryThumbRest = RawTouch.None; |
| 1825 | public RawTouch PrimaryTouchpad = RawTouch.None; |
| 1826 | public RawTouch SecondaryIndexTrigger = RawTouch.None; |
| 1827 | public RawTouch SecondaryThumbstick = RawTouch.None; |
| 1828 | public RawTouch SecondaryThumbRest = RawTouch.None; |
| 1829 | public RawTouch SecondaryTouchpad = RawTouch.None; |
| 1830 | |
| 1831 | public RawTouch ToRawMask(Touch virtualMask) |
| 1832 | { |
| 1833 | RawTouch rawMask = 0; |
| 1834 | |
| 1835 | if (virtualMask == Touch.None) |
| 1836 | return RawTouch.None; |
| 1837 | |
| 1838 | if ((virtualMask & Touch.One) != 0) |
| 1839 | rawMask |= One; |
| 1840 | if ((virtualMask & Touch.Two) != 0) |
| 1841 | rawMask |= Two; |
| 1842 | if ((virtualMask & Touch.Three) != 0) |
| 1843 | rawMask |= Three; |
| 1844 | if ((virtualMask & Touch.Four) != 0) |
| 1845 | rawMask |= Four; |
| 1846 | if ((virtualMask & Touch.PrimaryIndexTrigger) != 0) |
| 1847 | rawMask |= PrimaryIndexTrigger; |
| 1848 | if ((virtualMask & Touch.PrimaryThumbstick) != 0) |
| 1849 | rawMask |= PrimaryThumbstick; |
| 1850 | if ((virtualMask & Touch.PrimaryThumbRest) != 0) |
| 1851 | rawMask |= PrimaryThumbRest; |
| 1852 | if ((virtualMask & Touch.PrimaryTouchpad) != 0) |
| 1853 | rawMask |= PrimaryTouchpad; |
| 1854 | if ((virtualMask & Touch.SecondaryIndexTrigger) != 0) |
| 1855 | rawMask |= SecondaryIndexTrigger; |
| 1856 | if ((virtualMask & Touch.SecondaryThumbstick) != 0) |
| 1857 | rawMask |= SecondaryThumbstick; |
| 1858 | if ((virtualMask & Touch.SecondaryThumbRest) != 0) |
| 1859 | rawMask |= SecondaryThumbRest; |
| 1860 | if ((virtualMask & Touch.SecondaryTouchpad) != 0) |
| 1861 | rawMask |= SecondaryTouchpad; |
| 1862 | |
| 1863 | return rawMask; |
| 1864 | } |
| 1865 | } |
| 1866 | |
| 1867 | public class VirtualNearTouchMap |
| 1868 | { |
| 1869 | public RawNearTouch None = RawNearTouch.None; |
| 1870 | public RawNearTouch PrimaryIndexTrigger = RawNearTouch.None; |
| 1871 | public RawNearTouch PrimaryThumbButtons = RawNearTouch.None; |
| 1872 | public RawNearTouch SecondaryIndexTrigger = RawNearTouch.None; |
| 1873 | public RawNearTouch SecondaryThumbButtons = RawNearTouch.None; |
| 1874 | |
| 1875 | public RawNearTouch ToRawMask(NearTouch virtualMask) |
| 1876 | { |
| 1877 | RawNearTouch rawMask = 0; |
| 1878 | |
| 1879 | if (virtualMask == NearTouch.None) |
| 1880 | return RawNearTouch.None; |
| 1881 | |
| 1882 | if ((virtualMask & NearTouch.PrimaryIndexTrigger) != 0) |
| 1883 | rawMask |= PrimaryIndexTrigger; |
| 1884 | if ((virtualMask & NearTouch.PrimaryThumbButtons) != 0) |
| 1885 | rawMask |= PrimaryThumbButtons; |
| 1886 | if ((virtualMask & NearTouch.SecondaryIndexTrigger) != 0) |
| 1887 | rawMask |= SecondaryIndexTrigger; |
| 1888 | if ((virtualMask & NearTouch.SecondaryThumbButtons) != 0) |
| 1889 | rawMask |= SecondaryThumbButtons; |
| 1890 | |
| 1891 | return rawMask; |
| 1892 | } |
| 1893 | } |
| 1894 | |
| 1895 | public class VirtualAxis1DMap |
| 1896 | { |
| 1897 | public RawAxis1D None = RawAxis1D.None; |
| 1898 | public RawAxis1D PrimaryIndexTrigger = RawAxis1D.None; |
| 1899 | public RawAxis1D PrimaryHandTrigger = RawAxis1D.None; |
| 1900 | public RawAxis1D SecondaryIndexTrigger = RawAxis1D.None; |
| 1901 | public RawAxis1D SecondaryHandTrigger = RawAxis1D.None; |
| 1902 | |
| 1903 | public RawAxis1D ToRawMask(Axis1D virtualMask) |
| 1904 | { |
| 1905 | RawAxis1D rawMask = 0; |
| 1906 | |
| 1907 | if (virtualMask == Axis1D.None) |
| 1908 | return RawAxis1D.None; |
| 1909 | |
| 1910 | if ((virtualMask & Axis1D.PrimaryIndexTrigger) != 0) |
| 1911 | rawMask |= PrimaryIndexTrigger; |
| 1912 | if ((virtualMask & Axis1D.PrimaryHandTrigger) != 0) |
| 1913 | rawMask |= PrimaryHandTrigger; |
| 1914 | if ((virtualMask & Axis1D.SecondaryIndexTrigger) != 0) |
| 1915 | rawMask |= SecondaryIndexTrigger; |
| 1916 | if ((virtualMask & Axis1D.SecondaryHandTrigger) != 0) |
| 1917 | rawMask |= SecondaryHandTrigger; |
| 1918 | |
| 1919 | return rawMask; |
| 1920 | } |
| 1921 | } |
| 1922 | |
| 1923 | public class VirtualAxis2DMap |
| 1924 | { |
| 1925 | public RawAxis2D None = RawAxis2D.None; |
| 1926 | public RawAxis2D PrimaryThumbstick = RawAxis2D.None; |
| 1927 | public RawAxis2D PrimaryTouchpad = RawAxis2D.None; |
| 1928 | public RawAxis2D SecondaryThumbstick = RawAxis2D.None; |
| 1929 | public RawAxis2D SecondaryTouchpad = RawAxis2D.None; |
| 1930 | |
| 1931 | public RawAxis2D ToRawMask(Axis2D virtualMask) |
| 1932 | { |
| 1933 | RawAxis2D rawMask = 0; |
| 1934 | |
| 1935 | if (virtualMask == Axis2D.None) |
| 1936 | return RawAxis2D.None; |
| 1937 | |
| 1938 | if ((virtualMask & Axis2D.PrimaryThumbstick) != 0) |
| 1939 | rawMask |= PrimaryThumbstick; |
| 1940 | if ((virtualMask & Axis2D.PrimaryTouchpad) != 0) |
| 1941 | rawMask |= PrimaryTouchpad; |
| 1942 | if ((virtualMask & Axis2D.SecondaryThumbstick) != 0) |
| 1943 | rawMask |= SecondaryThumbstick; |
| 1944 | if ((virtualMask & Axis2D.SecondaryTouchpad) != 0) |
| 1945 | rawMask |= SecondaryTouchpad; |
| 1946 | |
| 1947 | return rawMask; |
| 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | public Controller controllerType = Controller.None; |
| 1952 | public VirtualButtonMap buttonMap = new VirtualButtonMap(); |
| 1953 | public VirtualTouchMap touchMap = new VirtualTouchMap(); |
| 1954 | public VirtualNearTouchMap nearTouchMap = new VirtualNearTouchMap(); |
| 1955 | public VirtualAxis1DMap axis1DMap = new VirtualAxis1DMap(); |
| 1956 | public VirtualAxis2DMap axis2DMap = new VirtualAxis2DMap(); |
| 1957 | public OVRPlugin.ControllerState4 previousState = new OVRPlugin.ControllerState4(); |
| 1958 | public OVRPlugin.ControllerState4 currentState = new OVRPlugin.ControllerState4(); |
| 1959 | public bool shouldApplyDeadzone = true; |
| 1960 | |
| 1961 | public OVRControllerBase() |
| 1962 | { |
| 1963 | ConfigureButtonMap(); |
| 1964 | ConfigureTouchMap(); |
| 1965 | ConfigureNearTouchMap(); |
| 1966 | ConfigureAxis1DMap(); |
| 1967 | ConfigureAxis2DMap(); |
| 1968 | } |
| 1969 | |
| 1970 | public virtual Controller Update() |
| 1971 | { |
| 1972 | OVRPlugin.ControllerState4 state; |
| 1973 | |
| 1974 | if (OVRManager.loadedXRDevice == OVRManager.XRDevice.OpenVR && ( (controllerType & Controller.Touch) != 0) ) |
| 1975 | state = GetOpenVRControllerState(controllerType); |
| 1976 | else |
| 1977 | state = OVRPlugin.GetControllerState4((uint)controllerType); |
| 1978 | |
| 1979 | if (state.LIndexTrigger >= AXIS_AS_BUTTON_THRESHOLD) |
| 1980 | state.Buttons |= (uint)RawButton.LIndexTrigger; |
| 1981 | if (state.LHandTrigger >= AXIS_AS_BUTTON_THRESHOLD) |
| 1982 | state.Buttons |= (uint)RawButton.LHandTrigger; |
| 1983 | if (state.LThumbstick.y >= AXIS_AS_BUTTON_THRESHOLD) |
| 1984 | state.Buttons |= (uint)RawButton.LThumbstickUp; |
| 1985 | if (state.LThumbstick.y <= -AXIS_AS_BUTTON_THRESHOLD) |
| 1986 | state.Buttons |= (uint)RawButton.LThumbstickDown; |
| 1987 | if (state.LThumbstick.x <= -AXIS_AS_BUTTON_THRESHOLD) |
| 1988 | state.Buttons |= (uint)RawButton.LThumbstickLeft; |
| 1989 | if (state.LThumbstick.x >= AXIS_AS_BUTTON_THRESHOLD) |
| 1990 | state.Buttons |= (uint)RawButton.LThumbstickRight; |
| 1991 | |
| 1992 | if (state.RIndexTrigger >= AXIS_AS_BUTTON_THRESHOLD) |
| 1993 | state.Buttons |= (uint)RawButton.RIndexTrigger; |
| 1994 | if (state.RHandTrigger >= AXIS_AS_BUTTON_THRESHOLD) |
| 1995 | state.Buttons |= (uint)RawButton.RHandTrigger; |
| 1996 | if (state.RThumbstick.y >= AXIS_AS_BUTTON_THRESHOLD) |
| 1997 | state.Buttons |= (uint)RawButton.RThumbstickUp; |
| 1998 | if (state.RThumbstick.y <= -AXIS_AS_BUTTON_THRESHOLD) |
| 1999 | state.Buttons |= (uint)RawButton.RThumbstickDown; |
| 2000 | if (state.RThumbstick.x <= -AXIS_AS_BUTTON_THRESHOLD) |
| 2001 | state.Buttons |= (uint)RawButton.RThumbstickLeft; |
| 2002 | if (state.RThumbstick.x >= AXIS_AS_BUTTON_THRESHOLD) |
| 2003 | state.Buttons |= (uint)RawButton.RThumbstickRight; |
| 2004 | |
| 2005 | previousState = currentState; |
| 2006 | currentState = state; |
| 2007 | |
| 2008 | return ((Controller)currentState.ConnectedControllers & controllerType); |
| 2009 | } |
| 2010 | |
| 2011 | private OVRPlugin.ControllerState4 GetOpenVRControllerState(Controller controllerType) |
| 2012 | { |
| 2013 | OVRPlugin.ControllerState4 state = new OVRPlugin.ControllerState4(); |
| 2014 | |
| 2015 | if ((controllerType & Controller.LTouch) == Controller.LTouch && IsValidOpenVRDevice(openVRControllerDetails[0].deviceID)) |
| 2016 | { |
| 2017 | OVR.OpenVR.VRControllerState_t leftControllerState = openVRControllerDetails[0].state; |
| 2018 | if ((leftControllerState.ulButtonPressed & ((ulong)OpenVRButton.Two)) == (ulong)OpenVRButton.Two) |
| 2019 | state.Buttons |= (uint)RawButton.Y; |
| 2020 | if ((leftControllerState.ulButtonPressed & ((ulong)OpenVRButton.Thumbstick)) == (ulong)OpenVRButton.Thumbstick) |
| 2021 | state.Buttons |= (uint)RawButton.LThumbstick; |
| 2022 | |
| 2023 | state.LIndexTrigger = leftControllerState.rAxis1.x; |
| 2024 | |
| 2025 | if (openVRControllerDetails[0].controllerType == OpenVRController.OculusTouch || openVRControllerDetails[0].controllerType == OpenVRController.ViveController) |
| 2026 | { |
| 2027 | state.LThumbstick.x = leftControllerState.rAxis0.x; |
| 2028 | state.LThumbstick.y = leftControllerState.rAxis0.y; |
| 2029 | } |
| 2030 | else if (openVRControllerDetails[0].controllerType == OpenVRController.WindowsMRController) |
| 2031 | { |
| 2032 | state.LThumbstick.x = leftControllerState.rAxis2.x; |
| 2033 | state.LThumbstick.y = leftControllerState.rAxis2.y; |
| 2034 | } |
| 2035 | |
| 2036 | if (openVRControllerDetails[0].controllerType == OpenVRController.OculusTouch) |
| 2037 | state.LHandTrigger = leftControllerState.rAxis2.x; |
| 2038 | else if (openVRControllerDetails[0].controllerType == OpenVRController.ViveController || openVRControllerDetails[0].controllerType == OpenVRController.WindowsMRController) |
| 2039 | state.LHandTrigger = ((leftControllerState.ulButtonPressed & ((ulong)OpenVRButton.Grip)) == ((ulong)OpenVRButton.Grip)) ? 1 : 0; |
| 2040 | |
| 2041 | } |
| 2042 | |
| 2043 | if ((controllerType & Controller.RTouch) == Controller.RTouch && IsValidOpenVRDevice(openVRControllerDetails[1].deviceID)) |
| 2044 | { |
| 2045 | OVR.OpenVR.VRControllerState_t rightControllerState = openVRControllerDetails[1].state; |
| 2046 | if ((rightControllerState.ulButtonPressed & ((ulong)OpenVRButton.Two)) == (ulong)OpenVRButton.Two) |
| 2047 | state.Buttons |= (uint)RawButton.B; |
| 2048 | if ((rightControllerState.ulButtonPressed & ((ulong)OpenVRButton.Thumbstick)) == (ulong)OpenVRButton.Thumbstick) |
| 2049 | state.Buttons |= (uint)RawButton.RThumbstick; |
| 2050 | |
| 2051 | state.RIndexTrigger = rightControllerState.rAxis1.x; |
| 2052 | |
| 2053 | if (openVRControllerDetails[1].controllerType == OpenVRController.OculusTouch || openVRControllerDetails[1].controllerType == OpenVRController.ViveController) |
| 2054 | { |
| 2055 | state.RThumbstick.x = rightControllerState.rAxis0.x; |
| 2056 | state.RThumbstick.y = rightControllerState.rAxis0.y; |
| 2057 | } |
| 2058 | else if (openVRControllerDetails[1].controllerType == OpenVRController.WindowsMRController) |
| 2059 | { |
| 2060 | state.RThumbstick.x = rightControllerState.rAxis2.x; |
| 2061 | state.RThumbstick.y = rightControllerState.rAxis2.y; |
| 2062 | } |
| 2063 | |
| 2064 | if (openVRControllerDetails[1].controllerType == OpenVRController.OculusTouch) |
| 2065 | state.RHandTrigger = rightControllerState.rAxis2.x; |
| 2066 | else if (openVRControllerDetails[1].controllerType == OpenVRController.ViveController || openVRControllerDetails[1].controllerType == OpenVRController.WindowsMRController) |
| 2067 | state.RHandTrigger = ((rightControllerState.ulButtonPressed & ((ulong)OpenVRButton.Grip)) == ((ulong)OpenVRButton.Grip)) ? 1 : 0; |
| 2068 | |
| 2069 | } |
| 2070 | |
| 2071 | return state; |
| 2072 | } |
| 2073 | |
| 2074 | public virtual void SetControllerVibration(float frequency, float amplitude) |
| 2075 | { |
| 2076 | OVRPlugin.SetControllerVibration((uint)controllerType, frequency, amplitude); |
| 2077 | } |
| 2078 | |
| 2079 | public virtual void RecenterController() |
| 2080 | { |
| 2081 | OVRPlugin.RecenterTrackingOrigin(OVRPlugin.RecenterFlags.Controllers); |
| 2082 | } |
| 2083 | |
| 2084 | public virtual bool WasRecentered() |
| 2085 | { |
| 2086 | return false; |
| 2087 | } |
| 2088 | |
| 2089 | public virtual byte GetRecenterCount() |
| 2090 | { |
| 2091 | return 0; |
| 2092 | } |
| 2093 | |
| 2094 | public virtual byte GetBatteryPercentRemaining() |
| 2095 | { |
| 2096 | return 0; |
| 2097 | } |
| 2098 | |
| 2099 | public abstract void ConfigureButtonMap(); |
| 2100 | public abstract void ConfigureTouchMap(); |
| 2101 | public abstract void ConfigureNearTouchMap(); |
| 2102 | public abstract void ConfigureAxis1DMap(); |
| 2103 | public abstract void ConfigureAxis2DMap(); |
| 2104 | |
| 2105 | public RawButton ResolveToRawMask(Button virtualMask) |
| 2106 | { |
| 2107 | return buttonMap.ToRawMask(virtualMask); |
| 2108 | } |
| 2109 | |
| 2110 | public RawTouch ResolveToRawMask(Touch virtualMask) |
| 2111 | { |
| 2112 | return touchMap.ToRawMask(virtualMask); |
| 2113 | } |
| 2114 | |
| 2115 | public RawNearTouch ResolveToRawMask(NearTouch virtualMask) |
| 2116 | { |
| 2117 | return nearTouchMap.ToRawMask(virtualMask); |
| 2118 | } |
| 2119 | |
| 2120 | public RawAxis1D ResolveToRawMask(Axis1D virtualMask) |
| 2121 | { |
| 2122 | return axis1DMap.ToRawMask(virtualMask); |
| 2123 | } |
| 2124 | |
| 2125 | public RawAxis2D ResolveToRawMask(Axis2D virtualMask) |
| 2126 | { |
| 2127 | return axis2DMap.ToRawMask(virtualMask); |
| 2128 | } |
| 2129 | } |
| 2130 | |
| 2131 | private class OVRControllerTouch : OVRControllerBase |
| 2132 | { |
| 2133 | public OVRControllerTouch() |
| 2134 | { |
| 2135 | controllerType = Controller.Touch; |
| 2136 | } |
| 2137 | |
| 2138 | public override void ConfigureButtonMap() |
| 2139 | { |
| 2140 | buttonMap.None = RawButton.None; |
| 2141 | buttonMap.One = RawButton.A; |
| 2142 | buttonMap.Two = RawButton.B; |
| 2143 | buttonMap.Three = RawButton.X; |
| 2144 | buttonMap.Four = RawButton.Y; |
| 2145 | buttonMap.Start = RawButton.Start; |
| 2146 | buttonMap.Back = RawButton.None; |
| 2147 | buttonMap.PrimaryShoulder = RawButton.None; |
| 2148 | buttonMap.PrimaryIndexTrigger = RawButton.LIndexTrigger; |
| 2149 | buttonMap.PrimaryHandTrigger = RawButton.LHandTrigger; |
| 2150 | buttonMap.PrimaryThumbstick = RawButton.LThumbstick; |
| 2151 | buttonMap.PrimaryThumbstickUp = RawButton.LThumbstickUp; |
| 2152 | buttonMap.PrimaryThumbstickDown = RawButton.LThumbstickDown; |
| 2153 | buttonMap.PrimaryThumbstickLeft = RawButton.LThumbstickLeft; |
| 2154 | buttonMap.PrimaryThumbstickRight = RawButton.LThumbstickRight; |
| 2155 | buttonMap.PrimaryTouchpad = RawButton.None; |
| 2156 | buttonMap.SecondaryShoulder = RawButton.None; |
| 2157 | buttonMap.SecondaryIndexTrigger = RawButton.RIndexTrigger; |
| 2158 | buttonMap.SecondaryHandTrigger = RawButton.RHandTrigger; |
| 2159 | buttonMap.SecondaryThumbstick = RawButton.RThumbstick; |
| 2160 | buttonMap.SecondaryThumbstickUp = RawButton.RThumbstickUp; |
| 2161 | buttonMap.SecondaryThumbstickDown = RawButton.RThumbstickDown; |
| 2162 | buttonMap.SecondaryThumbstickLeft = RawButton.RThumbstickLeft; |
| 2163 | buttonMap.SecondaryThumbstickRight = RawButton.RThumbstickRight; |
| 2164 | buttonMap.SecondaryTouchpad = RawButton.None; |
| 2165 | buttonMap.DpadUp = RawButton.None; |
| 2166 | buttonMap.DpadDown = RawButton.None; |
| 2167 | buttonMap.DpadLeft = RawButton.None; |
| 2168 | buttonMap.DpadRight = RawButton.None; |
| 2169 | buttonMap.Up = RawButton.LThumbstickUp; |
| 2170 | buttonMap.Down = RawButton.LThumbstickDown; |
| 2171 | buttonMap.Left = RawButton.LThumbstickLeft; |
| 2172 | buttonMap.Right = RawButton.LThumbstickRight; |
| 2173 | } |
| 2174 | |
| 2175 | public override void ConfigureTouchMap() |
| 2176 | { |
| 2177 | touchMap.None = RawTouch.None; |
| 2178 | touchMap.One = RawTouch.A; |
| 2179 | touchMap.Two = RawTouch.B; |
| 2180 | touchMap.Three = RawTouch.X; |
| 2181 | touchMap.Four = RawTouch.Y; |
| 2182 | touchMap.PrimaryIndexTrigger = RawTouch.LIndexTrigger; |
| 2183 | touchMap.PrimaryThumbstick = RawTouch.LThumbstick; |
| 2184 | touchMap.PrimaryThumbRest = RawTouch.LThumbRest; |
| 2185 | touchMap.PrimaryTouchpad = RawTouch.None; |
| 2186 | touchMap.SecondaryIndexTrigger = RawTouch.RIndexTrigger; |
| 2187 | touchMap.SecondaryThumbstick = RawTouch.RThumbstick; |
| 2188 | touchMap.SecondaryThumbRest = RawTouch.RThumbRest; |
| 2189 | touchMap.SecondaryTouchpad = RawTouch.None; |
| 2190 | } |
| 2191 | |
| 2192 | public override void ConfigureNearTouchMap() |
| 2193 | { |
| 2194 | nearTouchMap.None = RawNearTouch.None; |
| 2195 | nearTouchMap.PrimaryIndexTrigger = RawNearTouch.LIndexTrigger; |
| 2196 | nearTouchMap.PrimaryThumbButtons = RawNearTouch.LThumbButtons; |
| 2197 | nearTouchMap.SecondaryIndexTrigger = RawNearTouch.RIndexTrigger; |
| 2198 | nearTouchMap.SecondaryThumbButtons = RawNearTouch.RThumbButtons; |
| 2199 | } |
| 2200 | |
| 2201 | public override void ConfigureAxis1DMap() |
| 2202 | { |
| 2203 | axis1DMap.None = RawAxis1D.None; |
| 2204 | axis1DMap.PrimaryIndexTrigger = RawAxis1D.LIndexTrigger; |
| 2205 | axis1DMap.PrimaryHandTrigger = RawAxis1D.LHandTrigger; |
| 2206 | axis1DMap.SecondaryIndexTrigger = RawAxis1D.RIndexTrigger; |
| 2207 | axis1DMap.SecondaryHandTrigger = RawAxis1D.RHandTrigger; |
| 2208 | } |
| 2209 | |
| 2210 | public override void ConfigureAxis2DMap() |
| 2211 | { |
| 2212 | axis2DMap.None = RawAxis2D.None; |
| 2213 | axis2DMap.PrimaryThumbstick = RawAxis2D.LThumbstick; |
| 2214 | axis2DMap.PrimaryTouchpad = RawAxis2D.None; |
| 2215 | axis2DMap.SecondaryThumbstick = RawAxis2D.RThumbstick; |
| 2216 | axis2DMap.SecondaryTouchpad = RawAxis2D.None; |
| 2217 | } |
| 2218 | |
| 2219 | public override bool WasRecentered() |
| 2220 | { |
| 2221 | return ((currentState.LRecenterCount + currentState.RRecenterCount) != (previousState.LRecenterCount + previousState.RRecenterCount)); |
| 2222 | } |
| 2223 | |
| 2224 | public override byte GetRecenterCount() |
| 2225 | { |
| 2226 | return (byte)(currentState.LRecenterCount + currentState.RRecenterCount); |
| 2227 | } |
| 2228 | |
| 2229 | public override byte GetBatteryPercentRemaining() |
| 2230 | { |
| 2231 | byte leftBattery = currentState.LBatteryPercentRemaining; |
| 2232 | byte rightBattery = currentState.RBatteryPercentRemaining; |
| 2233 | byte minBattery = (leftBattery <= rightBattery) ? leftBattery : rightBattery; |
| 2234 | |
| 2235 | return minBattery; |
| 2236 | } |
| 2237 | } |
| 2238 | |
| 2239 | private class OVRControllerLTouch : OVRControllerBase |
| 2240 | { |
| 2241 | public OVRControllerLTouch() |
| 2242 | { |
| 2243 | controllerType = Controller.LTouch; |
| 2244 | } |
| 2245 | |
| 2246 | public override void ConfigureButtonMap() |
| 2247 | { |
| 2248 | buttonMap.None = RawButton.None; |
| 2249 | buttonMap.One = RawButton.X; |
| 2250 | buttonMap.Two = RawButton.Y; |
| 2251 | buttonMap.Three = RawButton.None; |
| 2252 | buttonMap.Four = RawButton.None; |
| 2253 | buttonMap.Start = RawButton.Start; |
| 2254 | buttonMap.Back = RawButton.None; |
| 2255 | buttonMap.PrimaryShoulder = RawButton.None; |
| 2256 | buttonMap.PrimaryIndexTrigger = RawButton.LIndexTrigger; |
| 2257 | buttonMap.PrimaryHandTrigger = RawButton.LHandTrigger; |
| 2258 | buttonMap.PrimaryThumbstick = RawButton.LThumbstick; |
| 2259 | buttonMap.PrimaryThumbstickUp = RawButton.LThumbstickUp; |
| 2260 | buttonMap.PrimaryThumbstickDown = RawButton.LThumbstickDown; |
| 2261 | buttonMap.PrimaryThumbstickLeft = RawButton.LThumbstickLeft; |
| 2262 | buttonMap.PrimaryThumbstickRight = RawButton.LThumbstickRight; |
| 2263 | buttonMap.PrimaryTouchpad = RawButton.None; |
| 2264 | buttonMap.SecondaryShoulder = RawButton.None; |
| 2265 | buttonMap.SecondaryIndexTrigger = RawButton.None; |
| 2266 | buttonMap.SecondaryHandTrigger = RawButton.None; |
| 2267 | buttonMap.SecondaryThumbstick = RawButton.None; |
| 2268 | buttonMap.SecondaryThumbstickUp = RawButton.None; |
| 2269 | buttonMap.SecondaryThumbstickDown = RawButton.None; |
| 2270 | buttonMap.SecondaryThumbstickLeft = RawButton.None; |
| 2271 | buttonMap.SecondaryThumbstickRight = RawButton.None; |
| 2272 | buttonMap.SecondaryTouchpad = RawButton.None; |
| 2273 | buttonMap.DpadUp = RawButton.None; |
| 2274 | buttonMap.DpadDown = RawButton.None; |
| 2275 | buttonMap.DpadLeft = RawButton.None; |
| 2276 | buttonMap.DpadRight = RawButton.None; |
| 2277 | buttonMap.Up = RawButton.LThumbstickUp; |
| 2278 | buttonMap.Down = RawButton.LThumbstickDown; |
| 2279 | buttonMap.Left = RawButton.LThumbstickLeft; |
| 2280 | buttonMap.Right = RawButton.LThumbstickRight; |
| 2281 | } |
| 2282 | |
| 2283 | public override void ConfigureTouchMap() |
| 2284 | { |
| 2285 | touchMap.None = RawTouch.None; |
| 2286 | touchMap.One = RawTouch.X; |
| 2287 | touchMap.Two = RawTouch.Y; |
| 2288 | touchMap.Three = RawTouch.None; |
| 2289 | touchMap.Four = RawTouch.None; |
| 2290 | touchMap.PrimaryIndexTrigger = RawTouch.LIndexTrigger; |
| 2291 | touchMap.PrimaryThumbstick = RawTouch.LThumbstick; |
| 2292 | touchMap.PrimaryThumbRest = RawTouch.LThumbRest; |
| 2293 | touchMap.PrimaryTouchpad = RawTouch.None; |
| 2294 | touchMap.SecondaryIndexTrigger = RawTouch.None; |
| 2295 | touchMap.SecondaryThumbstick = RawTouch.None; |
| 2296 | touchMap.SecondaryThumbRest = RawTouch.None; |
| 2297 | touchMap.SecondaryTouchpad = RawTouch.None; |
| 2298 | } |
| 2299 | |
| 2300 | public override void ConfigureNearTouchMap() |
| 2301 | { |
| 2302 | nearTouchMap.None = RawNearTouch.None; |
| 2303 | nearTouchMap.PrimaryIndexTrigger = RawNearTouch.LIndexTrigger; |
| 2304 | nearTouchMap.PrimaryThumbButtons = RawNearTouch.LThumbButtons; |
| 2305 | nearTouchMap.SecondaryIndexTrigger = RawNearTouch.None; |
| 2306 | nearTouchMap.SecondaryThumbButtons = RawNearTouch.None; |
| 2307 | } |
| 2308 | |
| 2309 | public override void ConfigureAxis1DMap() |
| 2310 | { |
| 2311 | axis1DMap.None = RawAxis1D.None; |
| 2312 | axis1DMap.PrimaryIndexTrigger = RawAxis1D.LIndexTrigger; |
| 2313 | axis1DMap.PrimaryHandTrigger = RawAxis1D.LHandTrigger; |
| 2314 | axis1DMap.SecondaryIndexTrigger = RawAxis1D.None; |
| 2315 | axis1DMap.SecondaryHandTrigger = RawAxis1D.None; |
| 2316 | } |
| 2317 | |
| 2318 | public override void ConfigureAxis2DMap() |
| 2319 | { |
| 2320 | axis2DMap.None = RawAxis2D.None; |
| 2321 | axis2DMap.PrimaryThumbstick = RawAxis2D.LThumbstick; |
| 2322 | axis2DMap.PrimaryTouchpad = RawAxis2D.None; |
| 2323 | axis2DMap.SecondaryThumbstick = RawAxis2D.None; |
| 2324 | axis2DMap.SecondaryTouchpad = RawAxis2D.None; |
| 2325 | } |
| 2326 | |
| 2327 | public override bool WasRecentered() |
| 2328 | { |
| 2329 | return (currentState.LRecenterCount != previousState.LRecenterCount); |
| 2330 | } |
| 2331 | |
| 2332 | public override byte GetRecenterCount() |
| 2333 | { |
| 2334 | return currentState.LRecenterCount; |
| 2335 | } |
| 2336 | |
| 2337 | public override byte GetBatteryPercentRemaining() |
| 2338 | { |
| 2339 | return currentState.LBatteryPercentRemaining; |
| 2340 | } |
| 2341 | } |
| 2342 | |
| 2343 | private class OVRControllerRTouch : OVRControllerBase |
| 2344 | { |
| 2345 | public OVRControllerRTouch() |
| 2346 | { |
| 2347 | controllerType = Controller.RTouch; |
| 2348 | } |
| 2349 | |
| 2350 | public override void ConfigureButtonMap() |
| 2351 | { |
| 2352 | buttonMap.None = RawButton.None; |
| 2353 | buttonMap.One = RawButton.A; |
| 2354 | buttonMap.Two = RawButton.B; |
| 2355 | buttonMap.Three = RawButton.None; |
| 2356 | buttonMap.Four = RawButton.None; |
| 2357 | buttonMap.Start = RawButton.None; |
| 2358 | buttonMap.Back = RawButton.None; |
| 2359 | buttonMap.PrimaryShoulder = RawButton.None; |
| 2360 | buttonMap.PrimaryIndexTrigger = RawButton.RIndexTrigger; |
| 2361 | buttonMap.PrimaryHandTrigger = RawButton.RHandTrigger; |
| 2362 | buttonMap.PrimaryThumbstick = RawButton.RThumbstick; |
| 2363 | buttonMap.PrimaryThumbstickUp = RawButton.RThumbstickUp; |
| 2364 | buttonMap.PrimaryThumbstickDown = RawButton.RThumbstickDown; |
| 2365 | buttonMap.PrimaryThumbstickLeft = RawButton.RThumbstickLeft; |
| 2366 | buttonMap.PrimaryThumbstickRight = RawButton.RThumbstickRight; |
| 2367 | buttonMap.PrimaryTouchpad = RawButton.None; |
| 2368 | buttonMap.SecondaryShoulder = RawButton.None; |
| 2369 | buttonMap.SecondaryIndexTrigger = RawButton.None; |
| 2370 | buttonMap.SecondaryHandTrigger = RawButton.None; |
| 2371 | buttonMap.SecondaryThumbstick = RawButton.None; |
| 2372 | buttonMap.SecondaryThumbstickUp = RawButton.None; |
| 2373 | buttonMap.SecondaryThumbstickDown = RawButton.None; |
| 2374 | buttonMap.SecondaryThumbstickLeft = RawButton.None; |
| 2375 | buttonMap.SecondaryThumbstickRight = RawButton.None; |
| 2376 | buttonMap.SecondaryTouchpad = RawButton.None; |
| 2377 | buttonMap.DpadUp = RawButton.None; |
| 2378 | buttonMap.DpadDown = RawButton.None; |
| 2379 | buttonMap.DpadLeft = RawButton.None; |
| 2380 | buttonMap.DpadRight = RawButton.None; |
| 2381 | buttonMap.Up = RawButton.RThumbstickUp; |
| 2382 | buttonMap.Down = RawButton.RThumbstickDown; |
| 2383 | buttonMap.Left = RawButton.RThumbstickLeft; |
| 2384 | buttonMap.Right = RawButton.RThumbstickRight; |
| 2385 | } |
| 2386 | |
| 2387 | public override void ConfigureTouchMap() |
| 2388 | { |
| 2389 | touchMap.None = RawTouch.None; |
| 2390 | touchMap.One = RawTouch.A; |
| 2391 | touchMap.Two = RawTouch.B; |
| 2392 | touchMap.Three = RawTouch.None; |
| 2393 | touchMap.Four = RawTouch.None; |
| 2394 | touchMap.PrimaryIndexTrigger = RawTouch.RIndexTrigger; |
| 2395 | touchMap.PrimaryThumbstick = RawTouch.RThumbstick; |
| 2396 | touchMap.PrimaryThumbRest = RawTouch.RThumbRest; |
| 2397 | touchMap.PrimaryTouchpad = RawTouch.None; |
| 2398 | touchMap.SecondaryIndexTrigger = RawTouch.None; |
| 2399 | touchMap.SecondaryThumbstick = RawTouch.None; |
| 2400 | touchMap.SecondaryThumbRest = RawTouch.None; |
| 2401 | touchMap.SecondaryTouchpad = RawTouch.None; |
| 2402 | } |
| 2403 | |
| 2404 | public override void ConfigureNearTouchMap() |
| 2405 | { |
| 2406 | nearTouchMap.None = RawNearTouch.None; |
| 2407 | nearTouchMap.PrimaryIndexTrigger = RawNearTouch.RIndexTrigger; |
| 2408 | nearTouchMap.PrimaryThumbButtons = RawNearTouch.RThumbButtons; |
| 2409 | nearTouchMap.SecondaryIndexTrigger = RawNearTouch.None; |
| 2410 | nearTouchMap.SecondaryThumbButtons = RawNearTouch.None; |
| 2411 | } |
| 2412 | |
| 2413 | public override void ConfigureAxis1DMap() |
| 2414 | { |
| 2415 | axis1DMap.None = RawAxis1D.None; |
| 2416 | axis1DMap.PrimaryIndexTrigger = RawAxis1D.RIndexTrigger; |
| 2417 | axis1DMap.PrimaryHandTrigger = RawAxis1D.RHandTrigger; |
| 2418 | axis1DMap.SecondaryIndexTrigger = RawAxis1D.None; |
| 2419 | axis1DMap.SecondaryHandTrigger = RawAxis1D.None; |
| 2420 | } |
| 2421 | |
| 2422 | public override void ConfigureAxis2DMap() |
| 2423 | { |
| 2424 | axis2DMap.None = RawAxis2D.None; |
| 2425 | axis2DMap.PrimaryThumbstick = RawAxis2D.RThumbstick; |
| 2426 | axis2DMap.PrimaryTouchpad = RawAxis2D.None; |
| 2427 | axis2DMap.SecondaryThumbstick = RawAxis2D.None; |
| 2428 | axis2DMap.SecondaryTouchpad = RawAxis2D.None; |
| 2429 | } |
| 2430 | |
| 2431 | public override bool WasRecentered() |
| 2432 | { |
| 2433 | return (currentState.RRecenterCount != previousState.RRecenterCount); |
| 2434 | } |
| 2435 | |
| 2436 | public override byte GetRecenterCount() |
| 2437 | { |
| 2438 | return currentState.RRecenterCount; |
| 2439 | } |
| 2440 | |
| 2441 | public override byte GetBatteryPercentRemaining() |
| 2442 | { |
| 2443 | return currentState.RBatteryPercentRemaining; |
| 2444 | } |
| 2445 | } |
| 2446 | |
| 2447 | private class OVRControllerRemote : OVRControllerBase |
| 2448 | { |
| 2449 | public OVRControllerRemote() |
| 2450 | { |
| 2451 | controllerType = Controller.Remote; |
| 2452 | } |
| 2453 | |
| 2454 | public override void ConfigureButtonMap() |
| 2455 | { |
| 2456 | buttonMap.None = RawButton.None; |
| 2457 | buttonMap.One = RawButton.Start; |
| 2458 | buttonMap.Two = RawButton.Back; |
| 2459 | buttonMap.Three = RawButton.None; |
| 2460 | buttonMap.Four = RawButton.None; |
| 2461 | buttonMap.Start = RawButton.Start; |
| 2462 | buttonMap.Back = RawButton.Back; |
| 2463 | buttonMap.PrimaryShoulder = RawButton.None; |
| 2464 | buttonMap.PrimaryIndexTrigger = RawButton.None; |
| 2465 | buttonMap.PrimaryHandTrigger = RawButton.None; |
| 2466 | buttonMap.PrimaryThumbstick = RawButton.None; |
| 2467 | buttonMap.PrimaryThumbstickUp = RawButton.None; |
| 2468 | buttonMap.PrimaryThumbstickDown = RawButton.None; |
| 2469 | buttonMap.PrimaryThumbstickLeft = RawButton.None; |
| 2470 | buttonMap.PrimaryThumbstickRight = RawButton.None; |
| 2471 | buttonMap.PrimaryTouchpad = RawButton.None; |
| 2472 | buttonMap.SecondaryShoulder = RawButton.None; |
| 2473 | buttonMap.SecondaryIndexTrigger = RawButton.None; |
| 2474 | buttonMap.SecondaryHandTrigger = RawButton.None; |
| 2475 | buttonMap.SecondaryThumbstick = RawButton.None; |
| 2476 | buttonMap.SecondaryThumbstickUp = RawButton.None; |
| 2477 | buttonMap.SecondaryThumbstickDown = RawButton.None; |
| 2478 | buttonMap.SecondaryThumbstickLeft = RawButton.None; |
| 2479 | buttonMap.SecondaryThumbstickRight = RawButton.None; |
| 2480 | buttonMap.SecondaryTouchpad = RawButton.None; |
| 2481 | buttonMap.DpadUp = RawButton.DpadUp; |
| 2482 | buttonMap.DpadDown = RawButton.DpadDown; |
| 2483 | buttonMap.DpadLeft = RawButton.DpadLeft; |
| 2484 | buttonMap.DpadRight = RawButton.DpadRight; |
| 2485 | buttonMap.Up = RawButton.DpadUp; |
| 2486 | buttonMap.Down = RawButton.DpadDown; |
| 2487 | buttonMap.Left = RawButton.DpadLeft; |
| 2488 | buttonMap.Right = RawButton.DpadRight; |
| 2489 | } |
| 2490 | |
| 2491 | public override void ConfigureTouchMap() |
| 2492 | { |
| 2493 | touchMap.None = RawTouch.None; |
| 2494 | touchMap.One = RawTouch.None; |
| 2495 | touchMap.Two = RawTouch.None; |
| 2496 | touchMap.Three = RawTouch.None; |
| 2497 | touchMap.Four = RawTouch.None; |
| 2498 | touchMap.PrimaryIndexTrigger = RawTouch.None; |
| 2499 | touchMap.PrimaryThumbstick = RawTouch.None; |
| 2500 | touchMap.PrimaryThumbRest = RawTouch.None; |
| 2501 | touchMap.PrimaryTouchpad = RawTouch.None; |
| 2502 | touchMap.SecondaryIndexTrigger = RawTouch.None; |
| 2503 | touchMap.SecondaryThumbstick = RawTouch.None; |
| 2504 | touchMap.SecondaryThumbRest = RawTouch.None; |
| 2505 | touchMap.SecondaryTouchpad = RawTouch.None; |
| 2506 | } |
| 2507 | |
| 2508 | public override void ConfigureNearTouchMap() |
| 2509 | { |
| 2510 | nearTouchMap.None = RawNearTouch.None; |
| 2511 | nearTouchMap.PrimaryIndexTrigger = RawNearTouch.None; |
| 2512 | nearTouchMap.PrimaryThumbButtons = RawNearTouch.None; |
| 2513 | nearTouchMap.SecondaryIndexTrigger = RawNearTouch.None; |
| 2514 | nearTouchMap.SecondaryThumbButtons = RawNearTouch.None; |
| 2515 | } |
| 2516 | |
| 2517 | public override void ConfigureAxis1DMap() |
| 2518 | { |
| 2519 | axis1DMap.None = RawAxis1D.None; |
| 2520 | axis1DMap.PrimaryIndexTrigger = RawAxis1D.None; |
| 2521 | axis1DMap.PrimaryHandTrigger = RawAxis1D.None; |
| 2522 | axis1DMap.SecondaryIndexTrigger = RawAxis1D.None; |
| 2523 | axis1DMap.SecondaryHandTrigger = RawAxis1D.None; |
| 2524 | } |
| 2525 | |
| 2526 | public override void ConfigureAxis2DMap() |
| 2527 | { |
| 2528 | axis2DMap.None = RawAxis2D.None; |
| 2529 | axis2DMap.PrimaryThumbstick = RawAxis2D.None; |
| 2530 | axis2DMap.PrimaryTouchpad = RawAxis2D.None; |
| 2531 | axis2DMap.SecondaryThumbstick = RawAxis2D.None; |
| 2532 | axis2DMap.SecondaryTouchpad = RawAxis2D.None; |
| 2533 | } |
| 2534 | } |
| 2535 | |
| 2536 | private class OVRControllerGamepadPC : OVRControllerBase |
| 2537 | { |
| 2538 | public OVRControllerGamepadPC() |
| 2539 | { |
| 2540 | controllerType = Controller.Gamepad; |
| 2541 | } |
| 2542 | |
| 2543 | public override void ConfigureButtonMap() |
| 2544 | { |
| 2545 | buttonMap.None = RawButton.None; |
| 2546 | buttonMap.One = RawButton.A; |
| 2547 | buttonMap.Two = RawButton.B; |
| 2548 | buttonMap.Three = RawButton.X; |
| 2549 | buttonMap.Four = RawButton.Y; |
| 2550 | buttonMap.Start = RawButton.Start; |
| 2551 | buttonMap.Back = RawButton.Back; |
| 2552 | buttonMap.PrimaryShoulder = RawButton.LShoulder; |
| 2553 | buttonMap.PrimaryIndexTrigger = RawButton.LIndexTrigger; |
| 2554 | buttonMap.PrimaryHandTrigger = RawButton.None; |
| 2555 | buttonMap.PrimaryThumbstick = RawButton.LThumbstick; |
| 2556 | buttonMap.PrimaryThumbstickUp = RawButton.LThumbstickUp; |
| 2557 | buttonMap.PrimaryThumbstickDown = RawButton.LThumbstickDown; |
| 2558 | buttonMap.PrimaryThumbstickLeft = RawButton.LThumbstickLeft; |
| 2559 | buttonMap.PrimaryThumbstickRight = RawButton.LThumbstickRight; |
| 2560 | buttonMap.PrimaryTouchpad = RawButton.None; |
| 2561 | buttonMap.SecondaryShoulder = RawButton.RShoulder; |
| 2562 | buttonMap.SecondaryIndexTrigger = RawButton.RIndexTrigger; |
| 2563 | buttonMap.SecondaryHandTrigger = RawButton.None; |
| 2564 | buttonMap.SecondaryThumbstick = RawButton.RThumbstick; |
| 2565 | buttonMap.SecondaryThumbstickUp = RawButton.RThumbstickUp; |
| 2566 | buttonMap.SecondaryThumbstickDown = RawButton.RThumbstickDown; |
| 2567 | buttonMap.SecondaryThumbstickLeft = RawButton.RThumbstickLeft; |
| 2568 | buttonMap.SecondaryThumbstickRight = RawButton.RThumbstickRight; |
| 2569 | buttonMap.SecondaryTouchpad = RawButton.None; |
| 2570 | buttonMap.DpadUp = RawButton.DpadUp; |
| 2571 | buttonMap.DpadDown = RawButton.DpadDown; |
| 2572 | buttonMap.DpadLeft = RawButton.DpadLeft; |
| 2573 | buttonMap.DpadRight = RawButton.DpadRight; |
| 2574 | buttonMap.Up = RawButton.LThumbstickUp; |
| 2575 | buttonMap.Down = RawButton.LThumbstickDown; |
| 2576 | buttonMap.Left = RawButton.LThumbstickLeft; |
| 2577 | buttonMap.Right = RawButton.LThumbstickRight; |
| 2578 | } |
| 2579 | |
| 2580 | public override void ConfigureTouchMap() |
| 2581 | { |
| 2582 | touchMap.None = RawTouch.None; |
| 2583 | touchMap.One = RawTouch.None; |
| 2584 | touchMap.Two = RawTouch.None; |
| 2585 | touchMap.Three = RawTouch.None; |
| 2586 | touchMap.Four = RawTouch.None; |
| 2587 | touchMap.PrimaryIndexTrigger = RawTouch.None; |
| 2588 | touchMap.PrimaryThumbstick = RawTouch.None; |
| 2589 | touchMap.PrimaryThumbRest = RawTouch.None; |
| 2590 | touchMap.PrimaryTouchpad = RawTouch.None; |
| 2591 | touchMap.SecondaryIndexTrigger = RawTouch.None; |
| 2592 | touchMap.SecondaryThumbstick = RawTouch.None; |
| 2593 | touchMap.SecondaryThumbRest = RawTouch.None; |
| 2594 | touchMap.SecondaryTouchpad = RawTouch.None; |
| 2595 | } |
| 2596 | |
| 2597 | public override void ConfigureNearTouchMap() |
| 2598 | { |
| 2599 | nearTouchMap.None = RawNearTouch.None; |
| 2600 | nearTouchMap.PrimaryIndexTrigger = RawNearTouch.None; |
| 2601 | nearTouchMap.PrimaryThumbButtons = RawNearTouch.None; |
| 2602 | nearTouchMap.SecondaryIndexTrigger = RawNearTouch.None; |
| 2603 | nearTouchMap.SecondaryThumbButtons = RawNearTouch.None; |
| 2604 | } |
| 2605 | |
| 2606 | public override void ConfigureAxis1DMap() |
| 2607 | { |
| 2608 | axis1DMap.None = RawAxis1D.None; |
| 2609 | axis1DMap.PrimaryIndexTrigger = RawAxis1D.LIndexTrigger; |
| 2610 | axis1DMap.PrimaryHandTrigger = RawAxis1D.None; |
| 2611 | axis1DMap.SecondaryIndexTrigger = RawAxis1D.RIndexTrigger; |
| 2612 | axis1DMap.SecondaryHandTrigger = RawAxis1D.None; |
| 2613 | } |
| 2614 | |
| 2615 | public override void ConfigureAxis2DMap() |
| 2616 | { |
| 2617 | axis2DMap.None = RawAxis2D.None; |
| 2618 | axis2DMap.PrimaryThumbstick = RawAxis2D.LThumbstick; |
| 2619 | axis2DMap.PrimaryTouchpad = RawAxis2D.None; |
| 2620 | axis2DMap.SecondaryThumbstick = RawAxis2D.RThumbstick; |
| 2621 | axis2DMap.SecondaryTouchpad = RawAxis2D.None; |
| 2622 | } |
| 2623 | } |
| 2624 | |
| 2625 | private class OVRControllerGamepadMac : OVRControllerBase |
| 2626 | { |
| 2627 | /// <summary> An axis on the gamepad. </summary> |
| 2628 | private enum AxisGPC |
| 2629 | { |
| 2630 | None = -1, |
| 2631 | LeftXAxis = 0, |
| 2632 | LeftYAxis, |
| 2633 | RightXAxis, |
| 2634 | RightYAxis, |
| 2635 | LeftTrigger, |
| 2636 | RightTrigger, |
| 2637 | DPad_X_Axis, |
| 2638 | DPad_Y_Axis, |
| 2639 | Max, |
| 2640 | }; |
| 2641 | |
| 2642 | /// <summary> A button on the gamepad. </summary> |
| 2643 | public enum ButtonGPC |
| 2644 | { |
| 2645 | None = -1, |
| 2646 | A = 0, |
| 2647 | B, |
| 2648 | X, |
| 2649 | Y, |
| 2650 | Up, |
| 2651 | Down, |
| 2652 | Left, |
| 2653 | Right, |
| 2654 | Start, |
| 2655 | Back, |
| 2656 | LStick, |
| 2657 | RStick, |
| 2658 | LeftShoulder, |
| 2659 | RightShoulder, |
| 2660 | Max |
| 2661 | }; |
| 2662 | |
| 2663 | private bool initialized = false; |
| 2664 | |
| 2665 | public OVRControllerGamepadMac() |
| 2666 | { |
| 2667 | controllerType = Controller.Gamepad; |
| 2668 | |
| 2669 | initialized = OVR_GamepadController_Initialize(); |
| 2670 | } |
| 2671 | |
| 2672 | ~OVRControllerGamepadMac() |
| 2673 | { |
| 2674 | if (!initialized) |
| 2675 | return; |
| 2676 | |
| 2677 | OVR_GamepadController_Destroy(); |
| 2678 | } |
| 2679 | |
| 2680 | public override Controller Update() |
| 2681 | { |
| 2682 | if (!initialized) |
| 2683 | { |
| 2684 | return Controller.None; |
| 2685 | } |
| 2686 | |
| 2687 | OVRPlugin.ControllerState4 state = new OVRPlugin.ControllerState4(); |
| 2688 | |
| 2689 | bool result = OVR_GamepadController_Update(); |
| 2690 | |
| 2691 | if (result) |
| 2692 | state.ConnectedControllers = (uint)Controller.Gamepad; |
| 2693 | |
| 2694 | if (OVR_GamepadController_GetButton((int)ButtonGPC.A)) |
| 2695 | state.Buttons |= (uint)RawButton.A; |
| 2696 | if (OVR_GamepadController_GetButton((int)ButtonGPC.B)) |
| 2697 | state.Buttons |= (uint)RawButton.B; |
| 2698 | if (OVR_GamepadController_GetButton((int)ButtonGPC.X)) |
| 2699 | state.Buttons |= (uint)RawButton.X; |
| 2700 | if (OVR_GamepadController_GetButton((int)ButtonGPC.Y)) |
| 2701 | state.Buttons |= (uint)RawButton.Y; |
| 2702 | if (OVR_GamepadController_GetButton((int)ButtonGPC.Up)) |
| 2703 | state.Buttons |= (uint)RawButton.DpadUp; |
| 2704 | if (OVR_GamepadController_GetButton((int)ButtonGPC.Down)) |
| 2705 | state.Buttons |= (uint)RawButton.DpadDown; |
| 2706 | if (OVR_GamepadController_GetButton((int)ButtonGPC.Left)) |
| 2707 | state.Buttons |= (uint)RawButton.DpadLeft; |
| 2708 | if (OVR_GamepadController_GetButton((int)ButtonGPC.Right)) |
| 2709 | state.Buttons |= (uint)RawButton.DpadRight; |
| 2710 | if (OVR_GamepadController_GetButton((int)ButtonGPC.Start)) |
| 2711 | state.Buttons |= (uint)RawButton.Start; |
| 2712 | if (OVR_GamepadController_GetButton((int)ButtonGPC.Back)) |
| 2713 | state.Buttons |= (uint)RawButton.Back; |
| 2714 | if (OVR_GamepadController_GetButton((int)ButtonGPC.LStick)) |
| 2715 | state.Buttons |= (uint)RawButton.LThumbstick; |
| 2716 | if (OVR_GamepadController_GetButton((int)ButtonGPC.RStick)) |
| 2717 | state.Buttons |= (uint)RawButton.RThumbstick; |
| 2718 | if (OVR_GamepadController_GetButton((int)ButtonGPC.LeftShoulder)) |
| 2719 | state.Buttons |= (uint)RawButton.LShoulder; |
| 2720 | if (OVR_GamepadController_GetButton((int)ButtonGPC.RightShoulder)) |
| 2721 | state.Buttons |= (uint)RawButton.RShoulder; |
| 2722 | |
| 2723 | state.LThumbstick.x = OVR_GamepadController_GetAxis((int)AxisGPC.LeftXAxis); |
| 2724 | state.LThumbstick.y = OVR_GamepadController_GetAxis((int)AxisGPC.LeftYAxis); |
| 2725 | state.RThumbstick.x = OVR_GamepadController_GetAxis((int)AxisGPC.RightXAxis); |
| 2726 | state.RThumbstick.y = OVR_GamepadController_GetAxis((int)AxisGPC.RightYAxis); |
| 2727 | state.LIndexTrigger = OVR_GamepadController_GetAxis((int)AxisGPC.LeftTrigger); |
| 2728 | state.RIndexTrigger = OVR_GamepadController_GetAxis((int)AxisGPC.RightTrigger); |
| 2729 | |
| 2730 | if (state.LIndexTrigger >= AXIS_AS_BUTTON_THRESHOLD) |
| 2731 | state.Buttons |= (uint)RawButton.LIndexTrigger; |
| 2732 | if (state.LHandTrigger >= AXIS_AS_BUTTON_THRESHOLD) |
| 2733 | state.Buttons |= (uint)RawButton.LHandTrigger; |
| 2734 | if (state.LThumbstick.y >= AXIS_AS_BUTTON_THRESHOLD) |
| 2735 | state.Buttons |= (uint)RawButton.LThumbstickUp; |
| 2736 | if (state.LThumbstick.y <= -AXIS_AS_BUTTON_THRESHOLD) |
| 2737 | state.Buttons |= (uint)RawButton.LThumbstickDown; |
| 2738 | if (state.LThumbstick.x <= -AXIS_AS_BUTTON_THRESHOLD) |
| 2739 | state.Buttons |= (uint)RawButton.LThumbstickLeft; |
| 2740 | if (state.LThumbstick.x >= AXIS_AS_BUTTON_THRESHOLD) |
| 2741 | state.Buttons |= (uint)RawButton.LThumbstickRight; |
| 2742 | |
| 2743 | if (state.RIndexTrigger >= AXIS_AS_BUTTON_THRESHOLD) |
| 2744 | state.Buttons |= (uint)RawButton.RIndexTrigger; |
| 2745 | if (state.RHandTrigger >= AXIS_AS_BUTTON_THRESHOLD) |
| 2746 | state.Buttons |= (uint)RawButton.RHandTrigger; |
| 2747 | if (state.RThumbstick.y >= AXIS_AS_BUTTON_THRESHOLD) |
| 2748 | state.Buttons |= (uint)RawButton.RThumbstickUp; |
| 2749 | if (state.RThumbstick.y <= -AXIS_AS_BUTTON_THRESHOLD) |
| 2750 | state.Buttons |= (uint)RawButton.RThumbstickDown; |
| 2751 | if (state.RThumbstick.x <= -AXIS_AS_BUTTON_THRESHOLD) |
| 2752 | state.Buttons |= (uint)RawButton.RThumbstickLeft; |
| 2753 | if (state.RThumbstick.x >= AXIS_AS_BUTTON_THRESHOLD) |
| 2754 | state.Buttons |= (uint)RawButton.RThumbstickRight; |
| 2755 | |
| 2756 | previousState = currentState; |
| 2757 | currentState = state; |
| 2758 | |
| 2759 | return ((Controller)currentState.ConnectedControllers & controllerType); |
| 2760 | } |
| 2761 | |
| 2762 | public override void ConfigureButtonMap() |
| 2763 | { |
| 2764 | buttonMap.None = RawButton.None; |
| 2765 | buttonMap.One = RawButton.A; |
| 2766 | buttonMap.Two = RawButton.B; |
| 2767 | buttonMap.Three = RawButton.X; |
| 2768 | buttonMap.Four = RawButton.Y; |
| 2769 | buttonMap.Start = RawButton.Start; |
| 2770 | buttonMap.Back = RawButton.Back; |
| 2771 | buttonMap.PrimaryShoulder = RawButton.LShoulder; |
| 2772 | buttonMap.PrimaryIndexTrigger = RawButton.LIndexTrigger; |
| 2773 | buttonMap.PrimaryHandTrigger = RawButton.None; |
| 2774 | buttonMap.PrimaryThumbstick = RawButton.LThumbstick; |
| 2775 | buttonMap.PrimaryThumbstickUp = RawButton.LThumbstickUp; |
| 2776 | buttonMap.PrimaryThumbstickDown = RawButton.LThumbstickDown; |
| 2777 | buttonMap.PrimaryThumbstickLeft = RawButton.LThumbstickLeft; |
| 2778 | buttonMap.PrimaryThumbstickRight = RawButton.LThumbstickRight; |
| 2779 | buttonMap.PrimaryTouchpad = RawButton.None; |
| 2780 | buttonMap.SecondaryShoulder = RawButton.RShoulder; |
| 2781 | buttonMap.SecondaryIndexTrigger = RawButton.RIndexTrigger; |
| 2782 | buttonMap.SecondaryHandTrigger = RawButton.None; |
| 2783 | buttonMap.SecondaryThumbstick = RawButton.RThumbstick; |
| 2784 | buttonMap.SecondaryThumbstickUp = RawButton.RThumbstickUp; |
| 2785 | buttonMap.SecondaryThumbstickDown = RawButton.RThumbstickDown; |
| 2786 | buttonMap.SecondaryThumbstickLeft = RawButton.RThumbstickLeft; |
| 2787 | buttonMap.SecondaryThumbstickRight = RawButton.RThumbstickRight; |
| 2788 | buttonMap.SecondaryTouchpad = RawButton.None; |
| 2789 | buttonMap.DpadUp = RawButton.DpadUp; |
| 2790 | buttonMap.DpadDown = RawButton.DpadDown; |
| 2791 | buttonMap.DpadLeft = RawButton.DpadLeft; |
| 2792 | buttonMap.DpadRight = RawButton.DpadRight; |
| 2793 | buttonMap.Up = RawButton.LThumbstickUp; |
| 2794 | buttonMap.Down = RawButton.LThumbstickDown; |
| 2795 | buttonMap.Left = RawButton.LThumbstickLeft; |
| 2796 | buttonMap.Right = RawButton.LThumbstickRight; |
| 2797 | } |
| 2798 | |
| 2799 | public override void ConfigureTouchMap() |
| 2800 | { |
| 2801 | touchMap.None = RawTouch.None; |
| 2802 | touchMap.One = RawTouch.None; |
| 2803 | touchMap.Two = RawTouch.None; |
| 2804 | touchMap.Three = RawTouch.None; |
| 2805 | touchMap.Four = RawTouch.None; |
| 2806 | touchMap.PrimaryIndexTrigger = RawTouch.None; |
| 2807 | touchMap.PrimaryThumbstick = RawTouch.None; |
| 2808 | touchMap.PrimaryThumbRest = RawTouch.None; |
| 2809 | touchMap.PrimaryTouchpad = RawTouch.None; |
| 2810 | touchMap.SecondaryIndexTrigger = RawTouch.None; |
| 2811 | touchMap.SecondaryThumbstick = RawTouch.None; |
| 2812 | touchMap.SecondaryThumbRest = RawTouch.None; |
| 2813 | touchMap.SecondaryTouchpad = RawTouch.None; |
| 2814 | } |
| 2815 | |
| 2816 | public override void ConfigureNearTouchMap() |
| 2817 | { |
| 2818 | nearTouchMap.None = RawNearTouch.None; |
| 2819 | nearTouchMap.PrimaryIndexTrigger = RawNearTouch.None; |
| 2820 | nearTouchMap.PrimaryThumbButtons = RawNearTouch.None; |
| 2821 | nearTouchMap.SecondaryIndexTrigger = RawNearTouch.None; |
| 2822 | nearTouchMap.SecondaryThumbButtons = RawNearTouch.None; |
| 2823 | } |
| 2824 | |
| 2825 | public override void ConfigureAxis1DMap() |
| 2826 | { |
| 2827 | axis1DMap.None = RawAxis1D.None; |
| 2828 | axis1DMap.PrimaryIndexTrigger = RawAxis1D.LIndexTrigger; |
| 2829 | axis1DMap.PrimaryHandTrigger = RawAxis1D.None; |
| 2830 | axis1DMap.SecondaryIndexTrigger = RawAxis1D.RIndexTrigger; |
| 2831 | axis1DMap.SecondaryHandTrigger = RawAxis1D.None; |
| 2832 | } |
| 2833 | |
| 2834 | public override void ConfigureAxis2DMap() |
| 2835 | { |
| 2836 | axis2DMap.None = RawAxis2D.None; |
| 2837 | axis2DMap.PrimaryThumbstick = RawAxis2D.LThumbstick; |
| 2838 | axis2DMap.PrimaryTouchpad = RawAxis2D.None; |
| 2839 | axis2DMap.SecondaryThumbstick = RawAxis2D.RThumbstick; |
| 2840 | axis2DMap.SecondaryTouchpad = RawAxis2D.None; |
| 2841 | } |
| 2842 | |
| 2843 | public override void SetControllerVibration(float frequency, float amplitude) |
| 2844 | { |
| 2845 | int gpcNode = 0; |
| 2846 | float gpcFrequency = frequency * 200.0f; //Map frequency from 0-1 CAPI range to 0-200 GPC range |
| 2847 | float gpcStrength = amplitude; |
| 2848 | |
| 2849 | OVR_GamepadController_SetVibration(gpcNode, gpcStrength, gpcFrequency); |
| 2850 | } |
| 2851 | |
| 2852 | private const string DllName = "OVRGamepad"; |
| 2853 | |
| 2854 | [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] |
| 2855 | private static extern bool OVR_GamepadController_Initialize(); |
| 2856 | [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] |
| 2857 | private static extern bool OVR_GamepadController_Destroy(); |
| 2858 | [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] |
| 2859 | private static extern bool OVR_GamepadController_Update(); |
| 2860 | [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] |
| 2861 | private static extern float OVR_GamepadController_GetAxis(int axis); |
| 2862 | [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] |
| 2863 | private static extern bool OVR_GamepadController_GetButton(int button); |
| 2864 | [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] |
| 2865 | private static extern bool OVR_GamepadController_SetVibration(int node, float strength, float frequency); |
| 2866 | } |
| 2867 | |
| 2868 | private class OVRControllerGamepadAndroid : OVRControllerBase |
| 2869 | { |
| 2870 | public OVRControllerGamepadAndroid() |
| 2871 | { |
| 2872 | controllerType = Controller.Gamepad; |
| 2873 | } |
| 2874 | |
| 2875 | public override void ConfigureButtonMap() |
| 2876 | { |
| 2877 | buttonMap.None = RawButton.None; |
| 2878 | buttonMap.One = RawButton.A; |
| 2879 | buttonMap.Two = RawButton.B; |
| 2880 | buttonMap.Three = RawButton.X; |
| 2881 | buttonMap.Four = RawButton.Y; |
| 2882 | buttonMap.Start = RawButton.Start; |
| 2883 | buttonMap.Back = RawButton.Back; |
| 2884 | buttonMap.PrimaryShoulder = RawButton.LShoulder; |
| 2885 | buttonMap.PrimaryIndexTrigger = RawButton.LIndexTrigger; |
| 2886 | buttonMap.PrimaryHandTrigger = RawButton.None; |
| 2887 | buttonMap.PrimaryThumbstick = RawButton.LThumbstick; |
| 2888 | buttonMap.PrimaryThumbstickUp = RawButton.LThumbstickUp; |
| 2889 | buttonMap.PrimaryThumbstickDown = RawButton.LThumbstickDown; |
| 2890 | buttonMap.PrimaryThumbstickLeft = RawButton.LThumbstickLeft; |
| 2891 | buttonMap.PrimaryThumbstickRight = RawButton.LThumbstickRight; |
| 2892 | buttonMap.PrimaryTouchpad = RawButton.None; |
| 2893 | buttonMap.SecondaryShoulder = RawButton.RShoulder; |
| 2894 | buttonMap.SecondaryIndexTrigger = RawButton.RIndexTrigger; |
| 2895 | buttonMap.SecondaryHandTrigger = RawButton.None; |
| 2896 | buttonMap.SecondaryThumbstick = RawButton.RThumbstick; |
| 2897 | buttonMap.SecondaryThumbstickUp = RawButton.RThumbstickUp; |
| 2898 | buttonMap.SecondaryThumbstickDown = RawButton.RThumbstickDown; |
| 2899 | buttonMap.SecondaryThumbstickLeft = RawButton.RThumbstickLeft; |
| 2900 | buttonMap.SecondaryThumbstickRight = RawButton.RThumbstickRight; |
| 2901 | buttonMap.SecondaryTouchpad = RawButton.None; |
| 2902 | buttonMap.DpadUp = RawButton.DpadUp; |
| 2903 | buttonMap.DpadDown = RawButton.DpadDown; |
| 2904 | buttonMap.DpadLeft = RawButton.DpadLeft; |
| 2905 | buttonMap.DpadRight = RawButton.DpadRight; |
| 2906 | buttonMap.Up = RawButton.LThumbstickUp; |
| 2907 | buttonMap.Down = RawButton.LThumbstickDown; |
| 2908 | buttonMap.Left = RawButton.LThumbstickLeft; |
| 2909 | buttonMap.Right = RawButton.LThumbstickRight; |
| 2910 | } |
| 2911 | |
| 2912 | public override void ConfigureTouchMap() |
| 2913 | { |
| 2914 | touchMap.None = RawTouch.None; |
| 2915 | touchMap.One = RawTouch.None; |
| 2916 | touchMap.Two = RawTouch.None; |
| 2917 | touchMap.Three = RawTouch.None; |
| 2918 | touchMap.Four = RawTouch.None; |
| 2919 | touchMap.PrimaryIndexTrigger = RawTouch.None; |
| 2920 | touchMap.PrimaryThumbstick = RawTouch.None; |
| 2921 | touchMap.PrimaryThumbRest = RawTouch.None; |
| 2922 | touchMap.PrimaryTouchpad = RawTouch.None; |
| 2923 | touchMap.SecondaryIndexTrigger = RawTouch.None; |
| 2924 | touchMap.SecondaryThumbstick = RawTouch.None; |
| 2925 | touchMap.SecondaryThumbRest = RawTouch.None; |
| 2926 | touchMap.SecondaryTouchpad = RawTouch.None; |
| 2927 | } |
| 2928 | |
| 2929 | public override void ConfigureNearTouchMap() |
| 2930 | { |
| 2931 | nearTouchMap.None = RawNearTouch.None; |
| 2932 | nearTouchMap.PrimaryIndexTrigger = RawNearTouch.None; |
| 2933 | nearTouchMap.PrimaryThumbButtons = RawNearTouch.None; |
| 2934 | nearTouchMap.SecondaryIndexTrigger = RawNearTouch.None; |
| 2935 | nearTouchMap.SecondaryThumbButtons = RawNearTouch.None; |
| 2936 | } |
| 2937 | |
| 2938 | public override void ConfigureAxis1DMap() |
| 2939 | { |
| 2940 | axis1DMap.None = RawAxis1D.None; |
| 2941 | axis1DMap.PrimaryIndexTrigger = RawAxis1D.LIndexTrigger; |
| 2942 | axis1DMap.PrimaryHandTrigger = RawAxis1D.None; |
| 2943 | axis1DMap.SecondaryIndexTrigger = RawAxis1D.RIndexTrigger; |
| 2944 | axis1DMap.SecondaryHandTrigger = RawAxis1D.None; |
| 2945 | } |
| 2946 | |
| 2947 | public override void ConfigureAxis2DMap() |
| 2948 | { |
| 2949 | axis2DMap.None = RawAxis2D.None; |
| 2950 | axis2DMap.PrimaryThumbstick = RawAxis2D.LThumbstick; |
| 2951 | axis2DMap.PrimaryTouchpad = RawAxis2D.None; |
| 2952 | axis2DMap.SecondaryThumbstick = RawAxis2D.RThumbstick; |
| 2953 | axis2DMap.SecondaryTouchpad = RawAxis2D.None; |
| 2954 | } |
| 2955 | } |
| 2956 | |
| 2957 | private class OVRControllerTouchpad : OVRControllerBase |
| 2958 | { |
| 2959 | private OVRPlugin.Vector2f moveAmount; |
| 2960 | private float maxTapMagnitude = 0.1f; |
| 2961 | private float minMoveMagnitude = 0.15f; |
| 2962 | |
| 2963 | public OVRControllerTouchpad() |
| 2964 | { |
| 2965 | controllerType = Controller.Touchpad; |
| 2966 | } |
| 2967 | |
| 2968 | public override Controller Update() |
| 2969 | { |
| 2970 | Controller res = base.Update(); |
| 2971 | |
| 2972 | if (GetDown(RawTouch.LTouchpad, OVRInput.Controller.Touchpad)) |
| 2973 | { |
| 2974 | moveAmount = currentState.LTouchpad; |
| 2975 | } |
| 2976 | |
| 2977 | if (GetUp(RawTouch.LTouchpad, OVRInput.Controller.Touchpad)) |
| 2978 | { |
| 2979 | moveAmount.x = previousState.LTouchpad.x - moveAmount.x; |
| 2980 | moveAmount.y = previousState.LTouchpad.y - moveAmount.y; |
| 2981 | |
| 2982 | Vector2 move = new Vector2(moveAmount.x, moveAmount.y); |
| 2983 | float moveMag = move.magnitude; |
| 2984 | |
| 2985 | if (moveMag < maxTapMagnitude) |
| 2986 | { |
| 2987 | // Emit Touchpad Tap |
| 2988 | currentState.Buttons |= (uint)RawButton.Start; |
| 2989 | currentState.Buttons |= (uint)RawButton.LTouchpad; |
| 2990 | } |
| 2991 | else if (moveMag >= minMoveMagnitude) |
| 2992 | { |
| 2993 | move.Normalize(); |
| 2994 | |
| 2995 | // Left/Right |
| 2996 | if (Mathf.Abs(move.x) > Mathf.Abs(move.y)) |
| 2997 | { |
| 2998 | if (move.x < 0.0f) |
| 2999 | { |
| 3000 | currentState.Buttons |= (uint)RawButton.DpadLeft; |
| 3001 | } |
| 3002 | else |
| 3003 | { |
| 3004 | currentState.Buttons |= (uint)RawButton.DpadRight; |
| 3005 | } |
| 3006 | } |
| 3007 | // Up/Down |
| 3008 | else |
| 3009 | { |
| 3010 | if (move.y < 0.0f) |
| 3011 | { |
| 3012 | currentState.Buttons |= (uint)RawButton.DpadDown; |
| 3013 | } |
| 3014 | else |
| 3015 | { |
| 3016 | currentState.Buttons |= (uint)RawButton.DpadUp; |
| 3017 | } |
| 3018 | } |
| 3019 | } |
| 3020 | } |
| 3021 | |
| 3022 | return res; |
| 3023 | } |
| 3024 | |
| 3025 | public override void ConfigureButtonMap() |
| 3026 | { |
| 3027 | buttonMap.None = RawButton.None; |
| 3028 | buttonMap.One = RawButton.LTouchpad; |
| 3029 | buttonMap.Two = RawButton.Back; |
| 3030 | buttonMap.Three = RawButton.None; |
| 3031 | buttonMap.Four = RawButton.None; |
| 3032 | buttonMap.Start = RawButton.Start; |
| 3033 | buttonMap.Back = RawButton.Back; |
| 3034 | buttonMap.PrimaryShoulder = RawButton.None; |
| 3035 | buttonMap.PrimaryIndexTrigger = RawButton.None; |
| 3036 | buttonMap.PrimaryHandTrigger = RawButton.None; |
| 3037 | buttonMap.PrimaryThumbstick = RawButton.None; |
| 3038 | buttonMap.PrimaryThumbstickUp = RawButton.None; |
| 3039 | buttonMap.PrimaryThumbstickDown = RawButton.None; |
| 3040 | buttonMap.PrimaryThumbstickLeft = RawButton.None; |
| 3041 | buttonMap.PrimaryThumbstickRight = RawButton.None; |
| 3042 | buttonMap.PrimaryTouchpad = RawButton.LTouchpad; |
| 3043 | buttonMap.SecondaryShoulder = RawButton.None; |
| 3044 | buttonMap.SecondaryIndexTrigger = RawButton.None; |
| 3045 | buttonMap.SecondaryHandTrigger = RawButton.None; |
| 3046 | buttonMap.SecondaryThumbstick = RawButton.None; |
| 3047 | buttonMap.SecondaryThumbstickUp = RawButton.None; |
| 3048 | buttonMap.SecondaryThumbstickDown = RawButton.None; |
| 3049 | buttonMap.SecondaryThumbstickLeft = RawButton.None; |
| 3050 | buttonMap.SecondaryThumbstickRight = RawButton.None; |
| 3051 | buttonMap.SecondaryTouchpad = RawButton.None; |
| 3052 | buttonMap.DpadUp = RawButton.DpadUp; |
| 3053 | buttonMap.DpadDown = RawButton.DpadDown; |
| 3054 | buttonMap.DpadLeft = RawButton.DpadLeft; |
| 3055 | buttonMap.DpadRight = RawButton.DpadRight; |
| 3056 | buttonMap.Up = RawButton.DpadUp; |
| 3057 | buttonMap.Down = RawButton.DpadDown; |
| 3058 | buttonMap.Left = RawButton.DpadLeft; |
| 3059 | buttonMap.Right = RawButton.DpadRight; |
| 3060 | } |
| 3061 | |
| 3062 | public override void ConfigureTouchMap() |
| 3063 | { |
| 3064 | touchMap.None = RawTouch.None; |
| 3065 | touchMap.One = RawTouch.LTouchpad; |
| 3066 | touchMap.Two = RawTouch.None; |
| 3067 | touchMap.Three = RawTouch.None; |
| 3068 | touchMap.Four = RawTouch.None; |
| 3069 | touchMap.PrimaryIndexTrigger = RawTouch.None; |
| 3070 | touchMap.PrimaryThumbstick = RawTouch.None; |
| 3071 | touchMap.PrimaryThumbRest = RawTouch.None; |
| 3072 | touchMap.PrimaryTouchpad = RawTouch.LTouchpad; |
| 3073 | touchMap.SecondaryIndexTrigger = RawTouch.None; |
| 3074 | touchMap.SecondaryThumbstick = RawTouch.None; |
| 3075 | touchMap.SecondaryThumbRest = RawTouch.None; |
| 3076 | touchMap.SecondaryTouchpad = RawTouch.None; |
| 3077 | } |
| 3078 | |
| 3079 | public override void ConfigureNearTouchMap() |
| 3080 | { |
| 3081 | nearTouchMap.None = RawNearTouch.None; |
| 3082 | nearTouchMap.PrimaryIndexTrigger = RawNearTouch.None; |
| 3083 | nearTouchMap.PrimaryThumbButtons = RawNearTouch.None; |
| 3084 | nearTouchMap.SecondaryIndexTrigger = RawNearTouch.None; |
| 3085 | nearTouchMap.SecondaryThumbButtons = RawNearTouch.None; |
| 3086 | } |
| 3087 | |
| 3088 | public override void ConfigureAxis1DMap() |
| 3089 | { |
| 3090 | axis1DMap.None = RawAxis1D.None; |
| 3091 | axis1DMap.PrimaryIndexTrigger = RawAxis1D.None; |
| 3092 | axis1DMap.PrimaryHandTrigger = RawAxis1D.None; |
| 3093 | axis1DMap.SecondaryIndexTrigger = RawAxis1D.None; |
| 3094 | axis1DMap.SecondaryHandTrigger = RawAxis1D.None; |
| 3095 | } |
| 3096 | |
| 3097 | public override void ConfigureAxis2DMap() |
| 3098 | { |
| 3099 | axis2DMap.None = RawAxis2D.None; |
| 3100 | axis2DMap.PrimaryThumbstick = RawAxis2D.None; |
| 3101 | axis2DMap.PrimaryTouchpad = RawAxis2D.LTouchpad; |
| 3102 | axis2DMap.SecondaryThumbstick = RawAxis2D.None; |
| 3103 | axis2DMap.SecondaryTouchpad = RawAxis2D.None; |
| 3104 | } |
| 3105 | } |
| 3106 | |
| 3107 | private class OVRControllerLTrackedRemote : OVRControllerBase |
| 3108 | { |
| 3109 | private bool emitSwipe; |
| 3110 | private OVRPlugin.Vector2f moveAmount; |
| 3111 | private float minMoveMagnitude = 0.3f; |
| 3112 | |
| 3113 | public OVRControllerLTrackedRemote() |
| 3114 | { |
| 3115 | controllerType = Controller.LTrackedRemote; |
| 3116 | } |
| 3117 | |
| 3118 | public override void ConfigureButtonMap() |
| 3119 | { |
| 3120 | buttonMap.None = RawButton.None; |
| 3121 | buttonMap.One = RawButton.LTouchpad; |
| 3122 | buttonMap.Two = RawButton.Back; |
| 3123 | buttonMap.Three = RawButton.None; |
| 3124 | buttonMap.Four = RawButton.None; |
| 3125 | buttonMap.Start = RawButton.Start; |
| 3126 | buttonMap.Back = RawButton.Back; |
| 3127 | buttonMap.PrimaryShoulder = RawButton.None; |
| 3128 | buttonMap.PrimaryIndexTrigger = RawButton.LIndexTrigger; |
| 3129 | buttonMap.PrimaryHandTrigger = RawButton.LHandTrigger; |
| 3130 | buttonMap.PrimaryThumbstick = RawButton.None; |
| 3131 | buttonMap.PrimaryThumbstickUp = RawButton.None; |
| 3132 | buttonMap.PrimaryThumbstickDown = RawButton.None; |
| 3133 | buttonMap.PrimaryThumbstickLeft = RawButton.None; |
| 3134 | buttonMap.PrimaryThumbstickRight = RawButton.None; |
| 3135 | buttonMap.PrimaryTouchpad = RawButton.LTouchpad; |
| 3136 | buttonMap.SecondaryShoulder = RawButton.None; |
| 3137 | buttonMap.SecondaryIndexTrigger = RawButton.None; |
| 3138 | buttonMap.SecondaryHandTrigger = RawButton.None; |
| 3139 | buttonMap.SecondaryThumbstick = RawButton.None; |
| 3140 | buttonMap.SecondaryThumbstickUp = RawButton.None; |
| 3141 | buttonMap.SecondaryThumbstickDown = RawButton.None; |
| 3142 | buttonMap.SecondaryThumbstickLeft = RawButton.None; |
| 3143 | buttonMap.SecondaryThumbstickRight = RawButton.None; |
| 3144 | buttonMap.SecondaryTouchpad = RawButton.None; |
| 3145 | buttonMap.DpadUp = RawButton.DpadUp; |
| 3146 | buttonMap.DpadDown = RawButton.DpadDown; |
| 3147 | buttonMap.DpadLeft = RawButton.DpadLeft; |
| 3148 | buttonMap.DpadRight = RawButton.DpadRight; |
| 3149 | buttonMap.Up = RawButton.DpadUp; |
| 3150 | buttonMap.Down = RawButton.DpadDown; |
| 3151 | buttonMap.Left = RawButton.DpadLeft; |
| 3152 | buttonMap.Right = RawButton.DpadRight; |
| 3153 | } |
| 3154 | |
| 3155 | public override void ConfigureTouchMap() |
| 3156 | { |
| 3157 | touchMap.None = RawTouch.None; |
| 3158 | touchMap.One = RawTouch.LTouchpad; |
| 3159 | touchMap.Two = RawTouch.None; |
| 3160 | touchMap.Three = RawTouch.None; |
| 3161 | touchMap.Four = RawTouch.None; |
| 3162 | touchMap.PrimaryIndexTrigger = RawTouch.LIndexTrigger; |
| 3163 | touchMap.PrimaryThumbstick = RawTouch.None; |
| 3164 | touchMap.PrimaryThumbRest = RawTouch.None; |
| 3165 | touchMap.PrimaryTouchpad = RawTouch.LTouchpad; |
| 3166 | touchMap.SecondaryIndexTrigger = RawTouch.None; |
| 3167 | touchMap.SecondaryThumbstick = RawTouch.None; |
| 3168 | touchMap.SecondaryThumbRest = RawTouch.None; |
| 3169 | touchMap.SecondaryTouchpad = RawTouch.None; |
| 3170 | } |
| 3171 | |
| 3172 | public override void ConfigureNearTouchMap() |
| 3173 | { |
| 3174 | nearTouchMap.None = RawNearTouch.None; |
| 3175 | nearTouchMap.PrimaryIndexTrigger = RawNearTouch.None; |
| 3176 | nearTouchMap.PrimaryThumbButtons = RawNearTouch.None; |
| 3177 | nearTouchMap.SecondaryIndexTrigger = RawNearTouch.None; |
| 3178 | nearTouchMap.SecondaryThumbButtons = RawNearTouch.None; |
| 3179 | } |
| 3180 | |
| 3181 | public override void ConfigureAxis1DMap() |
| 3182 | { |
| 3183 | axis1DMap.None = RawAxis1D.None; |
| 3184 | axis1DMap.PrimaryIndexTrigger = RawAxis1D.LIndexTrigger; |
| 3185 | axis1DMap.PrimaryHandTrigger = RawAxis1D.LHandTrigger; |
| 3186 | axis1DMap.SecondaryIndexTrigger = RawAxis1D.None; |
| 3187 | axis1DMap.SecondaryHandTrigger = RawAxis1D.None; |
| 3188 | } |
| 3189 | |
| 3190 | public override void ConfigureAxis2DMap() |
| 3191 | { |
| 3192 | axis2DMap.None = RawAxis2D.None; |
| 3193 | axis2DMap.PrimaryThumbstick = RawAxis2D.None; |
| 3194 | axis2DMap.PrimaryTouchpad = RawAxis2D.LTouchpad; |
| 3195 | axis2DMap.SecondaryThumbstick = RawAxis2D.None; |
| 3196 | axis2DMap.SecondaryTouchpad = RawAxis2D.None; |
| 3197 | } |
| 3198 | |
| 3199 | public override Controller Update() |
| 3200 | { |
| 3201 | Controller res = base.Update(); |
| 3202 | |
| 3203 | if (GetDown(RawTouch.LTouchpad, OVRInput.Controller.LTrackedRemote)) |
| 3204 | { |
| 3205 | emitSwipe = true; |
| 3206 | moveAmount = currentState.LTouchpad; |
| 3207 | } |
| 3208 | |
| 3209 | if (GetDown(RawButton.LTouchpad, OVRInput.Controller.LTrackedRemote)) |
| 3210 | { |
| 3211 | emitSwipe = false; |
| 3212 | } |
| 3213 | |
| 3214 | if (GetUp(RawTouch.LTouchpad, OVRInput.Controller.LTrackedRemote) && emitSwipe) |
| 3215 | { |
| 3216 | emitSwipe = false; |
| 3217 | |
| 3218 | moveAmount.x = previousState.LTouchpad.x - moveAmount.x; |
| 3219 | moveAmount.y = previousState.LTouchpad.y - moveAmount.y; |
| 3220 | |
| 3221 | Vector2 move = new Vector2(moveAmount.x, moveAmount.y); |
| 3222 | |
| 3223 | if (move.magnitude >= minMoveMagnitude) |
| 3224 | { |
| 3225 | move.Normalize(); |
| 3226 | |
| 3227 | // Left/Right |
| 3228 | if (Mathf.Abs(move.x) > Mathf.Abs(move.y)) |
| 3229 | { |
| 3230 | if (move.x < 0.0f) |
| 3231 | { |
| 3232 | currentState.Buttons |= (uint)RawButton.DpadLeft; |
| 3233 | } |
| 3234 | else |
| 3235 | { |
| 3236 | currentState.Buttons |= (uint)RawButton.DpadRight; |
| 3237 | } |
| 3238 | } |
| 3239 | // Up/Down |
| 3240 | else |
| 3241 | { |
| 3242 | if (move.y < 0.0f) |
| 3243 | { |
| 3244 | currentState.Buttons |= (uint)RawButton.DpadDown; |
| 3245 | } |
| 3246 | else |
| 3247 | { |
| 3248 | currentState.Buttons |= (uint)RawButton.DpadUp; |
| 3249 | } |
| 3250 | } |
| 3251 | } |
| 3252 | } |
| 3253 | |
| 3254 | return res; |
| 3255 | } |
| 3256 | |
| 3257 | public override bool WasRecentered() |
| 3258 | { |
| 3259 | return (currentState.LRecenterCount != previousState.LRecenterCount); |
| 3260 | } |
| 3261 | |
| 3262 | public override byte GetRecenterCount() |
| 3263 | { |
| 3264 | return currentState.LRecenterCount; |
| 3265 | } |
| 3266 | |
| 3267 | public override byte GetBatteryPercentRemaining() |
| 3268 | { |
| 3269 | return currentState.LBatteryPercentRemaining; |
| 3270 | } |
| 3271 | } |
| 3272 | |
| 3273 | private class OVRControllerRTrackedRemote : OVRControllerBase |
| 3274 | { |
| 3275 | private bool emitSwipe; |
| 3276 | private OVRPlugin.Vector2f moveAmount; |
| 3277 | private float minMoveMagnitude = 0.3f; |
| 3278 | |
| 3279 | public OVRControllerRTrackedRemote() |
| 3280 | { |
| 3281 | controllerType = Controller.RTrackedRemote; |
| 3282 | } |
| 3283 | |
| 3284 | public override void ConfigureButtonMap() |
| 3285 | { |
| 3286 | buttonMap.None = RawButton.None; |
| 3287 | buttonMap.One = RawButton.RTouchpad; |
| 3288 | buttonMap.Two = RawButton.Back; |
| 3289 | buttonMap.Three = RawButton.None; |
| 3290 | buttonMap.Four = RawButton.None; |
| 3291 | buttonMap.Start = RawButton.Start; |
| 3292 | buttonMap.Back = RawButton.Back; |
| 3293 | buttonMap.PrimaryShoulder = RawButton.None; |
| 3294 | buttonMap.PrimaryIndexTrigger = RawButton.RIndexTrigger; |
| 3295 | buttonMap.PrimaryHandTrigger = RawButton.RHandTrigger; |
| 3296 | buttonMap.PrimaryThumbstick = RawButton.None; |
| 3297 | buttonMap.PrimaryThumbstickUp = RawButton.None; |
| 3298 | buttonMap.PrimaryThumbstickDown = RawButton.None; |
| 3299 | buttonMap.PrimaryThumbstickLeft = RawButton.None; |
| 3300 | buttonMap.PrimaryThumbstickRight = RawButton.None; |
| 3301 | buttonMap.PrimaryTouchpad = RawButton.RTouchpad; |
| 3302 | buttonMap.SecondaryShoulder = RawButton.None; |
| 3303 | buttonMap.SecondaryIndexTrigger = RawButton.None; |
| 3304 | buttonMap.SecondaryHandTrigger = RawButton.None; |
| 3305 | buttonMap.SecondaryThumbstick = RawButton.None; |
| 3306 | buttonMap.SecondaryThumbstickUp = RawButton.None; |
| 3307 | buttonMap.SecondaryThumbstickDown = RawButton.None; |
| 3308 | buttonMap.SecondaryThumbstickLeft = RawButton.None; |
| 3309 | buttonMap.SecondaryThumbstickRight = RawButton.None; |
| 3310 | buttonMap.SecondaryTouchpad = RawButton.None; |
| 3311 | buttonMap.DpadUp = RawButton.DpadUp; |
| 3312 | buttonMap.DpadDown = RawButton.DpadDown; |
| 3313 | buttonMap.DpadLeft = RawButton.DpadLeft; |
| 3314 | buttonMap.DpadRight = RawButton.DpadRight; |
| 3315 | buttonMap.Up = RawButton.DpadUp; |
| 3316 | buttonMap.Down = RawButton.DpadDown; |
| 3317 | buttonMap.Left = RawButton.DpadLeft; |
| 3318 | buttonMap.Right = RawButton.DpadRight; |
| 3319 | } |
| 3320 | |
| 3321 | public override void ConfigureTouchMap() |
| 3322 | { |
| 3323 | touchMap.None = RawTouch.None; |
| 3324 | touchMap.One = RawTouch.RTouchpad; |
| 3325 | touchMap.Two = RawTouch.None; |
| 3326 | touchMap.Three = RawTouch.None; |
| 3327 | touchMap.Four = RawTouch.None; |
| 3328 | touchMap.PrimaryIndexTrigger = RawTouch.RIndexTrigger; |
| 3329 | touchMap.PrimaryThumbstick = RawTouch.None; |
| 3330 | touchMap.PrimaryThumbRest = RawTouch.None; |
| 3331 | touchMap.PrimaryTouchpad = RawTouch.RTouchpad; |
| 3332 | touchMap.SecondaryIndexTrigger = RawTouch.None; |
| 3333 | touchMap.SecondaryThumbstick = RawTouch.None; |
| 3334 | touchMap.SecondaryThumbRest = RawTouch.None; |
| 3335 | touchMap.SecondaryTouchpad = RawTouch.None; |
| 3336 | } |
| 3337 | |
| 3338 | public override void ConfigureNearTouchMap() |
| 3339 | { |
| 3340 | nearTouchMap.None = RawNearTouch.None; |
| 3341 | nearTouchMap.PrimaryIndexTrigger = RawNearTouch.None; |
| 3342 | nearTouchMap.PrimaryThumbButtons = RawNearTouch.None; |
| 3343 | nearTouchMap.SecondaryIndexTrigger = RawNearTouch.None; |
| 3344 | nearTouchMap.SecondaryThumbButtons = RawNearTouch.None; |
| 3345 | } |
| 3346 | |
| 3347 | public override void ConfigureAxis1DMap() |
| 3348 | { |
| 3349 | axis1DMap.None = RawAxis1D.None; |
| 3350 | axis1DMap.PrimaryIndexTrigger = RawAxis1D.RIndexTrigger; |
| 3351 | axis1DMap.PrimaryHandTrigger = RawAxis1D.RHandTrigger; |
| 3352 | axis1DMap.SecondaryIndexTrigger = RawAxis1D.None; |
| 3353 | axis1DMap.SecondaryHandTrigger = RawAxis1D.None; |
| 3354 | } |
| 3355 | |
| 3356 | public override void ConfigureAxis2DMap() |
| 3357 | { |
| 3358 | axis2DMap.None = RawAxis2D.None; |
| 3359 | axis2DMap.PrimaryThumbstick = RawAxis2D.None; |
| 3360 | axis2DMap.PrimaryTouchpad = RawAxis2D.RTouchpad; |
| 3361 | axis2DMap.SecondaryThumbstick = RawAxis2D.None; |
| 3362 | axis2DMap.SecondaryTouchpad = RawAxis2D.None; |
| 3363 | } |
| 3364 | |
| 3365 | public override Controller Update() |
| 3366 | { |
| 3367 | Controller res = base.Update(); |
| 3368 | |
| 3369 | if (GetDown(RawTouch.RTouchpad, OVRInput.Controller.RTrackedRemote)) |
| 3370 | { |
| 3371 | emitSwipe = true; |
| 3372 | moveAmount = currentState.RTouchpad; |
| 3373 | } |
| 3374 | |
| 3375 | if (GetDown(RawButton.RTouchpad, OVRInput.Controller.RTrackedRemote)) |
| 3376 | { |
| 3377 | emitSwipe = false; |
| 3378 | } |
| 3379 | |
| 3380 | if (GetUp(RawTouch.RTouchpad, OVRInput.Controller.RTrackedRemote) && emitSwipe) |
| 3381 | { |
| 3382 | emitSwipe = false; |
| 3383 | |
| 3384 | moveAmount.x = previousState.RTouchpad.x - moveAmount.x; |
| 3385 | moveAmount.y = previousState.RTouchpad.y - moveAmount.y; |
| 3386 | |
| 3387 | Vector2 move = new Vector2(moveAmount.x, moveAmount.y); |
| 3388 | |
| 3389 | if (move.magnitude >= minMoveMagnitude) |
| 3390 | { |
| 3391 | move.Normalize(); |
| 3392 | |
| 3393 | // Left/Right |
| 3394 | if (Mathf.Abs(move.x) > Mathf.Abs(move.y)) |
| 3395 | { |
| 3396 | if (move.x < 0.0f) |
| 3397 | { |
| 3398 | currentState.Buttons |= (uint)RawButton.DpadLeft; |
| 3399 | } |
| 3400 | else |
| 3401 | { |
| 3402 | currentState.Buttons |= (uint)RawButton.DpadRight; |
| 3403 | } |
| 3404 | } |
| 3405 | // Up/Down |
| 3406 | else |
| 3407 | { |
| 3408 | if (move.y < 0.0f) |
| 3409 | { |
| 3410 | currentState.Buttons |= (uint)RawButton.DpadDown; |
| 3411 | } |
| 3412 | else |
| 3413 | { |
| 3414 | currentState.Buttons |= (uint)RawButton.DpadUp; |
| 3415 | } |
| 3416 | } |
| 3417 | } |
| 3418 | } |
| 3419 | |
| 3420 | return res; |
| 3421 | } |
| 3422 | |
| 3423 | public override bool WasRecentered() |
| 3424 | { |
| 3425 | return (currentState.RRecenterCount != previousState.RRecenterCount); |
| 3426 | } |
| 3427 | |
| 3428 | public override byte GetRecenterCount() |
| 3429 | { |
| 3430 | return currentState.RRecenterCount; |
| 3431 | } |
| 3432 | |
| 3433 | public override byte GetBatteryPercentRemaining() |
| 3434 | { |
| 3435 | return currentState.RBatteryPercentRemaining; |
| 3436 | } |
| 3437 | } |
| 3438 | } |