Reset target settings in UI when switching target types.
[enjoyable.git] / TargetMouseMove.m
1 //
2 // TargetMouseMove.m
3 // Enjoy
4 //
5 // Created by Yifeng Huang on 7/26/12.
6 // Copyright (c) 2012 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "TargetMouseMove.h"
10
11 #import "JoystickController.h"
12
13 @implementation TargetMouseMove
14
15 -(BOOL) isContinuous {
16 return true;
17 }
18
19 @synthesize dir;
20
21 + (NSString *)serializationCode {
22 return @"mmove";
23 }
24
25 - (NSDictionary *)serialize {
26 return @{ @"type": @"mmove", @"dir": @(self.dir) };
27 }
28
29 + (Target *)targetDeserialize:(NSDictionary *)serialization
30 withConfigs:(NSArray *)configs {
31 TargetMouseMove *target = [[TargetMouseMove alloc] init];
32 target.dir = [serialization[@"dir"] intValue];
33 return target;
34 }
35
36 - (BOOL)update:(JoystickController *)jc {
37 //printf("Dir %d inputValue %f\n", [self dir], [self inputValue]);
38 if (fabs(self.magnitude) < 0.01)
39 return NO; // dead zone
40
41 NSRect screenRect = [[NSScreen mainScreen] frame];
42 NSInteger height = screenRect.size.height;
43
44 // TODO
45 float speed = 4.0;
46 if ([jc frontWindowOnly])
47 speed = 12.0;
48 float dx = 0.0, dy = 0.0;
49 if ([self dir] == 0)
50 dx = self.magnitude * speed;
51 else
52 dy = self.magnitude * speed;
53 NSPoint mouseLoc = jc.mouseLoc;
54 mouseLoc.x += dx;
55 mouseLoc.y -= dy;
56 jc.mouseLoc = mouseLoc;
57
58 CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved,
59 CGPointMake(mouseLoc.x, height - mouseLoc.y),
60 0);
61 CGEventSetType(move, kCGEventMouseMoved);
62 CGEventSetIntegerValueField(move, kCGMouseEventDeltaX, dx);
63 CGEventSetIntegerValueField(move, kCGMouseEventDeltaY, dy);
64
65 if ([jc frontWindowOnly]) {
66 ProcessSerialNumber psn;
67 GetFrontProcess(&psn);
68 CGEventPostToPSN(&psn, move);
69 }
70 else {
71 CGEventPost(kCGHIDEventTap, move);
72 }
73
74 CFRelease(move);
75 return dx || dy;
76 }
77
78 @end