4

So I have a game for Mac OS X built in cocos2D.

I'm using gestures to simulate keyboard commands to control my character and it works really well.

I submitted my game to the AirSpace store and it got rejected on the grounds that the Leap should be used to control my menus as well which is fair enough I guess.

Thing is for the life of me I cannot figure out how this is done. There are no examples out there to show me how to implement it and nothing in the SDK example that makes it clear either.

Does anyone have any examples they'd care to share, I only need it to hijack my cursor and allow a click when held over a button. I really didn't think something so complex would be needed on simply for basic menu navigation.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
PocketTim
  • 87
  • 10

1 Answers1

4

If this is a Mac only game you should have access to the Quartz event api. This is the easiest way to generate mouse events in OS X...

I would recommend simply tracking the palm (hand) position, and moving the cursor based on that.

This is how I do my palm tracking:

float handX = ([[hand palmPosition] x]);
float handY = (-[[hand palmPosition] y] + 150);

The "+ 150" is the number of millimetres above the Leap device, to use as the '0' location. From there you can move the cursor based on the hand offset from 0.

The function I use to move the cursor (using Quartz):

- (void)mouseEventWithType:(CGEventType)type loc:(CGPoint)loc deltaX:(float)dX deltaY:(float)dY
{
  CGEventRef theEvent = CGEventCreateMouseEvent(NULL, type, loc, kCGMouseButtonLeft);
  CGEventSetIntegerValueField(theEvent, kCGMouseEventDeltaX, dX);
  CGEventSetIntegerValueField(theEvent, kCGMouseEventDeltaY, dY);
  CGEventPost(kCGHIDEventTap, theEvent);
  CFRelease(theEvent);
}

and an example function call:

[self mouseEventWithType:kCGEventMouseMoved loc:CGPointMake(newLocX, newLocY) deltaX:dX deltaY:dY];

This call will move the mouse. Basically you just pass the new location of the mouse, and the corresponding deltas, relative to the last cursor position.

I can provide more examples such as examples for getting mouse location, clicking the mouse, or even a full mouse moving program...

EDIT 1:

To handle click and drag with Quartz, you can call the same function as above only pass in kCGEventLeftMouseDown.

The catch is that in order to drag you cannot call the kCGEventMouseMoved you must instead pass kCGEventLeftMouseDragged while the drag is happening.

Once the drag is done you must pass a kCGEventLeftMouseUp.

To do a single click (no drag) you simply call mouse down and then up right after, without any drag...

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
Max
  • 4,529
  • 1
  • 25
  • 29
  • This worked perfectly thanks so much. The only thing left to figure out is how to implement a held click and drag. As I have a page control layer adopted from my iOS version that I'd like to keep in. – PocketTim Aug 06 '13 at 09:01
  • Awesome, thank you. I know this will help a lot of other people too as there are a lot of people searching but not many people answering. – PocketTim Aug 06 '13 at 22:21
  • Your solution helped me to solve my problem as well. Thanks very much. I have a quick question, tough, if you are still around. What exactly are the `dX, dY` for? For my application all I need is to set the `loc` variable. Changing the `dX, dY` has no effect at all. Thanks. – Chrigi Aug 25 '13 at 09:53
  • The 'd' in 'dX' and 'dY' stands for delta. It is required to properly move the mouse. If you remove it, it may still work, however you could experience unexpected results. It is not hard to calculate them based on the previous mouse location. I can add an example if desired. – Max Aug 25 '13 at 16:19
  • I am doing a similar thing to this by moving mouse cursor with iPhone touchpad, the data i have is current mouse position and current touchpad X and Y position. Now how can I map touchpad X,Y to mouse position? – hariszaman Nov 10 '13 at 13:00