X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=TargetMouseMove.m;h=8bbfd42f7d366e1383e245ad4d8fa7e295a81359;hp=86dd7367749b562936018f232bfb253b8da3caa4;hb=03b4a589de71a49ad00450701630673631e88647;hpb=51ca12b552a9c17c4d4029b0340e193b273044a8 diff --git a/TargetMouseMove.m b/TargetMouseMove.m index 86dd736..8bbfd42 100644 --- a/TargetMouseMove.m +++ b/TargetMouseMove.m @@ -8,56 +8,64 @@ #import "TargetMouseMove.h" -@implementation TargetMouseMove +#import "JoystickController.h" --(BOOL) isContinuous { - return true; +@implementation TargetMouseMove { + int sign; } -@synthesize dir; - --(NSString*) stringify { - return [[NSString alloc] initWithFormat: @"mmove~%d", dir]; +-(BOOL) isContinuous { + return YES; } -+(TargetMouseMove*) unstringifyImpl: (NSArray*) comps { - NSParameterAssert([comps count] == 2); - TargetMouseMove* target = [[TargetMouseMove alloc] init]; - [target setDir: [comps[1] integerValue]]; - return target; ++ (NSString *)serializationCode { + return @"mmove"; } --(void) trigger: (JoystickController *)jc { - return; +- (NSDictionary *)serialize { + return @{ @"type": @"mmove", @"axis": @(_axis) }; } --(void) untrigger: (JoystickController *)jc { - return; ++ (Target *)targetDeserialize:(NSDictionary *)serialization + withConfigs:(NSArray *)configs { + TargetMouseMove *target = [[TargetMouseMove alloc] init]; + target.axis = [serialization[@"axis"] intValue]; + return target; } --(void) update: (JoystickController *)jc { - //printf("Dir %d inputValue %f\n", [self dir], [self inputValue]); - if (fabs([self inputValue]) < 0.01) - return; // dead zone +- (BOOL)update:(JoystickController *)jc { + if (fabsf(self.magnitude) < 0.01) { + 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; + } NSRect screenRect = [[NSScreen mainScreen] frame]; NSInteger height = screenRect.size.height; // TODO - double speed = 4.0; + float speed = 4.f; if ([jc frontWindowOnly]) - speed = 12.0; - double dx = 0.0, dy = 0.0; - if ([self dir] == 0) - dx = [self inputValue] * speed; + speed = 12.f; + float dx = 0.f, dy = 0.f; + if (_axis == 0) + dx = self.magnitude * speed; else - dy = [self inputValue] * speed; - NSPoint *mouseLoc = &jc->mouseLoc; - mouseLoc->x += dx; - mouseLoc->y -= dy; + dy = self.magnitude * speed; + NSPoint mouseLoc = jc.mouseLoc; + mouseLoc.x += dx; + mouseLoc.y -= dy; + jc.mouseLoc = mouseLoc; CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved, - CGPointMake(mouseLoc->x, height - mouseLoc->y), + CGPointMake(mouseLoc.x, height - mouseLoc.y), 0); CGEventSetType(move, kCGEventMouseMoved); CGEventSetIntegerValueField(move, kCGMouseEventDeltaX, dx); @@ -73,6 +81,7 @@ } CFRelease(move); + return YES; } @end