Split NJMappingController view handling off into NJMappingViewController. This is...
[enjoyable.git] / Classes / NJOutputMouseScroll.m
1 //
2 // NJOutputMouseScroll.m
3 // Enjoy
4 //
5 // Created by Yifeng Huang on 7/28/12.
6 //
7
8 #import "NJOutputMouseScroll.h"
9
10 @implementation NJOutputMouseScroll
11
12 + (NSString *)serializationCode {
13 return @"mouse scroll";
14 }
15
16 - (NSDictionary *)serialize {
17 return @{ @"type": self.class.serializationCode,
18 @"direction": @(_direction),
19 @"speed": @(_speed),
20 @"smooth": @(_smooth),
21 };
22 }
23
24 + (NJOutput *)outputDeserialize:(NSDictionary *)serialization
25 withMappings:(id <NSFastEnumeration>)mappings {
26 NJOutputMouseScroll *output = [[NJOutputMouseScroll alloc] init];
27 output.direction = [serialization[@"direction"] intValue];
28 output.speed = [serialization[@"speed"] floatValue];
29 output.smooth = [serialization[@"smooth"] boolValue];
30 return output;
31 }
32
33 - (BOOL)isContinuous {
34 return _smooth;
35 }
36
37 - (int)wheel:(int)n {
38 int amount = abs(_direction) == n ? _direction / n : 0;
39 if (self.smooth)
40 amount *= _speed * self.magnitude;
41 return amount;
42 }
43
44 - (void)trigger {
45 if (!_smooth) {
46 CGEventRef scroll = CGEventCreateScrollWheelEvent(NULL,
47 kCGScrollEventUnitLine,
48 2,
49 [self wheel:1],
50 [self wheel:2]);
51 CGEventPost(kCGHIDEventTap, scroll);
52 CFRelease(scroll);
53 }
54 }
55
56 - (BOOL)update:(NJDeviceController *)jc {
57 if (self.magnitude < 0.05f)
58 return NO; // dead zone
59
60 CGEventRef scroll = CGEventCreateScrollWheelEvent(NULL,
61 kCGScrollEventUnitPixel,
62 2,
63 [self wheel:1],
64 [self wheel:2]);
65 CGEventPost(kCGHIDEventTap, scroll);
66 CFRelease(scroll);
67
68 return YES;
69 }
70
71 @end