From ac333642fdbc754dd823160da106db836dcff84b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesu=CC=81s=20A=2E=20A=CC=81lvarez?= Date: Sat, 14 May 2016 14:04:18 +0200 Subject: [PATCH] limit max speed to 4x, don't persist speed setting --- Mini vMac/AppDelegate.h | 11 ------- Mini vMac/AppDelegate.m | 44 ++++++++++++++++------------ Mini vMac/Base.lproj/Main.storyboard | 2 -- Mini vMac/SettingsViewController.m | 12 ++------ 4 files changed, 27 insertions(+), 42 deletions(-) diff --git a/Mini vMac/AppDelegate.h b/Mini vMac/AppDelegate.h index 2f627d2..dc99f54 100644 --- a/Mini vMac/AppDelegate.h +++ b/Mini vMac/AppDelegate.h @@ -11,21 +11,10 @@ extern NSString * const MNVMDidInsertDiskNotification; extern NSString * const MNVMDidEjectDiskNotification; -typedef enum : NSUInteger { - EmulationSpeedMax = -1, - EmulationSpeed1x = 0, - EmulationSpeed2x, - EmulationSpeed4x, - EmulationSpeed8x, - EmulationSpeed16x, - EmulationSpeed32x -} EmulationSpeed; - @interface AppDelegate : UIResponder @property (strong, nonatomic) UIWindow *window; @property (assign, nonatomic, getter=isEmulatorRunning) BOOL emulatorRunning; -@property (nonatomic, assign) EmulationSpeed emulationSpeed; @property (nonatomic, readonly) NSString *documentsPath; @property (nonatomic, readonly) NSArray *diskImageExtensions; diff --git a/Mini vMac/AppDelegate.m b/Mini vMac/AppDelegate.m index 9a784ba..17c41d6 100644 --- a/Mini vMac/AppDelegate.m +++ b/Mini vMac/AppDelegate.m @@ -40,7 +40,12 @@ NSString * const MNVMDidEjectDiskNotification = @"MNVMDidEjectDisk"; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { sharedAppDelegate = self; - + [self initDefaults]; + [self performSelector:@selector(runEmulator) withObject:nil afterDelay:0.1]; + return YES; +} + +- (void)initDefaults { // default settings NSString *defaultKeyboardLayout = @"US.nfkeyboardlayout"; NSLocale *locale = [NSLocale currentLocale]; @@ -49,15 +54,22 @@ NSString * const MNVMDidEjectDiskNotification = @"MNVMDidEjectDisk"; } else if ([[locale objectForKey:NSLocaleLanguageCode] isEqualToString:@"es"]) { defaultKeyboardLayout = @"Spanish.nfkeyboardlayout"; } - NSDictionary *defaults = @{@"speedValue": @(WantInitSpeedValue), - @"trackpad": @([UIDevice currentDevice].userInterfaceIdiom != UIUserInterfaceIdiomPad), - @"frameskip": @(0), - @"keyboardLayout": defaultKeyboardLayout - }; - [[NSUserDefaults standardUserDefaults] registerDefaults:defaults]; - - [self performSelector:@selector(runEmulator) withObject:nil afterDelay:0.1]; - return YES; + NSDictionary *defaultValues = @{@"trackpad": @([UIDevice currentDevice].userInterfaceIdiom != UIUserInterfaceIdiomPad), + @"frameskip": @(0), + @"keyboardLayout": defaultKeyboardLayout + }; + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + [defaults registerDefaults:defaultValues]; + [defaults setValue:@(WantInitSpeedValue) forKey:@"speedValue"]; + [defaults addObserver:self forKeyPath:@"speedValue" options:0 context:NULL]; +} + +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { + if (object == [NSUserDefaults standardUserDefaults]) { + if ([keyPath isEqualToString:@"speedValue"]) { + SpeedValue = [[NSUserDefaults standardUserDefaults] integerForKey:@"speedValue"]; + } + } } - (void)applicationDidEnterBackground:(UIApplication *)application { @@ -181,6 +193,9 @@ NSString * const MNVMDidEjectDiskNotification = @"MNVMDidEjectDisk"; - (void)runEmulator { SpeedValue = [[NSUserDefaults standardUserDefaults] integerForKey:@"speedValue"]; + if (SpeedValue > 3) { + SpeedValue = 3; + } RunEmulator(); } @@ -192,15 +207,6 @@ NSString * const MNVMDidEjectDiskNotification = @"MNVMDidEjectDisk"; SetSpeedStopped(emulatorRunning); } -- (EmulationSpeed)emulationSpeed { - return SpeedValue; -} - -- (void)setEmulationSpeed:(EmulationSpeed)emulationSpeed { - SpeedValue = emulationSpeed; - [[NSUserDefaults standardUserDefaults] setInteger:emulationSpeed forKey:@"speedValue"]; -} - #pragma mark - Mouse - (void)setMouseX:(NSInteger)x Y:(NSInteger)y { diff --git a/Mini vMac/Base.lproj/Main.storyboard b/Mini vMac/Base.lproj/Main.storyboard index 3ee6850..af4db76 100644 --- a/Mini vMac/Base.lproj/Main.storyboard +++ b/Mini vMac/Base.lproj/Main.storyboard @@ -64,8 +64,6 @@ - - diff --git a/Mini vMac/SettingsViewController.m b/Mini vMac/SettingsViewController.m index cf172ef..04d0976 100644 --- a/Mini vMac/SettingsViewController.m +++ b/Mini vMac/SettingsViewController.m @@ -33,14 +33,7 @@ - (IBAction)changeSpeed:(UISegmentedControl*)sender { if ([sender isKindOfClass:[UISegmentedControl class]]) { - EmulationSpeed speedValues[] = { - EmulationSpeed1x, - EmulationSpeed2x, - EmulationSpeed4x, - EmulationSpeed8x, - EmulationSpeed16x, - EmulationSpeedMax}; - [AppDelegate sharedInstance].emulationSpeed = speedValues[sender.selectedSegmentIndex]; + [[NSUserDefaults standardUserDefaults] setInteger:sender.selectedSegmentIndex forKey:@"speedValue"]; } } @@ -81,8 +74,7 @@ if (section == 0) { cell = [tableView dequeueReusableCellWithIdentifier:@"speed" forIndexPath:indexPath]; UISegmentedControl *speedControl = (UISegmentedControl*)[cell viewWithTag:128]; - EmulationSpeed speed = [AppDelegate sharedInstance].emulationSpeed; - speedControl.selectedSegmentIndex = speed == EmulationSpeedMax ? 5 : speed; + speedControl.selectedSegmentIndex = [defaults integerForKey:@"speedValue"]; } else if (section == 1) { cell = [tableView dequeueReusableCellWithIdentifier:@"mouse" forIndexPath:indexPath]; UISegmentedControl *mouseControl = (UISegmentedControl*)[cell viewWithTag:128];