123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- using System;
- using Leap.Unity.Attributes;
- using UnityEngine;
- namespace Leap.Unity
- {
- public abstract class AbstractHoldDetector : Detector
- {
- protected abstract void ensureUpToDate();
- public IHandModel HandModel
- {
- get
- {
- return this._handModel;
- }
- set
- {
- this._handModel = value;
- }
- }
- protected virtual void Awake()
- {
- if (base.GetComponent<IHandModel>() != null && this.ControlsTransform)
- {
- Debug.LogWarning("Detector should not be control the IHandModel's transform. Either attach it to its own transform or set ControlsTransform to false.");
- }
- if (this._handModel == null)
- {
- this._handModel = base.GetComponentInParent<IHandModel>();
- if (this._handModel == null)
- {
- Debug.LogWarning("The HandModel field of Detector was unassigned and the detector has been disabled.");
- base.enabled = false;
- }
- }
- }
- protected virtual void Update()
- {
- this.ensureUpToDate();
- }
- public virtual bool IsHolding
- {
- get
- {
- this.ensureUpToDate();
- return base.IsActive;
- }
- }
- public virtual bool DidChangeFromLastFrame
- {
- get
- {
- this.ensureUpToDate();
- return this._didChange;
- }
- }
- public virtual bool DidStartHold
- {
- get
- {
- this.ensureUpToDate();
- return this.DidChangeFromLastFrame && this.IsHolding;
- }
- }
- public virtual bool DidRelease
- {
- get
- {
- this.ensureUpToDate();
- return this.DidChangeFromLastFrame && !this.IsHolding;
- }
- }
- public float LastHoldTime
- {
- get
- {
- this.ensureUpToDate();
- return this._lastHoldTime;
- }
- }
- public float LastReleaseTime
- {
- get
- {
- this.ensureUpToDate();
- return this._lastReleaseTime;
- }
- }
- public Vector3 Position
- {
- get
- {
- this.ensureUpToDate();
- return this._position;
- }
- }
- public Vector3 LastActivePosition
- {
- get
- {
- return this._lastPosition;
- }
- }
- public Quaternion Rotation
- {
- get
- {
- this.ensureUpToDate();
- return this._rotation;
- }
- }
- public Quaternion LastActiveRotation
- {
- get
- {
- return this._lastRotation;
- }
- }
- public Vector3 Direction
- {
- get
- {
- return this._direction;
- }
- }
- public Vector3 LastActiveDirection
- {
- get
- {
- return this._lastDirection;
- }
- }
- public Vector3 Normal
- {
- get
- {
- return this._normal;
- }
- }
- public Vector3 LastActiveNormal
- {
- get
- {
- return this._lastNormal;
- }
- }
- public float Distance
- {
- get
- {
- return this._distance;
- }
- }
- public float LastActiveDistance
- {
- get
- {
- return this._lastDistance;
- }
- }
- protected virtual void changeState(bool shouldBeActive)
- {
- bool isActive = base.IsActive;
- if (shouldBeActive)
- {
- this._lastHoldTime = Time.time;
- this.Activate();
- }
- else
- {
- this._lastReleaseTime = Time.time;
- this.Deactivate();
- }
- if (isActive != base.IsActive)
- {
- this._didChange = true;
- }
- }
- [AutoFind(AutoFindLocations.Parents)]
- [SerializeField]
- protected IHandModel _handModel;
- public bool ControlsTransform = true;
- protected int _lastUpdateFrame = -1;
- protected bool _didChange;
- protected Vector3 _position;
- protected Quaternion _rotation;
- protected Vector3 _direction = Vector3.forward;
- protected Vector3 _normal = Vector3.up;
- protected float _distance;
- protected float _lastHoldTime;
- protected float _lastReleaseTime;
- protected Vector3 _lastPosition = Vector3.zero;
- protected Quaternion _lastRotation = Quaternion.identity;
- protected Vector3 _lastDirection = Vector3.forward;
- protected Vector3 _lastNormal = Vector3.up;
- protected float _lastDistance = 1f;
- }
- }
|