5 // Created by Yifeng Huang on 7/26/12.
6 // Copyright (c) 2012 __MyCompanyName__. All rights reserved.
9 #import "TargetMouseMove.h"
11 #import "JoystickController.h"
13 @implementation TargetMouseMove {
17 -(BOOL) isContinuous {
23 + (NSString *)serializationCode {
27 - (NSDictionary *)serialize {
28 return @{ @"type": @"mmove", @"dir": @(self.dir) };
31 + (Target *)targetDeserialize:(NSDictionary *)serialization
32 withConfigs:(NSArray *)configs {
33 TargetMouseMove *target = [[TargetMouseMove alloc] init];
34 target.dir = [serialization[@"dir"] intValue];
38 - (BOOL)update:(JoystickController *)jc {
39 if (fabsf(self.magnitude) < 0.01) {
41 return NO; // dead zone
44 // If the action crossed over High/Low, this target is done.
46 sign = self.magnitude < 0 ? -1 : 1;
47 else if (sign / self.magnitude < 0) {
52 NSRect screenRect = [[NSScreen mainScreen] frame];
53 NSInteger height = screenRect.size.height;
57 if ([jc frontWindowOnly])
59 float dx = 0.f, dy = 0.f;
61 dx = self.magnitude * speed;
63 dy = self.magnitude * speed;
64 NSPoint mouseLoc = jc.mouseLoc;
67 jc.mouseLoc = mouseLoc;
69 CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved,
70 CGPointMake(mouseLoc.x, height - mouseLoc.y),
72 CGEventSetType(move, kCGEventMouseMoved);
73 CGEventSetIntegerValueField(move, kCGMouseEventDeltaX, dx);
74 CGEventSetIntegerValueField(move, kCGMouseEventDeltaY, dy);
76 if ([jc frontWindowOnly]) {
77 ProcessSerialNumber psn;
78 GetFrontProcess(&psn);
79 CGEventPostToPSN(&psn, move);
82 CGEventPost(kCGHIDEventTap, move);