X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=Classes%2FNJOutputMouseScroll.m;fp=Classes%2FNJOutputMouseScroll.m;h=812685f527de245846749a33bd9b42d5a37fb4b0;hp=0000000000000000000000000000000000000000;hb=0064c1fbff36795885a9724081af2a17d83c20a3;hpb=56d825ba259066d847a9fc3f9c8c0c0a362a1507 diff --git a/Classes/NJOutputMouseScroll.m b/Classes/NJOutputMouseScroll.m new file mode 100644 index 0000000..812685f --- /dev/null +++ b/Classes/NJOutputMouseScroll.m @@ -0,0 +1,61 @@ +// +// NJOutputMouseScroll.m +// Enjoy +// +// Created by Yifeng Huang on 7/28/12. +// + +#import "NJOutputMouseScroll.h" + +@implementation NJOutputMouseScroll + ++ (NSString *)serializationCode { + return @"mouse scroll"; +} + +- (NSDictionary *)serialize { + return @{ @"type": self.class.serializationCode, + @"direction": @(_direction), + @"speed": @(_speed) + }; +} + ++ (NJOutput *)outputDeserialize:(NSDictionary *)serialization + withMappings:(NSArray *)mappings { + NJOutputMouseScroll *output = [[NJOutputMouseScroll alloc] init]; + output.direction = [serialization[@"direction"] intValue]; + output.speed = [serialization[@"direction"] floatValue]; + return output; +} + +- (BOOL)isContinuous { + return !!self.speed; +} + +- (void)trigger { + if (!self.speed) { + CGEventRef scroll = CGEventCreateScrollWheelEvent(NULL, + kCGScrollEventUnitLine, + 1, + _direction); + CGEventPost(kCGHIDEventTap, scroll); + CFRelease(scroll); + } +} + +- (BOOL)update:(NJDeviceController *)jc { + if (self.magnitude < 0.05f) + return NO; // dead zone + + int amount = (int)(_speed * self.magnitude * _direction); + CGEventRef scroll = CGEventCreateScrollWheelEvent(NULL, + kCGScrollEventUnitPixel, + 1, + amount); + CGEventPost(kCGHIDEventTap, scroll); + CFRelease(scroll); + + return YES; +} + +@end