| 1 | /************************************************************************************ |
| 2 | Filename : OVRTouchpad.cs |
| 3 | Content : Interface to touchpad |
| 4 | Created : November 13, 2013 |
| 5 | Copyright : Copyright Facebook Technologies, LLC and its affiliates. |
| 6 | All rights reserved. |
| 7 | |
| 8 | Licensed under the Oculus Audio SDK License Version 3.3 (the "License"); |
| 9 | you may not use the Oculus Audio SDK except in compliance with the License, |
| 10 | which is provided at the time of installation or download, or which |
| 11 | otherwise accompanies this software in either electronic or hard copy form. |
| 12 | |
| 13 | You may obtain a copy of the License at |
| 14 | |
| 15 | https://developer.oculus.com/licenses/audio-3.3/ |
| 16 | |
| 17 | Unless required by applicable law or agreed to in writing, the Oculus Audio SDK |
| 18 | distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | See the License for the specific language governing permissions and |
| 21 | limitations under the License. |
| 22 | ************************************************************************************/ |
| 23 | using UnityEngine; |
| 24 | using System; |
| 25 | |
| 26 | //------------------------------------------------------------------------------------- |
| 27 | // ***** OVRTouchpad |
| 28 | // |
| 29 | // OVRTouchpad is an interface class to a touchpad. |
| 30 | // |
| 31 | public static class OVRTouchpad |
| 32 | { |
| 33 | //------------------------- |
| 34 | // Input enums |
| 35 | public enum TouchEvent { SingleTap, DoubleTap, Left, Right, Up, Down }; |
| 36 | |
| 37 | // mouse |
| 38 | static Vector3 moveAmountMouse; |
| 39 | static float minMovMagnitudeMouse = 25.0f; |
| 40 | |
| 41 | public delegate void OVRTouchpadCallback<TouchEvent>(TouchEvent arg); |
| 42 | static public Delegate touchPadCallbacks = null; |
| 43 | |
| 44 | //Disable the unused variable warning |
| 45 | #pragma warning disable 0414 |
| 46 | |
| 47 | //Ensures that the TouchpadHelper will be created automatically upon start of the game. |
| 48 | static private OVRTouchpadHelper touchpadHelper = |
| 49 | ( new GameObject("OVRTouchpadHelper") ).AddComponent< OVRTouchpadHelper >(); |
| 50 | |
| 51 | #pragma warning restore 0414 |
| 52 | |
| 53 | // We will call this to create the TouchpadHelper class. This will |
| 54 | // add the Touchpad game object into the world and we can call into |
| 55 | // TouchEvent static functions to hook delegates into for touch capture |
| 56 | static public void Create() |
| 57 | { |
| 58 | // Does nothing but call constructor to add game object into scene |
| 59 | } |
| 60 | |
| 61 | // Update |
| 62 | static public void Update() |
| 63 | { |
| 64 | // MOUSE INPUT |
| 65 | |
| 66 | if(Input.GetMouseButtonDown(0)) |
| 67 | { |
| 68 | moveAmountMouse = Input.mousePosition; |
| 69 | } |
| 70 | else if(Input.GetMouseButtonUp(0)) |
| 71 | { |
| 72 | moveAmountMouse -= Input.mousePosition; |
| 73 | HandleInputMouse(ref moveAmountMouse); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // OnDisable |
| 78 | static public void OnDisable() |
| 79 | { |
| 80 | } |
| 81 | |
| 82 | // HandleInputMouse |
| 83 | static void HandleInputMouse(ref Vector3 move) |
| 84 | { |
| 85 | if (touchPadCallbacks == null) |
| 86 | { |
| 87 | return; |
| 88 | } |
| 89 | OVRTouchpadCallback<TouchEvent> callback = touchPadCallbacks as OVRTouchpadCallback<TouchEvent>; |
| 90 | |
| 91 | if ( move.magnitude < minMovMagnitudeMouse) |
| 92 | { |
| 93 | callback(TouchEvent.SingleTap); |
| 94 | } |
| 95 | else |
| 96 | { |
| 97 | move.Normalize(); |
| 98 | |
| 99 | // Left/Right |
| 100 | if (Mathf.Abs(move.x) > Mathf.Abs(move.y)) |
| 101 | { |
| 102 | if (move.x > 0.0f) |
| 103 | callback(TouchEvent.Left); |
| 104 | else |
| 105 | callback(TouchEvent.Right); |
| 106 | } |
| 107 | // Up/Down |
| 108 | else |
| 109 | { |
| 110 | if (move.y > 0.0f) |
| 111 | callback(TouchEvent.Down); |
| 112 | else |
| 113 | callback(TouchEvent.Up); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | static public void AddListener(OVRTouchpadCallback<TouchEvent> handler) |
| 119 | { |
| 120 | touchPadCallbacks = (OVRTouchpadCallback<TouchEvent>)touchPadCallbacks + handler; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | //------------------------------------------------------------------------------------- |
| 125 | // ***** OVRTouchpadHelper |
| 126 | // |
| 127 | // This singleton class gets created and stays resident in the application. It is used to |
| 128 | // trap the touchpad values, which get broadcast to any listener on the "Touchpad" channel. |
| 129 | // |
| 130 | // This class also demontrates how to make calls from any class that needs these events by |
| 131 | // setting up a listener to "Touchpad" channel. |
| 132 | public sealed class OVRTouchpadHelper : MonoBehaviour |
| 133 | { |
| 134 | void Awake () |
| 135 | { |
| 136 | DontDestroyOnLoad(gameObject); |
| 137 | } |
| 138 | |
| 139 | void Start () |
| 140 | { |
| 141 | // Add a listener to the OVRTouchpad for testing |
| 142 | OVRTouchpad.AddListener(LocalTouchEventCallback); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | void Update () |
| 147 | { |
| 148 | OVRTouchpad.Update(); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | public void OnDisable() |
| 153 | { |
| 154 | OVRTouchpad.OnDisable(); |
| 155 | } |
| 156 | |
| 157 | // LocalTouchEventCallback |
| 158 | void LocalTouchEventCallback(OVRTouchpad.TouchEvent touchEvent) |
| 159 | { |
| 160 | switch(touchEvent) |
| 161 | { |
| 162 | case(OVRTouchpad.TouchEvent.SingleTap): |
| 163 | // OVRLipSyncDebugConsole.Clear(); |
| 164 | // OVRLipSyncDebugConsole.ClearTimeout(1.5f); |
| 165 | // OVRLipSyncDebugConsole.Log("TP-SINGLE TAP"); |
| 166 | break; |
| 167 | |
| 168 | case(OVRTouchpad.TouchEvent.DoubleTap): |
| 169 | // OVRLipSyncDebugConsole.Clear(); |
| 170 | // OVRLipSyncDebugConsole.ClearTimeout(1.5f); |
| 171 | // OVRLipSyncDebugConsole.Log("TP-DOUBLE TAP"); |
| 172 | break; |
| 173 | |
| 174 | case(OVRTouchpad.TouchEvent.Left): |
| 175 | // OVRLipSyncDebugConsole.Clear(); |
| 176 | // OVRLipSyncDebugConsole.ClearTimeout(1.5f); |
| 177 | // OVRLipSyncDebugConsole.Log("TP-SWIPE LEFT"); |
| 178 | break; |
| 179 | |
| 180 | case(OVRTouchpad.TouchEvent.Right): |
| 181 | // OVRLipSyncDebugConsole.Clear(); |
| 182 | // OVRLipSyncDebugConsole.ClearTimeout(1.5f); |
| 183 | // OVRLipSyncDebugConsole.Log("TP-SWIPE RIGHT"); |
| 184 | break; |
| 185 | |
| 186 | case(OVRTouchpad.TouchEvent.Up): |
| 187 | // OVRLipSyncDebugConsole.Clear(); |
| 188 | // OVRLipSyncDebugConsole.ClearTimeout(1.5f); |
| 189 | // OVRLipSyncDebugConsole.Log("TP-SWIPE UP"); |
| 190 | break; |
| 191 | |
| 192 | case(OVRTouchpad.TouchEvent.Down): |
| 193 | // OVRLipSyncDebugConsole.Clear(); |
| 194 | // OVRLipSyncDebugConsole.ClearTimeout(1.5f); |
| 195 | // OVRLipSyncDebugConsole.Log("TP-SWIPE DOWN"); |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | } |
| 201 | |