X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=TargetMouseScroll.m;h=6595628002913477619858d8264a941826ab34ad;hp=4d6f409a97d2804765f74109447c2c3642837763;hb=635728b6f6775384f7cbc212a4b05198ef024c15;hpb=16a7c0a04bd05f4012d5a97a53520802969cfc86 diff --git a/TargetMouseScroll.m b/TargetMouseScroll.m index 4d6f409..6595628 100644 --- a/TargetMouseScroll.m +++ b/TargetMouseScroll.m @@ -3,21 +3,20 @@ // Enjoy // // Created by Yifeng Huang on 7/28/12. -// Copyright (c) 2012 __MyCompanyName__. All rights reserved. // #import "TargetMouseScroll.h" -@implementation TargetMouseScroll - -@synthesize amount; +@implementation TargetMouseScroll { + int sign; +} + (NSString *)serializationCode { return @"mscroll"; } - (NSDictionary *)serialize { - return @{ @"type": @"mscroll", @"amount": @(self.amount) }; + return @{ @"type": @"mscroll", @"amount": @(_amount) }; } + (Target *)targetDeserialize:(NSDictionary *)serialization @@ -26,13 +25,45 @@ target.amount = [serialization[@"amount"] intValue]; return target; } --(void) trigger { + +- (void)trigger { + if (!self.magnitude) { + CGEventRef scroll = CGEventCreateScrollWheelEvent(NULL, + kCGScrollEventUnitLine, + 1, + _amount); + CGEventPost(kCGHIDEventTap, scroll); + CFRelease(scroll); + } +} + +- (BOOL)update:(JoystickController *)jc { + if (fabsf(self.magnitude) < 0.01f) { + sign = 0; + return NO; // dead zone + } + + // If the action crossed over High/Low, this target is done. + if (!sign) + sign = self.magnitude < 0 ? -1 : 1; + else if (sign / self.magnitude < 0) { + sign = 0; + return NO; + } + + int amount = (int)(16.f * fabsf(self.magnitude) * _amount); CGEventRef scroll = CGEventCreateScrollWheelEvent(NULL, - kCGScrollEventUnitLine, + kCGScrollEventUnitPixel, 1, - self.amount); + amount); CGEventPost(kCGHIDEventTap, scroll); CFRelease(scroll); + + return YES; +} + +- (BOOL)isContinuous { + return YES; } @end