cocoaTouch/SettingsViewController.m
changeset 2701 3a8560c00f78
parent 2698 90585aba87ad
child 2702 48fc46a922fd
equal deleted inserted replaced
2700:726e4a230b48 2701:3a8560c00f78
     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, musicOn, effectsOn, altDamageOn, volumeSlider, volumeLabel;
    14 @synthesize username, password, musicSwitch, effectsSwitch, altDamageSwitch, volumeSlider, volumeLabel, table, volumeCell;
       
    15 
       
    16 
       
    17 -(void) loadView {
       
    18 	self.musicSwitch = [[UISwitch alloc] init];
       
    19 	self.effectsSwitch = [[UISwitch alloc] init];
       
    20 	self.altDamageSwitch = [[UISwitch alloc] init];
       
    21 	[super loadView];
       
    22 }
    15 
    23 
    16 -(void) viewDidLoad {
    24 -(void) viewDidLoad {
    17 	NSString *filePath = [[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"];
    25 	NSString *filePath = [[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"];
    18 	
    26 	
    19 	if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {	
    27 	if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {	
    20 		NSUserDefaults *data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
    28 		NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
    21 		username.text = [data objectForKey:@"username"];
    29 		username.text = [data objectForKey:@"username"];
    22 		password.text = [data objectForKey:@"password"];
    30 		password.text = [data objectForKey:@"password"];
    23 		if (1 == [[data objectForKey:@"music"] intValue]) {
    31 		if (1 == [[data objectForKey:@"music"] intValue]) {
    24 			musicOn.on = YES;
    32 			musicSwitch.on = YES;
    25 		} else {
    33 		} else {
    26 			musicOn.on = NO;
    34 			musicSwitch.on = NO;
    27 		}
    35 		}
    28 		if (1 == [[data objectForKey:@"effects"] intValue]) {
    36 		if (1 == [[data objectForKey:@"effects"] intValue]) {
    29 			effectsOn.on = YES;
    37 			effectsSwitch.on = YES;
    30 		} else {
    38 		} else {
    31 			effectsOn.on = NO;
    39 			effectsSwitch.on = NO;
    32 		}
    40 		}
    33 		if (1 == [[data objectForKey:@"alternate"] intValue]) {
    41 		if (1 == [[data objectForKey:@"alternate"] intValue]) {
    34 			altDamageOn.on = YES;
    42 			altDamageSwitch.on = YES;
    35 		} else {
    43 		} else {
    36 			altDamageOn.on = NO;
    44 			altDamageSwitch.on = NO;
    37 		}		
    45 		}		
    38 		
    46 		
    39 		[volumeSlider setValue:[[data objectForKey:@"volume"] intValue] animated:NO];
    47 		[volumeSlider setValue:[[data objectForKey:@"volume"] intValue] animated:NO];
    40 		
    48 		[data release];
    41 		NSString *tmpVol = [[NSString alloc] initWithFormat:@"%d", (int) volumeSlider.value];
       
    42 		volumeLabel.text = tmpVol;
       
    43 		[tmpVol release];
       
    44 	} else {
    49 	} else {
    45 		[NSException raise:@"File NOT found" format:@"The file settings.plist was not found at %@", filePath];
    50 		[NSException raise:@"File NOT found" format:@"The file settings.plist was not found at %@", filePath];
    46 	}
    51 	}
    47 /*	
    52 	
    48 	UIApplication *app = [UIApplication sharedApplication];
    53 	NSString *tmpVol = [[NSString alloc] initWithFormat:@"%d", (int) volumeSlider.value];
    49 	[[NSNotificationCenter defaultCenter] addObserver:self
    54 	volumeLabel.text = tmpVol;
    50 											 selector:@selector(applicationWillTerminate:)
    55 	[tmpVol release];
    51 												 name:UIApplicationWillTerminateNotification
    56 	
    52 											   object:app];
    57 	username.textColor = [UIColor grayColor];
    53 */
    58 	password.textColor = [UIColor grayColor];
       
    59 	volumeLabel.textColor = [UIColor grayColor];
       
    60 	table.backgroundColor = [UIColor clearColor];
       
    61 	table.allowsSelection = NO;
    54 	[super viewDidLoad];
    62 	[super viewDidLoad];
    55 }
    63 }
    56 
    64 
    57 -(void) viewDidUnload {
    65 -(void) viewDidUnload {
    58 	self.username = nil;
    66 	self.username = nil;
    59 	self.password = nil;
    67 	self.password = nil;
    60 	self.musicOn = nil;
    68 	self.musicSwitch = nil;
    61 	self.effectsOn = nil;
    69 	self.effectsSwitch = nil;
    62 	self.altDamageOn = nil;
    70 	self.altDamageSwitch = nil;
    63 	self.volumeLabel = nil;
    71 	self.volumeLabel = nil;
    64 	self.volumeSlider = nil;
    72 	self.volumeSlider = nil;
       
    73 	self.table = nil;
       
    74 	self.volumeCell = nil;
    65 	[super viewDidUnload];
    75 	[super viewDidUnload];
    66 }
    76 }
    67 
    77 
    68 //- (void)applicationWillTerminate:(NSNotification *)notification {
    78 //- (void)applicationWillTerminate:(NSNotification *)notification {
    69 -(void) viewWillDisappear:(BOOL)animated {
    79 -(void) viewWillDisappear:(BOOL)animated {
    70 	NSMutableDictionary *saveArray = [[NSMutableDictionary alloc] init];
    80 	NSMutableDictionary *saveDict = [[NSMutableDictionary alloc] init];
    71 	NSString *tmpMus = (musicOn.on) ? @"1" : @"0";
    81 	NSString *tmpMus = (musicSwitch.on) ? @"1" : @"0";
    72 	NSString *tmpEff = (effectsOn.on) ? @"1" : @"0";
    82 	NSString *tmpEff = (effectsSwitch.on) ? @"1" : @"0";
    73 	NSString *tmpAlt = (altDamageOn.on) ? @"1" : @"0";
    83 	NSString *tmpAlt = (altDamageSwitch.on) ? @"1" : @"0";
    74 	
    84 	
    75 	[saveArray setObject:username.text forKey:@"username"];
    85 	[saveDict setObject:username.text forKey:@"username"];
    76 	[saveArray setObject:password.text forKey:@"password"];
    86 	[saveDict setObject:password.text forKey:@"password"];
    77 	[saveArray setObject:tmpMus forKey:@"music"];
    87 	[saveDict setObject:tmpMus forKey:@"music"];
    78 	[saveArray setObject:tmpEff forKey:@"effects"];
    88 	[saveDict setObject:tmpEff forKey:@"effects"];
    79 	[saveArray setObject:tmpAlt forKey:@"alternate"];
    89 	[saveDict setObject:tmpAlt forKey:@"alternate"];
    80 	[saveArray setObject:volumeLabel.text forKey:@"volume"];
    90 	[saveDict setObject:volumeLabel.text forKey:@"volume"];
    81 	
    91 	
    82 	[saveArray writeToFile:[[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"] atomically:YES];
    92 	[saveDict writeToFile:[[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"] atomically:YES];
    83 	[saveArray release];
    93 	[saveDict release];
    84 	[super viewWillDisappear:animated];
    94 	[super viewWillDisappear:animated];
    85 }
    95 }
    86  
    96  
    87 /*
    97 /*
    88 // Override to allow orientations other than the default portrait orientation.
    98 // Override to allow orientations other than the default portrait orientation.
   112 }
   122 }
   113 
   123 
   114 -(void) dealloc {
   124 -(void) dealloc {
   115 	[username release];
   125 	[username release];
   116 	[password release];
   126 	[password release];
   117 	[musicOn release];
   127 	[musicSwitch release];
   118 	[effectsOn release];
   128 	[effectsSwitch release];
   119 	[altDamageOn release];
   129 	[altDamageSwitch release];
   120 	[volumeLabel release];
   130 	[volumeLabel release];
   121 	[volumeSlider release];
   131 	[volumeSlider release];
   122     [super dealloc];
   132 	[table release];
       
   133 	[volumeCell release];
       
   134 	[super dealloc];
       
   135 }
       
   136 
       
   137 #pragma mark -
       
   138 #pragma mark TableView Methods
       
   139 #define kNetworkFields 0
       
   140 #define kAudioFields 1
       
   141 #define kOtherFields 2
       
   142 
       
   143 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
       
   144 	return 3;
       
   145 }
       
   146 
       
   147 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
       
   148 	switch (section) {
       
   149 		case kNetworkFields:
       
   150 			return 2;
       
   151 			break;
       
   152 		case kAudioFields:
       
   153 			return 3;
       
   154 			break;
       
   155 		case kOtherFields:
       
   156 			return 1;
       
   157 			break;
       
   158 		default:
       
   159 			NSLog(@"Warning: unset case value for numberOfRowsInSection!");
       
   160 			break;
       
   161 	}
       
   162 	return 0;
       
   163 }
       
   164 
       
   165 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       
   166 	static NSString *cellIdentifier = @"systemSettingsCell";
       
   167 	
       
   168 	UITableViewCell *cell;
       
   169 	if ( !(kAudioFields == [indexPath section] && 2 == [indexPath row]) ){
       
   170 		cell = [aTableView dequeueReusableCellWithIdentifier:cellIdentifier];
       
   171 		if (nil == cell) {
       
   172 			cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
       
   173 						       reuseIdentifier:cellIdentifier] autorelease];
       
   174 		}
       
   175 	}
       
   176 	
       
   177 	switch ([indexPath section]) {
       
   178 		case kNetworkFields:
       
   179 			switch ([indexPath row]) {
       
   180 				case 0:
       
   181 					cell.textLabel.text = NSLocalizedString(@"Nickname", @"");
       
   182 					cell.accessoryView = username;
       
   183 					break;
       
   184 				case 1:
       
   185 					cell.textLabel.text = NSLocalizedString(@"Password", @"");
       
   186 					cell.accessoryView = password;
       
   187 					break;
       
   188 				default:
       
   189 					NSLog(@"Warning: unset case value in kNetworkFields section!");
       
   190 					break;
       
   191 			}
       
   192 			break;
       
   193 		case kAudioFields:
       
   194 			switch ([indexPath row]) {
       
   195 				case 0:
       
   196 					cell.accessoryView = musicSwitch;
       
   197 					cell.textLabel.text = NSLocalizedString(@"Music", @"");
       
   198 					break;
       
   199 				case 1:
       
   200 					cell.accessoryView = effectsSwitch;
       
   201 					cell.textLabel.text = NSLocalizedString(@"Sound Effects", @"");
       
   202 					break;
       
   203 				case 2:
       
   204 					cell = volumeCell;
       
   205 					break;
       
   206 				default:
       
   207 					NSLog(@"Warning: unset case value in kAudioFields section!");
       
   208 					break;
       
   209 			}
       
   210 			break;
       
   211 		case kOtherFields:
       
   212 			cell.accessoryView = altDamageSwitch;
       
   213 			cell.textLabel.text = NSLocalizedString(@"Alternate Damage", @"");
       
   214 			break;
       
   215 		default:
       
   216 			break;
       
   217 	}
       
   218 	
       
   219 	return cell;
       
   220 }
       
   221 
       
   222 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
       
   223 	UIView *containerView =	[[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)] autorelease];
       
   224 	UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 40)] autorelease];
       
   225 	headerLabel.textColor = [UIColor lightGrayColor];
       
   226 	headerLabel.shadowColor = [UIColor blackColor];
       
   227 	headerLabel.shadowOffset = CGSizeMake(0, 1);
       
   228 	headerLabel.font = [UIFont boldSystemFontOfSize:20];
       
   229 	headerLabel.backgroundColor = [UIColor clearColor];
       
   230 
       
   231 	switch (section) {
       
   232 		case kNetworkFields:
       
   233 			headerLabel.text = NSLocalizedString(@"Network Configuration", @"");
       
   234 			break;
       
   235 		case kAudioFields:
       
   236 			headerLabel.text = NSLocalizedString(@"Audio Preferences", @"");
       
   237 			break;
       
   238 		case kOtherFields:
       
   239 			headerLabel.text = NSLocalizedString(@"Other Settings", @"");
       
   240 			break;
       
   241 		default:
       
   242 			NSLog(@"Warning: unset case value in titleForHeaderInSection!");
       
   243 			headerLabel.text = @"!!!";
       
   244 			break;
       
   245 	}
       
   246 	
       
   247 	[containerView addSubview:headerLabel];
       
   248 	return containerView;
       
   249 }
       
   250 
       
   251 -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
       
   252 	if (kAudioFields == [indexPath section] && 2 == [indexPath row])
       
   253 		return volumeCell.frame.size.height;
       
   254 	else
       
   255 		return table.rowHeight;
       
   256 }
       
   257 
       
   258 -(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
       
   259 	return 57;
   123 }
   260 }
   124 
   261 
   125 
   262 
   126 @end
   263 @end