More better names.
[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 *)outputWithSerialization:(NSDictionary *)serialization {
25 NJOutputMouseScroll *output = [[NJOutputMouseScroll alloc] init];
26 output.direction = [serialization[@"direction"] intValue];
27 output.speed = [serialization[@"speed"] floatValue];
28 output.smooth = [serialization[@"smooth"] boolValue];
29 return output;
30 }
31
32 - (BOOL)isContinuous {
33 return _smooth;
34 }
35
36 - (int)wheel:(int)n {
37 int amount = abs(_direction) == n ? _direction / n : 0;
38 if (self.smooth)
39 amount *= _speed * self.magnitude;
40 return amount;
41 }
42
43 - (void)trigger {
44 if (!_smooth) {
45 CGEventRef scroll = CGEventCreateScrollWheelEvent(NULL,
46 kCGScrollEventUnitLine,
47 2,
48 [self wheel:1],
49 [self wheel:2]);
50 CGEventPost(kCGHIDEventTap, scroll);
51 CFRelease(scroll);
52 }
53 }
54
55 - (BOOL)update:(NJInputController *)ic {
56 if (self.magnitude < 0.05f)
57 return NO; // dead zone
58
59 CGEventRef scroll = CGEventCreateScrollWheelEvent(NULL,
60 kCGScrollEventUnitPixel,
61 2,
62 [self wheel:1],
63 [self wheel:2]);
64 CGEventPost(kCGHIDEventTap, scroll);
65 CFRelease(scroll);
66
67 return YES;
68 }
69
70 @end