Tanoda
ExtendedFingerDetector.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using UnityEngine;
10using System.Collections;
11using System;
13
14namespace Leap.Unity {
15
34 [Tooltip("The interval in seconds at which to check this detector's conditions.")]
35 [Units("seconds")]
36 [MinValue(0)]
37 public float Period = .1f; //seconds
38
44 [Tooltip("The hand model to watch. Set automatically if detector is on a hand.")]
45 public HandModelBase HandModel = null;
46
48 [Header("Finger States")]
49 [Tooltip("Required state of the thumb.")]
52 [Tooltip("Required state of the index finger.")]
55 [Tooltip("Required state of the middle finger.")]
58 [Tooltip("Required state of the ring finger.")]
61 [Tooltip("Required state of the little finger.")]
63
65 [Header("Min and Max Finger Counts")]
66 [Range(0,5)]
67 [Tooltip("The minimum number of fingers extended.")]
68 public int MinimumExtendedCount = 0;
70 [Range(0, 5)]
71 [Tooltip("The maximum number of fingers extended.")]
72 public int MaximumExtendedCount = 5;
76 [Header("")]
77 [Tooltip("Draw this detector's Gizmos, if any. (Gizmos must be on in Unity edtor, too.)")]
78 public bool ShowGizmos = true;
79
80 private IEnumerator watcherCoroutine;
81
82 void OnValidate() {
83 int required = 0, forbidden = 0;
84 PointingState[] stateArray = { Thumb, Index, Middle, Ring, Pinky };
85 for(int i=0; i<stateArray.Length; i++) {
86 var state = stateArray[i];
87 switch (state) {
88 case PointingState.Extended:
89 required++;
90 break;
91 case PointingState.NotExtended:
92 forbidden++;
93 break;
94 default:
95 break;
96 }
100 }
101
102 }
103
104 void Awake () {
105 watcherCoroutine = extendedFingerWatcher();
106 }
107
108 void OnEnable () {
109 StartCoroutine(watcherCoroutine);
110 }
111
112 void OnDisable () {
113 StopCoroutine(watcherCoroutine);
114 Deactivate();
115 }
116
117 IEnumerator extendedFingerWatcher() {
118 Hand hand;
119 while(true){
120 bool fingerState = false;
121 if(HandModel != null && HandModel.IsTracked){
122 hand = HandModel.GetLeapHand();
123 if(hand != null){
124 fingerState = matchFingerState(hand.Fingers[0], Thumb)
125 && matchFingerState(hand.Fingers[1], Index)
126 && matchFingerState(hand.Fingers[2], Middle)
127 && matchFingerState(hand.Fingers[3], Ring)
128 && matchFingerState(hand.Fingers[4], Pinky);
129
130 int extendedCount = 0;
131 for (int f = 0; f < 5; f++) {
132 if (hand.Fingers[f].IsExtended) {
133 extendedCount++;
134 }
135 }
136 fingerState = fingerState &&
137 (extendedCount <= MaximumExtendedCount) &&
138 (extendedCount >= MinimumExtendedCount);
139 if(HandModel.IsTracked && fingerState){
140 Activate();
141 } else if(!HandModel.IsTracked || !fingerState) {
142 Deactivate();
143 }
144 }
145 } else if(IsActive){
146 Deactivate();
147 }
148 yield return new WaitForSeconds(Period);
149 }
150 }
151
152 private bool matchFingerState (Finger finger, PointingState requiredState) {
153 return (requiredState == PointingState.Either) ||
154 (requiredState == PointingState.Extended && finger.IsExtended) ||
155 (requiredState == PointingState.NotExtended && !finger.IsExtended);
156 }
157
158 #if UNITY_EDITOR
159 void OnDrawGizmos () {
160 if (ShowGizmos && HandModel != null && HandModel.IsTracked) {
161 PointingState[] state = { Thumb, Index, Middle, Ring, Pinky };
162 Hand hand = HandModel.GetLeapHand();
163 int extendedCount = 0;
164 int notExtendedCount = 0;
165 for (int f = 0; f < 5; f++) {
166 Finger finger = hand.Fingers[f];
167 if (finger.IsExtended) extendedCount++;
168 else notExtendedCount++;
169 if (matchFingerState(finger, state[f]) &&
170 (extendedCount <= MaximumExtendedCount) &&
171 (extendedCount >= MinimumExtendedCount)) {
172 Gizmos.color = OnColor;
173 } else {
174 Gizmos.color = OffColor;
175 }
176 Gizmos.DrawWireSphere(finger.TipPosition.ToVector3(), finger.Width);
177 }
178 }
179 }
180 #endif
181 }
182
185}
Es.InkPainter.Math Math
Definition: PaintTest.cs:7
List< Finger > Fingers
The list of Finger objects detected in this frame that are attached to this hand, given in order from...
Definition: Hand.cs:159
virtual void Deactivate()
Definition: Detector.cs:66
virtual void Activate()
Definition: Detector.cs:54
abstract Hand GetLeapHand()
A data structure that represents either a value of type A or a value of type B. The value can never b...
Definition: Either.cs:18