Skip to content

Instantly share code, notes, and snippets.

@sdragou
Created February 17, 2013 18:34
Show Gist options
  • Save sdragou/4972682 to your computer and use it in GitHub Desktop.
Save sdragou/4972682 to your computer and use it in GitHub Desktop.
#include "test2.h"
#pragma once
namespace test_WFA {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Threading;
using namespace Leap;
/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
static SampleListener* listener;
static Controller* controller;
public: System::Windows::Forms::TextBox^ textBox1;
public: System::Windows::Forms::TextBox^ textBox2;
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
listener=new SampleListener();
controller=new Controller(*listener);
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
delete listener;
delete controller;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
private: System::Windows::Forms::Button^ button2;
Thread^ myThread;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(66, 30);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"GO";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(147, 30);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 23);
this->button2->TabIndex = 1;
this->button2->Text = L"STOP";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(66, 60);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(100, 22);
this->textBox1->TabIndex = 2;
//
// textBox2
//
this->textBox2->Location = System::Drawing::Point(66, 89);
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(100, 22);
this->textBox2->TabIndex = 3;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(267, 139);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
//myThread = gcnew Thread(gcnew ThreadStart(this, &test_WFA::Form1::ThreadProc));
//myThread->Start();
controller->addListener(*listener);
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
//myThread->Suspend();
controller->removeListener(*listener);
}
};
}
#include <Windows.h>
#include <Tlhelp32.h>
#include <time.h>
#include "Leap.h"
#include "stdafx.h"
namespace test_WFA {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Threading;
using namespace Leap;
ref class Form1;
class SampleListener;
class SampleListener : public Listener {
public:
virtual void onInit(const Controller&);
virtual void onConnect(const Controller&);
virtual void onDisconnect(const Controller&);
virtual void onExit(const Controller&);
virtual void onFrame(const Controller&);
};
void SampleListener::onInit(const Controller& controller) {
std::cout << "Initialized" << std::endl;
}
void SampleListener::onConnect(const Controller& controller) {
std::cout << "Connected" << std::endl;
}
void SampleListener::onDisconnect(const Controller& controller) {
std::cout << "Disconnected" << std::endl;
}
void SampleListener::onExit(const Controller& controller) {
std::cout << "Exited" << std::endl;
}
void SampleListener::onFrame(const Controller& controller) {
// Get the most recent frame and report some basic information
const Frame frame = controller.frame();
if (!frame.hands().empty()) {
// Get the first hand
const Hand hand = frame.hands()[0];
Vector avgPos;
avgPos.x=0;
avgPos.y=0;
avgPos.z=0;
// Check if the hand has any fingers
const FingerList fingers = hand.fingers();
if (!fingers.empty()) {
// Calculate the hand's average finger tip position
Vector avgPos;
for (int i = 0; i < fingers.count(); ++i) {
avgPos += fingers[i].tipPosition();
}
avgPos /= (float)fingers.count();
}
// Get the hand's sphere radius and palm position
// Get the hand's normal vector and direction
const Vector normal = hand.palmNormal();
const Vector direction = hand.direction();
const double XSCALEFACTOR = 65535 / (GetSystemMetrics(SM_CXSCREEN) - 1);
const double YSCALEFACTOR = 65535 / (GetSystemMetrics(SM_CYSCREEN) - 1);
// calculate target position relative to application
INPUT Input={0};
Input.type = INPUT_MOUSE;
// set move cursor directly and left click
Input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE ;
Input.mi.dx = ((400.0+avgPos.x)/800.0)*65535;
// Here I want to access Form1.textbox1.Text to write mi.dx
Input.mi.dy = ((avgPos.y)/440.0)*65535;
SendInput(1,&Input,sizeof(INPUT));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment