cocoaTouch/SettingsViewController.m
changeset 2720 a5111ec4d25f
parent 2702 48fc46a922fd
child 2723 eaa6ac1e95ea
equal deleted inserted replaced
2719:0efcaa69d30c 2720:a5111ec4d25f
     9 #import "SettingsViewController.h"
     9 #import "SettingsViewController.h"
    10 #import "SDL_uikitappdelegate.h"
    10 #import "SDL_uikitappdelegate.h"
    11 
    11 
    12 @implementation SettingsViewController
    12 @implementation SettingsViewController
    13 
    13 
    14 @synthesize username, password, musicSwitch, soundsSwitch, altDamageSwitch, volumeSlider, volumeLabel, table, volumeCell;
    14 @synthesize username, password, musicSwitch, soundsSwitch, altDamageSwitch, 
       
    15 	    volumeSlider, volumeLabel, table, volumeCell, buttonContainer;
    15 
    16 
    16 
    17 
    17 -(void) loadView {
    18 -(void) loadView {
    18 	self.musicSwitch = [[UISwitch alloc] init];
    19 	self.musicSwitch = [[UISwitch alloc] init];
    19 	self.soundsSwitch = [[UISwitch alloc] init];
    20 	self.soundsSwitch = [[UISwitch alloc] init];
    25 }
    26 }
    26 
    27 
    27 -(void) viewDidLoad {
    28 -(void) viewDidLoad {
    28 	NSString *filePath = [[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"];
    29 	NSString *filePath = [[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"];
    29 	
    30 	
       
    31 	needsReset = NO;
    30 	if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {	
    32 	if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {	
    31 		NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
    33 		NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
    32 		username.text = [data objectForKey:@"username"];
    34 		username.text = [data objectForKey:@"username"];
    33 		password.text = [data objectForKey:@"password"];
    35 		password.text = [data objectForKey:@"password"];
    34 		if (1 == [[data objectForKey:@"music"] intValue]) {
    36 		if (1 == [[data objectForKey:@"music"] intValue]) {
    60 	username.textColor = [UIColor grayColor];
    62 	username.textColor = [UIColor grayColor];
    61 	password.textColor = [UIColor grayColor];
    63 	password.textColor = [UIColor grayColor];
    62 	volumeLabel.textColor = [UIColor grayColor];
    64 	volumeLabel.textColor = [UIColor grayColor];
    63 	table.backgroundColor = [UIColor clearColor];
    65 	table.backgroundColor = [UIColor clearColor];
    64 	table.allowsSelection = NO;
    66 	table.allowsSelection = NO;
       
    67 	buttonContainer.backgroundColor = [UIColor clearColor];
       
    68 	table.tableFooterView = buttonContainer;
       
    69 	
    65 	[super viewDidLoad];
    70 	[super viewDidLoad];
    66 }
    71 }
    67 
    72 
    68 -(void) viewDidUnload {
    73 -(void) viewDidUnload {
    69 	self.username = nil;
    74 	self.username = nil;
    73 	self.altDamageSwitch = nil;
    78 	self.altDamageSwitch = nil;
    74 	self.volumeLabel = nil;
    79 	self.volumeLabel = nil;
    75 	self.volumeSlider = nil;
    80 	self.volumeSlider = nil;
    76 	self.table = nil;
    81 	self.table = nil;
    77 	self.volumeCell = nil;
    82 	self.volumeCell = nil;
       
    83 	self.buttonContainer = nil;
    78 	[super viewDidUnload];
    84 	[super viewDidUnload];
    79 }
    85 }
    80 
    86 
    81 //- (void)applicationWillTerminate:(NSNotification *)notification {
    87 //- (void)applicationWillTerminate:(NSNotification *)notification {
    82 -(void) viewWillDisappear:(BOOL)animated {
    88 -(void) viewWillDisappear:(BOOL)animated {
    83 	NSMutableDictionary *saveDict = [[NSMutableDictionary alloc] init];
    89 	if (!needsReset) {
    84 	NSString *tmpMus = (musicSwitch.on) ? @"1" : @"0";
    90 		NSMutableDictionary *saveDict = [[NSMutableDictionary alloc] init];
    85 	NSString *tmpEff = (soundsSwitch.on) ? @"1" : @"0";
    91 		NSString *tmpMus = (musicSwitch.on) ? @"1" : @"0";
    86 	NSString *tmpAlt = (altDamageSwitch.on) ? @"1" : @"0";
    92 		NSString *tmpEff = (soundsSwitch.on) ? @"1" : @"0";
    87 	
    93 		NSString *tmpAlt = (altDamageSwitch.on) ? @"1" : @"0";
    88 	[saveDict setObject:username.text forKey:@"username"];
    94 		
    89 	[saveDict setObject:password.text forKey:@"password"];
    95 		[saveDict setObject:username.text forKey:@"username"];
    90 	[saveDict setObject:tmpMus forKey:@"music"];
    96 		[saveDict setObject:password.text forKey:@"password"];
    91 	[saveDict setObject:tmpEff forKey:@"sounds"];
    97 		[saveDict setObject:tmpMus forKey:@"music"];
    92 	[saveDict setObject:tmpAlt forKey:@"alternate"];
    98 		[saveDict setObject:tmpEff forKey:@"sounds"];
    93 	[saveDict setObject:volumeLabel.text forKey:@"volume"];
    99 		[saveDict setObject:tmpAlt forKey:@"alternate"];
    94 	
   100 		[saveDict setObject:volumeLabel.text forKey:@"volume"];
    95 	[saveDict writeToFile:[[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"] atomically:YES];
   101 		
    96 	[saveDict release];
   102 		[saveDict writeToFile:[[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"] atomically:YES];
       
   103 		[saveDict release];
       
   104 	}
    97 	[super viewWillDisappear:animated];
   105 	[super viewWillDisappear:animated];
    98 }
   106 }
    99 
   107 
   100 -(void) dealloc {
   108 -(void) dealloc {
   101 	[username release];
   109 	[username release];
   105 	[altDamageSwitch release];
   113 	[altDamageSwitch release];
   106 	[volumeLabel release];
   114 	[volumeLabel release];
   107 	[volumeSlider release];
   115 	[volumeSlider release];
   108 	[table release];
   116 	[table release];
   109 	[volumeCell release];
   117 	[volumeCell release];
       
   118 	[buttonContainer release];
   110 	[super dealloc];
   119 	[super dealloc];
   111 }
   120 }
   112 /*
   121 /*
   113 // Override to allow orientations other than the default portrait orientation.
   122 // Override to allow orientations other than the default portrait orientation.
   114 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
   123 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
   146 
   155 
   147 // don't enable music when sound is off
   156 // don't enable music when sound is off
   148 -(void) checkValueSwitch {
   157 -(void) checkValueSwitch {
   149 	if (NO == self.soundsSwitch.on) {
   158 	if (NO == self.soundsSwitch.on) {
   150 		[musicSwitch setOn:!musicSwitch.on animated:YES];
   159 		[musicSwitch setOn:!musicSwitch.on animated:YES];
       
   160 	}
       
   161 }
       
   162 
       
   163 #pragma mark -
       
   164 #pragma mark UIActionSheet Methods
       
   165 -(IBAction) deleteData: (id)sender {
       
   166 	UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Are you reeeeeally sure?"
       
   167 								 delegate:self
       
   168 							cancelButtonTitle:@"Well, maybe not..."
       
   169 						   destructiveButtonTitle:@"Sure, let's start over"
       
   170 							otherButtonTitles:nil];
       
   171 	[actionSheet showInView:self.view];
       
   172 	[actionSheet release];
       
   173 }
       
   174 
       
   175 -(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex {
       
   176 	if ([actionSheet cancelButtonIndex] != buttonIndex) {
       
   177 		needsReset = YES;
       
   178 		
       
   179 		// get the document dirctory
       
   180 		NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
       
   181 		NSString *documentsDirectory = [paths objectAtIndex:0];
       
   182 		
       
   183 		// get the content of the directory
       
   184 		NSFileManager *fm = [NSFileManager defaultManager];
       
   185 		NSArray *dirContent = [fm directoryContentsAtPath:documentsDirectory];
       
   186 		NSError *error;
       
   187 		
       
   188 		// delete data
       
   189 		for (NSString *fileName in dirContent) {
       
   190 			[fm removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:fileName] error:&error];
       
   191 		}
       
   192 		
       
   193 		// force resetting
       
   194 		UIAlertView *anAlert = [[UIAlertView alloc] initWithTitle:@"Hit Home Button to Exit" 
       
   195 								  message:@"\nEverything is gone!\nNow you need to restart the game..." 
       
   196 								 delegate:self
       
   197 							cancelButtonTitle:nil
       
   198 							otherButtonTitles:nil];
       
   199 		[anAlert show];
       
   200 		[anAlert release];
   151 	}
   201 	}
   152 }
   202 }
   153 
   203 
   154 #pragma mark -
   204 #pragma mark -
   155 #pragma mark TableView Methods
   205 #pragma mark TableView Methods
   245 	headerLabel.font = [UIFont boldSystemFontOfSize:20];
   295 	headerLabel.font = [UIFont boldSystemFontOfSize:20];
   246 	headerLabel.backgroundColor = [UIColor clearColor];
   296 	headerLabel.backgroundColor = [UIColor clearColor];
   247 
   297 
   248 	switch (section) {
   298 	switch (section) {
   249 		case kNetworkFields:
   299 		case kNetworkFields:
   250 			headerLabel.text = NSLocalizedString(@"Network Configuration", @"");
   300 			headerLabel.text = NSLocalizedString(@"Network Configuration", @"Network Configuration");
   251 			break;
   301 			break;
   252 		case kAudioFields:
   302 		case kAudioFields:
   253 			headerLabel.text = NSLocalizedString(@"Audio Preferences", @"");
   303 			headerLabel.text = NSLocalizedString(@"Audio Preferences", @"");
   254 			break;
   304 			break;
   255 		case kOtherFields:
   305 		case kOtherFields:
   271 	else
   321 	else
   272 		return table.rowHeight;
   322 		return table.rowHeight;
   273 }
   323 }
   274 
   324 
   275 -(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
   325 -(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
   276 	return 57;
   326 	return 57.0;
   277 }
   327 }
   278 
       
   279 
   328 
   280 @end
   329 @end