Big rename part 1: 'action' to 'input'.
[enjoyable.git] / TargetMouseScroll.m
1 //
2 // TargetMouseScroll.m
3 // Enjoy
4 //
5 // Created by Yifeng Huang on 7/28/12.
6 //
7
8 #import "TargetMouseScroll.h"
9
10 @implementation TargetMouseScroll {
11 int sign;
12 }
13
14 + (NSString *)serializationCode {
15 return @"mscroll";
16 }
17
18 - (NSDictionary *)serialize {
19 return @{ @"type": @"mscroll", @"amount": @(_amount) };
20 }
21
22 + (Target *)targetDeserialize:(NSDictionary *)serialization
23 withConfigs:(NSArray *)configs {
24 TargetMouseScroll *target = [[TargetMouseScroll alloc] init];
25 target.amount = [serialization[@"amount"] intValue];
26 return target;
27 }
28
29 - (void)trigger {
30 if (!self.magnitude) {
31 CGEventRef scroll = CGEventCreateScrollWheelEvent(NULL,
32 kCGScrollEventUnitLine,
33 1,
34 _amount);
35 CGEventPost(kCGHIDEventTap, scroll);
36 CFRelease(scroll);
37 }
38 }
39
40 - (BOOL)update:(NJInputController *)jc {
41 if (fabsf(self.magnitude) < 0.01f) {
42 sign = 0;
43 return NO; // dead zone
44 }
45
46 // If the input crossed over High/Low, this target is done.
47 if (!sign)
48 sign = self.magnitude < 0 ? -1 : 1;
49 else if (sign / self.magnitude < 0) {
50 sign = 0;
51 return NO;
52 }
53
54 int amount = (int)(16.f * fabsf(self.magnitude) * _amount);
55 CGEventRef scroll = CGEventCreateScrollWheelEvent(NULL,
56 kCGScrollEventUnitPixel,
57 1,
58 amount);
59 CGEventPost(kCGHIDEventTap, scroll);
60 CFRelease(scroll);
61
62 return YES;
63 }
64
65 - (BOOL)isContinuous {
66 return YES;
67 }
68
69 @end