Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
6.1 KiB

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