AbstractHoldDetector.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System;
  2. using Leap.Unity.Attributes;
  3. using UnityEngine;
  4. namespace Leap.Unity
  5. {
  6. public abstract class AbstractHoldDetector : Detector
  7. {
  8. protected abstract void ensureUpToDate();
  9. public IHandModel HandModel
  10. {
  11. get
  12. {
  13. return this._handModel;
  14. }
  15. set
  16. {
  17. this._handModel = value;
  18. }
  19. }
  20. protected virtual void Awake()
  21. {
  22. if (base.GetComponent<IHandModel>() != null && this.ControlsTransform)
  23. {
  24. Debug.LogWarning("Detector should not be control the IHandModel's transform. Either attach it to its own transform or set ControlsTransform to false.");
  25. }
  26. if (this._handModel == null)
  27. {
  28. this._handModel = base.GetComponentInParent<IHandModel>();
  29. if (this._handModel == null)
  30. {
  31. Debug.LogWarning("The HandModel field of Detector was unassigned and the detector has been disabled.");
  32. base.enabled = false;
  33. }
  34. }
  35. }
  36. protected virtual void Update()
  37. {
  38. this.ensureUpToDate();
  39. }
  40. public virtual bool IsHolding
  41. {
  42. get
  43. {
  44. this.ensureUpToDate();
  45. return base.IsActive;
  46. }
  47. }
  48. public virtual bool DidChangeFromLastFrame
  49. {
  50. get
  51. {
  52. this.ensureUpToDate();
  53. return this._didChange;
  54. }
  55. }
  56. public virtual bool DidStartHold
  57. {
  58. get
  59. {
  60. this.ensureUpToDate();
  61. return this.DidChangeFromLastFrame && this.IsHolding;
  62. }
  63. }
  64. public virtual bool DidRelease
  65. {
  66. get
  67. {
  68. this.ensureUpToDate();
  69. return this.DidChangeFromLastFrame && !this.IsHolding;
  70. }
  71. }
  72. public float LastHoldTime
  73. {
  74. get
  75. {
  76. this.ensureUpToDate();
  77. return this._lastHoldTime;
  78. }
  79. }
  80. public float LastReleaseTime
  81. {
  82. get
  83. {
  84. this.ensureUpToDate();
  85. return this._lastReleaseTime;
  86. }
  87. }
  88. public Vector3 Position
  89. {
  90. get
  91. {
  92. this.ensureUpToDate();
  93. return this._position;
  94. }
  95. }
  96. public Vector3 LastActivePosition
  97. {
  98. get
  99. {
  100. return this._lastPosition;
  101. }
  102. }
  103. public Quaternion Rotation
  104. {
  105. get
  106. {
  107. this.ensureUpToDate();
  108. return this._rotation;
  109. }
  110. }
  111. public Quaternion LastActiveRotation
  112. {
  113. get
  114. {
  115. return this._lastRotation;
  116. }
  117. }
  118. public Vector3 Direction
  119. {
  120. get
  121. {
  122. return this._direction;
  123. }
  124. }
  125. public Vector3 LastActiveDirection
  126. {
  127. get
  128. {
  129. return this._lastDirection;
  130. }
  131. }
  132. public Vector3 Normal
  133. {
  134. get
  135. {
  136. return this._normal;
  137. }
  138. }
  139. public Vector3 LastActiveNormal
  140. {
  141. get
  142. {
  143. return this._lastNormal;
  144. }
  145. }
  146. public float Distance
  147. {
  148. get
  149. {
  150. return this._distance;
  151. }
  152. }
  153. public float LastActiveDistance
  154. {
  155. get
  156. {
  157. return this._lastDistance;
  158. }
  159. }
  160. protected virtual void changeState(bool shouldBeActive)
  161. {
  162. bool isActive = base.IsActive;
  163. if (shouldBeActive)
  164. {
  165. this._lastHoldTime = Time.time;
  166. this.Activate();
  167. }
  168. else
  169. {
  170. this._lastReleaseTime = Time.time;
  171. this.Deactivate();
  172. }
  173. if (isActive != base.IsActive)
  174. {
  175. this._didChange = true;
  176. }
  177. }
  178. [AutoFind(AutoFindLocations.Parents)]
  179. [SerializeField]
  180. protected IHandModel _handModel;
  181. public bool ControlsTransform = true;
  182. protected int _lastUpdateFrame = -1;
  183. protected bool _didChange;
  184. protected Vector3 _position;
  185. protected Quaternion _rotation;
  186. protected Vector3 _direction = Vector3.forward;
  187. protected Vector3 _normal = Vector3.up;
  188. protected float _distance;
  189. protected float _lastHoldTime;
  190. protected float _lastReleaseTime;
  191. protected Vector3 _lastPosition = Vector3.zero;
  192. protected Quaternion _lastRotation = Quaternion.identity;
  193. protected Vector3 _lastDirection = Vector3.forward;
  194. protected Vector3 _lastNormal = Vector3.up;
  195. protected float _lastDistance = 1f;
  196. }
  197. }