//Handle Leap Gesture processing.
//Trigger the corresponding effects in the particle field.
void GesturesDemo::processGestures() {
  Leap::Frame frame = controller.frame();

  if ( lastFrame == frame ) {
    return;
  }

  Leap::GestureList gestures =  lastFrame.isValid()       ?
                                frame.gestures(lastFrame) :
                                frame.gestures();

  lastFrame = frame;

  size_t numGestures = gestures.count();

  for (size_t i=0; i < numGestures; i++) {
    if (gestures[i].type() == Leap::Gesture::TYPE_SCREEN_TAP) {
      printf("screen screen tap gesture");
      Leap::ScreenTapGesture tap = gestures[i];
      ci::Vec3f tapLoc = normalizeCoords(tap.position());
      field.Repel(tap.id(), ci::Vec2f(tapLoc.x, tapLoc.y), 3.0);
    } else if (gestures[i].type() == Leap::Gesture::TYPE_KEY_TAP) {
      printf("screen key tap gesture");
      Leap::KeyTapGesture tap = gestures[i];
      ci::Vec3f tapLoc = normalizeCoords(tap.position());
      field.Repel(tap.id(), ci::Vec2f(tapLoc.x, tapLoc.y), -3.0);
    } else if (gestures[i].type() == Leap::Gesture::TYPE_SWIPE) {
      printf(" swipe  gesture");
      Leap::SwipeGesture swipe = gestures[i];
      Leap::Vector diff = 0.004f*(swipe.position() - swipe.startPosition());
      ci::Vec3f curSwipe(diff.x, -diff.y, diff.z);
      field.Translate(swipe.id(), curSwipe);
    } else if (gestures[i].type() == Leap::Gesture::TYPE_CIRCLE) {
      printf(" circle gesture");
      Leap::CircleGesture circle = gestures[i];
      float progress = circle.progress();
      if (progress >= 1.0f) {
        ci::Vec3f center = normalizeCoords(circle.center());
        ci::Vec3f normal(circle.normal().x, circle.normal().y, circle.normal().z);
        double curAngle = 6.5;
        if (normal.z < 0) {
          curAngle *= -1;
        }
        field.Rotate(circle.id(), ci::Vec2f(center.x, center.y), circle.radius()/250, curAngle);
      }
    }
  }
}
//--------------------------------------------------------------
void ofxLeapMotion::updateGestures(){
	
	Leap::Frame frame = ourController->frame();
	
	if(lastFrame == frame){
		return;
	}
	
	Leap::GestureList gestures = lastFrame.isValid() ? frame.gestures(lastFrame) : frame.gestures();
	
	lastFrame = frame;
	
	size_t numGestures = gestures.count();
	
	for(size_t i=0; i < numGestures; i++){
		
		// screen tap gesture (forward poke / tap)
		if(gestures[i].type() == Leap::Gesture::TYPE_SCREEN_TAP){
			Leap::ScreenTapGesture tap = gestures[i];

			screenTapPosition = getMappedofPoint(tap.position());   // screen tap gesture data = tap position
			screenTapDirection = getofPoint(tap.direction());       // screen tap gesture data = tap direction

			iGestures = 1;
		}
		
		// key tap gesture (down tap)
		else if(gestures[i].type() == Leap::Gesture::TYPE_KEY_TAP){
			Leap::KeyTapGesture tap = gestures[i];

			keyTapPosition = getofPoint(tap.position());            // key tap gesture data = tap position

			iGestures = 2;
		}
		
		// swipe gesture
		else if(gestures[i].type() == Leap::Gesture::TYPE_SWIPE){
			Leap::SwipeGesture swipe = gestures[i];
			Leap::Vector diff = 0.04f*(swipe.position() - swipe.startPosition());
			ofVec3f curSwipe(diff.x, -diff.y, diff.z);
			
			// swipe left
			if(curSwipe.x < -3 && curSwipe.x > -20){
				iGestures = 4;
			}
			// swipe right
			else if(curSwipe.x > 3 && curSwipe.x < 20){
				iGestures = 3;
			}
			// swipe up
			if(curSwipe.y < -3 && curSwipe.y > -20){
				iGestures = 6;
			}
			// swipe down
			else if(curSwipe.y > 3 && curSwipe.y < 20){
				iGestures = 5;
			}
			
			// 3D swiping
			// swipe forward
			if(curSwipe.z < -5){
				iGestures = 7;
			}
			// swipe back
			else if(curSwipe.z > 5){
				iGestures = 8;
			}
			
			// more swipe gesture data
			swipeSpeed = swipe.speed();                             // swipe speed in mm/s
			swipeDurationSeconds = swipe.durationSeconds();         // swipe duration in seconds
			swipeDurationMicros = swipe.duration();                 // swipe duration in micros
			swipe.position();
		}
		
		// circle gesture
		else if(gestures[i].type() == Leap::Gesture::TYPE_CIRCLE){
			Leap::CircleGesture circle = gestures[i];
			circleProgress = circle.progress();                     // circle progress

			if(circleProgress >= 1.0f){
				
				circleCenter = getMappedofPoint(circle.center());                           // changed to global
				circleNormal.set(circle.normal().x, circle.normal().y, circle.normal().z);  // changed to global

				double curAngle = 6.5;
				if(circleNormal.z < 0){
					curAngle *= -1;
				}
				
				if(curAngle < 0){
					// clockwise rotation
					iGestures = 10;
				}
				else{
					// counter-clockwise rotation
					iGestures = 9;
				}
			}
		}
		
		// kill gesture when done
		// gestures 5 & 6 are always in a STATE_STOP so we exclude
		if(gestures[i].type() != 5 && gestures[i].type() != 6){
			if(gestures[i].state() == Leap::Gesture::STATE_STOP){
				iGestures = 0;
			}
		}
	}
}