X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=Classes%2FNJOutputMouseMove.m;fp=Classes%2FNJOutputMouseMove.m;h=884a3d3cf8d358c53f9ead6a17d6b10870b03e50;hp=0000000000000000000000000000000000000000;hb=0064c1fbff36795885a9724081af2a17d83c20a3;hpb=56d825ba259066d847a9fc3f9c8c0c0a362a1507 diff --git a/Classes/NJOutputMouseMove.m b/Classes/NJOutputMouseMove.m new file mode 100644 index 0000000..884a3d3 --- /dev/null +++ b/Classes/NJOutputMouseMove.m @@ -0,0 +1,76 @@ +// +// NJOutputMouseMove.m +// Enjoy +// +// Created by Yifeng Huang on 7/26/12. +// + +#import "NJOutputMouseMove.h" + +#import "NJDeviceController.h" + +@implementation NJOutputMouseMove + ++ (NSString *)serializationCode { + return @"mouse move"; +} + +- (NSDictionary *)serialize { + return @{ @"type": self.class.serializationCode, + @"axis": @(_axis), + @"speed": @(_speed), + }; +} + ++ (NJOutput *)outputDeserialize:(NSDictionary *)serialization + withMappings:(NSArray *)mappings { + NJOutputMouseMove *output = [[NJOutputMouseMove alloc] init]; + output.axis = [serialization[@"axis"] intValue]; + output.speed = [serialization[@"speed"] floatValue]; + if (!output.speed) + output.speed = 4; + return output; +} + +- (BOOL)isContinuous { + return YES; +} + +- (BOOL)update:(NJDeviceController *)jc { + if (self.magnitude < 0.05) + return NO; // dead zone + + CGFloat height = NSScreen.mainScreen.frame.size.height; + + float dx = 0.f, dy = 0.f; + switch (_axis) { + case 0: + dx = -self.magnitude * _speed; + break; + case 1: + dx = self.magnitude * _speed; + break; + case 2: + dy = -self.magnitude * _speed; + break; + case 3: + dy = self.magnitude * _speed; + break; + } + NSPoint mouseLoc = jc.mouseLoc; + mouseLoc.x += dx; + mouseLoc.y -= dy; + jc.mouseLoc = mouseLoc; + + CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved, + CGPointMake(mouseLoc.x, height - mouseLoc.y), + 0); + CGEventSetType(move, kCGEventMouseMoved); + CGEventSetIntegerValueField(move, kCGMouseEventDeltaX, (int)dx); + CGEventSetIntegerValueField(move, kCGMouseEventDeltaY, (int)dy); + CGEventPost(kCGHIDEventTap, move); + CFRelease(move); + return YES; +} + +@end