cocoaTouch/SettingsViewController.m
changeset 2743 39d097ac2276
parent 2740 03df0573a9fd
child 2799 558b29bf00c5
equal deleted inserted replaced
2742:21c0d2e69753 2743:39d097ac2276
    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, 
    14 @synthesize username, password, musicSwitch, soundsSwitch, altDamageSwitch, 
    15 	    volumeSlider, volumeLabel, table, volumeCell, buttonContainer;
    15 	    settingsTable, buttonContainer, parentView;
    16 
    16 
    17 
    17 
    18 -(void) loadView {
    18 -(void) loadView {
    19 	self.musicSwitch = [[UISwitch alloc] init];
    19 	self.musicSwitch = [[UISwitch alloc] init];
    20 	self.soundsSwitch = [[UISwitch alloc] init];
    20 	self.soundsSwitch = [[UISwitch alloc] init];
    46 		if (1 == [[data objectForKey:@"alternate"] intValue]) {
    46 		if (1 == [[data objectForKey:@"alternate"] intValue]) {
    47 			altDamageSwitch.on = YES;
    47 			altDamageSwitch.on = YES;
    48 		} else {
    48 		} else {
    49 			altDamageSwitch.on = NO;
    49 			altDamageSwitch.on = NO;
    50 		}		
    50 		}		
    51 		
       
    52 		[volumeSlider setValue:[[data objectForKey:@"volume"] intValue] animated:NO];
       
    53 		[data release];
    51 		[data release];
    54 	} else {
    52 	} else {
    55 		[NSException raise:@"File NOT found" format:@"The file settings.plist was not found at %@", filePath];
    53 		[NSException raise:@"File NOT found" format:@"The file settings.plist was not found at %@", filePath];
    56 	}
    54 	}
    57 	
    55 	
    58 	NSString *tmpVol = [[NSString alloc] initWithFormat:@"%d", (int) volumeSlider.value];
       
    59 	volumeLabel.text = tmpVol;
       
    60 	[tmpVol release];
       
    61 	
    56 	
    62 	username.textColor = [UIColor grayColor];
    57 	username.textColor = [UIColor grayColor];
    63 	password.textColor = [UIColor grayColor];
    58 	password.textColor = [UIColor grayColor];
    64 	volumeLabel.textColor = [UIColor grayColor];
    59 	settingsTable.backgroundColor = [UIColor clearColor];
    65 	table.backgroundColor = [UIColor clearColor];
    60 	settingsTable.allowsSelection = NO;
    66 	table.allowsSelection = NO;
       
    67 	buttonContainer.backgroundColor = [UIColor clearColor];
    61 	buttonContainer.backgroundColor = [UIColor clearColor];
    68 	table.tableFooterView = buttonContainer;
    62 	settingsTable.tableFooterView = buttonContainer;
    69 	
    63 	
    70 	[super viewDidLoad];
    64 	[super viewDidLoad];
    71 }
    65 }
    72 
    66 
    73 -(void) viewDidUnload {
    67 -(void) viewDidUnload {
    74 	self.username = nil;
    68 	self.username = nil;
    75 	self.password = nil;
    69 	self.password = nil;
    76 	self.musicSwitch = nil;
    70 	self.musicSwitch = nil;
    77 	self.soundsSwitch = nil;
    71 	self.soundsSwitch = nil;
    78 	self.altDamageSwitch = nil;
    72 	self.altDamageSwitch = nil;
    79 	self.volumeLabel = nil;
    73 	self.settingsTable = nil;
    80 	self.volumeSlider = nil;
       
    81 	self.table = nil;
       
    82 	self.volumeCell = nil;
       
    83 	self.buttonContainer = nil;
    74 	self.buttonContainer = nil;
    84 	[super viewDidUnload];
    75 	[super viewDidUnload];
    85 }
    76 }
    86 
    77 
    87 //- (void)applicationWillTerminate:(NSNotification *)notification {
    78 //- (void)applicationWillTerminate:(NSNotification *)notification {
    95 		[saveDict setObject:username.text forKey:@"username"];
    86 		[saveDict setObject:username.text forKey:@"username"];
    96 		[saveDict setObject:password.text forKey:@"password"];
    87 		[saveDict setObject:password.text forKey:@"password"];
    97 		[saveDict setObject:tmpMus forKey:@"music"];
    88 		[saveDict setObject:tmpMus forKey:@"music"];
    98 		[saveDict setObject:tmpEff forKey:@"sounds"];
    89 		[saveDict setObject:tmpEff forKey:@"sounds"];
    99 		[saveDict setObject:tmpAlt forKey:@"alternate"];
    90 		[saveDict setObject:tmpAlt forKey:@"alternate"];
   100 		[saveDict setObject:volumeLabel.text forKey:@"volume"];
       
   101 		
    91 		
   102 		[saveDict writeToFile:[[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"] atomically:YES];
    92 		[saveDict writeToFile:[[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"] atomically:YES];
   103 		[saveDict release];
    93 		[saveDict release];
   104 	}
    94 	}
   105 	[super viewWillDisappear:animated];
    95 	[super viewWillDisappear:animated];
   109 	[username release];
    99 	[username release];
   110 	[password release];
   100 	[password release];
   111 	[musicSwitch release];
   101 	[musicSwitch release];
   112 	[soundsSwitch release];
   102 	[soundsSwitch release];
   113 	[altDamageSwitch release];
   103 	[altDamageSwitch release];
   114 	[volumeLabel release];
   104 	[settingsTable release];
   115 	[volumeSlider release];
       
   116 	[table release];
       
   117 	[volumeCell release];
       
   118 	[buttonContainer release];
   105 	[buttonContainer release];
   119 	[super dealloc];
   106 	[super dealloc];
   120 }
   107 }
   121 
   108 
   122 // Override to allow orientations other than the default portrait orientation.
   109 // Override to allow orientations other than the default portrait orientation.
   133 }
   120 }
   134 
   121 
   135 // makes the keyboard go away when "Done" is tapped
   122 // makes the keyboard go away when "Done" is tapped
   136 -(IBAction) textFieldDoneEditing: (id)sender {
   123 -(IBAction) textFieldDoneEditing: (id)sender {
   137 	[sender resignFirstResponder];
   124 	[sender resignFirstResponder];
   138 }
       
   139 
       
   140 // update the value of the label when slider is updated
       
   141 -(IBAction) sliderChanged: (id) sender {
       
   142 	UISlider *slider = (UISlider *)sender;
       
   143 	int progress = slider.value;
       
   144 	NSString *newLabel = [[NSString alloc] initWithFormat:@"%d",progress];
       
   145 	self.volumeLabel.text = newLabel;
       
   146 	[newLabel release];
       
   147 }
   125 }
   148 
   126 
   149 // set music off when sound is turned off
   127 // set music off when sound is turned off
   150 -(void) sameValueSwitch {
   128 -(void) sameValueSwitch {
   151 	if (YES == self.musicSwitch.on) {
   129 	if (YES == self.musicSwitch.on) {
   174 	 */
   152 	 */
   175 	[UIView beginAnimations:@"Get Back" context:NULL];
   153 	[UIView beginAnimations:@"Get Back" context:NULL];
   176 	[UIView setAnimationDuration:3];
   154 	[UIView setAnimationDuration:3];
   177 	[UIView setAnimationDuration:UIViewAnimationCurveEaseOut];
   155 	[UIView setAnimationDuration:UIViewAnimationCurveEaseOut];
   178 	
   156 	
   179 	self.view.frame = CGRectMake(0, -480, 480, 320);
   157 	self.view.frame = CGRectMake(0, -320, 480, 320);
       
   158 	self.parentView.frame = CGRectMake(0, 0, 480, 320);
   180 	[UIView commitAnimations];
   159 	[UIView commitAnimations];
   181 	
   160 
   182 	[self.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:2];
   161 	[self.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:2];
       
   162 	self.parentView = nil;
   183 }
   163 }
   184 
   164 
   185 -(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex {
   165 -(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex {
   186 	if ([actionSheet cancelButtonIndex] != buttonIndex) {
   166 	if ([actionSheet cancelButtonIndex] != buttonIndex) {
   187 		needsReset = YES;
   167 		needsReset = YES;
   225 	switch (section) {
   205 	switch (section) {
   226 		case kNetworkFields:
   206 		case kNetworkFields:
   227 			return 2;
   207 			return 2;
   228 			break;
   208 			break;
   229 		case kAudioFields:
   209 		case kAudioFields:
   230 			return 3;
   210 			return 2;
   231 			break;
   211 			break;
   232 		case kOtherFields:
   212 		case kOtherFields:
   233 			return 1;
   213 			return 1;
   234 			break;
   214 			break;
   235 		default:
   215 		default:
   274 					cell.textLabel.text = NSLocalizedString(@"Sound", @"");
   254 					cell.textLabel.text = NSLocalizedString(@"Sound", @"");
   275 					break;
   255 					break;
   276 				case 1:
   256 				case 1:
   277 					cell.accessoryView = musicSwitch;
   257 					cell.accessoryView = musicSwitch;
   278 					cell.textLabel.text = NSLocalizedString(@"Music", @"");
   258 					cell.textLabel.text = NSLocalizedString(@"Music", @"");
   279 					break;
       
   280 				case 2:
       
   281 					cell = volumeCell;
       
   282 					break;
   259 					break;
   283 				default:
   260 				default:
   284 					NSLog(@"Warning: unset case value in kAudioFields section!");
   261 					NSLog(@"Warning: unset case value in kAudioFields section!");
   285 					break;
   262 					break;
   286 			}
   263 			}
   323 	
   300 	
   324 	[containerView addSubview:headerLabel];
   301 	[containerView addSubview:headerLabel];
   325 	return containerView;
   302 	return containerView;
   326 }
   303 }
   327 
   304 
       
   305 /*
   328 -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   306 -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   329 	if (kAudioFields == [indexPath section] && 2 == [indexPath row])
   307 	if (kAudioFields == [indexPath section] && 2 == [indexPath row])
   330 		return volumeCell.frame.size.height;
   308 		return volumeCell.frame.size.height;
   331 	else
   309 	else
   332 		return table.rowHeight;
   310 		return table.rowHeight;
   333 }
   311 }
       
   312 */
   334 
   313 
   335 -(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
   314 -(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
   336 	return 57.0;
   315 	return 57.0;
   337 }
   316 }
   338 
   317