Pergunta

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.

Foi útil?

Solução

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...

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top