added declaration for LeapMotionTS

This commit is contained in:
Victor Norgren
2013-05-22 23:59:36 +08:00
parent 672810dc83
commit b14478ab1a
2 changed files with 368 additions and 0 deletions
+89
View File
@@ -0,0 +1,89 @@
import Leap = module('../LeapMotionTS');
var controller:Leap.Controller = new Leap.Controller();
controller.addEventListener( Leap.LeapEvent.LEAPMOTION_FRAME, ( event:Leap.LeapEvent ) => {
var frame:Leap.Frame = event.frame;
console.log( "Frame id: " + frame.id + ", timestamp: " + frame.timestamp + ", hands: " + frame.hands.length + ", fingers: " + frame.fingers.length + ", tools: " + frame.tools.length + ", gestures: " + frame.gestures().length );
if ( frame.hands.length > 0 )
{
// Get the first hand
var hand:Leap.Hand = frame.hands[ 0 ];
// Check if the hand has any fingers
var fingers:Leap.Finger[] = hand.fingers;
if ( fingers.length > 0 )
{
// Calculate the hand's average finger tip position
var avgPos:Leap.Vector3 = Leap.Vector3.zero();
for(var i:number = 0; i < fingers.length; i++ )
avgPos = avgPos.plus( (<Leap.Finger>fingers[ i ]).tipPosition );
avgPos = avgPos.divide( fingers.length );
console.log( "Hand has " + fingers.length + " fingers, average finger tip position: " + avgPos );
}
// Get the hand's sphere radius and palm position
console.log( "Hand sphere radius: " + hand.sphereRadius + " mm, palm position: " + hand.palmPosition );
// Get the hand's normal vector and direction
var normal:Leap.Vector3 = hand.palmNormal;
var direction:Leap.Vector3 = hand.direction;
// Calculate the hand's pitch, roll, and yaw angles
console.log( "Hand pitch: " + Leap.LeapUtil.toDegrees( direction.pitch ) + " degrees, " + "roll: " + Leap.LeapUtil.toDegrees( normal.roll ) + " degrees, " + "yaw: " + Leap.LeapUtil.toDegrees( direction.yaw ) + " degrees\n" );
}
var gestures:Leap.Gesture[] = frame.gestures();
for ( var i:number = 0; i < gestures.length; i++ )
{
var gesture:Leap.Gesture = gestures[ i ];
switch ( gesture.type )
{
case Leap.Gesture.TYPE_CIRCLE:
var circle:Leap.CircleGesture = <Leap.CircleGesture>gesture;
// Calculate clock direction using the angle between circle normal and pointable
var clockwiseness:String;
if ( circle.pointable.direction.angleTo( circle.normal ) <= Math.PI / 4 )
{
// Clockwise if angle is less than 90 degrees
clockwiseness = "clockwise";
}
else
{
clockwiseness = "counterclockwise";
}
// Calculate angle swept since last frame
var sweptAngle:number = 0;
if ( circle.state != Leap.Gesture.STATE_START )
{
var previousGesture:Leap.Gesture = controller.frame( 1 ).gesture( circle.id );
if( previousGesture.isValid() )
{
var previousUpdate:Leap.CircleGesture = (<Leap.CircleGesture>controller.frame( 1 ).gesture( circle.id ) );
sweptAngle = ( circle.progress - previousUpdate.progress ) * 2 * Math.PI;
}
}
console.log( "Circle id: " + circle.id + ", " + circle.state + ", progress: " + circle.progress + ", radius: " + circle.radius + ", angle: " + Leap.LeapUtil.toDegrees( sweptAngle ) + ", " + clockwiseness );
break;
case Leap.Gesture.TYPE_SWIPE:
var swipe:Leap.SwipeGesture = <Leap.SwipeGesture>gesture;
console.log( "Swipe id: " + swipe.id + ", " + swipe.state + ", position: " + swipe.position + ", direction: " + swipe.direction + ", speed: " + swipe.speed );
break;
case Leap.Gesture.TYPE_SCREEN_TAP:
var screenTap:Leap.ScreenTapGesture = <Leap.ScreenTapGesture>gesture;
console.log( "Screen Tap id: " + screenTap.id + ", " + screenTap.state + ", position: " + screenTap.position + ", direction: " + screenTap.direction );
break;
case Leap.Gesture.TYPE_KEY_TAP:
var keyTap:Leap.KeyTapGesture = <Leap.KeyTapGesture>gesture;
console.log( "Key Tap id: " + keyTap.id + ", " + keyTap.state + ", position: " + keyTap.position + ", direction: " + keyTap.direction );
break;
default:
console.log( "Unknown gesture type." );
break;
}
}
} );
+279
View File
@@ -0,0 +1,279 @@
// Type definitions for Leap Motion TS (SDK version 0.7.9)
// Project: https://github.com/logotype/LeapMotionTS and http://www.leapmotion.com
// Definitions by: Victor Norgren <https://github.com/logotype/>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
export declare class EventDispatcher {
private _listeners;
constructor();
public hasEventListener(type: string, listener: Function): boolean;
public addEventListener(typeStr: string, listenerFunc: Function): void;
public removeEventListener(typeStr: string, listenerFunc: Function): void;
public dispatchEvent(evt: LeapEvent): void;
}
export interface Listener {
onConnect(controller: Controller): void;
onDisconnect(controller: Controller): void;
onExit(controller: Controller): void;
onFrame(controller: Controller, frame: Frame): void;
onInit(controller: Controller): void;
}
export declare class DefaultListener extends EventDispatcher implements Listener {
constructor();
public onConnect(controller: Controller): void;
public onDisconnect(controller: Controller): void;
public onExit(controller: Controller): void;
public onFrame(controller: Controller, frame: Frame): void;
public onInit(controller: Controller): void;
}
export declare class LeapEvent {
static LEAPMOTION_INIT: string;
static LEAPMOTION_CONNECTED: string;
static LEAPMOTION_DISCONNECTED: string;
static LEAPMOTION_EXIT: string;
static LEAPMOTION_FRAME: string;
private _type;
private _target;
public frame: Frame;
constructor(type: string, targetListener: Listener, frame?: Frame);
public getTarget(): any;
public getType(): string;
}
export declare class LeapUtil {
static PI: number;
static DEG_TO_RAD: number;
static RAD_TO_DEG: number;
static TWO_PI: number;
static HALF_PI: number;
static EPSILON: number;
constructor();
static toDegrees(radians: number): number;
static isNearZero(value: number): boolean;
static vectorIsNearZero(inVector: Vector3): boolean;
static extractRotation(mtxTransform: Matrix): Matrix;
static rotationInverse(mtxRot: Matrix): Matrix;
static rigidInverse(mtxTransform: Matrix): Matrix;
static componentWiseMin(vLHS: Vector3, vRHS: Vector3): Vector3;
static componentWiseMax(vLHS: Vector3, vRHS: Vector3): Vector3;
static componentWiseScale(vLHS: Vector3, vRHS: Vector3): Vector3;
static componentWiseReciprocal(inVector: Vector3): Vector3;
static minComponent(inVector: Vector3): number;
static maxComponent(inVector: Vector3): number;
static heading(inVector: Vector3): number;
static elevation(inVector: Vector3): number;
static normalizeSpherical(vSpherical: Vector3): Vector3;
static cartesianToSpherical(vCartesian: Vector3): Vector3;
static sphericalToCartesian(vSpherical: Vector3): Vector3;
static clamp(inVal: number, minVal: number, maxVal: number): number;
static lerp(a: number, b: number, coefficient: number): number;
static lerpVector(vec1: Vector3, vec2: Vector3, coefficient: number): Vector3;
}
export declare class Controller extends EventDispatcher {
private listener;
public frameHistory: Frame[];
private latestFrame;
public connection: WebSocket;
public _isConnected: boolean;
public _isGesturesEnabled: boolean;
constructor(host?: string);
private static getHandByID(frame, id);
private static getPointableByID(frame, id);
public frame(history?: number): Frame;
public setListener(listener: Listener): void;
public enableGesture(type: number, enable?: boolean): void;
public isGestureEnabled(type: number): boolean;
public isConnected(): boolean;
}
export declare class Pointable {
public direction: Vector3;
public frame: Frame;
public hand: Hand;
public id: number;
public length: number;
public width: number;
public tipPosition: Vector3;
public tipVelocity: Vector3;
public isFinger: boolean;
public isTool: boolean;
constructor();
public isValid(): boolean;
public isEqualTo(other: Pointable): boolean;
static invalid(): Pointable;
public toString(): string;
}
export declare class Gesture {
static STATE_INVALID: number;
static STATE_START: number;
static STATE_UPDATE: number;
static STATE_STOP: number;
static TYPE_INVALID: number;
static TYPE_SWIPE: number;
static TYPE_CIRCLE: number;
static TYPE_SCREEN_TAP: number;
static TYPE_KEY_TAP: number;
public duration: number;
public durationSeconds: number;
public frame: Frame;
public hands: Hand[];
public id: number;
public pointables: Pointable[];
public state: number;
public type: number;
constructor();
public isEqualTo(other: Gesture): boolean;
public isValid(): boolean;
static invalid(): Gesture;
public toString(): string;
}
export declare class Finger extends Pointable {
constructor();
static invalid(): Finger;
}
export declare class Tool extends Pointable {
constructor();
static invalid(): Tool;
}
export declare class Hand {
public direction: Vector3;
public fingers: Finger[];
public frame: Frame;
public id: number;
public palmNormal: Vector3;
public palmPosition: Vector3;
public palmVelocity: Vector3;
public pointables: Pointable[];
public sphereCenter: Vector3;
public sphereRadius: number;
public tools: Tool[];
public rotation: Matrix;
public scaleFactorNumber: number;
public translationVector: Vector3;
constructor();
public isValid(): boolean;
public isEqualTo(other: Hand): boolean;
public finger(id: number): Finger;
public tool(id: number): Tool;
public pointable(id: number): Pointable;
public rotationAxis(sinceFrame: Frame): Vector3;
public rotationAngle(sinceFrame: Frame, axis?: Vector3): number;
public rotationMatrix(sinceFrame: Frame): Matrix;
public scaleFactor(sinceFrame: Frame): number;
public translation(sinceFrame: Frame): Vector3;
static invalid(): Hand;
}
export declare class Frame {
public fingers: Finger[];
public hands: Hand[];
public pointables: Pointable[];
public _gestures: Gesture[];
public id: number;
public timestamp: number;
public tools: Tool[];
public rotation: Matrix;
public scaleFactorNumber: number;
public translationVector: Vector3;
public controller: Controller;
constructor();
public hand(id: number): Hand;
public finger(id: number): Finger;
public tool(id: number): Tool;
public pointable(id: number): Pointable;
public gesture(id: number): Gesture;
public gestures(sinceFrame?: Frame): Gesture[];
public rotationAxis(sinceFrame: Frame): Vector3;
public rotationAngle(sinceFrame: Frame, axis?: Vector3): number;
public rotationMatrix(sinceFrame: Frame): Matrix;
public scaleFactor(sinceFrame: Frame): number;
public translation(sinceFrame: Frame): Vector3;
public isEqualTo(other: Frame): boolean;
public isValid(): boolean;
static invalid(): Frame;
}
export declare class Matrix {
public origin: Vector3;
public xBasis: Vector3;
public yBasis: Vector3;
public zBasis: Vector3;
constructor(x: Vector3, y: Vector3, z: Vector3, _origin?: Vector3);
public setRotation(_axis: Vector3, angleRadians: number): void;
public transformPoint(inVector: Vector3): Vector3;
public transformDirection(inVector: Vector3): Vector3;
public rigidInverse(): Matrix;
public multiply(other: Matrix): Matrix;
public multiplyAssign(other: Matrix): Matrix;
public isEqualTo(other: Matrix): boolean;
static identity(): Matrix;
public toString(): string;
}
export declare class CircleGesture extends Gesture {
static classType: number;
public center: Vector3;
public normal: Vector3;
public pointable: Pointable;
public progress: number;
public radius: number;
constructor();
}
export declare class KeyTapGesture extends Gesture {
static classType: number;
public direction: Vector3;
public pointable: Pointable;
public position: Vector3;
public progress: number;
constructor();
}
export declare class ScreenTapGesture extends Gesture {
static classType: number;
public direction: Vector3;
public pointable: Pointable;
public position: Vector3;
public progress: number;
constructor();
}
export declare class SwipeGesture extends Gesture {
static classType: number;
public direction: Vector3;
public pointable: Pointable;
public position: Vector3;
public speed: number;
public startPosition: Vector3;
constructor();
}
export declare class Vector3 {
public x: number;
public y: number;
public z: number;
constructor(x: number, y: number, z: number);
public opposite(): Vector3;
public plus(other: Vector3): Vector3;
public plusAssign(other: Vector3): Vector3;
public minus(other: Vector3): Vector3;
public minusAssign(other: Vector3): Vector3;
public multiply(scalar: number): Vector3;
public multiplyAssign(scalar: number): Vector3;
public divide(scalar: number): Vector3;
public divideAssign(scalar: number): Vector3;
public isEqualTo(other: Vector3): boolean;
public angleTo(other: Vector3): number;
public cross(other: Vector3): Vector3;
public distanceTo(other: Vector3): number;
public dot(other: Vector3): number;
public isValid(): boolean;
static invalid(): Vector3;
public magnitude(): number;
public magnitudeSquared(): number;
public normalized(): Vector3;
public pitch : number;
public yaw : number;
public roll : number;
static zero(): Vector3;
static xAxis(): Vector3;
static yAxis(): Vector3;
static zAxis(): Vector3;
static left(): Vector3;
static right(): Vector3;
static down(): Vector3;
static up(): Vector3;
static forward(): Vector3;
static backward(): Vector3;
public toString(): string;
}