5 // Created by Yifeng Huang on 7/26/12.
8 #import "TargetMouseMove.h"
10 #import "JoystickController.h"
12 @implementation TargetMouseMove {
16 -(BOOL) isContinuous {
20 + (NSString *)serializationCode {
24 - (NSDictionary *)serialize {
25 return @{ @"type": @"mmove", @"axis": @(_axis) };
28 + (Target *)targetDeserialize:(NSDictionary *)serialization
29 withConfigs:(NSArray *)configs {
30 TargetMouseMove *target = [[TargetMouseMove alloc] init];
31 target.axis = [serialization[@"axis"] intValue];
35 - (BOOL)update:(JoystickController *)jc {
36 if (fabsf(self.magnitude) < 0.01) {
38 return NO; // dead zone
41 // If the action crossed over High/Low, this target is done.
43 sign = self.magnitude < 0 ? -1 : 1;
44 else if (sign / self.magnitude < 0) {
49 NSRect screenRect = [[NSScreen mainScreen] frame];
50 CGFloat height = screenRect.size.height;
54 if ([jc frontWindowOnly])
56 float dx = 0.f, dy = 0.f;
58 dx = self.magnitude * speed;
60 dy = self.magnitude * speed;
61 NSPoint mouseLoc = jc.mouseLoc;
64 jc.mouseLoc = mouseLoc;
66 CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved,
67 CGPointMake(mouseLoc.x, height - mouseLoc.y),
69 CGEventSetType(move, kCGEventMouseMoved);
70 CGEventSetIntegerValueField(move, kCGMouseEventDeltaX, (int)dx);
71 CGEventSetIntegerValueField(move, kCGMouseEventDeltaY, (int)dy);
73 if ([jc frontWindowOnly]) {
74 ProcessSerialNumber psn;
75 GetFrontProcess(&psn);
76 CGEventPostToPSN(&psn, move);
79 CGEventPost(kCGHIDEventTap, move);