Clean-up of Joystick class. Refactor constructor to avoid mandatory 'post-constructor...
[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 @implementation TargetMouseMove
12
13 -(BOOL) isContinuous {
14 return true;
15 }
16
17 @synthesize dir;
18
19 -(NSString*) stringify {
20 return [[NSString alloc] initWithFormat: @"mmove~%d", dir];
21 }
22
23 +(TargetMouseMove*) unstringifyImpl: (NSArray*) comps {
24 NSParameterAssert([comps count] == 2);
25 TargetMouseMove* target = [[TargetMouseMove alloc] init];
26 [target setDir: [comps[1] integerValue]];
27 return target;
28 }
29
30 -(void) trigger: (JoystickController *)jc {
31 return;
32 }
33
34 -(void) untrigger: (JoystickController *)jc {
35 return;
36 }
37
38 -(void) update: (JoystickController *)jc {
39 //printf("Dir %d inputValue %f\n", [self dir], [self inputValue]);
40 if (fabs([self inputValue]) < 0.01)
41 return; // dead zone
42
43 NSRect screenRect = [[NSScreen mainScreen] frame];
44 NSInteger height = screenRect.size.height;
45
46 // TODO
47 double speed = 4.0;
48 if ([jc frontWindowOnly])
49 speed = 12.0;
50 double dx = 0.0, dy = 0.0;
51 if ([self dir] == 0)
52 dx = [self inputValue] * speed;
53 else
54 dy = [self inputValue] * speed;
55 NSPoint *mouseLoc = &jc->mouseLoc;
56 mouseLoc->x += dx;
57 mouseLoc->y -= dy;
58
59 CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved,
60 CGPointMake(mouseLoc->x, height - mouseLoc->y),
61 0);
62 CGEventSetType(move, kCGEventMouseMoved);
63 CGEventSetIntegerValueField(move, kCGMouseEventDeltaX, dx);
64 CGEventSetIntegerValueField(move, kCGMouseEventDeltaY, dy);
65
66 if ([jc frontWindowOnly]) {
67 ProcessSerialNumber psn;
68 GetFrontProcess(&psn);
69 CGEventPostToPSN(&psn, move);
70 }
71 else {
72 CGEventPost(kCGHIDEventTap, move);
73 }
74
75 CFRelease(move);
76 }
77
78 @end