cocoaTouch/SettingsViewController.m
changeset 2696 41aa7b56c17b
parent 2694 dcd248e04f3d
child 2698 90585aba87ad
equal deleted inserted replaced
2695:ed789a7ef68d 2696:41aa7b56c17b
     5 //  Created by Vittorio on 08/01/10.
     5 //  Created by Vittorio on 08/01/10.
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
     7 //
     7 //
     8 
     8 
     9 #import "SettingsViewController.h"
     9 #import "SettingsViewController.h"
    10 
    10 #import "SDL_uikitappdelegate.h"
    11 
    11 
    12 @implementation SettingsViewController
    12 @implementation SettingsViewController
    13 
    13 
    14 @synthesize username, password, musicOn, effectsOn, volumeSlider, volumeLabel;
    14 @synthesize username, password, musicOn, effectsOn, altDamageOn, volumeSlider, volumeLabel;
    15 
       
    16 -(NSString *)dataFilePath: (NSString *)fileName {
       
    17 	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
       
    18 	NSString *documentsDirectory = [paths objectAtIndex:0];
       
    19 	return [documentsDirectory stringByAppendingPathComponent:fileName];
       
    20 }
       
    21 
       
    22 
    15 
    23 -(void) viewDidLoad {
    16 -(void) viewDidLoad {
    24 	NSString *filePath = [self dataFilePath:@"settings.plist"];
    17 	NSString *filePath = [SDLUIKitDelegate dataFilePath:@"settings.plist"];
    25 	
    18 	
    26 	if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {	
    19 	if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {	
    27 		NSUserDefaults *data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
    20 		NSUserDefaults *data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
    28 		username.text = [data objectForKey:@"username"];
    21 		username.text = [data objectForKey:@"username"];
    29 		password.text = [data objectForKey:@"password"];
    22 		password.text = [data objectForKey:@"password"];
    35 		if (1 == [[data objectForKey:@"effects"] intValue]) {
    28 		if (1 == [[data objectForKey:@"effects"] intValue]) {
    36 			effectsOn.on = YES;
    29 			effectsOn.on = YES;
    37 		} else {
    30 		} else {
    38 			effectsOn.on = NO;
    31 			effectsOn.on = NO;
    39 		}
    32 		}
       
    33 		if (1 == [[data objectForKey:@"alternate"] intValue]) {
       
    34 			altDamageOn.on = YES;
       
    35 		} else {
       
    36 			altDamageOn.on = NO;
       
    37 		}		
       
    38 		
    40 		[volumeSlider setValue:[[data objectForKey:@"volume"] intValue] animated:NO];
    39 		[volumeSlider setValue:[[data objectForKey:@"volume"] intValue] animated:NO];
       
    40 		
    41 		NSString *tmpVol = [[NSString alloc] initWithFormat:@"%d", (int) volumeSlider.value];
    41 		NSString *tmpVol = [[NSString alloc] initWithFormat:@"%d", (int) volumeSlider.value];
    42 		volumeLabel.text = tmpVol;
    42 		volumeLabel.text = tmpVol;
    43 		[tmpVol release];
    43 		[tmpVol release];
       
    44 	} else {
       
    45 		[NSException raise:@"File NOT found" format:@"The file settings.plist was not found at %@", filePath];
    44 	}
    46 	}
    45 	
       
    46 	
       
    47 /*	
    47 /*	
    48 	UIApplication *app = [UIApplication sharedApplication];
    48 	UIApplication *app = [UIApplication sharedApplication];
    49 	[[NSNotificationCenter defaultCenter] addObserver:self
    49 	[[NSNotificationCenter defaultCenter] addObserver:self
    50 											 selector:@selector(applicationWillTerminate:)
    50 											 selector:@selector(applicationWillTerminate:)
    51 												 name:UIApplicationWillTerminateNotification
    51 												 name:UIApplicationWillTerminateNotification
    52 											   object:app];
    52 											   object:app];
    53 */	[super viewDidLoad];
    53 */
       
    54 	[super viewDidLoad];
    54 }
    55 }
    55 
    56 
    56 -(void) viewDidUnload {
    57 -(void) viewDidUnload {
    57 	self.username = nil;
    58 	self.username = nil;
    58 	self.password = nil;
    59 	self.password = nil;
    59 	self.musicOn = nil;
    60 	self.musicOn = nil;
    60 	self.effectsOn = nil;
    61 	self.effectsOn = nil;
       
    62 	self.altDamageOn = nil;
    61 	self.volumeLabel = nil;
    63 	self.volumeLabel = nil;
    62 	self.volumeSlider = nil;
    64 	self.volumeSlider = nil;
    63 	[super viewDidUnload];
    65 	[super viewDidUnload];
    64 }
    66 }
    65 
    67 
    66 //- (void)applicationWillTerminate:(NSNotification *)notification {
    68 //- (void)applicationWillTerminate:(NSNotification *)notification {
    67 -(void) viewWillDisappear:(BOOL)animated {
    69 -(void) viewWillDisappear:(BOOL)animated {
    68 	NSMutableDictionary *saveArray = [[NSMutableDictionary alloc] init];
    70 	NSMutableDictionary *saveArray = [[NSMutableDictionary alloc] init];
    69 	NSString *tmpMus = (musicOn.on) ? @"1" : @"0";
    71 	NSString *tmpMus = (musicOn.on) ? @"1" : @"0";
    70 	NSString *tmpEff = (effectsOn.on) ? @"1" : @"0";
    72 	NSString *tmpEff = (effectsOn.on) ? @"1" : @"0";
       
    73 	NSString *tmpAlt = (altDamageOn.on) ? @"1" : @"0";
    71 	
    74 	
    72 	[saveArray setObject:username.text forKey:@"username"];
    75 	[saveArray setObject:username.text forKey:@"username"];
    73 	[saveArray setObject:password.text forKey:@"password"];
    76 	[saveArray setObject:password.text forKey:@"password"];
    74 	[saveArray setObject:tmpMus forKey:@"music"];
    77 	[saveArray setObject:tmpMus forKey:@"music"];
    75 	[saveArray setObject:tmpEff forKey:@"effects"];
    78 	[saveArray setObject:tmpEff forKey:@"effects"];
       
    79 	[saveArray setObject:tmpAlt forKey:@"alternate"];
    76 	[saveArray setObject:volumeLabel.text forKey:@"volume"];
    80 	[saveArray setObject:volumeLabel.text forKey:@"volume"];
    77 	
    81 	
    78 	[saveArray writeToFile:[self dataFilePath:@"settings.plist"] atomically:YES];
    82 	[saveArray writeToFile:[SDLUIKitDelegate dataFilePath:@"settings.plist"] atomically:YES];
    79 	[saveArray release];
    83 	[saveArray release];
    80 	[super viewWillDisappear:animated];
    84 	[super viewWillDisappear:animated];
    81 }
    85 }
    82  
    86  
    83 /*
    87 /*
    86     // Return YES for supported orientations
    90     // Return YES for supported orientations
    87     return (interfaceOrientation == UIInterfaceOrientationPortrait);
    91     return (interfaceOrientation == UIInterfaceOrientationPortrait);
    88 }
    92 }
    89 */
    93 */
    90 
    94 
       
    95 // makes the keyboard go away when background is tapped
    91 -(IBAction) backgroundTap: (id)sender {
    96 -(IBAction) backgroundTap: (id)sender {
    92 	[username resignFirstResponder];
    97 	[username resignFirstResponder];
    93 	[password resignFirstResponder];
    98 	[password resignFirstResponder];
    94 }
    99 }
    95 
   100 
       
   101 // makes the keyboard go away when "Done" is tapped
    96 -(IBAction) textFieldDoneEditing: (id)sender {
   102 -(IBAction) textFieldDoneEditing: (id)sender {
    97 	[sender resignFirstResponder];
   103 	[sender resignFirstResponder];
    98 }
   104 }
    99 
   105 
   100 -(IBAction) sliderChanged: (id) sender {
   106 -(IBAction) sliderChanged: (id) sender {
   108 -(void) dealloc {
   114 -(void) dealloc {
   109 	[username release];
   115 	[username release];
   110 	[password release];
   116 	[password release];
   111 	[musicOn release];
   117 	[musicOn release];
   112 	[effectsOn release];
   118 	[effectsOn release];
       
   119 	[altDamageOn release];
   113 	[volumeLabel release];
   120 	[volumeLabel release];
   114 	[volumeSlider release];
   121 	[volumeSlider release];
   115     [super dealloc];
   122     [super dealloc];
   116 }
   123 }
   117 
   124