X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=TargetMouseScroll.m;h=8f87c02e32557ee7d0b99a2e2c20dc03b580b177;hp=be64f7a2986c4e9824a0534ccd4d7dea6dec744f;hb=e2a4d830dd9817f6a515a3b1b6aa152d3bb98c2b;hpb=51ca12b552a9c17c4d4029b0340e193b273044a8 diff --git a/TargetMouseScroll.m b/TargetMouseScroll.m index be64f7a..8f87c02 100644 --- a/TargetMouseScroll.m +++ b/TargetMouseScroll.m @@ -3,33 +3,67 @@ // Enjoy // // Created by Yifeng Huang on 7/28/12. -// Copyright (c) 2012 __MyCompanyName__. All rights reserved. // #import "TargetMouseScroll.h" -@implementation TargetMouseScroll +@implementation TargetMouseScroll { + int sign; +} -@synthesize howMuch; ++ (NSString *)serializationCode { + return @"mscroll"; +} --(NSString*) stringify { - return [[NSString alloc] initWithFormat: @"mscroll~%d", howMuch]; +- (NSDictionary *)serialize { + return @{ @"type": @"mscroll", @"amount": @(_amount) }; } -+(TargetMouseScroll*) unstringifyImpl: (NSArray*) comps { - NSParameterAssert([comps count] == 2); - TargetMouseScroll* target = [[TargetMouseScroll alloc] init]; - [target setHowMuch: [comps[1] integerValue]]; ++ (Target *)targetDeserialize:(NSDictionary *)serialization + withMappings:(NSArray *)mappings { + TargetMouseScroll *target = [[TargetMouseScroll alloc] init]; + target.amount = [serialization[@"amount"] intValue]; return target; } --(void) trigger: (JoystickController *)jc { +- (void)trigger { + if (!self.magnitude) { + CGEventRef scroll = CGEventCreateScrollWheelEvent(NULL, + kCGScrollEventUnitLine, + 1, + _amount); + CGEventPost(kCGHIDEventTap, scroll); + CFRelease(scroll); + } +} + +- (BOOL)update:(NJInputController *)jc { + if (fabsf(self.magnitude) < 0.01f) { + sign = 0; + return NO; // dead zone + } + + // If the input 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 howMuch]); + amount); CGEventPost(kCGHIDEventTap, scroll); CFRelease(scroll); + + return YES; +} + +- (BOOL)isContinuous { + return YES; } @end