Fixed signedness.
[enjoyable.git] / 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 };
21 }
22
23 + (NJOutput *)outputDeserialize:(NSDictionary *)serialization
24 withMappings:(NSArray *)mappings {
25 NJOutputMouseScroll *output = [[NJOutputMouseScroll alloc] init];
26 output.direction = [serialization[@"direction"] intValue];
27 output.speed = [serialization[@"direction"] floatValue];
28 return output;
29 }
30
31 - (BOOL)isContinuous {
32 return !!self.speed;
33 }
34
35 - (void)trigger {
36 if (!self.speed) {
37 CGEventRef scroll = CGEventCreateScrollWheelEvent(NULL,
38 kCGScrollEventUnitLine,
39 1,
40 _direction);
41 CGEventPost(kCGHIDEventTap, scroll);
42 CFRelease(scroll);
43 }
44 }
45
46 - (BOOL)update:(NJDeviceController *)jc {
47 if (self.magnitude < 0.05f)
48 return NO; // dead zone
49
50 int amount = (int)(_speed * self.magnitude * _direction);
51 CGEventRef scroll = CGEventCreateScrollWheelEvent(NULL,
52 kCGScrollEventUnitPixel,
53 1,
54 amount);
55 CGEventPost(kCGHIDEventTap, scroll);
56 CFRelease(scroll);
57
58 return YES;
59 }
60
61 @end