void SampleListener::onFrame(const Controller& controller) { // This function needs to be broken up, it's way too long const Frame frame = controller.frame(); //int currid = 1; //check if we already processed this frame if (frame.id() == this->lastFrameID) return; this->lastFrameID = frame.id(); //stop activity if we've not been used for ACTIVETIMEOUT if ((ACTIVETIMEOUT + this->lastEvent) < time(0) && this->active) { std::cout << "auto deactivated" << std::endl; this->active = false; } if (frame.fingers().count() == 2) { GestureList gestures = frame.gestures(); for (int it = 0; it < gestures.count(); ++it) { if (gestures[it].type() != Gesture::TYPE_KEY_TAP) continue; if ((this->lastToggle + TOGGLE_FRAME_LIMIT) > frame.id()) //avoid detecting the same tap twice continue; KeyTapGesture gesture = gestures[it]; Vector v = gesture.direction(); this->active = !this->active; this->lastToggle = frame.id(); this->lastEvent = time(0); std::cout << (this->active ? "activated" : "deactivated") << std::endl; break; //make sure we don't accidentally use the same list twice } } if (!this->active) return; if (frame.fingers().count() == 1 & side != 2) { PointableList pointables = frame.pointables(); InteractionBox iBox = frame.interactionBox(); for (int p = 0; p < pointables.count(); ++p) { Pointable pointable = pointables[p]; Vector normalizedPosition = iBox.normalizePoint(pointable.stabilizedTipPosition()); float distance = pointable.touchDistance(); float x = normalizedPosition.x * (Mouse->w_width+250); float y = (Mouse->w_height + 250) - normalizedPosition.y * (Mouse->w_height + 250); if (side == 0) { Mouse->move((int)x, (int)y); pressedDelay = 0; } if (distance < 0) { clickcount++; side = 1; if (clickcount > sensibility) { if (pressstate == 0) { Mouse->leftPress(); pressstate = 1; } else { if (clickcount >= 30) { if (rclick == 0) Mouse->move((int)x,(int)y); std::cout << "move side: "<< side << std::endl; clickcount = 20; } } } } else if (side == 1) { Mouse->leftRelease(); std::cout << "released side: " << side << std::endl; side = 0; pressstate = 0; pressedDelay = 0; clickcount = 0; rclick = 0; } } this->lastEvent = time(0); } else if (frame.fingers().count() > 1 & side != 1) { clickcount = 0; std::cout << "active side: " << side << std::endl; // Get gestures const GestureList gestures = frame.gestures(); for (int g = 0; g < gestures.count(); ++g) { Gesture gesture = gestures[g]; switch (gesture.type()) { case Gesture::TYPE_CIRCLE : { CircleGesture circle = gesture; if (gesture.state() == 3 & circle.progress() > 1) { side = 0; rclick = 1; Mouse->rightPress(); Mouse->rightRelease(); } break; } case Gesture::TYPE_SWIPE : { side = 2; SwipeGesture swipe = gesture; Vector d = swipe.direction(); // std::cout << "Swipe id: " << d(1) << std::endl; if (d[1] > 0.8) { this->mystate = 1; //up // std::cout << "wheel up " <<std::endl; } else if (d[1] < -0.8) { this->mystate = 2; //down //std::cout << "wheel down " <<std::endl; } if (swipe.state() == 3) { if (this->mystate == 1) { side = 0; this->mystate = 0; Mouse->wheelUp(); } else if (this->mystate == 2) { side = 0; this->mystate = 0; Mouse->wheelDown(); } } break; } default: //std::cout << "Unknown gesture type." << std::endl; break; } } this->lastEvent = time(0); } }
void LeapController::update() { hands = leap.getLeapHands(); if(leap.isFrameNew() && hands.size()){ for(int i = 0; i < hands.size(); i++){ Hand hand = hands[i]; if (handsPrevious.size() == hands.size() && hand.fingers().count() == 3){ float dx = hand.palmPosition().x - handsPrevious[i].palmPosition().x; float dy = hand.palmPosition().z - handsPrevious[i].palmPosition().z; float dz = -hand.palmPosition().y + handsPrevious[i].palmPosition().y; int numFingers = hands[i].fingers().count(); // horizontalPan = (numFingers == 2); horizontalPan = false; verticalPan = true; pan(dx,dy,dz); } } } Frame frame = controller.frame(); GestureList gestures = framePrevious.isValid() ? frame.gestures(framePrevious) : frame.gestures(); framePrevious = frame; for (size_t i=0; i < gestures.count(); i++) { if (gestures[i].type() == Gesture::TYPE_SCREEN_TAP) { ScreenTapGesture tap = gestures[i]; static GestureEventArgs args; args.pos = ofVec3f(tap.position().x, tap.position().y, tap.position().z); ofNotifyEvent(onTapScreen, args, this); } else if (gestures[i].type() == Gesture::TYPE_KEY_TAP) { KeyTapGesture tap = gestures[i]; static GestureEventArgs args; args.pos = ofVec3f(tap.position().x, tap.position().y, tap.position().z); ofNotifyEvent(onTapDown, args, this); //cout << "LEAP TAP GESTURE AT: " << pos << endl; } else if (gestures[i].type() == Gesture::TYPE_SWIPE) { SwipeGesture swipe = gestures[i]; Vector diff = 0.004f*(swipe.position() - swipe.startPosition()); static GestureEventArgs args; args.pos = ofVec3f(swipe.position().x, swipe.position().y, swipe.position().z); args.startPos = ofVec3f(swipe.startPosition().x, swipe.startPosition().y, swipe.startPosition().z); args.state = swipe.state(); ofNotifyEvent(onSwipe, args, this); //cout << "LEAP SWIPE GESTURE" << endl; } else if (gestures[i].type() == Gesture::TYPE_CIRCLE) { CircleGesture circle = gestures[i]; float progress = circle.progress(); if (progress >= 1.0f) { double curAngle = 6.5; //cout << "LEAP CIRCLE GESTURE" << endl; } static GestureEventArgs args; args.pos = ofVec3f(circle.center().x, circle.center().y, circle.center().z); args.normal = ofVec3f(circle.normal().x, circle.normal().y, circle.normal().z); args.progress = circle.progress(); ofNotifyEvent(onCircle, args, this); } } handsPrevious = hands; leap.markFrameAsOld(); SceneController::update(); }