Exemple #1
0
//--------------------------------------------------------------
void ofApp::draw(){
    string touchStatus;
    stringstream info;
    
    cam.begin();
    
    ofDrawGrid(500, 5, true);
    
    light1.enable();
    light2.enable();
    
    ofSetColor(255, 215, 0);
    Frame frame = controller.frame();
    for (int i=0; i < frame.hands().count(); i++) {
        Hand hand = frame.hands()[i];
        for (int j = 0; j < hand.fingers().count(); j ++) {
            if (j == 0) {
                Finger finger = frame.fingers()[j];
                ofSpherePrimitive sphere;
                sphere.setPosition(finger.tipPosition().x, finger.tipPosition().y, finger.tipPosition().z);
                sphere.draw();
                info << "Finger position x : " << finger.tipPosition().x << " y : " << finger.tipPosition().y <<  " z : " << finger.tipPosition().z << endl;
            }
        }
    }
    cam.end();
    
    
    PointableList pointables = controller.frame().pointables();
    InteractionBox iBox = controller.frame().interactionBox();
    for (int p = 0; p < pointables.count(); p++) {
        Pointable pointable = pointables[p];
        Vector normalizedPosition = iBox.normalizePoint(pointable.stabilizedTipPosition());
        float x = normalizedPosition.x * ofGetWidth();
        float y = ofGetHeight() - normalizedPosition.y * ofGetHeight();
        
        if (pointable.touchDistance() > 0 && pointable.touchZone() != Leap::Pointable::ZONE_NONE) {
            ofSetColor(0, 255, 0);
            touchStatus = "Hover";
        } else if (pointable.touchDistance() <= 0) {
            ofSetColor(255, 0, 0);
            touchStatus = "Touch";
        } else {
            ofSetColor(0, 0, 255);
            touchStatus = "None";
        }
        ofCircle(x, y, 30);
        info << "Point Number : " << p << endl;
        info << "Touch distance : " << ofToString(pointable.touchDistance()) << endl;
        info << "Circle x : " << x << " y : " << y << endl;
    }
    
    ofDrawBitmapString("Touch Status : " + touchStatus, 20, 20);
    ofDrawBitmapString(info.str(), 20, 40);
}
Exemple #2
0
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);
  }
}