using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Leap;
using System.Linq;
public class LeapGestures : MonoBehaviour
{
public static bool Gesture_left = false;
public static bool Gesture_right = false;
public static bool Gesture_up = false;
public static bool Gesture_down = false;
public static bool Gesture_zoom = false;
public static float movePOs = 0.0f;
private LeapProvider mProvider;
private Frame mFrame;
private Hand mHand;
private UnityEngine.Vector3 leftPosition;
private UnityEngine.Vector3 rightPosition;
public static float zoom = 1.0f;
[Tooltip("Velocity (m/s) of Palm ")]
public float smallestVelocity = 1.45f;
[Tooltip("Velocity (m/s) of Single Direction ")]
[Range(0, 1)]
public float deltaVelocity = 1.0f;
void Start()
{
mProvider = FindObjectOfType<LeapProvider>() as LeapProvider;
}
void Update()
{
mFrame = mProvider.CurrentFrame;
if (mFrame.Hands.Count > 0)
{
if (mFrame.Hands.Count == 2)
zoom = CalcuateDistance(mFrame);
if (mFrame.Hands.Count == 1)
LRUDGestures(mFrame, ref movePOs);
}
}
float CalcuateDistance(Frame mFrame)
{
Gesture_zoom = true;
Gesture_left = false;
Gesture_right = false;
float distance = 0f;
foreach (var itemHands in mFrame.Hands)
{
if (itemHands.IsLeft)
{
leftPosition = itemHands.PalmPosition;
}
if (itemHands.IsRight)
{
rightPosition = itemHands.PalmPosition;
}
}
if (leftPosition != Vector3.zero && rightPosition != Vector3.zero)
{
Vector3 leftPos = new Vector3(leftPosition.x, leftPosition.y, leftPosition.z);
Vector3 rightPos = new Vector3(rightPosition.x, rightPosition.y, rightPosition.z);
distance = 10 * Vector3.Distance(leftPos, rightPos);
print("distance" + distance);
}
if (distance != 0)
return distance;
else
return distance = 1;
}
void LRUDGestures(Frame mFrame, ref float movePOs)
{
Gesture_zoom = false;
foreach (var item in mFrame.Hands)
{
int numFinger = item.fingers.Count();
if (item.GrabStrength == 0)
{
}
else if (item.GrabStrength == 1)
{
movePOs = item.PalmPosition.x;
if (isMoveLeft(item))
{
Gesture_left = true;
Gesture_right = false;
print("move left");
}
else if (isMoveRight(item))
{
Gesture_left = false;
Gesture_right = true;
print("move Right");
}
else if (isMoveUp(item))
{
Gesture_left = false;
Gesture_right = false;
print("move Up");
}
else if (isMoveDown(item))
{
Gesture_left = false;
Gesture_right = false;
print("move Down");
}
else if (isMoveForward(item))
{
Gesture_left = false;
Gesture_right = false;
print("move Forward");
}
else if (isMoveBack(item))
{
Gesture_left = false;
Gesture_right = false;
print("move back");
}
}
}
}
public bool isGrabHand(Hand hand)
{
return hand.GrabStrength > 0.8f;
}
public bool isMoveRight(Hand hand)
{
return hand.PalmVelocity.x > deltaVelocity && !isStationary(hand);
}
public bool isMoveLeft(Hand hand)
{
return hand.PalmVelocity.x < -deltaVelocity && !isStationary(hand);
}
public bool isMoveUp(Hand hand)
{
return hand.PalmVelocity.y > deltaVelocity && !isStationary(hand);
}
public bool isMoveDown(Hand hand)
{
return hand.PalmVelocity.y < -deltaVelocity && !isStationary(hand);
}
public bool isMoveForward(Hand hand)
{
return hand.PalmVelocity.z > deltaVelocity && !isStationary(hand);
}
public bool isMoveBack(Hand hand)
{
return hand.PalmVelocity.z < -deltaVelocity && !isStationary(hand);
}
public bool isStationary(Hand hand)
{
return hand.PalmVelocity.magnitude < smallestVelocity;
}
}