Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mouse drag integration #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Classes/NJOutput.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import "NJOutputMouseMove.h"
#import "NJOutputMouseButton.h"
#import "NJOutputMouseScroll.h"
#import "NJOutputMouseDrag.h"

@implementation NJOutput {
BOOL running;
Expand Down Expand Up @@ -45,7 +46,8 @@ + (NJOutput *)outputWithSerialization:(NSDictionary *)serialization {
NJOutputMapping.class,
NJOutputMouseMove.class,
NJOutputMouseButton.class,
NJOutputMouseScroll.class
NJOutputMouseScroll.class,
NJOutputMouseDrag.class
]) {
if ([type isEqualToString:cls.serializationCode])
return [cls outputWithSerialization:serialization];
Expand Down
16 changes: 16 additions & 0 deletions Classes/NJOutputMouseDrag.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// NJOutputMouseDrag.h
// Enjoyable
//
// Created by Giovanni Muzzolini on 09/10/20.
//

#import "NJOutput.h"

@interface NJOutputMouseDrag : NJOutput

@property (nonatomic, assign) CGMouseButton button;
@property (nonatomic, assign) int axis;
@property (nonatomic, assign) float speed;

@end
108 changes: 108 additions & 0 deletions Classes/NJOutputMouseDrag.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//
// NJOutputMouseDrag.m
// Enjoyable
//
// Created by Giovanni Muzzolini on 09/10/20.
//

#import "NJOutputMouseDrag.h"

#import "NJInputController.h"

@implementation NJOutputMouseDrag

+ (NSString *)serializationCode {
return @"mouse drag";
}

- (NSDictionary *)serialize {
return @{ @"type": self.class.serializationCode,
@"axis": @(_axis),
@"speed": @(_speed),
@"button": @(_button)
};
}

+ (NJOutput *)outputWithSerialization:(NSDictionary *)serialization {
NJOutputMouseDrag *output = [[NJOutputMouseDrag alloc] init];
output.axis = [serialization[@"axis"] intValue];
output.speed = [serialization[@"speed"] floatValue];
output.button = [serialization[@"button"] intValue];
if (output.speed == 0)
output.speed = 10;
return output;
}

- (BOOL)isContinuous {
return YES;
}

#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;
NSPoint clickLoc = NSEvent.mouseLocation;

if(!CGEventSourceButtonState(kCGEventSourceStateHIDSystemState, _button)){

// trigger mouseDown

CGEventType downEventType = _button == kCGMouseButtonLeft ? kCGEventLeftMouseDown
: _button == kCGMouseButtonRight ? kCGEventRightMouseDown
: kCGEventOtherMouseDown;

CGEventRef down = CGEventCreateMouseEvent(NULL,
downEventType,
CGPointMake(clickLoc.x, height - clickLoc.y),
_button);
CGEventSetIntegerValueField(down, kCGMouseEventClickState, 1);
CGEventPost(kCGHIDEventTap, down);
CFRelease(down);

}

CGSize size = NSScreen.mainScreen.frame.size;

CGFloat dx = 0, dy = 0;
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 = 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;

// trigger mouseDrag

CGEventType eventType = _button == kCGMouseButtonLeft ? kCGEventLeftMouseDragged
: _button == kCGMouseButtonRight ? kCGEventRightMouseDragged
: kCGEventOtherMouseDragged;

CGEventRef drag = CGEventCreateMouseEvent(NULL, eventType,
CGPointMake(mouseLoc.x, size.height - mouseLoc.y),
_button);
CGEventSetIntegerValueField(drag, kCGMouseEventDeltaX, (int)dx);
CGEventSetIntegerValueField(drag, kCGMouseEventDeltaY, (int)dy);
CGEventPost(kCGHIDEventTap, drag);

CFRelease(drag);

return YES;
}

@end
6 changes: 6 additions & 0 deletions Classes/NJOutputViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
@property (nonatomic, strong) IBOutlet NSPopUpButton *mappingPopup;
@property (nonatomic, strong) IBOutlet NSButton *smoothCheck;
@property (nonatomic, strong) IBOutlet NSButton *unknownMapping;
@property (nonatomic, strong) IBOutlet NSSegmentedControl *dragBtnSelect;
@property (nonatomic, strong) IBOutlet NSSegmentedControl *dragDirSelect;
@property (nonatomic, strong) IBOutlet NSSlider *dragSpeedSlider;

@property (nonatomic, weak) IBOutlet id <NJOutputViewControllerDelegate> delegate;

Expand All @@ -40,6 +43,9 @@
- (IBAction)mouseSpeedChanged:(id)sender;
- (IBAction)scrollSpeedChanged:(id)sender;
- (IBAction)scrollTypeChanged:(id)sender;
- (IBAction)dragButtonChanged:(id)sender;
- (IBAction)dragDirectionChanged:(id)sender;
- (IBAction)dragSpeedChanged:(id)sender;

@end

Expand Down
49 changes: 49 additions & 0 deletions Classes/NJOutputViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#import "NJOutputMouseButton.h"
#import "NJOutputMouseMove.h"
#import "NJOutputMouseScroll.h"
#import "NJOutputMouseDrag.h"

@implementation NJOutputViewController {
NJInput *_input;
Expand Down Expand Up @@ -80,6 +81,20 @@ - (void)cleanUpInterface {
if (self.scrollDirSelect.selectedSegment == -1)
self.scrollDirSelect.selectedSegment = 0;
}

if (row != 6) {
self.dragDirSelect.selectedSegment = -1;
self.dragSpeedSlider.doubleValue = self.dragSpeedSlider.minValue;
self.dragBtnSelect.selectedSegment = -1;
[self.dragDirSelect resignIfFirstResponder];
} else {
if (self.dragDirSelect.selectedSegment == -1)
self.dragDirSelect.selectedSegment = 0;
if (self.dragSpeedSlider.floatValue == 0)
self.dragSpeedSlider.floatValue = 10;
if (self.dragBtnSelect.selectedSegment == -1)
self.dragBtnSelect.selectedSegment = 0;
}

}

Expand Down Expand Up @@ -153,6 +168,24 @@ - (IBAction)scrollTypeChanged:(NSButton *)sender {
[self commit];
}

- (void)dragButtonChanged:(NSView *)sender {
[self.radioButtons selectCellAtRow:6 column:0];
[sender.window makeFirstResponder:sender];
[self commit];
}

- (void)dragDirectionChanged:(NSView *)sender {
[self.radioButtons selectCellAtRow:6 column:0];
[sender.window makeFirstResponder:sender];
[self commit];
}

- (void)dragSpeedChanged:(NSSlider *)sender {
[self.radioButtons selectCellAtRow:6 column:0];
[sender.window makeFirstResponder:sender];
[self commit];
}

- (NJOutput *)makeOutput {
switch (self.radioButtons.selectedRow) {
case 0:
Expand Down Expand Up @@ -190,6 +223,13 @@ - (NJOutput *)makeOutput {
ms.smooth = self.smoothCheck.state == NSOnState;
return ms;
}
case 6: {
NJOutputMouseDrag *md = [[NJOutputMouseDrag alloc] init];
md.axis = (int)self.dragDirSelect.selectedSegment;
md.speed = self.dragSpeedSlider.floatValue;
md.button = (int)[self.dragBtnSelect.cell tagForSegment:self.dragBtnSelect.selectedSegment];
return md;
}
default:
return nil;
}
Expand All @@ -216,6 +256,9 @@ - (void)setEnabled:(BOOL)enabled {
self.scrollDirSelect.enabled = enabled;
self.smoothCheck.enabled = enabled;
self.scrollSpeedSlider.enabled = enabled && self.smoothCheck.state;
self.dragDirSelect.enabled = enabled;
self.dragSpeedSlider.enabled = enabled;
self.dragBtnSelect.enabled = enabled;
if (!enabled)
self.unknownMapping.hidden = YES;
}
Expand Down Expand Up @@ -263,6 +306,12 @@ - (void)loadOutput:(NJOutput *)output forInput:(NJInput *)input {
self.scrollSpeedSlider.floatValue = speed;
self.smoothCheck.state = smooth ? NSOnState : NSOffState;
self.scrollSpeedSlider.enabled = smooth;
}
else if ([output isKindOfClass:NJOutputMouseDrag.class]) {
[self.radioButtons selectCellAtRow:6 column:0];
self.dragDirSelect.selectedSegment = [(NJOutputMouseDrag *)output axis];
self.dragSpeedSlider.floatValue = [(NJOutputMouseDrag *)output speed];
[self.dragBtnSelect selectSegmentWithTag:[(NJOutputMouseDrag *)output button]];
} else {
[self.radioButtons selectCellAtRow:self.enabled ? 0 : -1 column:0];
}
Expand Down
6 changes: 6 additions & 0 deletions Enjoyable.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
EEF17D6A16E8E2EF00D7DC4D /* NJOutputMouseButton.m in Sources */ = {isa = PBXBuildFile; fileRef = EEF17D5716E8E2EF00D7DC4D /* NJOutputMouseButton.m */; };
EEF17D6B16E8E2EF00D7DC4D /* NJOutputMouseMove.m in Sources */ = {isa = PBXBuildFile; fileRef = EEF17D5916E8E2EF00D7DC4D /* NJOutputMouseMove.m */; };
EEF17D6C16E8E2EF00D7DC4D /* NJOutputMouseScroll.m in Sources */ = {isa = PBXBuildFile; fileRef = EEF17D5B16E8E2EF00D7DC4D /* NJOutputMouseScroll.m */; };
F279F40125305D3D00676675 /* NJOutputMouseDrag.m in Sources */ = {isa = PBXBuildFile; fileRef = F279F40025305D3D00676675 /* NJOutputMouseDrag.m */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -150,6 +151,8 @@
EEF17D5916E8E2EF00D7DC4D /* NJOutputMouseMove.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = NJOutputMouseMove.m; path = Classes/NJOutputMouseMove.m; sourceTree = "<group>"; };
EEF17D5A16E8E2EF00D7DC4D /* NJOutputMouseScroll.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NJOutputMouseScroll.h; path = Classes/NJOutputMouseScroll.h; sourceTree = "<group>"; };
EEF17D5B16E8E2EF00D7DC4D /* NJOutputMouseScroll.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = NJOutputMouseScroll.m; path = Classes/NJOutputMouseScroll.m; sourceTree = "<group>"; };
F279F3FF25305CFA00676675 /* NJOutputMouseDrag.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = NJOutputMouseDrag.h; path = Classes/NJOutputMouseDrag.h; sourceTree = "<group>"; };
F279F40025305D3D00676675 /* NJOutputMouseDrag.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = NJOutputMouseDrag.m; path = Classes/NJOutputMouseDrag.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -323,6 +326,8 @@
EEF17D5916E8E2EF00D7DC4D /* NJOutputMouseMove.m */,
EEF17D5A16E8E2EF00D7DC4D /* NJOutputMouseScroll.h */,
EEF17D5B16E8E2EF00D7DC4D /* NJOutputMouseScroll.m */,
F279F3FF25305CFA00676675 /* NJOutputMouseDrag.h */,
F279F40025305D3D00676675 /* NJOutputMouseDrag.m */,
);
name = Output;
sourceTree = "<group>";
Expand Down Expand Up @@ -465,6 +470,7 @@
EEF17D3616E8E2E100D7DC4D /* NSMutableArray+MoveObject.m in Sources */,
EEF17D3716E8E2E100D7DC4D /* NSString+FixFilename.m in Sources */,
EEF17D3816E8E2E100D7DC4D /* NSView+FirstResponder.m in Sources */,
F279F40125305D3D00676675 /* NJOutputMouseDrag.m in Sources */,
EEF17D5C16E8E2EF00D7DC4D /* EnjoyableApplicationDelegate.m in Sources */,
EEF17D5D16E8E2EF00D7DC4D /* NJDevice.m in Sources */,
EEF17D5E16E8E2EF00D7DC4D /* NJInputController.m in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>663</string>
<string>673</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>NSHumanReadableCopyright</key>
Expand Down
Loading