project_files/HedgewarsMobile/Classes/GeneralSettingsViewController.m
changeset 3546 ccf4854df294
parent 3514 59dbd31e9953
child 3547 02875b1145b7
equal deleted inserted replaced
3545:b07ee704f35d 3546:ccf4854df294
     1 //
       
     2 //  SettingsViewController.m
       
     3 //  hwengine
       
     4 //
       
     5 //  Created by Vittorio on 08/01/10.
       
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
       
     7 //
       
     8 
       
     9 #import "GeneralSettingsViewController.h"
       
    10 #import "CommodityFunctions.h"
       
    11 
       
    12 @implementation GeneralSettingsViewController
       
    13 @synthesize settingsDictionary, textFieldBeingEdited, musicSwitch, soundSwitch, altDamageSwitch;
       
    14 
       
    15 
       
    16 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
       
    17 	return rotationManager(interfaceOrientation);
       
    18 }
       
    19 
       
    20 
       
    21 #pragma mark -
       
    22 #pragma mark textfield methods
       
    23 // return to previous table
       
    24 -(void) cancel:(id) sender {
       
    25     if (textFieldBeingEdited != nil)
       
    26         [self.textFieldBeingEdited resignFirstResponder];
       
    27 }
       
    28 
       
    29 // set the new value
       
    30 -(BOOL) save:(id) sender {
       
    31     if (textFieldBeingEdited != nil) {
       
    32         if (textFieldBeingEdited.tag == 0) {
       
    33             [self.settingsDictionary setObject:textFieldBeingEdited.text forKey:@"username"];
       
    34         } else {
       
    35             [self.settingsDictionary setObject:textFieldBeingEdited.text forKey:@"password"];
       
    36         }
       
    37         
       
    38         isWriteNeeded = YES;
       
    39         [self.textFieldBeingEdited resignFirstResponder];
       
    40         return YES;
       
    41     }
       
    42     return NO;
       
    43 }
       
    44 
       
    45 // the textfield is being modified, update the navigation controller
       
    46 -(void) textFieldDidBeginEditing:(UITextField *)aTextField{   
       
    47     self.textFieldBeingEdited = aTextField;
       
    48     UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"from the settings table")
       
    49                                                                      style:UIBarButtonItemStylePlain
       
    50                                                                     target:self
       
    51                                                                     action:@selector(cancel:)];
       
    52     self.navigationItem.leftBarButtonItem = cancelButton;
       
    53     [cancelButton release];
       
    54     
       
    55     UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save",@"from the settings table")
       
    56                                                                      style:UIBarButtonItemStyleDone
       
    57                                                                     target:self
       
    58                                                                     action:@selector(save:)];
       
    59     self.navigationItem.rightBarButtonItem = saveButton;
       
    60     [saveButton release];
       
    61 }
       
    62 
       
    63 // the textfield has been modified, check for empty strings and restore original navigation bar
       
    64 -(void) textFieldDidEndEditing:(UITextField *)aTextField{
       
    65     self.textFieldBeingEdited = nil;
       
    66     self.navigationItem.rightBarButtonItem = self.navigationItem.backBarButtonItem;
       
    67     self.navigationItem.leftBarButtonItem = nil;
       
    68 }
       
    69 
       
    70 // limit the size of the field to 64 characters like in original frontend
       
    71 -(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
       
    72     int limit = 64;
       
    73     return !([textField.text length] > limit && [string length] > range.length);
       
    74 }
       
    75 
       
    76 
       
    77 #pragma mark -
       
    78 #pragma mark View Lifecycle
       
    79 -(void) viewDidLoad {
       
    80 	[super viewDidLoad];
       
    81     self.musicSwitch = [[UISwitch alloc] init];
       
    82 	self.soundSwitch = [[UISwitch alloc] init];
       
    83 	self.altDamageSwitch = [[UISwitch alloc] init];
       
    84 	[self.soundSwitch addTarget:self action:@selector(alsoTurnOffMusic:) forControlEvents:UIControlEventValueChanged];
       
    85 	[self.musicSwitch addTarget:self action:@selector(dontTurnOnMusic:) forControlEvents:UIControlEventValueChanged];
       
    86 	[self.altDamageSwitch addTarget:self action:@selector(justUpdateDictionary:) forControlEvents:UIControlEventValueChanged];
       
    87     
       
    88     NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()];
       
    89     self.settingsDictionary = dictionary;
       
    90     [dictionary release];
       
    91 }
       
    92 
       
    93 -(void) viewWillAppear:(BOOL)animated {
       
    94     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
       
    95     isWriteNeeded = NO;
       
    96     
       
    97     musicSwitch.on = [[settingsDictionary objectForKey:@"music"] boolValue];
       
    98     soundSwitch.on = [[settingsDictionary objectForKey:@"sound"] boolValue];
       
    99     altDamageSwitch.on = [[settingsDictionary objectForKey:@"alternate"] boolValue];
       
   100     
       
   101     [super viewWillAppear:animated];
       
   102 }
       
   103 
       
   104 -(void) viewWillDisappear:(BOOL)animated {
       
   105     [super viewWillDisappear:animated];
       
   106     
       
   107     if (isWriteNeeded) {
       
   108        	NSLog(@"writing preferences to file");
       
   109         [self.settingsDictionary writeToFile:SETTINGS_FILE() atomically:YES];
       
   110         isWriteNeeded = NO;
       
   111     }
       
   112 }
       
   113 
       
   114 #pragma mark -
       
   115 // if the sound system is off, turn off also the background music 
       
   116 -(void) alsoTurnOffMusic:(id) sender {
       
   117     [self.settingsDictionary setObject:[NSNumber numberWithBool:soundSwitch.on] forKey:@"sound"];
       
   118 	if (YES == self.musicSwitch.on) {
       
   119 		[musicSwitch setOn:NO animated:YES];
       
   120         [self.settingsDictionary setObject:[NSNumber numberWithBool:musicSwitch.on] forKey:@"music"];
       
   121 	}
       
   122     isWriteNeeded = YES;
       
   123 }
       
   124 
       
   125 // if the sound system is off, don't enable background music 
       
   126 -(void) dontTurnOnMusic:(id) sender {
       
   127 	if (NO == self.soundSwitch.on) {
       
   128 		[musicSwitch setOn:NO animated:YES];
       
   129 	} else {
       
   130         [self.settingsDictionary setObject:[NSNumber numberWithBool:musicSwitch.on] forKey:@"music"];
       
   131         isWriteNeeded = YES;
       
   132     }
       
   133 }
       
   134 
       
   135 -(void) justUpdateDictionary:(id) sender {
       
   136     UISwitch *theSwitch = (UISwitch *)sender;
       
   137     [self.settingsDictionary setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"alternate"];
       
   138     isWriteNeeded = YES;
       
   139 }
       
   140 
       
   141 /*
       
   142 #pragma mark -
       
   143 #pragma mark UIActionSheet Methods
       
   144 -(IBAction) deleteData: (id)sender {
       
   145 	UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Are you reeeeeally sure?", @"")
       
   146 								 delegate:self
       
   147 							cancelButtonTitle:NSLocalizedString(@"Well, maybe not...", @"")
       
   148 						   destructiveButtonTitle:NSLocalizedString(@"As sure as I can be!", @"")
       
   149 							otherButtonTitles:nil];
       
   150 	[actionSheet showInView:self.view];
       
   151 	[actionSheet release];
       
   152 }
       
   153 
       
   154 -(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex {
       
   155 	if ([actionSheet cancelButtonIndex] != buttonIndex) {
       
   156 		// get the documents dirctory
       
   157 		NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
       
   158 		NSString *documentsDirectory = [paths objectAtIndex:0];
       
   159 		
       
   160 		// get the content of the directory
       
   161 		NSFileManager *fm = [NSFileManager defaultManager];
       
   162 		NSArray *dirContent = [fm directoryContentsAtPath:documentsDirectory];
       
   163 		NSError *error;
       
   164 		
       
   165 		// delete data
       
   166 		for (NSString *fileName in dirContent) {
       
   167 			[fm removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:fileName] error:&error];
       
   168 		}
       
   169 		
       
   170 		// force resetting
       
   171 		UIAlertView *anAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Hit Home Button to Exit", @"")
       
   172 								  message:NSLocalizedString(@"\nEverything is gone!\nNow you need to restart the game...", @"")
       
   173 								 delegate:self
       
   174 							cancelButtonTitle:nil
       
   175 							otherButtonTitles:nil];
       
   176 		[anAlert show];
       
   177 		[anAlert release];
       
   178 	}
       
   179 }
       
   180 */
       
   181 
       
   182 #pragma mark -
       
   183 #pragma mark TableView Methods
       
   184 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
       
   185 	return 3;
       
   186 }
       
   187 
       
   188 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
       
   189 	switch (section) {
       
   190 		case kNetworkFields:
       
   191 			return 2;
       
   192 			break;
       
   193 		case kAudioFields:
       
   194 			return 2;
       
   195 			break;
       
   196 		case kOtherFields:
       
   197 			return 1;
       
   198 			break;
       
   199 		default:
       
   200 			break;
       
   201 	}
       
   202 	return 0;
       
   203 }
       
   204 
       
   205 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       
   206 	static NSString *cellIdentifier = @"systemSettingsCell";
       
   207 	NSInteger row = [indexPath row];
       
   208     NSInteger section = [indexPath section];
       
   209     
       
   210 	UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:cellIdentifier];
       
   211     if (nil == cell) {
       
   212         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
       
   213         if (section == kNetworkFields) {
       
   214             UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(-15, 10, 100, 25)];
       
   215             label.textAlignment = UITextAlignmentRight;
       
   216             label.backgroundColor = [UIColor clearColor];
       
   217             label.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize] + 2];
       
   218             if (row == 0) 
       
   219                 label.text = NSLocalizedString(@"Nickname","from the settings table");
       
   220             else 
       
   221                 label.text = NSLocalizedString(@"Password","from the settings table");
       
   222             [cell.contentView addSubview:label];
       
   223             [label release];
       
   224             
       
   225             UITextField *aTextField = [[UITextField alloc] initWithFrame:
       
   226                                        CGRectMake(110, 12, (cell.frame.size.width + cell.frame.size.width/3) - 90, 25)];
       
   227             aTextField.clearsOnBeginEditing = NO;
       
   228             aTextField.returnKeyType = UIReturnKeyDone;
       
   229             aTextField.adjustsFontSizeToFitWidth = YES;
       
   230             aTextField.delegate = self;
       
   231             aTextField.tag = row;
       
   232             aTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
       
   233             [aTextField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit];
       
   234             [cell.contentView addSubview:aTextField];
       
   235             [aTextField release];
       
   236         }
       
   237     }
       
   238     
       
   239     cell.accessoryType = UITableViewCellAccessoryNone;
       
   240     cell.selectionStyle = UITableViewCellSelectionStyleNone;
       
   241     cell.imageView.image = nil;
       
   242     
       
   243     UITextField *aTextField;
       
   244     switch (section) {
       
   245         case kNetworkFields:
       
   246             for (UIView *oneView in cell.contentView.subviews) 
       
   247                 if ([oneView isMemberOfClass:[UITextField class]]) 
       
   248                     aTextField = (UITextField *)oneView;
       
   249 
       
   250             switch (row) {
       
   251 				case 0:                    
       
   252                     aTextField.placeholder = NSLocalizedString(@"Insert your username (if you have one)",@"");
       
   253                     aTextField.text = [self.settingsDictionary objectForKey:@"username"];
       
   254 					break;
       
   255 				case 1:                    
       
   256                     aTextField.placeholder = NSLocalizedString(@"Insert your password",@"");
       
   257                     aTextField.text = [self.settingsDictionary objectForKey:@"password"];
       
   258                     aTextField.secureTextEntry = YES;
       
   259 					break;
       
   260 				default:
       
   261 					break;
       
   262 			}
       
   263 			break;
       
   264             
       
   265 		case kAudioFields:
       
   266 			switch (row) {
       
   267 				case 0:
       
   268 					cell.textLabel.text = NSLocalizedString(@"Sound", @"");
       
   269 					cell.accessoryView = soundSwitch;
       
   270 					break;
       
   271 				case 1:
       
   272 					cell.textLabel.text = NSLocalizedString(@"Music", @"");
       
   273 					cell.accessoryView = musicSwitch;
       
   274 					break;
       
   275 				default:
       
   276 					break;
       
   277 			}
       
   278 			break;
       
   279             
       
   280 		case kOtherFields:
       
   281             cell.selectionStyle = UITableViewCellSelectionStyleNone;
       
   282 			cell.textLabel.text = NSLocalizedString(@"Alternate Damage", @"");
       
   283 			cell.accessoryView = altDamageSwitch;
       
   284 			break;
       
   285 		default:
       
   286 			break;
       
   287 	}
       
   288 	return cell;
       
   289 }
       
   290 
       
   291 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
       
   292     NSString *sectionTitle = nil;
       
   293     switch (section) {
       
   294 		case kNetworkFields:
       
   295 			sectionTitle = NSLocalizedString(@"Network Configuration", @"");
       
   296 			break;
       
   297 		case kAudioFields:
       
   298 			sectionTitle = NSLocalizedString(@"Audio Preferences", @"");
       
   299 			break;
       
   300 		case kOtherFields:
       
   301 			sectionTitle = NSLocalizedString(@"Other Settings", @"");
       
   302 			break;
       
   303 		default:
       
   304 			NSLog(@"Nope");
       
   305 			break;
       
   306 	}
       
   307     return sectionTitle;
       
   308 }
       
   309 
       
   310 /*
       
   311 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
       
   312 	UIView *containerView =	[[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)] autorelease];
       
   313 	UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 40)] autorelease];
       
   314 	headerLabel.textColor = [UIColor lightGrayColor];
       
   315 	headerLabel.shadowColor = [UIColor blackColor];
       
   316 	headerLabel.shadowOffset = CGSizeMake(0, 1);
       
   317 	headerLabel.font = [UIFont boldSystemFontOfSize:20];
       
   318 	headerLabel.backgroundColor = [UIColor clearColor];
       
   319 
       
   320 	switch (section) {
       
   321 		case kNetworkFields:
       
   322 			headerLabel.text = NSLocalizedString(@"Network Configuration", @"");
       
   323 			break;
       
   324 		case kAudioFields:
       
   325 			headerLabel.text = NSLocalizedString(@"Audio Preferences", @"");
       
   326 			break;
       
   327 		case kOtherFields:
       
   328 			headerLabel.text = NSLocalizedString(@"Other Settings", @"");
       
   329 			break;
       
   330 		default:
       
   331 			NSLog(@"Warning: unset case value in titleForHeaderInSection!");
       
   332 			headerLabel.text = @"!";
       
   333 			break;
       
   334 	}
       
   335 	
       
   336 	[containerView addSubview:headerLabel];
       
   337 	return containerView;
       
   338 }
       
   339 
       
   340 -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
       
   341 	if (kAudioFields == [indexPath section] && 2 == [indexPath row])
       
   342 		return volumeCell.frame.size.height;
       
   343 	else
       
   344 		return table.rowHeight;
       
   345 }
       
   346 
       
   347 -(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
       
   348 	return 57.0;
       
   349 }
       
   350 */
       
   351 
       
   352 
       
   353 #pragma mark -
       
   354 #pragma mark Table view delegate
       
   355 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       
   356     UITableViewCell *cell;
       
   357     if (kNetworkFields == [indexPath section]) {
       
   358         cell = [aTableView cellForRowAtIndexPath:indexPath];
       
   359         for (UIView *oneView in cell.contentView.subviews) {
       
   360             if ([oneView isMemberOfClass:[UITextField class]]) {
       
   361                 textFieldBeingEdited = (UITextField *)oneView;
       
   362                 [textFieldBeingEdited becomeFirstResponder];
       
   363             }
       
   364         }
       
   365         [aTableView deselectRowAtIndexPath:indexPath animated:NO];
       
   366     }
       
   367 }
       
   368 
       
   369 
       
   370 #pragma mark -
       
   371 #pragma mark Memory management
       
   372 -(void) didReceiveMemoryWarning {
       
   373     // Releases the view if it doesn't have a superview.
       
   374     [super didReceiveMemoryWarning];
       
   375     // Relinquish ownership any cached data, images, etc that aren't in use.
       
   376 }
       
   377 
       
   378 -(void) viewDidUnload {
       
   379     self.settingsDictionary = nil;
       
   380 	self.textFieldBeingEdited = nil;
       
   381 	self.musicSwitch = nil;
       
   382 	self.soundSwitch = nil;
       
   383 	self.altDamageSwitch = nil;
       
   384 	[super viewDidUnload];
       
   385     MSG_DIDUNLOAD();
       
   386 }
       
   387 
       
   388 -(void) dealloc {
       
   389     [settingsDictionary release];
       
   390 	[textFieldBeingEdited release];
       
   391 	[musicSwitch release];
       
   392 	[soundSwitch release];
       
   393 	[altDamageSwitch release];
       
   394 	[super dealloc];
       
   395 }
       
   396 
       
   397 
       
   398 @end