ExtendedFingerDetector.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections;
  3. using Leap.Unity.Attributes;
  4. using UnityEngine;
  5. namespace Leap.Unity
  6. {
  7. public class ExtendedFingerDetector : Detector
  8. {
  9. private void OnValidate()
  10. {
  11. int num = 0;
  12. int num2 = 0;
  13. PointingState[] array = new PointingState[]
  14. {
  15. this.Thumb,
  16. this.Index,
  17. this.Middle,
  18. this.Ring,
  19. this.Pinky
  20. };
  21. foreach (PointingState pointingState in array)
  22. {
  23. if (pointingState != PointingState.Extended)
  24. {
  25. if (pointingState == PointingState.NotExtended)
  26. {
  27. num2++;
  28. }
  29. }
  30. else
  31. {
  32. num++;
  33. }
  34. this.MinimumExtendedCount = Math.Max(num, this.MinimumExtendedCount);
  35. this.MaximumExtendedCount = Math.Min(5 - num2, this.MaximumExtendedCount);
  36. this.MaximumExtendedCount = Math.Max(num, this.MaximumExtendedCount);
  37. }
  38. }
  39. private void Awake()
  40. {
  41. this.watcherCoroutine = this.extendedFingerWatcher();
  42. }
  43. private void OnEnable()
  44. {
  45. base.StartCoroutine(this.watcherCoroutine);
  46. }
  47. private void OnDisable()
  48. {
  49. base.StopCoroutine(this.watcherCoroutine);
  50. this.Deactivate();
  51. }
  52. private IEnumerator extendedFingerWatcher()
  53. {
  54. for (;;)
  55. {
  56. bool fingerState = false;
  57. if (this.HandModel != null && this.HandModel.IsTracked)
  58. {
  59. Hand hand = this.HandModel.GetLeapHand();
  60. if (hand != null)
  61. {
  62. fingerState = (this.matchFingerState(hand.Fingers[0], this.Thumb) && this.matchFingerState(hand.Fingers[1], this.Index) && this.matchFingerState(hand.Fingers[2], this.Middle) && this.matchFingerState(hand.Fingers[3], this.Ring) && this.matchFingerState(hand.Fingers[4], this.Pinky));
  63. int num = 0;
  64. for (int i = 0; i < 4; i++)
  65. {
  66. if (hand.Fingers[i].IsExtended)
  67. {
  68. num++;
  69. }
  70. }
  71. fingerState = (fingerState && num <= this.MaximumExtendedCount && num >= this.MinimumExtendedCount);
  72. if (this.HandModel.IsTracked && fingerState)
  73. {
  74. this.Activate();
  75. }
  76. else if (!this.HandModel.IsTracked || !fingerState)
  77. {
  78. this.Deactivate();
  79. }
  80. }
  81. }
  82. else if (base.IsActive)
  83. {
  84. this.Deactivate();
  85. }
  86. yield return new WaitForSeconds(this.Period);
  87. }
  88. yield break;
  89. }
  90. private bool matchFingerState(Finger finger, PointingState requiredState)
  91. {
  92. return requiredState == PointingState.Either || (requiredState == PointingState.Extended && finger.IsExtended) || (requiredState == PointingState.NotExtended && !finger.IsExtended);
  93. }
  94. [Tooltip("The interval in seconds at which to check this detector's conditions.")]
  95. public float Period = 0.1f;
  96. [AutoFind(AutoFindLocations.Parents)]
  97. [Tooltip("The hand model to watch. Set automatically if detector is on a hand.")]
  98. public IHandModel HandModel;
  99. public PointingState Thumb = PointingState.Either;
  100. public PointingState Index = PointingState.Either;
  101. public PointingState Middle = PointingState.Either;
  102. public PointingState Ring = PointingState.Either;
  103. public PointingState Pinky = PointingState.Either;
  104. [Range(0f, 5f)]
  105. public int MinimumExtendedCount;
  106. [Range(0f, 5f)]
  107. public int MaximumExtendedCount = 5;
  108. private IEnumerator watcherCoroutine;
  109. }
  110. }