From c6f4b9e0f6d98a86bcc91541a874d80253684ac8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesu=CC=81s=20A=2E=20A=CC=81lvarez?= Date: Sat, 14 May 2016 13:12:21 +0200 Subject: [PATCH] add trackpad style input --- Mini vMac.xcodeproj/project.pbxproj | 6 ++ Mini vMac/TrackPad.h | 13 ++++ Mini vMac/TrackPad.m | 93 +++++++++++++++++++++++++++++ Mini vMac/ViewController.m | 5 +- 4 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 Mini vMac/TrackPad.h create mode 100644 Mini vMac/TrackPad.m diff --git a/Mini vMac.xcodeproj/project.pbxproj b/Mini vMac.xcodeproj/project.pbxproj index 10d35ca..b92805f 100644 --- a/Mini vMac.xcodeproj/project.pbxproj +++ b/Mini vMac.xcodeproj/project.pbxproj @@ -13,6 +13,7 @@ 28BA897F1CE7315400A98104 /* KBKeyboardLayout.m in Sources */ = {isa = PBXBuildFile; fileRef = 28BA89771CE7315400A98104 /* KBKeyboardLayout.m */; }; 28BA89801CE7315400A98104 /* KBKeyboardView.m in Sources */ = {isa = PBXBuildFile; fileRef = 28BA89791CE7315400A98104 /* KBKeyboardView.m */; }; 28BA89821CE7336500A98104 /* Keyboard Layouts in Resources */ = {isa = PBXBuildFile; fileRef = 28BA89811CE7336500A98104 /* Keyboard Layouts */; }; + 28BA89851CE73E7200A98104 /* TrackPad.m in Sources */ = {isa = PBXBuildFile; fileRef = 28BA89841CE73E7200A98104 /* TrackPad.m */; }; 28CE8EB51CD4C3B200FE25A8 /* GLOBGLUE.c in Sources */ = {isa = PBXBuildFile; fileRef = 28CE8E931CD4C3B200FE25A8 /* GLOBGLUE.c */; }; 28CE8EB61CD4C3B200FE25A8 /* IWMEMDEV.c in Sources */ = {isa = PBXBuildFile; fileRef = 28CE8E961CD4C3B200FE25A8 /* IWMEMDEV.c */; }; 28CE8EB71CD4C3B200FE25A8 /* KBRDEMDV.c in Sources */ = {isa = PBXBuildFile; fileRef = 28CE8E981CD4C3B200FE25A8 /* KBRDEMDV.c */; }; @@ -51,6 +52,8 @@ 28BA89781CE7315400A98104 /* KBKeyboardView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KBKeyboardView.h; sourceTree = ""; }; 28BA89791CE7315400A98104 /* KBKeyboardView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KBKeyboardView.m; sourceTree = ""; }; 28BA89811CE7336500A98104 /* Keyboard Layouts */ = {isa = PBXFileReference; lastKnownFileType = folder; path = "Keyboard Layouts"; sourceTree = ""; }; + 28BA89831CE73E7200A98104 /* TrackPad.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TrackPad.h; sourceTree = ""; }; + 28BA89841CE73E7200A98104 /* TrackPad.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TrackPad.m; sourceTree = ""; }; 28CE8E881CD4C33E00FE25A8 /* CNFGGLOB.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CNFGGLOB.h; sourceTree = ""; }; 28CE8E891CD4C33E00FE25A8 /* CNFGRAPI.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CNFGRAPI.h; sourceTree = ""; }; 28CE8E8A1CD4C33E00FE25A8 /* EMCONFIG.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EMCONFIG.h; sourceTree = ""; }; @@ -215,6 +218,8 @@ 28CE8ED51CD4F56C00FE25A8 /* ScreenView.m */, 28D5A3FB1CD6868E001A33F6 /* TouchScreen.h */, 28D5A3FC1CD6868E001A33F6 /* TouchScreen.m */, + 28BA89831CE73E7200A98104 /* TrackPad.h */, + 28BA89841CE73E7200A98104 /* TrackPad.m */, 28F676C61CD15E0B00FC6FA6 /* ViewController.h */, 28F676C71CD15E0B00FC6FA6 /* ViewController.m */, 28848B601CDE97D600B86C45 /* InsertDiskViewController.h */, @@ -317,6 +322,7 @@ 28CE8EC11CD4C3B200FE25A8 /* SNDEMDEV.c in Sources */, 28CE8EB51CD4C3B200FE25A8 /* GLOBGLUE.c in Sources */, 28CE8EBF1CD4C3B200FE25A8 /* SCRNEMDV.c in Sources */, + 28BA89851CE73E7200A98104 /* TrackPad.m in Sources */, 28CE8EBD1CD4C3B200FE25A8 /* RTCEMDEV.c in Sources */, 28CE8EB61CD4C3B200FE25A8 /* IWMEMDEV.c in Sources */, 28CE8ED61CD4F56C00FE25A8 /* ScreenView.m in Sources */, diff --git a/Mini vMac/TrackPad.h b/Mini vMac/TrackPad.h new file mode 100644 index 0000000..09e5231 --- /dev/null +++ b/Mini vMac/TrackPad.h @@ -0,0 +1,13 @@ +// +// TrackPad.h +// Mini vMac for iOS +// +// Created by Jesús A. Álvarez on 18/04/2016. +// Copyright © 2016 namedfork. All rights reserved. +// + +#import + +@interface TrackPad : UIControl + +@end diff --git a/Mini vMac/TrackPad.m b/Mini vMac/TrackPad.m new file mode 100644 index 0000000..3f9013a --- /dev/null +++ b/Mini vMac/TrackPad.m @@ -0,0 +1,93 @@ +// +// TrackPad.m +// Mini vMac for iOS +// +// Created by Jesús A. Álvarez on 18/04/2016. +// Copyright © 2016 namedfork. All rights reserved. +// + +#import "TrackPad.h" +#import "AppDelegate.h" + +#define TRACKPAD_ACCEL_N 1 +#define TRACKPAD_ACCEL_T 0.2 +#define TRACKPAD_ACCEL_D 20 + +@implementation TrackPad +{ + NSTimeInterval touchTimeThreshold; + NSTimeInterval previousClickTime, previousTouchTime; + CGFloat touchDistanceThreshold; + CGPoint previousTouchLoc; + BOOL click, drag; + NSMutableSet *currentTouches; +} + +- (instancetype)initWithFrame:(CGRect)frame { + if ((self = [super initWithFrame:frame])) { + touchTimeThreshold = 0.25; + touchDistanceThreshold = 16; + currentTouches = [NSMutableSet setWithCapacity:4]; + } + return self; +} + +- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { + click = YES; + CGPoint touchLoc = [touches.anyObject locationInView:self]; + if ((event.timestamp - previousTouchTime < touchTimeThreshold) && + fabs(previousTouchLoc.x - touchLoc.x) < touchDistanceThreshold && + fabs(previousTouchLoc.y - touchLoc.y) < touchDistanceThreshold) { + drag = YES; + [[AppDelegate sharedInstance] setMouseButton:YES]; + } + previousTouchTime = event.timestamp; + previousTouchLoc = touchLoc; +} + +- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { + CGPoint touchLoc = [touches.anyObject locationInView:self]; + CGPoint locDiff = touchLoc; + locDiff.x -= previousTouchLoc.x; + locDiff.y -= previousTouchLoc.y; + // acceleration + NSTimeInterval timeDiff = 100 * (event.timestamp - previousTouchTime); + NSTimeInterval accel = TRACKPAD_ACCEL_N / (TRACKPAD_ACCEL_T + ((timeDiff * timeDiff)/TRACKPAD_ACCEL_D)); + locDiff.x *= accel; + locDiff.y *= accel; + click = NO; + [[AppDelegate sharedInstance] moveMouseX:locDiff.x Y:locDiff.y]; + previousTouchTime = event.timestamp; + previousTouchLoc = touchLoc; +} + +- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { + CGPoint touchLoc = [touches.anyObject locationInView:self]; + if (click && (event.timestamp - previousTouchTime < touchTimeThreshold)) { + [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(mouseClick) object:nil]; + [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(mouseUp) object:nil]; + [self performSelector:@selector(mouseClick) withObject:nil afterDelay:touchTimeThreshold]; + } + click = NO; + if (drag) { + [[AppDelegate sharedInstance] setMouseButton:NO]; + drag = NO; + } + + previousTouchLoc = touchLoc; + previousTouchTime = event.timestamp; +} + +- (void)mouseClick { + if (drag) { + return; + } + [[AppDelegate sharedInstance] setMouseButton:YES]; + [self performSelector:@selector(mouseUp) withObject:nil afterDelay:2.0/60.0]; +} + +- (void)mouseUp { + [[AppDelegate sharedInstance] setMouseButton:NO]; +} + +@end diff --git a/Mini vMac/ViewController.m b/Mini vMac/ViewController.m index 8bf6379..6736116 100644 --- a/Mini vMac/ViewController.m +++ b/Mini vMac/ViewController.m @@ -8,6 +8,7 @@ #import "ViewController.h" #import "TouchScreen.h" +#import "TrackPad.h" #import "AppDelegate.h" #import "KBKeyboardView.h" #import "KBKeyboardLayout.h" @@ -47,7 +48,9 @@ [pointingDeviceView removeFromSuperview]; pointingDeviceView = nil; } - pointingDeviceView = [[TouchScreen alloc] initWithFrame:self.view.bounds]; + BOOL useTrackPad = [[NSUserDefaults standardUserDefaults] boolForKey:@"trackpad"]; + Class pointingDeviceClass = useTrackPad ? [TrackPad class] : [TouchScreen class]; + pointingDeviceView = [[pointingDeviceClass alloc] initWithFrame:self.view.bounds]; pointingDeviceView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; [self.view insertSubview:pointingDeviceView aboveSubview:self.screenView]; }