Tanoda
AbstractHoldDetector.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;
11
12namespace Leap.Unity {
13
20 public abstract class AbstractHoldDetector : Detector {
21
23 protected abstract void ensureUpToDate();
24
25 [SerializeField]
27 public HandModelBase HandModel { get { return _handModel; } set { _handModel = value; } }
28
35 [Tooltip("Whether to change the transform of the parent object.")]
36 public bool ControlsTransform = true;
40 [Tooltip("Draw this detector's Gizmos, if any. (Gizmos must be on in Unity edtor, too.)")]
41 public bool ShowGizmos = true;
42
43 protected int _lastUpdateFrame = -1;
44
45 protected bool _didChange = false;
46
47 protected Vector3 _position;
48 protected Quaternion _rotation;
49 protected Vector3 _direction = Vector3.forward;
50 protected Vector3 _normal = Vector3.up;
51 protected float _distance;
52
53 protected float _lastHoldTime = 0.0f;
54 protected float _lastReleaseTime = 0.0f;
55 protected Vector3 _lastPosition = Vector3.zero;
56 protected Quaternion _lastRotation = Quaternion.identity;
57 protected Vector3 _lastDirection = Vector3.forward;
58 protected Vector3 _lastNormal = Vector3.up;
59 protected float _lastDistance = 1.0f;
60
61
62 protected virtual void Awake() {
63 if (GetComponent<HandModelBase>() != null && ControlsTransform == true) {
64 Debug.LogWarning("Detector should not be control the HandModelBase's transform. Either attach it to its own transform or set ControlsTransform to false.");
65 }
66 if (_handModel == null) {
67 _handModel = GetComponentInParent<HandModelBase>();
68 if (_handModel == null) {
69 Debug.LogWarning("The HandModel field of Detector was unassigned and the detector has been disabled.");
70 enabled = false;
71 }
72 }
73 }
74
75 protected virtual void Update() {
76 //We ensure the data is up to date at all times because
77 //there are some values (like LastPinchTime) that cannot
78 //be updated on demand
80 }
81
85 public virtual bool IsHolding {
86 get {
88 return IsActive;
89 }
90 }
91
96 public virtual bool DidChangeFromLastFrame {
97 get {
99 return _didChange;
100 }
101 }
102
106 public virtual bool DidStartHold {
107 get {
110 }
111 }
112
116 public virtual bool DidRelease {
117 get {
120 }
121 }
122
126 public float LastHoldTime {
127 get {
129 return _lastHoldTime;
130 }
131 }
132
136 public float LastReleaseTime {
137 get {
139 return _lastReleaseTime;
140 }
141 }
142
147 public Vector3 Position {
148 get {
150 return _position;
151 }
152 }
153 public Vector3 LastActivePosition { get { return _lastPosition; } }
158 public Quaternion Rotation {
159 get {
161 return _rotation;
162 }
163 }
164 public Quaternion LastActiveRotation { get { return _lastRotation; } }
165
166 public Vector3 Direction { get { return _direction; } }
167 public Vector3 LastActiveDirection { get { return _lastDirection; } }
168 public Vector3 Normal { get { return _normal; } }
169 public Vector3 LastActiveNormal { get { return _lastNormal; } }
170 public float Distance { get { return _distance; } }
171 public float LastActiveDistance { get { return _lastDistance; } }
172
173
174 protected virtual void changeState(bool shouldBeActive) {
175 bool currentState = IsActive;
176 if (shouldBeActive) {
177 _lastHoldTime = Time.time;
178 Activate();
179 } else {
180 _lastReleaseTime = Time.time;
181 Deactivate();
182 }
183 if (currentState != IsActive) {
184 _didChange = true;
185 }
186 }
187
188 #if UNITY_EDITOR
189 protected virtual void OnDrawGizmos () {
190 if (ShowGizmos) {
192 Color centerColor;
193 Vector3 centerPosition = _position;
194 Quaternion circleRotation = _rotation;
195 if (IsHolding) {
196 centerColor = OnColor;
197 } else {
198 centerColor = OffColor;
199 }
200 Vector3 axis;
201 float angle;
202 circleRotation.ToAngleAxis(out angle, out axis);
203 Utils.DrawCircle(centerPosition, Normal, Distance / 2, centerColor);
204 Gizmos.color = NormalColor;
205 Gizmos.DrawLine(centerPosition, centerPosition + Direction * Distance / 2);
206 Gizmos.color = DirectionColor;
207 Gizmos.DrawLine(centerPosition, centerPosition + Normal * Distance / 2);
208 }
209 }
210 #endif
211
212 }
213}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
UnityEngine.Color Color
Definition: TestScript.cs:32
float LastHoldTime
Returns the value of Time.time during the most recent pinch event.
virtual void changeState(bool shouldBeActive)
float LastReleaseTime
Returns the value of Time.time during the most recent unpinch event.
virtual bool IsHolding
Returns whether or not the dectector is currently detecting a pinch or grab.
Vector3 Position
Returns the position value of the detected pinch or grab. If a pinch or grab is not currently being d...
virtual bool DidStartHold
Returns whether or not the value of IsHolding changed to true between this frame and the previous.
virtual bool DidChangeFromLastFrame
Returns whether or not the value of IsPinching is different than the value reported during the previo...
Quaternion Rotation
Returns the rotation value of the detected pinch or grab. If a pinch or grab is not currently being d...
virtual bool DidRelease
Returns whether or not the value of IsHolding changed to false between this frame and the previous.
virtual void Deactivate()
Definition: Detector.cs:66
Color DirectionColor
Definition: Detector.cs:77
virtual void Activate()
Definition: Detector.cs:54