Beispiel #1
0
int main(int argc, char** argv)
{
	struct sockaddr_in si_other;
	int s, slen = sizeof(si_other);
	char buf[BUFLEN];
	char message[BUFLEN];
	WSADATA wsa;

	//Initialise winsock
	printf("\nInitialising Winsock...");
	if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
	{
		printf("Failed. Error Code : %d", WSAGetLastError());
		exit(EXIT_FAILURE);
	}
	printf("Initialised.\n");

	//create socket
	//if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR)
	if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET)
	{
		printf("socket() failed with error code : %d", WSAGetLastError());
		exit(EXIT_FAILURE);
	}
	char str[INET_ADDRSTRLEN];
	//setup address structure
	memset((char *)&si_other, 0, sizeof(si_other));
	si_other.sin_family = AF_INET;
	si_other.sin_port = htons(PORT);
	//si_other.sin_addr.S_un.S_addr = inet_pton(AF_INET, SERVER, &(si_other.sin_addr));
	inet_pton(AF_INET, SERVER, &(si_other.sin_addr.S_un.S_addr));
	/*if (inet_pton(AF_INET, SERVER, &(si_other.sin_addr.S_un.S_addr)))
	{
		inet_ntop(AF_INET, &(si_other.sin_addr.S_un.S_addr), str, INET_ADDRSTRLEN);
		std::cout << ("%s\n", str);
	}*/

	// We catch any exceptions that might occur below -- see the catch statement for more details.
	try {

		// First, we create a Hub with our application identifier. Be sure not to use the com.example namespace when
		// publishing your application. The Hub provides access to one or more Myos.
		myo::Hub hub("om.example.emg-data-sample");

		std::cout << "Attempting to find a Myo..." << std::endl;

		// Next, we attempt to find a Myo to use. If a Myo is already paired in Myo Connect, this will return that Myo
		// immediately.
		// waitForMyo() takes a timeout value in milliseconds. In this case we will try to find a Myo for 10 seconds, and
		// if that fails, the function will return a null pointer.
		myo::Myo* myo = hub.waitForMyo(10000);

		// If waitForMyo() returned a null pointer, we failed to find a Myo, so exit with an error message.
		if (!myo) {
			throw std::runtime_error("Unable to find a Myo!");
		}

		// We've found a Myo.
		std::cout << "Connected to a Myo armband!" << std::endl << std::endl;

		// Next we enable EMG streaming on the found Myo.
		myo->setStreamEmg(myo::Myo::streamEmgEnabled);

		// Create a sample listener and controller for Leap Motion
		SampleListener listener;
		Controller controller;

		// Next we construct an instance of our DeviceListener, so that we can register it with the Hub.
		DataCollector collector;
		double timeElasped = 0.000;
		const double minMax[10] = { 32, 85, 36, 100, 37, 107, 36, 100, 36, 90 }; //T.I.M.R.P

		// Hub::addListener() takes the address of any object whose class inherits from DeviceListener, and will cause
		// Hub::run() to send events to all registered device listeners.
		hub.addListener(&collector);

		//controller.addListener(listener);

		if (argc > 1 && strcmp(argv[1], "--bg") == 0)
			controller.setPolicy(Leap::Controller::POLICY_BACKGROUND_FRAMES);
		
		myfile << std::fixed;
		myfile << std::setprecision(2);

		// Finally we enter our main loop.
		while (1) {
			//collector.tic();
			// In each iteration of our main loop, we run the Myo event loop for a set number of milliseconds.
			// In this case, we wish to update our display 50 times a second, so we run for 1000/20 milliseconds.
			hub.run(1000 / 100);
			// After processing events, we call the print() member function we defined above to print out the values we've
			// obtained from any events that have occurred.
			collector.print();
			int i = 0;
			int j = 1;
			int h = 0;
			double fingDis[5];
			const Frame frame = controller.frame();
			HandList hands = frame.hands();
			for (HandList::const_iterator hl = hands.begin(); hl != hands.end(); ++hl) {
				// Get the first hand
				const Hand hand = *hl;
				// Get fingers
				const FingerList fingers = hand.fingers();
				
				for (FingerList::const_iterator fl = fingers.begin(); fl != fingers.end(); ++fl) {
					const Finger finger = *fl;

					//myfile << " " << hand.palmPosition().distanceTo(finger.tipPosition());
					/*myfile << std::string(4, ' ') << fingerNames[finger.type()]
						<< ": " << listener.mapping(hand.palmPosition().distanceTo(finger.tipPosition()), minMax[i + i], minMax[i + j]);*/
					fingDis[h] = listener.mapping(hand.palmPosition().distanceTo(finger.tipPosition()), minMax[i + i], minMax[i + j]);
					//fingDis[h] = hand.palmPosition().distanceTo(finger.tipPosition());
					i++;
					j++;
					h++;
						if (i == 5 && j == 6 && h == 5)
						{
							string tmp = to_string(fingDis[0]) + " " + to_string(fingDis[1]) + " " + to_string(fingDis[2]) + " " + to_string(fingDis[3]) + " " + to_string(fingDis[4]);
							//string tmp = to_string('0');
							strcpy_s(message, tmp.c_str());
							//send message
							if (sendto(s, message, strlen(message), 0, (struct sockaddr *) &si_other, slen) == SOCKET_ERROR)
							{
								printf("sendto() failed with error code : %d", WSAGetLastError());
								//exit(EXIT_FAILURE);
							}
							std::cout << "Data Sent";
							i = 0;
							j = 1;
							h = 0;
						}
				}
			}

			//timeElasped = timeElasped + ((double)(clock() - tictoc_stack.top())) / CLOCKS_PER_SEC;
			/*myfile << " Time elapsed: "
				<< ((double)(clock() - tictoc_stack.top())) / CLOCKS_PER_SEC;*/
			//tictoc_stack.pop();
			//myfile << " " << timeElasped << endl;

		}
		
		// If a standard exception occurred, we print out its message and exit.
	}
	catch (const std::exception& e) {
		std::cerr << "Error: " << e.what() << std::endl;
		std::cerr << "Press enter to continue.";
		std::cin.ignore();
		return 1;
	}
	closesocket(s);
	WSACleanup();
}
Beispiel #2
0
void SampleListener::onFrame(const Controller& controller)
{
    bool thand, index = false;

    // Get the most recent frame and report some basic information
    const Frame frame = controller.frame();

    // Receive Hand List
    HandList hands = frame.hands();

    // For all hands
    for (HandList::const_iterator hl = hands.begin(); hl != hands.end(); ++hl)
    {
        // Get the first hand
        const Hand hand         = *hl;

        // Only for right hand
        if( hand.isRight() )
        {
            // Get fingers
            const FingerList fingers = hand.fingers();
            // For all fingers in the list
            for (FingerList::const_iterator fl = fingers.begin(); fl != fingers.end(); ++fl)
            {
                      Bone      bone;
                const Finger    finger = *fl;

                // If thamb or Index finger
                if( finger.type() == Finger::TYPE_THUMB )
                {
                    // Receive nided bone
                    bone        = finger.bone(Bone::TYPE_DISTAL);
                    m_vThand    = bone.center();

                    thand = true;
                }
                else if( finger.type() == Finger::TYPE_INDEX )
                {
                    // Receive nided bone
                    bone        = finger.bone(Bone::TYPE_DISTAL);
                    m_vIndex    = bone.center();

                    index = true;
                }
            }
        }
    }

    bool newState;
    if( !thand || !index )
    {
        newState = false;
    }
    else
    {
        float distance = m_vThand.distanceTo(m_vIndex);
        qDebug () << "Distance: " << distance;

        if( distance < 40 )
        {
            newState = true;
        }
        else
        {
            newState = false;
        }
    }

    if( newState != m_bLastState )
    {
        Q_EMIT releySignal( newState );
        m_bLastState = newState;
    }
}
Beispiel #3
0
void LeapHander::frame(pugi::xml_node &frameNode){
	Leap::Frame currentFrame = m_sampleListener.frame();
	m_lock.lock();
	frameNode.append_attribute("id").set_value(currentFrame.id());
	pugi::xml_node handList = frameNode.append_child("hands");
	HandList hands = currentFrame.hands();
	
	for (HandList::const_iterator hl = hands.begin(); hl != hands.end(); ++hl) {
		// Get the first hand
		const Hand hand = *hl;
		pugi::xml_node handNode = handList.append_child("hand");
		handNode.append_attribute("id").set_value(hand.id());
		std::string handType;
		if (hand.isLeft()) {
			handType = "Left";
		}
		else {
			handType = "Right";
		}
		handNode.append_attribute("type").set_value(handType.c_str());
		
		pugi::xml_node positionNode = handNode.append_child("position");
		positionToXml(positionNode, hand.palmPosition());
		
		/*pugi::xml_node normalNode = handNode.append_child("normal");
		positionToXml(normalNode, hand.palmNormal());

		pugi::xml_node directionNode = handNode.append_child("direction");
		positionToXml(directionNode, hand.direction());

		pugi::xml_node rotationNode = handNode.append_child("basis");
		rotationToXml(rotationNode, hand.basis());*/
		//// Get fingers
		pugi::xml_node fingerList = handNode.append_child("fingers");
		const FingerList fingers = hand.fingers();
		for (FingerList::const_iterator fl = fingers.begin(); fl != fingers.end(); ++fl) {
			const Finger finger = *fl;
			pugi::xml_node fingerNode = fingerList.append_child("finger");
			fingerNode.append_attribute("id").set_value(finger.id());
			fingerNode.append_attribute("name").set_value(fingerNames[finger.type()].c_str());
			pugi::xml_node boneList = fingerNode.append_child("bones");
			

			// Get finger bones
			for (int b = 0; b < 4; ++b) {
				Bone::Type boneType = static_cast<Bone::Type>(b);
				Bone bone = finger.bone(boneType);
				pugi::xml_node boneNode = boneList.append_child("bone");
				boneNode.append_attribute("length").set_value(bone.length());
				boneNode.append_attribute("name").set_value(boneNames[boneType].c_str());

				pugi::xml_node prevJoint = boneNode.append_child("prevJoint");
				positionToXml(prevJoint, bone.prevJoint());

				pugi::xml_node nextJoint = boneNode.append_child("nextJoint");
				positionToXml(nextJoint, bone.nextJoint());

				/*pugi::xml_node rotation = boneNode.append_child("basis");
				rotationToXml(rotation, bone.basis());*/
			}
		}
	}
	m_lock.unlock();
}
void AirwritingListener::onFrame(const Controller& controller) {
    // Get the most recent frame and report some basic information
    const Frame frame = controller.frame();
    unsigned long long timestamp = frame.timestamp();
    
    /*
    std::cout << "Frame id: " << frame.id()
    << ", timestamp: " << frame.timestamp()
    << ", hands: " << frame.hands().count()
    << ", extended fingers: " << frame.fingers().extended().count()
    << ", tools: " << frame.tools().count()
    << ", gestures: " << frame.gestures().count() << std::endl;
    */

    
    HandList hands = frame.hands();
    for (HandList::const_iterator hl = hands.begin(); hl != hands.end(); ++hl) {
        // Get the first hand
        const Hand hand = *hl;
        std::string handType = hand.isLeft() ? "Left hand" : "Right hand";
        
        /*
        std::cout << std::string(2, ' ') << handType << ", id: " << hand.id()
        << ", palm position: " << hand.palmPosition() << std::endl;
        */
        
        /*
        // Get the hand's normal vector and direction
        const Vector normal = hand.palmNormal();
        const Vector direction = hand.direction();
        */
        
        /*
        // Calculate the hand's pitch, roll, and yaw angles
        std::cout << std::string(2, ' ') <<  "pitch: " << direction.pitch() * RAD_TO_DEG << " degrees, "
        << "roll: " << normal.roll() * RAD_TO_DEG << " degrees, "
        << "yaw: " << direction.yaw() * RAD_TO_DEG << " degrees" << std::endl;
        */
        
        /*
        // Get the Arm bone
        Arm arm = hand.arm();
        std::cout << std::string(2, ' ') <<  "Arm direction: " << arm.direction()
        << " wrist position: " << arm.wristPosition()
        << " elbow position: " << arm.elbowPosition() << std::endl;
        */
        
        // Get fingers
        const FingerList fingers = hand.fingers();
        for (FingerList::const_iterator fl = fingers.begin(); fl != fingers.end(); ++fl) {
            const Finger finger = *fl;
            sample(finger, timestamp);
            
            /*
            std::cout << std::string(4, ' ') <<  fingerNames[finger.type()]
            << " finger, id: " << finger.id()
            << ", length: " << finger.length()
            << "mm, width: " << finger.width() << std::endl;
            */
            
            /*
            // Get finger bones
            for (int b = 0; b < 4; ++b) {
                Bone::Type boneType = static_cast<Bone::Type>(b);
                Bone bone = finger.bone(boneType);
                std::cout << std::string(6, ' ') <<  boneNames[boneType]
                << " bone, start: " << bone.prevJoint()
                << ", end: " << bone.nextJoint()
                << ", direction: " << bone.direction() << std::endl;
            }
            */
        }
    }
    
    
    /*
    // Get tools
    const ToolList tools = frame.tools();
    for (ToolList::const_iterator tl = tools.begin(); tl != tools.end(); ++tl) {
        const Tool tool = *tl;
        std::cout << std::string(2, ' ') <<  "Tool, id: " << tool.id()
        << ", position: " << tool.tipPosition()
        << ", direction: " << tool.direction() << 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;
                std::string clockwiseness;
                
                if (circle.pointable().direction().angleTo(circle.normal()) <= PI/2) {
                    clockwiseness = "clockwise";
                } else {
                    clockwiseness = "counterclockwise";
                }
                
                // Calculate angle swept since last frame
                float sweptAngle = 0;
                if (circle.state() != Gesture::STATE_START) {
                    CircleGesture previousUpdate = CircleGesture(controller.frame(1).gesture(circle.id()));
                    sweptAngle = (circle.progress() - previousUpdate.progress()) * 2 * PI;
                }
                /*
                std::cout << std::string(2, ' ')
                << "Circle id: " << gesture.id()
                << ", state: " << stateNames[gesture.state()]
                << ", progress: " << circle.progress()
                << ", radius: " << circle.radius()
                << ", angle " << sweptAngle * RAD_TO_DEG
                <<  ", " << clockwiseness << std::endl;
                */
                break;
            }
            case Gesture::TYPE_SWIPE:
            {
                SwipeGesture swipe = gesture;
                /*
                std::cout << std::string(2, ' ')
                << "Swipe id: " << gesture.id()
                << ", state: " << stateNames[gesture.state()]
                << ", direction: " << swipe.direction()
                << ", speed: " << swipe.speed() << std::endl;
                */
                break;
            }
            case Gesture::TYPE_KEY_TAP:
            {
                KeyTapGesture tap = gesture;
                push(timestamp);
                /*
                std::cout << std::string(2, ' ')
                << "Key Tap id: " << gesture.id()
                << ", state: " << stateNames[gesture.state()]
                << ", position: " << tap.position()
                << ", direction: " << tap.direction()<< std::endl;
                */
                break;
            }
            case Gesture::TYPE_SCREEN_TAP:
            {
                ScreenTapGesture screentap = gesture;
                /*
                std::cout << std::string(2, ' ')
                << "Screen Tap id: " << gesture.id()
                << ", state: " << stateNames[gesture.state()]
                << ", position: " << screentap.position()
                << ", direction: " << screentap.direction()<< std::endl;
                */
                break;
            }
            default:
                /*
                std::cout << std::string(2, ' ')  << "Unknown gesture type." << std::endl;
                */
                break;
        }
    }

    
    /*
    if (!frame.hands().isEmpty() || !gestures.isEmpty()) {
        std::cout << std::endl;
    }
    */
    
}
Beispiel #5
0
void LeapBrowser::drawHands()
{
	Frame frame = leap_controller.frame();

	math::vector trans = leap_transform.translation;
	math::quater rot_quat = leap_transform.rotation;
	math::vector rot_vect = math::ln( rot_quat );
	
	double angle = rot_vect.length() * 2.0;
	math::vector axis = ( angle != 0 ? rot_vect / angle : rot_vect );

	glMatrixMode( GL_MODELVIEW );
	glPushMatrix();
	glTranslatef( trans.x(), trans.y(), trans.z() );
	if( angle != 0 )
	{
		glRotatef( angle * 180.0 / M_PI, axis.x(), axis.y(), axis.z() );
	}

	HandList hands = frame.hands();
	for( HandList::const_iterator hl = hands.begin(); hl != hands.end(); ++hl )
	{
		const Hand hand = *hl;

		float joint_radius = 5.0f;
		float bone_radius = 4.5f;

		Vector prev_palm_start(0,0,0);
		Vector prev_palm_end(0,0,0);

		int f = 0;
		const FingerList fingers = hand.fingers();
		for( FingerList::const_iterator fl = fingers.begin(); fl != fingers.end(); ++fl, ++f )
		{
			const Finger finger = *fl;

			Vector curr_palm_start(0,0,0);
			Vector curr_palm_end(0,0,0);

			for( int b=0; b < 4; b++ )
			{
				Bone::Type bone_type = static_cast<Bone::Type>( b );
				Bone bone = finger.bone( bone_type );

				Vector start = bone.prevJoint();
				Vector end = bone.nextJoint();

				math::position p0( start.x, start.y, start.z );
				math::position p1( end.x, end.y, end.z );

				if( is_tracking_pose == true && finger.type() == Finger::Type::TYPE_INDEX && b==3 )
				{
					drawing_tool.setColor( 1, 0, 0, 1 );
				}
				else
				{
					drawing_tool.setColor( 0.5, 0.7, 0.5, 1 );
				}
				drawing_tool.drawSphere( p1, joint_radius );
				
				drawing_tool.setColor( 0.5, 0.7, 0.5, 1 );
				drawing_tool.drawSphere( p0, joint_radius );
				drawing_tool.drawCylinder( p0, p1, bone_radius );

				//
				if( b == 0 && fl != fingers.begin() || b == 1 && fl == fingers.begin() )
				{
					curr_palm_start = start;
					curr_palm_end = end;
				}
			}

			if( f > 1 )	//fl != fingers.begin() )
			{
				drawing_tool.setColor( 0.5, 0.7, 0.5, 1 );
				drawing_tool.applyColor();

				glBegin( GL_QUADS );
				glVertex3f( prev_palm_start.x, prev_palm_start.y, prev_palm_start.z );
				glVertex3f( prev_palm_end.x, prev_palm_end.y, prev_palm_end.z );
				glVertex3f( curr_palm_end.x, curr_palm_end.y, curr_palm_end.z );
				glVertex3f( curr_palm_start.x, curr_palm_start.y, curr_palm_start.z );
				glEnd();
			}

			prev_palm_start = curr_palm_start;
			prev_palm_end = curr_palm_end;
		}
	}

	glPopMatrix();
}
Beispiel #6
0
void QTVS_Leap::FingerLogic(handIndex hIndex)
{

  if (ui.checkBox_gesturesParangus->isChecked())
    ParangusGestureLogic();

  if (ui.checkBox_gesturesLeap->isChecked())
    LeapGestureLogic();

//DEBUG: Atm we cancel
  // return;

  // 0 -> 4 = left hand
  // 5 -> 9 = right hand
  // So lets use a shift and no redundant code
  int iHandToFingerShift = 0;
  if (hIndex == handRight)
    iHandToFingerShift += 5;

  for (FingerList::const_iterator fl = fingers.begin(); fl != fingers.end(); ++fl) {
    const Finger finger = *fl;
    // std::cout << std::string(4, ' ') <<  fingerNames[finger.type()]
    //           << " finger, id: " << finger.id()
    //           << ", length: " << finger.length()
    //           << "mm, width: " << finger.width() << std::endl;
    Leap::Vector position = finger.stabilizedTipPosition();

    // Convert Leap::Vector position to Screen Coords
    QPoint screenPosition = FingerCursorPosition(position);


    // Lerp coords for smoothness if required
    int xCoord = lerp(fingerTraces.at(finger.type() + iHandToFingerShift)->geometry().left(), screenPosition.x(), dMouseLerpValue);
    int yCoord = lerp(fingerTraces.at(finger.type() + iHandToFingerShift)->geometry().top(), screenPosition.y(), dMouseLerpValue);

    // Qt Doesn't allow different threads to overwride gui locations.
    // Therefore, the usage of events are well, used.
    // This updates visual finger representation to correct location
    QMoveEvent *tEvent = new QMoveEvent(QPoint(xCoord, yCoord), fingerTraces.at(finger.type() + iHandToFingerShift)->pos());
    QCoreApplication::postEvent(fingerTraces.at(finger.type() + iHandToFingerShift), tEvent);

    // Z axis does NOT use stabilized position.
    // Stabilized position is generally used for X/Y movement (2D display)
    // Therefore Z is updated quite poorely. So we used unStabalized position instead
    // Setup position limits (manual testing for values
    position = finger.tipPosition();
    float zFinger = position.z < 0 ? 0 : position.z > 200 ? 200 : position.z;

    // Convert to percentages
    // trim lowerend of percentages
    zFinger /= 200;
    if (zFinger < 0.1) zFinger = 0.1;

    if (hIndex == handLeft)
    {
        if(ui.checkBox_FingerDragsWindows->isChecked())
        {
      // We're on index finger and its close to screen / center of Z-plane on leap motion
      if (finger.type() == leapIndex && zFinger < 0.12)
      {
        POINT pt;
        pt.x = screenPosition.x();
        pt.y = screenPosition.y();


        // if our rect was reset
        // Therefore, we just started to drag
        if (debugWindowDrag_Left.left == -1)
        {
          // Find window under point
          // Find window's dimmensions
          // Find difference between player's finger coords and window's coords
          debugWindowHWND_Left = GetRealParent(WindowFromPoint(pt));

          // Set it as active window if need be.
          if(debugWindowHWND_Left != GetForegroundWindow() && ui.checkBox_DragSetsActive->isChecked())
          {
            SetForegroundWindow(debugWindowHWND_Left);
            // Backup incase SetForegroundWindow fails
            // TODO: Perhaps replace in future? since foreground window DOES faile occasionally.
            // SetWindowPos(debugWindowHWND_Left,HWND_TOPMOST,0,0,0,0, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE);
          }

          // Restore window if maximized Section
          WINDOWPLACEMENT wndpl = { sizeof(WINDOWPLACEMENT) };
          GetWindowPlacement(debugWindowHWND_Left, &wndpl);

          // Determine if window is maximized to begin with
          if (wndpl.showCmd == SW_MAXIMIZE)
          {
            // Setup the restore command and send it
            wndpl.showCmd = SW_RESTORE;
            SetWindowPlacement(debugWindowHWND_Left, &wndpl);

            // Center restored window around player's fingers
            int iTempWindowWidth = wndpl.rcNormalPosition.right - wndpl.rcNormalPosition.left;
            int iTempWindowHeight = wndpl.rcNormalPosition.bottom - wndpl.rcNormalPosition.top;
            MoveWindow(debugWindowHWND_Left, pt.x - iTempWindowWidth / 2, pt.y - iTempWindowHeight / 2,
                       iTempWindowWidth, iTempWindowHeight, true);
          }

          GetWindowRect(debugWindowHWND_Left, &debugWindowDrag_Left);
          debugWindowDrag_Left.left = pt.x - debugWindowDrag_Left.left;
          debugWindowDrag_Left.top = pt.y -  debugWindowDrag_Left.top;
          debugWindowDrag_Left.bottom = pt.y;//debugWindowDrag_Left.bottom - pt.y;
          debugWindowDrag_Left.right = pt.x;//debugWindowDrag_Left.bottom - pt.y;
        }

        // Setup temp rect for window's width/height
        RECT tRect;
        GetWindowRect(debugWindowHWND_Left, &tRect);

        debugDisplayString = QString::number(pt.y);

        // If left hand and right hand have different HWNDs on drag
        // Proceed with normal drag
        if (debugWindowHWND_Left != debugWindowHWND_Right)
        {
          WINDOWPLACEMENT wndpl = { sizeof(WINDOWPLACEMENT) };
          GetWindowPlacement(debugWindowHWND_Left, &wndpl);

          // AeroSnap time

          // Maximize window on top
          if (pt.y <= 30 && wndpl.showCmd != SW_MAXIMIZE)
          {
            iDebugWindowRestore_LeftHand = 1;
            iDebug_PreviousWindowWidth_LeftHand = tRect.right - tRect.left;
            iDebug_PreviousWindowHeight_LeftHand = tRect.bottom - tRect.top;
            wndpl.showCmd = SW_SHOWMAXIMIZED;
            SetWindowPlacement(debugWindowHWND_Left, &wndpl);
          }

          // AeroSnap Left
          else if (pt.x <= 30)
          {
            if (iDebugWindowRestore_LeftHand == 0)
            {
              iDebugWindowRestore_LeftHand = 1;

              iDebug_PreviousWindowWidth_LeftHand = tRect.right - tRect.left;
              iDebug_PreviousWindowHeight_LeftHand = tRect.bottom - tRect.top;

              int wndwidth = (rcMonitor.right - rcMonitor.left) / 2;
              int wndheight = rcMonitor.bottom - rcMonitor.top;//max(min((rcMonitor.bottom-rcMonitor.top), state.mmi.ptMaxTrackSize.y), state.mmi.ptMinTrackSize.y);
              int posx = rcMonitor.left;
              int posy = rcMonitor.top + (rcMonitor.bottom - rcMonitor.top) / 2 - wndheight / 2; // Center vertically (if window has a max height)
              MoveWindow(debugWindowHWND_Left, posx, posy,
                         wndwidth, wndheight, true);
            }
          }

          // AeroSnap Left
          else if (pt.x >= (rcMonitor.right - rcMonitor.left) - 30)
          {
            if (iDebugWindowRestore_LeftHand == 0)
            {
              iDebugWindowRestore_LeftHand = 1;

              iDebug_PreviousWindowWidth_LeftHand = tRect.right - tRect.left;
              iDebug_PreviousWindowHeight_LeftHand = tRect.bottom - tRect.top;

              int wndwidth = (rcMonitor.right - rcMonitor.left) / 2;
              int wndheight = rcMonitor.bottom - rcMonitor.top;//max(min((rcMonitor.bottom-rcMonitor.top), state.mmi.ptMaxTrackSize.y), state.mmi.ptMinTrackSize.y);
              int posx = rcMonitor.right - wndwidth;
              int posy = rcMonitor.top + (rcMonitor.bottom - rcMonitor.top) / 2 - wndheight / 2; // Center vertically (if window has a max height)
              MoveWindow(debugWindowHWND_Left, posx, posy,
                         wndwidth, wndheight, true);
            }
          }


          // Normal Drag
          else if ( pt.y > 30 && pt.x > 30  && (pt.x < (rcMonitor.right - rcMonitor.left) - 30 ))
          {

            // some other custom snap is in effect
            if (iDebugWindowRestore_LeftHand == 1)
            {
              iDebugWindowRestore_LeftHand = 0;

              if (wndpl.showCmd == SW_MAXIMIZE)
              {
                // Setup the restore command and send it
                wndpl.showCmd = SW_RESTORE;
                SetWindowPlacement(debugWindowHWND_Left, &wndpl);
              }
              MoveWindow(debugWindowHWND_Left, pt.x - iDebug_PreviousWindowWidth_LeftHand / 2, pt.y - iDebug_PreviousWindowHeight_LeftHand / 2,
                         iDebug_PreviousWindowWidth_LeftHand, iDebug_PreviousWindowHeight_LeftHand, true);
            }

            // Nope, just normal drag
            else if (wndpl.showCmd != SW_MAXIMIZE)
              MoveWindow(debugWindowHWND_Left, pt.x - debugWindowDrag_Left.left, pt.y - debugWindowDrag_Left.top, tRect.right - tRect.left, tRect.bottom - tRect.top, true);
          }

        }

        // Left and Right hand have same HWNDs
        // Resize mode engadged
        else if(ui.checkBox_ResizeWindows->isChecked()) {
          // Left hand's overview:
          // Horizontal movement = simultaneious change in window's coords + change in width.
          // Vertical movement = change in window's height only.

          // Left reference points:
          // X = hwnd.left
          // Y = hwnd.bottom

          RECT resizeRect;

          resizeRect.top = tRect.top;
          resizeRect.left = tRect.left + (pt.x - debugWindowDrag_Left.right);// / 5;
          resizeRect.right = (tRect.right - resizeRect.left);// - (pt.x - debugWindowDrag_Left.left) ;/// 5;
          resizeRect.bottom = (tRect.bottom - tRect.top) + (pt.y - debugWindowDrag_Left.bottom);// / 5;

          debugWindowDrag_Left.right = pt.x;
          debugWindowDrag_Left.bottom = pt.y;

          MoveWindow(debugWindowHWND_Left, resizeRect.left, resizeRect.top, resizeRect.right , resizeRect.bottom, true);

        }

      }
      else if (finger.type() == leapIndex && zFinger > 0.15)
      {
        debugWindowDrag_Left.left = -1;
        debugWindowHWND_Left = NULL;
      }
    }
    }

    if (hIndex == handRight)
    {

        if(ui.checkBox_FingerDragsWindows->isChecked())
        {
      if (finger.type() == leapIndex && zFinger < 0.12)
      {

        // debugDisplayString = "TRYIGN";
        POINT pt;
        pt.x = screenPosition.x() - 15;
        pt.y = screenPosition.y() - 15;


        if (debugWindowDrag_Right.left == -1)
        {
          debugWindowHWND_Right = GetRealParent(WindowFromPoint(pt));
        
          if(debugWindowHWND_Right != GetForegroundWindow() && ui.checkBox_DragSetsActive->isChecked())
          {
            SetForegroundWindow(debugWindowHWND_Right);
            // Backup incase SetForegroundWindow fails
            // TODO: Perhaps replace in future? since foreground window DOES faile occasionally.
            // SetWindowPos(debugWindowHWND_Left,HWND_TOPMOST,0,0,0,0, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE);
          }

          GetWindowRect(debugWindowHWND_Right, &debugWindowDrag_Right);
          debugWindowDrag_Right.left = pt.x - debugWindowDrag_Right.left;
          debugWindowDrag_Right.top = pt.y -  debugWindowDrag_Right.top;
          debugWindowDrag_Right.bottom = pt.y;//debugWindowDrag_Left.bottom - pt.y;
          debugWindowDrag_Right.right = pt.x;//debugWindowDrag_Left.bottom - pt.y;
        }

        RECT tRect;
        GetWindowRect(debugWindowHWND_Right, &tRect);

        if (debugWindowHWND_Left != debugWindowHWND_Right)
        {
          WINDOWPLACEMENT wndpl = { sizeof(WINDOWPLACEMENT) };
          GetWindowPlacement(debugWindowHWND_Right, &wndpl);

          // AeroSnap time

          // Maximize window on top
          if (pt.y <= 30 && wndpl.showCmd != SW_MAXIMIZE)
          {
            iDebugWindowRestore_RightHand = 1;
            iDebug_PreviousWindowWidth_RightHand = tRect.right - tRect.left;
            iDebug_PreviousWindowHeight_RightHand = tRect.bottom - tRect.top;
            wndpl.showCmd = SW_SHOWMAXIMIZED;
            SetWindowPlacement(debugWindowHWND_Right, &wndpl);
          }

          // AeroSnap Left
          else if (pt.x <= 30)
          {
            if (iDebugWindowRestore_RightHand == 0)
            {
              iDebugWindowRestore_RightHand = 1;

              iDebug_PreviousWindowWidth_RightHand = tRect.right - tRect.left;
              iDebug_PreviousWindowHeight_RightHand = tRect.bottom - tRect.top;

              int wndwidth = (rcMonitor.right - rcMonitor.left) / 2;
              int wndheight = rcMonitor.bottom - rcMonitor.top;//max(min((rcMonitor.bottom-rcMonitor.top), state.mmi.ptMaxTrackSize.y), state.mmi.ptMinTrackSize.y);
              int posx = rcMonitor.left;
              int posy = rcMonitor.top + (rcMonitor.bottom - rcMonitor.top) / 2 - wndheight / 2; // Center vertically (if window has a max height)
              MoveWindow(debugWindowHWND_Right, posx, posy,
                         wndwidth, wndheight, true);
            }
          }

          // AeroSnap Left
          else if (pt.x >= (rcMonitor.right - rcMonitor.left) - 30)
          {
            if (iDebugWindowRestore_RightHand == 0)
            {
              iDebugWindowRestore_RightHand = 1;

              iDebug_PreviousWindowWidth_RightHand = tRect.right - tRect.left;
              iDebug_PreviousWindowHeight_RightHand = tRect.bottom - tRect.top;

              int wndwidth = (rcMonitor.right - rcMonitor.left) / 2;
              int wndheight = rcMonitor.bottom - rcMonitor.top;//max(min((rcMonitor.bottom-rcMonitor.top), state.mmi.ptMaxTrackSize.y), state.mmi.ptMinTrackSize.y);
              int posx = rcMonitor.right - wndwidth;
              int posy = rcMonitor.top + (rcMonitor.bottom - rcMonitor.top) / 2 - wndheight / 2; // Center vertically (if window has a max height)
              MoveWindow(debugWindowHWND_Right, posx, posy,
                         wndwidth, wndheight, true);
            }
          }


          // Normal Drag
          else if ( pt.y > 30 && pt.x > 30  && (pt.x < (rcMonitor.right - rcMonitor.left) - 30 ))
          {

            // some other custom snap is in effect
            if (iDebugWindowRestore_RightHand == 1)
            {
              iDebugWindowRestore_RightHand = 0;

              if (wndpl.showCmd == SW_MAXIMIZE)
              {
                // Setup the restore command and send it
                wndpl.showCmd = SW_RESTORE;
                SetWindowPlacement(debugWindowHWND_Right, &wndpl);
              }
              MoveWindow(debugWindowHWND_Right, pt.x - iDebug_PreviousWindowWidth_RightHand / 2, pt.y - iDebug_PreviousWindowHeight_RightHand / 2,
                         iDebug_PreviousWindowWidth_RightHand, iDebug_PreviousWindowHeight_RightHand, true);
            }

            // Nope, just normal drag
            else if (wndpl.showCmd != SW_MAXIMIZE)
              MoveWindow(debugWindowHWND_Right, pt.x - debugWindowDrag_Right.left, pt.y - debugWindowDrag_Right.top, tRect.right - tRect.left, tRect.bottom - tRect.top, true);
          }
        }
        else if (ui.checkBox_ResizeWindows->isChecked()){
          // Right hand's overview:
          // Horizontal movement = change in width.
          // Vertical movement = simultaneious change in window's coords + change in window's height only.

          // debug_iReferencePointX_Right =  tRect.left + debugWindowDrag_Right.left;// + debug_iReferencePointY_Left;
          // debug_iReferencePointY_Right =  tRect.top + debugWindowDrag_Right.top;// + debug_iReferencePointY_Left;



          // RECT resizeRect;

          // resizeRect.top = tRect.top;
          // resizeRect.left = tRect.left + (pt.x - debugWindowDrag_Left.right);// / 5;
          // resizeRect.right = (tRect.right - resizeRect.left);// - (pt.x - debugWindowDrag_Left.left) ;/// 5;
          // resizeRect.bottom = (tRect.bottom - tRect.top) + (pt.y - debugWindowDrag_Left.bottom);// / 5;

          // debugWindowDrag_Left.right = pt.x;
          // debugWindowDrag_Left.bottom = pt.y;



          RECT resizeRect;

          resizeRect.top = tRect.top + (pt.y - debugWindowDrag_Right.bottom);
          resizeRect.left = tRect.left;
          resizeRect.right = (tRect.right - tRect.left) + (pt.x - debugWindowDrag_Right.right);
          resizeRect.bottom = (tRect.bottom - resizeRect.top);// - (pt.y - debug_iReferencePointY_Right) / 5;


          // debugWindowDrag_Right.left += (pt.x - debug_iReferencePointX_Right) / 5;
          // debugWindowDrag_Right.top += (pt.y - debug_iReferencePointY_Right) / 5;
          debugWindowDrag_Right.right = pt.x;
          debugWindowDrag_Right.bottom = pt.y;


          MoveWindow(debugWindowHWND_Right, resizeRect.left, resizeRect.top, resizeRect.right , resizeRect.bottom, false);

        }
        // debugDisplayString = QString::number(finger.tipPosition().z);
      }
      else if (finger.type() == leapIndex && zFinger > 0.15)
      {
        debugWindowDrag_Right.left = -1;
        debugWindowHWND_Right = NULL;
      }
        }
    }
    zFinger *= 75.0;

    QResizeEvent *rEvent = new QResizeEvent(QSize(zFinger, zFinger), fingerTraces.at(finger.type() + iHandToFingerShift)->size());
    QCoreApplication::postEvent(fingerTraces.at(finger.type() + iHandToFingerShift), rEvent);


    // switch (finger.type())
    // {
    // case leapThumb:
    // {
    //   // if(finger.isExtended())
    //   // {
    //     // QShowEvent * sEvent = new QShowEvent();
    //     // QCoreApplication::postEvent(thumbTrace, sEvent);
    //   int xCoord = lerp(thumbTrace->geometry().left(), screenPosition.x(), dMouseLerpValue);
    //   int yCoord = lerp(thumbTrace->geometry().top(), screenPosition.y(), dMouseLerpValue);

    //   QMoveEvent *tEvent = new QMoveEvent(QPoint(xCoord, yCoord), thumbTrace->pos());
    //   QCoreApplication::postEvent(thumbTrace, tEvent);
    //   // }
    //   // else
    //   // {
    //     // QHideEvent * hEvent = new QHideEvent();
    //     // QCoreApplication::postEvent(thumbTrace, hEvent);
    //   // }
    //   // thumbTrace->move(xCoord, yCoord);
    // }
    //   // indexTrace;
    //   // middleTrace;
    //   // ringTrace;
    //   // pinkieTrace;

    // break;

    // case leapIndex:

    // {
    //   int xCoord = lerp(indexTrace->geometry().left(), screenPosition.x(), dMouseLerpValue);
    //   int yCoord = lerp(indexTrace->geometry().top(), screenPosition.y(), dMouseLerpValue);

    //   QMoveEvent *tEvent = new QMoveEvent(QPoint(xCoord, yCoord), indexTrace->pos());
    //   QCoreApplication::postEvent(indexTrace, tEvent);
    //   // thumbTrace->move(xCoord, yCoord);
    // }
    // break;

    // case leapMiddle:

    // {
    //   int xCoord = lerp(middleTrace->geometry().left(), screenPosition.x(), dMouseLerpValue);
    //   int yCoord = lerp(middleTrace->geometry().top(), screenPosition.y(), dMouseLerpValue);

    //   QMoveEvent *tEvent = new QMoveEvent(QPoint(xCoord, yCoord), middleTrace->pos());
    //   QCoreApplication::postEvent(middleTrace, tEvent);
    //   // thumbTrace->move(xCoord, yCoord);
    // }
    // break;

    // case leapRing:

    // {
    //   int xCoord = lerp(ringTrace->geometry().left(), screenPosition.x(), dMouseLerpValue);
    //   int yCoord = lerp(ringTrace->geometry().top(), screenPosition.y(), dMouseLerpValue);

    //   QMoveEvent *tEvent = new QMoveEvent(QPoint(xCoord, yCoord), ringTrace->pos());
    //   QCoreApplication::postEvent(ringTrace, tEvent);
    //   // thumbTrace->move(xCoord, yCoord);
    // }
    // break;

    // case leapPinkie:

    // {
    //   int xCoord = lerp(pinkieTrace->geometry().left(), screenPosition.x(), dMouseLerpValue);
    //   int yCoord = lerp(pinkieTrace->geometry().top(), screenPosition.y(), dMouseLerpValue);

    //   QMoveEvent *tEvent = new QMoveEvent(QPoint(xCoord, yCoord), pinkieTrace->pos());
    //   QCoreApplication::postEvent(pinkieTrace, tEvent);
    //   // thumbTrace->move(xCoord, yCoord);
    // }
    // break;
    // }
  }
}
Beispiel #7
0
void QTVS_Leap::ParangusGestureLogic()
{
  if (ui.checkBox_PalmForSwipes->isChecked())
  {

    Leap::Vector palmPosition = controller.frame(3).hands()[0].palmPosition();

    float previousPalmYPosAndDifference = palmPosition.y;
    float previousPalmXPosAndDifference = palmPosition.x;

    previousPalmYPosAndDifference = hand.palmPosition().y - previousPalmYPosAndDifference;
    previousPalmXPosAndDifference = hand.palmPosition().x - previousPalmXPosAndDifference;

    // std::cout << previousFingerXPosAndDifference << "\n";

    if (previousPalmYPosAndDifference < -2.5 && handCache.bGestureToggle)
    {
      std::cout << "down \n";
      handCache.bGestureToggle = false;
      QtConcurrent::run(this, &QTVS_Leap::ParangusGesture, NULL, swipe_Down);
    }

    if (previousPalmYPosAndDifference > 2.5 && handCache.bGestureToggle)
    {
      std::cout << "swipe_Up \n";
      handCache.bGestureToggle = false;
      QtConcurrent::run(this, &QTVS_Leap::ParangusGesture, NULL, swipe_Up);
    }

    if (previousPalmXPosAndDifference < -2.5 && handCache.bGestureToggle)
    {
      std::cout << "swipe_Left \n";
      handCache.bGestureToggle = false;
      QtConcurrent::run(this, &QTVS_Leap::ParangusGesture, NULL, swipe_Left);
    }

    if (previousPalmXPosAndDifference > 2.5 && handCache.bGestureToggle)
    {
      std::cout << "swipe_Right \n";
      handCache.bGestureToggle = false;
      QtConcurrent::run(this, &QTVS_Leap::ParangusGesture, NULL, swipe_Right);
    }


    if (!handCache.bGestureToggle &&
        abs(previousPalmYPosAndDifference) <= 0.5 &&
        abs(previousPalmXPosAndDifference) <= 0.5 )
      handCache.bGestureToggle = true;
    return;
  }

  // if palm for swipes isn't checked, we go for fingers instead:
  for (FingerList::const_iterator fl = fingers.begin(); fl != fingers.end(); ++fl)
  {
    const Finger finger = *fl;

//To simplify things down the road
    int leapFingerIndex = finger.type();

    if (finger.isExtended())
    {
      Leap::Vector fingerPosition = controller.frame(3).fingers()[leapFingerIndex].stabilizedTipPosition();

      float previousFingerYPosAndDifference = fingerPosition.y;
      float previousFingerXPosAndDifference = fingerPosition.x;

      previousFingerYPosAndDifference = finger.stabilizedTipPosition().y - previousFingerYPosAndDifference;
      previousFingerXPosAndDifference = finger.stabilizedTipPosition().x - previousFingerXPosAndDifference;

      // std::cout << previousFingerXPosAndDifference << "\n";

      if (previousFingerYPosAndDifference < -10 && handCache.fingers_p[leapFingerIndex].bGestureToggle)
      {
        handCache.fingers_p[leapFingerIndex].bGestureToggle = false;
        QtConcurrent::run(this, &QTVS_Leap::ParangusGesture, leapFingerIndex, swipe_Down);
      }

      if (previousFingerYPosAndDifference > 10 && handCache.fingers_p[leapFingerIndex].bGestureToggle)
      {
        handCache.fingers_p[leapFingerIndex].bGestureToggle = false;
        QtConcurrent::run(this, &QTVS_Leap::ParangusGesture, leapFingerIndex, swipe_Up);
      }

      if (previousFingerXPosAndDifference < -10 && handCache.fingers_p[leapFingerIndex].bGestureToggle)
      {
        handCache.fingers_p[leapFingerIndex].bGestureToggle = false;
        QtConcurrent::run(this, &QTVS_Leap::ParangusGesture, leapFingerIndex, swipe_Left);
      }

      if (previousFingerXPosAndDifference > 10 && handCache.fingers_p[leapFingerIndex].bGestureToggle)
      {
        handCache.fingers_p[leapFingerIndex].bGestureToggle = false;
        QtConcurrent::run(this, &QTVS_Leap::ParangusGesture, leapFingerIndex, swipe_Right);
      }


      if (!handCache.fingers_p[leapFingerIndex].bGestureToggle &&
          abs(previousFingerYPosAndDifference) <= 0.1 &&
          abs(previousFingerXPosAndDifference) <= 0.1 )
        handCache.fingers_p[leapFingerIndex].bGestureToggle = true;
    }
  }
}