Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leap motion, finally #115

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4,024 changes: 4,024 additions & 0 deletions ShiftIt/LeapSDK/Leap.h

Large diffs are not rendered by default.

982 changes: 982 additions & 0 deletions ShiftIt/LeapSDK/LeapMath.h

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions ShiftIt/LeapSDK/SILeapController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// SILeapController.h
// ShiftIt
//
// Created by myeyesareblind on 8/24/13.
//
//

#import <Foundation/Foundation.h>

typedef void(^SILeapControllerGestureHandleBlock)(NSString* actionIdentifier);

@interface SILeapController : NSObject

-(id) init;

@property (readwrite, copy) SILeapControllerGestureHandleBlock gestureHandleBlock;

@end
174 changes: 174 additions & 0 deletions ShiftIt/LeapSDK/SILeapController.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
//
// SILeapController.m
// ShiftIt
//
// Created by myeyesareblind on 8/24/13.
//
//

#import "SILeapController.h"
#import "Leap.h"
#import "SILeapControllerUtils.h"

class SILeapListener;

using namespace Leap;

@protocol SILeapListenerDelegate <NSObject>
- (void)leapListenerDidFindCircleGesture:(SILeapListener*)leapListener;
- (void)leapListener:(SILeapListener*)leapListener didFindSwipeGestureWithDirection:(SwipeGesture_Direction)swipeDirection;
@end


class SILeapListener : public Leap::Listener {
id<SILeapListenerDelegate> _delegate;
CFTimeInterval _lastGestureTime;
public:
SILeapListener():
_delegate(nil),
_lastGestureTime(CFAbsoluteTimeGetCurrent()){
}

void setDelegate(id<SILeapListenerDelegate> delegate){
_delegate = delegate;
}
id<SILeapListenerDelegate> getDelegate() {
return _delegate;
}

virtual void onInit(const Leap::Controller&);
virtual void onFrame(const Leap::Controller&);
};


void
SILeapListener::onInit(const Leap::Controller& controller) {
}


const float SWIPE_MINIMUM_TRIGGER_DISTANCE = 50.0f;
const float GESTURE_MINIMUM_DELAY_BETWEEN_ACTIONS = 1.0f;

void
SILeapListener::onFrame(const Leap::Controller& controller) {
const Frame frame = controller.frame();
const GestureList gestures = frame.gestures();

CFTimeInterval currentTime = CFAbsoluteTimeGetCurrent();
if (currentTime - _lastGestureTime < GESTURE_MINIMUM_DELAY_BETWEEN_ACTIONS) {
return ;
}
for (GestureList::const_iterator iterator = gestures.begin(); iterator != gestures.end(); ++iterator) {
Gesture singleGesture = *iterator;
if (! singleGesture.isValid()) {
continue ;
}

if (singleGesture.type() == Gesture::TYPE_CIRCLE) {
CircleGesture circleGesture = (CircleGesture) singleGesture;

if (circleGesture.state() == Gesture::STATE_STOP
&& circleGesture.progress() > 1) {
circleGesture.invalid();
[_delegate leapListenerDidFindCircleGesture:this];

_lastGestureTime = currentTime;
}

continue ;
}

if (singleGesture.type() == Gesture::TYPE_SWIPE) {
SwipeGesture swipeGesture = (SwipeGesture) singleGesture;

if (swipeGesture.state() == Gesture::STATE_STOP) {
Vector distanceVector = swipeGesture.startPosition() - swipeGesture.position();
float distance = distanceVector.magnitude();
if (distance > SWIPE_MINIMUM_TRIGGER_DISTANCE) {
SwipeGesture_Direction direction = swipeGesuteDirectionFromVector(distanceVector);
[_delegate leapListener:this didFindSwipeGestureWithDirection:direction];
_lastGestureTime = currentTime;
}
}
}
}
}


@interface SILeapController () <SILeapListenerDelegate> {
SILeapListener* leapListener;
Leap::Controller* leapController;
}
@end


@implementation SILeapController
@synthesize gestureHandleBlock;


-(id) init {
self = [super init];
if (!self)
return nil;
leapController = new Leap::Controller();
leapController->enableGesture(Gesture::TYPE_SWIPE);
leapController->enableGesture(Gesture::TYPE_CIRCLE);
leapController->setPolicyFlags(Controller::POLICY_BACKGROUND_FRAMES);

leapListener = new SILeapListener();
leapListener->setDelegate(self);

leapController->addListener(*leapListener);
return self;
}


- (void)leapListenerDidFindCircleGesture:(SILeapListener*)leapListener {
dispatch_async(dispatch_get_main_queue(), ^{
gestureHandleBlock(@"nextscreen");
});
}


- (void)leapListener:(SILeapListener*)leapListener didFindSwipeGestureWithDirection:(SwipeGesture_Direction)swipeDirection {
NSString* actionIdentifier = nil;
switch (swipeDirection) {
case SwipeGesture_Direction_Bottom:
actionIdentifier = @"bottom";
break;
case SwipeGesture_Direction_BottomLeft:
actionIdentifier = @"bl";
break;
case SwipeGesture_Direction_BottomRight:
actionIdentifier = @"br";
break;
case SwipeGesture_Direction_Left:
actionIdentifier = @"left";
break;
case SwipeGesture_Direction_Right:
actionIdentifier = @"right";
break;
case SwipeGesture_Direction_TopLeft:
actionIdentifier = @"tl";
break;
case SwipeGesture_Direction_Top:
actionIdentifier = @"top";
break;
case SwipeGesture_Direction_TopRight:
actionIdentifier = @"tr";
break;
}
dispatch_async(dispatch_get_main_queue(), ^{
gestureHandleBlock(actionIdentifier);
});
}


-(void)dealloc {
delete leapListener;
delete leapController;
[super dealloc];
}


@end
63 changes: 63 additions & 0 deletions ShiftIt/LeapSDK/SILeapControllerUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// SILeapControllerUtils.h
// ShiftIt
//
// Created by myeyesareblind on 8/24/13.
//
//

#ifndef ShiftIt_SILeapControllerUtils_h
#define ShiftIt_SILeapControllerUtils_h
#import "Leap.h"

typedef enum {
SwipeGesture_Direction_Right,
SwipeGesture_Direction_BottomRight,
SwipeGesture_Direction_Bottom,
SwipeGesture_Direction_BottomLeft,
SwipeGesture_Direction_Left, /// 4
SwipeGesture_Direction_TopLeft,
SwipeGesture_Direction_Top,
SwipeGesture_Direction_TopRight /// 7
} SwipeGesture_Direction;

const float M_PI_8 = (float) M_PI_4 / 2;

__attribute__((const))
static inline
SwipeGesture_Direction
swipeGesuteDirectionFromVector(Leap::Vector inVector) {
float angle = atan2f(inVector.y, inVector.x);

SwipeGesture_Direction returnDirection;
if (angle < -7 * M_PI_8) {
returnDirection = SwipeGesture_Direction_Right;
}
else if (angle < - 5 * M_PI_8) {
returnDirection = SwipeGesture_Direction_TopRight;
}
else if (angle < -3 * M_PI_8) {
returnDirection = SwipeGesture_Direction_Top;
}
else if (angle < -1 * M_PI_8) {
returnDirection = SwipeGesture_Direction_TopLeft;
}
else if (angle < M_PI_8) {
returnDirection = SwipeGesture_Direction_Left;
}
else if (angle < 3 * M_PI_8) {
returnDirection = SwipeGesture_Direction_BottomLeft;
}
else if (angle < 5 * M_PI_8) {
returnDirection = SwipeGesture_Direction_Bottom;
}
else if (angle < 7 * M_PI_8){
returnDirection = SwipeGesture_Direction_BottomRight;
}
else {
returnDirection = SwipeGesture_Direction_Right;
}
return returnDirection;
}

#endif
Binary file added ShiftIt/LeapSDK/libLeap.dylib
Binary file not shown.
Loading