OVRTouchpad.cs 2.8 KB

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