using System;
using Leap.Unity.Attributes;
using UnityEngine;

namespace Leap.Unity
{
	public class PinchDetector : AbstractHoldDetector
	{
		public bool IsPinching
		{
			get
			{
				return this.IsHolding;
			}
		}

		public bool DidStartPinch
		{
			get
			{
				return this.DidStartHold;
			}
		}

		public bool DidEndPinch
		{
			get
			{
				return this.DidRelease;
			}
		}

		protected virtual void OnValidate()
		{
			this.ActivateDistance = Mathf.Max(0f, this.ActivateDistance);
			this.DeactivateDistance = Mathf.Max(0f, this.DeactivateDistance);
			if (this.DeactivateDistance < this.ActivateDistance)
			{
				this.DeactivateDistance = this.ActivateDistance;
			}
		}

		protected override void ensureUpToDate()
		{
			if (Time.frameCount == this._lastUpdateFrame)
			{
				return;
			}
			this._lastUpdateFrame = Time.frameCount;
			this._didChange = false;
			Hand leapHand = this._handModel.GetLeapHand();
			if (leapHand == null || !this._handModel.IsTracked)
			{
				this.changeState(false);
				return;
			}
			this._distance = leapHand.PinchDistance * 0.001f;
			this._rotation = leapHand.Basis.CalculateRotation();
			this._position = ((leapHand.Fingers[0].TipPosition + leapHand.Fingers[1].TipPosition) * 0.5f).ToVector3();
			if (base.IsActive)
			{
				if (this._distance > this.DeactivateDistance)
				{
					this.changeState(false);
				}
			}
			else if (this._distance < this.ActivateDistance)
			{
				this.changeState(true);
			}
			if (base.IsActive)
			{
				this._lastPosition = this._position;
				this._lastRotation = this._rotation;
				this._lastDistance = this._distance;
				this._lastDirection = this._direction;
				this._lastNormal = this._normal;
			}
			if (this.ControlsTransform)
			{
				base.transform.position = this._position;
				base.transform.rotation = this._rotation;
			}
		}

		protected const float MM_TO_M = 0.001f;

		public float ActivateDistance = 0.03f;

		public float DeactivateDistance = 0.04f;

		[MinValue(0f)]
		[SerializeField]
		protected float _activatePinchDist = 0.03f;

		[MinValue(0f)]
		[SerializeField]
		protected float _deactivatePinchDist = 0.04f;

		protected bool _isPinching;

		protected float _lastPinchTime;

		protected float _lastUnpinchTime;

		protected Vector3 _pinchPos;

		protected Quaternion _pinchRotation;
	}
}
