OVRTouchpad.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Diagnostics;
  3. using UnityEngine;
  4. public static class OVRTouchpad
  5. {
  6. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  7. public static event EventHandler TouchHandler;
  8. public static void Create()
  9. {
  10. }
  11. public static void Update()
  12. {
  13. if (Input.GetMouseButtonDown(0))
  14. {
  15. OVRTouchpad.moveAmountMouse = Input.mousePosition;
  16. OVRTouchpad.touchState = OVRTouchpad.TouchState.Down;
  17. }
  18. else if (Input.GetMouseButtonUp(0))
  19. {
  20. OVRTouchpad.moveAmountMouse -= Input.mousePosition;
  21. OVRTouchpad.HandleInputMouse(ref OVRTouchpad.moveAmountMouse);
  22. OVRTouchpad.touchState = OVRTouchpad.TouchState.Init;
  23. }
  24. }
  25. public static void OnDisable()
  26. {
  27. }
  28. private static void HandleInput(OVRTouchpad.TouchState state, ref Vector2 move)
  29. {
  30. if (move.magnitude >= OVRTouchpad.minMovMagnitude && OVRTouchpad.touchState != OVRTouchpad.TouchState.Stationary)
  31. {
  32. if (OVRTouchpad.touchState == OVRTouchpad.TouchState.Move)
  33. {
  34. move.Normalize();
  35. if (Mathf.Abs(move.x) > Mathf.Abs(move.y))
  36. {
  37. if (move.x > 0f)
  38. {
  39. }
  40. }
  41. else if (move.y > 0f)
  42. {
  43. }
  44. }
  45. }
  46. }
  47. private static void HandleInputMouse(ref Vector3 move)
  48. {
  49. if (move.magnitude < OVRTouchpad.minMovMagnitudeMouse)
  50. {
  51. if (OVRTouchpad.TouchHandler != null)
  52. {
  53. OVRTouchpad.TouchHandler(null, new OVRTouchpad.TouchArgs
  54. {
  55. TouchType = OVRTouchpad.TouchEvent.SingleTap
  56. });
  57. }
  58. }
  59. else
  60. {
  61. move.Normalize();
  62. if (Mathf.Abs(move.x) > Mathf.Abs(move.y))
  63. {
  64. if (move.x > 0f)
  65. {
  66. if (OVRTouchpad.TouchHandler != null)
  67. {
  68. OVRTouchpad.TouchHandler(null, new OVRTouchpad.TouchArgs
  69. {
  70. TouchType = OVRTouchpad.TouchEvent.Left
  71. });
  72. }
  73. }
  74. else if (OVRTouchpad.TouchHandler != null)
  75. {
  76. OVRTouchpad.TouchHandler(null, new OVRTouchpad.TouchArgs
  77. {
  78. TouchType = OVRTouchpad.TouchEvent.Right
  79. });
  80. }
  81. }
  82. else if (move.y > 0f)
  83. {
  84. if (OVRTouchpad.TouchHandler != null)
  85. {
  86. OVRTouchpad.TouchHandler(null, new OVRTouchpad.TouchArgs
  87. {
  88. TouchType = OVRTouchpad.TouchEvent.Down
  89. });
  90. }
  91. }
  92. else if (OVRTouchpad.TouchHandler != null)
  93. {
  94. OVRTouchpad.TouchHandler(null, new OVRTouchpad.TouchArgs
  95. {
  96. TouchType = OVRTouchpad.TouchEvent.Up
  97. });
  98. }
  99. }
  100. }
  101. private static OVRTouchpad.TouchState touchState = OVRTouchpad.TouchState.Init;
  102. private static Vector2 moveAmount;
  103. private static float minMovMagnitude = 100f;
  104. private static Vector3 moveAmountMouse;
  105. private static float minMovMagnitudeMouse = 25f;
  106. private static OVRTouchpadHelper touchpadHelper = new GameObject("OVRTouchpadHelper").AddComponent<OVRTouchpadHelper>();
  107. public enum TouchEvent
  108. {
  109. SingleTap,
  110. Left,
  111. Right,
  112. Up,
  113. Down
  114. }
  115. public class TouchArgs : EventArgs
  116. {
  117. public OVRTouchpad.TouchEvent TouchType;
  118. }
  119. private enum TouchState
  120. {
  121. Init,
  122. Down,
  123. Stationary,
  124. Move,
  125. Up
  126. }
  127. }