InputDeviceHandPartCatchEvent.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using UnityEngine;
  7. using UnityEngine.EventSystems;
  8. namespace SC.XR.Unity.Module_InputSystem.InputDeviceHand {
  9. public class InputDeviceHandPartCatchEvent : InputDeviceHandPartEventBase {
  10. public InputDeviceHandPartCatchEvent(InputDevicePartDispatchEventHand inputDevicePartDispatchEventHand) : base(inputDevicePartDispatchEventHand) {
  11. }
  12. enum XDirection {
  13. Release = -1,
  14. Catch = 1,
  15. }
  16. List<Vector3> HandTrendList = new List<Vector3>(new Vector3[6]);
  17. protected int currentNum = 0;
  18. protected float timer = 0;
  19. private float catchDeltaTime = 0.07f;
  20. bool isForefingerCatch = false;
  21. bool isDistanceNear = false;
  22. float angle;
  23. float distance = 0;
  24. bool isDown = false;
  25. public override void OnSCStart() {
  26. base.OnSCStart();
  27. previousEvent = currentEvent = HandEventType.Null;
  28. isDown = false;
  29. timer = 0;
  30. isDistanceNear = false;
  31. isDistanceNear = false;
  32. eventfromdriver = false;
  33. }
  34. protected override void OnUpdateEvent() {
  35. if (eventfromdriver) {
  36. if (0 == handInfo.iv_extinfo.ges_dynamic[0]) {//Down
  37. EventPercent = 1;
  38. } else if (1 == handInfo.iv_extinfo.ges_dynamic[0] || 3 == handInfo.iv_extinfo.ges_dynamic[0]) {//Up
  39. EventPercent = 0;
  40. } else if (2 == handInfo.iv_extinfo.ges_dynamic[0]) {//Drag
  41. EventPercent = 1;
  42. }else if (-1 == handInfo.iv_extinfo.ges_dynamic[0]) {//no atcion Not Null
  43. //EventPercent = 0;
  44. }
  45. } else {
  46. angle = Vector3.Angle(
  47. (handInfo.GetJoint(FINGER.forefinger, JOINT.Two).localPosition - handInfo.GetJoint(FINGER.forefinger, JOINT.Three).localPosition),
  48. (handInfo.GetJoint(FINGER.forefinger, JOINT.Three).localPosition - handInfo.GetJoint(FINGER.forefinger, JOINT.Four).localPosition));
  49. if (angle > 28) {
  50. isForefingerCatch = true;
  51. } else if (angle < 20) {
  52. isForefingerCatch = false;
  53. }
  54. //DebugMy.Log(inputDevice26Dof.PartType+" "+FINGER.forefinger + " Angle :" + angle + ">28 ?", this);
  55. distance = Vector3.Distance(handInfo.GetJoint(FINGER.forefinger, JOINT.One).localPosition, handInfo.GetJoint(FINGER.thumb, JOINT.One).localPosition);
  56. if (distance < 0.035f) {
  57. isDistanceNear = true;
  58. } else if (distance < 0.050f) {
  59. isDistanceNear = false;
  60. }
  61. if (isDistanceNear == false) {
  62. distance = Vector3.Distance(handInfo.GetJoint(FINGER.forefinger, JOINT.Two).localPosition, handInfo.GetJoint(FINGER.thumb, JOINT.Two).localPosition);
  63. if (distance < 0.035f) {
  64. isDistanceNear = true;
  65. }
  66. }
  67. if (isDistanceNear && isForefingerCatch) {
  68. isDown = true;
  69. } else {
  70. isDown = false;
  71. timer = 0;
  72. }
  73. if (isDown == true) {
  74. timer += Time.deltaTime;
  75. //DebugMy.Log(inputDeviceHandPart.PartType + " timer: " + timer, this, true);
  76. }
  77. EventPercent = (Mathf.Clamp(1 - distance / 0.035f / 2, 0, 0.5f) + Mathf.Clamp(angle / 28 / 2, 0, 0.5f)) * Mathf.Clamp(timer / catchDeltaTime, 0.2f, 1);
  78. }
  79. if (EventPercent >= 1) {
  80. if (previousEvent == HandEventType.CatchDown || previousEvent == HandEventType.CatchDrag) {
  81. currentEvent = HandEventType.CatchDrag;
  82. } else {
  83. currentEvent = HandEventType.CatchDown;
  84. }
  85. } else {
  86. if (previousEvent == HandEventType.CatchDown || previousEvent == HandEventType.CatchDrag) {
  87. currentEvent = HandEventType.CatchUp;
  88. } else if (EventPercent > 0.2f) {
  89. currentEvent = HandEventType.CatchReady;
  90. } else {
  91. if (currentEvent == HandEventType.CatchReady || currentEvent == HandEventType.CatchUp) {
  92. currentEvent = HandEventType.CatchEnd;
  93. EventPercent = 0;
  94. } else {
  95. currentEvent = HandEventType.Null;
  96. }
  97. }
  98. }
  99. previousEvent = currentEvent;
  100. //if (currentEvent != HandEventType.Null) {
  101. // DebugMy.Log(inputDeviceHandPart.PartType + " Event -----> " + currentEvent + " " + EventPercent, this, true);
  102. //}
  103. }
  104. public override void OnSCDisable() {
  105. if (previousEvent == HandEventType.CatchDown || previousEvent == HandEventType.CatchDrag) {
  106. currentEvent = HandEventType.CatchUp;
  107. }
  108. base.OnSCDisable();
  109. }
  110. }
  111. }