X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=Classes%2FNJOutputMouseMove.m;h=09185932a057d7632af42dd1d8296c4472377346;hp=884a3d3cf8d358c53f9ead6a17d6b10870b03e50;hb=281e575060f936fd9483d1cf6416851b3783fe42;hpb=0064c1fbff36795885a9724081af2a17d83c20a3 diff --git a/Classes/NJOutputMouseMove.m b/Classes/NJOutputMouseMove.m index 884a3d3..0918593 100644 --- a/Classes/NJOutputMouseMove.m +++ b/Classes/NJOutputMouseMove.m @@ -7,7 +7,7 @@ #import "NJOutputMouseMove.h" -#import "NJDeviceController.h" +#import "NJInputController.h" @implementation NJOutputMouseMove @@ -22,13 +22,12 @@ }; } -+ (NJOutput *)outputDeserialize:(NSDictionary *)serialization - withMappings:(NSArray *)mappings { ++ (NJOutput *)outputWithSerialization:(NSDictionary *)serialization { NJOutputMouseMove *output = [[NJOutputMouseMove alloc] init]; output.axis = [serialization[@"axis"] intValue]; output.speed = [serialization[@"speed"] floatValue]; if (!output.speed) - output.speed = 4; + output.speed = 10; return output; } @@ -36,13 +35,15 @@ return YES; } -- (BOOL)update:(NJDeviceController *)jc { +#define CLAMP(a, l, h) MIN(h, MAX(a, l)) + +- (BOOL)update:(NJInputController *)ic { if (self.magnitude < 0.05) return NO; // dead zone - CGFloat height = NSScreen.mainScreen.frame.size.height; + CGSize size = NSScreen.mainScreen.frame.size; - float dx = 0.f, dy = 0.f; + CGFloat dx = 0, dy = 0; switch (_axis) { case 0: dx = -self.magnitude * _speed; @@ -57,18 +58,34 @@ dy = self.magnitude * _speed; break; } - NSPoint mouseLoc = jc.mouseLoc; - mouseLoc.x += dx; - mouseLoc.y -= dy; - jc.mouseLoc = mouseLoc; + NSPoint mouseLoc = ic.mouseLoc; + mouseLoc.x = CLAMP(mouseLoc.x + dx, 0, size.width - 1); + mouseLoc.y = CLAMP(mouseLoc.y - dy, 0, size.height - 1); + ic.mouseLoc = mouseLoc; CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved, - CGPointMake(mouseLoc.x, height - mouseLoc.y), + CGPointMake(mouseLoc.x, size.height - mouseLoc.y), 0); - CGEventSetType(move, kCGEventMouseMoved); CGEventSetIntegerValueField(move, kCGMouseEventDeltaX, (int)dx); CGEventSetIntegerValueField(move, kCGMouseEventDeltaY, (int)dy); CGEventPost(kCGHIDEventTap, move); + + if (CGEventSourceButtonState(kCGEventSourceStateHIDSystemState, kCGMouseButtonLeft)) { + CGEventSetType(move, kCGEventLeftMouseDragged); + CGEventSetIntegerValueField(move, kCGMouseEventButtonNumber, kCGMouseButtonLeft); + CGEventPost(kCGHIDEventTap, move); + } + if (CGEventSourceButtonState(kCGEventSourceStateHIDSystemState, kCGMouseButtonRight)) { + CGEventSetType(move, kCGEventRightMouseDragged); + CGEventSetIntegerValueField(move, kCGMouseEventButtonNumber, kCGMouseButtonRight); + CGEventPost(kCGHIDEventTap, move); + } + if (CGEventSourceButtonState(kCGEventSourceStateHIDSystemState, kCGMouseButtonCenter)) { + CGEventSetType(move, kCGEventOtherMouseDragged); + CGEventSetIntegerValueField(move, kCGMouseEventButtonNumber, kCGMouseButtonCenter); + CGEventPost(kCGHIDEventTap, move); + } + CFRelease(move); return YES; }