project_files/HedgewarsMobile/Classes/GeneralSettingsViewController.m
changeset 5206 db775bddf771
parent 4976 088d40d8aba2
child 5207 4c9ae0f484da
equal deleted inserted replaced
5205:78138ae93820 5206:db775bddf771
    21 
    21 
    22 #import "GeneralSettingsViewController.h"
    22 #import "GeneralSettingsViewController.h"
    23 #import "CommodityFunctions.h"
    23 #import "CommodityFunctions.h"
    24 
    24 
    25 @implementation GeneralSettingsViewController
    25 @implementation GeneralSettingsViewController
    26 @synthesize settingsDictionary;
    26 @synthesize settings;
    27 
    27 
    28 
    28 
    29 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    29 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    30     return rotationManager(interfaceOrientation);
    30     return rotationManager(interfaceOrientation);
    31 }
    31 }
    37 }
    37 }
    38 
    38 
    39 -(void) viewWillAppear:(BOOL)animated {
    39 -(void) viewWillAppear:(BOOL)animated {
    40     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    40     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    41 
    41 
    42     NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()];
    42     self.settings = [NSUserDefaults standardUserDefaults];
    43     self.settingsDictionary = dictionary;
       
    44     [dictionary release];
       
    45 
    43 
    46     [super viewWillAppear:animated];
    44     [super viewWillAppear:animated];
    47 }
    45 }
    48 
    46 
    49 -(void) viewWillDisappear:(BOOL)animated {
    47 -(void) viewWillDisappear:(BOOL)animated {
    50     [super viewWillDisappear:animated];
    48     [super viewWillDisappear:animated];
    51     [self.settingsDictionary writeToFile:SETTINGS_FILE() atomically:YES];
    49     [self.settings synchronize];
    52 
       
    53     // we save the sound settings to respect that flag everywhere
       
    54     NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
       
    55     [prefs setObject:[NSNumber numberWithBool:[[self.settingsDictionary objectForKey:@"sound"] boolValue]] forKey:@"audio"];
       
    56     [prefs synchronize];
       
    57 }
    50 }
    58 
    51 
    59 #pragma mark -
    52 #pragma mark -
    60 -(void) switchValueChanged:(id) sender {
    53 -(void) switchValueChanged:(id) sender {
    61     UISwitch *theSwitch = (UISwitch *)sender;
    54     UISwitch *theSwitch = (UISwitch *)sender;
    62     UISwitch *theOtherSwitch = nil;
    55     UISwitch *theOtherSwitch = nil;
    63 
    56 
    64     switch (theSwitch.tag) {
    57     switch (theSwitch.tag) {
    65         case 10:    //soundSwitch
    58         case 10:    //soundSwitch
    66             // this turn off also the switch below
    59             // this turn off also the switch below
    67             [self.settingsDictionary setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"sound"];
    60             [self.settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"sound"];
    68             [self.settingsDictionary setObject:[NSNumber numberWithBool:NO] forKey:@"music"];
    61             [self.settings setObject:[NSNumber numberWithBool:NO] forKey:@"music"];
    69             theOtherSwitch = (UISwitch *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]].accessoryView;
    62             theOtherSwitch = (UISwitch *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]].accessoryView;
    70             [theOtherSwitch setOn:NO animated:YES];
    63             [theOtherSwitch setOn:NO animated:YES];
    71             break;
    64             break;
    72         case 20:    //musicSwitch
    65         case 20:    //musicSwitch
    73             // if switch above is off, never turn on
    66             // if switch above is off, never turn on
    74             if (NO == [[self.settingsDictionary objectForKey:@"sound"] boolValue]) {
    67             if (NO == [[self.settings objectForKey:@"sound"] boolValue]) {
    75                 [self.settingsDictionary setObject:[NSNumber numberWithBool:NO] forKey:@"music"];
    68                 [self.settings setObject:[NSNumber numberWithBool:NO] forKey:@"music"];
    76                 theOtherSwitch = (UISwitch *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]].accessoryView;
    69                 theOtherSwitch = (UISwitch *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]].accessoryView;
    77                 [theOtherSwitch setOn:NO animated:YES];
    70                 [theOtherSwitch setOn:NO animated:YES];
    78             } else
    71             } else
    79                 [self.settingsDictionary setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"music"];
    72                 [self.settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"music"];
    80             break;
    73             break;
    81         case 30:    //alternateSwitch
    74         case 30:    //alternateSwitch
    82             [self.settingsDictionary setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"alternate"];
    75             [self.settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"alternate"];
    83             break;
    76             break;
    84         case 70:    //enhanced graphics
    77         case 70:    //enhanced graphics
    85             [self.settingsDictionary setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"enhanced"];
    78             [self.settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"enhanced"];
    86             break;
    79             break;
    87         case 80:    //nomultitasking
    80         case 80:    //nomultitasking
    88             [self.settingsDictionary setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"multitasking"];
    81             [self.settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"multitasking"];
    89             break;
    82             break;
    90         case 60:    //classic menu
    83         case 60:    //classic menu
    91             [self.settingsDictionary setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"menu"];
    84             [self.settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"classic_menu"];
    92             break;
    85             break;
    93         default:
    86         default:
    94             DLog(@"Wrong tag");
    87             DLog(@"Wrong tag");
    95             break;
    88             break;
    96     }
    89     }
    97 }
    90 }
    98 
    91 
    99 -(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue {
    92 -(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue {
   100     if (tagValue == 40)
    93     if (tagValue == 40)
   101         [self.settingsDictionary setObject:textString forKey:@"username"];
    94         [self.settings setObject:textString forKey:@"username"];
   102     else {
    95     else
   103         [self.settingsDictionary setObject:[NSNumber numberWithInt:[textString length]] forKey:@"password_length"];
    96         [self.settings setObject:[textString MD5hash] forKey:@"password"];
   104         [self.settingsDictionary setObject:[textString MD5hash] forKey:@"password"];
       
   105     }
       
   106 }
    97 }
   107 
    98 
   108 #pragma mark -
    99 #pragma mark -
   109 #pragma mark TableView Methods
   100 #pragma mark TableView Methods
   110 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   101 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   173             }
   164             }
   174             
   165             
   175             if (row == 0) {
   166             if (row == 0) {
   176                 editableCell.titleLabel.text = NSLocalizedString(@"Nickname","from the settings table");
   167                 editableCell.titleLabel.text = NSLocalizedString(@"Nickname","from the settings table");
   177                 editableCell.textField.placeholder = NSLocalizedString(@"Insert your username (if you have one)",@"");
   168                 editableCell.textField.placeholder = NSLocalizedString(@"Insert your username (if you have one)",@"");
   178                 editableCell.textField.text = [self.settingsDictionary objectForKey:@"username"];
   169                 editableCell.textField.text = [self.settings objectForKey:@"username"];
   179                 editableCell.textField.secureTextEntry = NO;
   170                 editableCell.textField.secureTextEntry = NO;
   180                 editableCell.tag = 40;
   171                 editableCell.tag = 40;
   181             } else {
   172             } else {
   182                 // create a dummy password for setting some visual content for the password
   173                 NSString *pwd = [self.settings objectForKey:@"password"];
   183                 int pwdLength = [[self.settingsDictionary objectForKey:@"password_length"] intValue];
       
   184                 char *dummyStr = (char *)malloc(sizeof(char)*pwdLength);
       
   185                 for (int i = 0; i < pwdLength; i++)
       
   186                     dummyStr[i] = i;
       
   187                 NSString *dummy = [[NSString alloc] initWithBytes:dummyStr length:pwdLength encoding:NSASCIIStringEncoding];
       
   188                 free(dummyStr);
       
   189                 editableCell.titleLabel.text = NSLocalizedString(@"Password","from the settings table");
   174                 editableCell.titleLabel.text = NSLocalizedString(@"Password","from the settings table");
   190                 editableCell.textField.placeholder = NSLocalizedString(@"Insert your password",@"");
   175                 editableCell.textField.placeholder = NSLocalizedString(@"Insert your password",@"");
   191                 editableCell.textField.text = dummy;
   176                 editableCell.textField.text = ([pwd length] == 0) ? @"0123456789" : pwd;
   192                 editableCell.textField.secureTextEntry = YES;
   177                 editableCell.textField.secureTextEntry = YES;
   193                 editableCell.tag = 50;
   178                 editableCell.tag = 50;
   194                 [dummy release];
       
   195             }
   179             }
   196             
   180             
   197             editableCell.accessoryView = nil;
   181             editableCell.accessoryView = nil;
   198             cell = editableCell;
   182             cell = editableCell;
   199             break;
   183             break;
   208             }
   192             }
   209             
   193             
   210             switchContent = (UISwitch *)cell.accessoryView;
   194             switchContent = (UISwitch *)cell.accessoryView;
   211             if (row == 0) {
   195             if (row == 0) {
   212                 cell.textLabel.text = NSLocalizedString(@"Sound", @"");
   196                 cell.textLabel.text = NSLocalizedString(@"Sound", @"");
   213                 switchContent.on = [[self.settingsDictionary objectForKey:@"sound"] boolValue];
   197                 switchContent.on = [[self.settings objectForKey:@"sound"] boolValue];
   214                 switchContent.tag = 10;
   198                 switchContent.tag = 10;
   215             } else {
   199             } else {
   216                 cell.textLabel.text = NSLocalizedString(@"Music", @"");
   200                 cell.textLabel.text = NSLocalizedString(@"Music", @"");
   217                 switchContent.on = [[self.settingsDictionary objectForKey:@"music"] boolValue];
   201                 switchContent.on = [[self.settings objectForKey:@"music"] boolValue];
   218                 switchContent.tag = 20;
   202                 switchContent.tag = 20;
   219             }
   203             }
   220             break;
   204             break;
   221         case 2:
   205         case 2:
   222             cell = [aTableView dequeueReusableCellWithIdentifier:cellIdentifier2];
   206             cell = [aTableView dequeueReusableCellWithIdentifier:cellIdentifier2];
   231             switchContent = (UISwitch *)cell.accessoryView;
   215             switchContent = (UISwitch *)cell.accessoryView;
   232             switch (row) {
   216             switch (row) {
   233                 case 0:
   217                 case 0:
   234                     cell.textLabel.text = NSLocalizedString(@"Alternate Damage", @"");
   218                     cell.textLabel.text = NSLocalizedString(@"Alternate Damage", @"");
   235                     cell.detailTextLabel.text = NSLocalizedString(@"Damage popups will notify you on every single hit", @"");
   219                     cell.detailTextLabel.text = NSLocalizedString(@"Damage popups will notify you on every single hit", @"");
   236                     switchContent.on = [[self.settingsDictionary objectForKey:@"alternate"] boolValue];
   220                     switchContent.on = [[self.settings objectForKey:@"alternate"] boolValue];
   237                     switchContent.tag = 30;
   221                     switchContent.tag = 30;
   238                     break;
   222                     break;
   239                 case 1:
   223                 case 1:
   240                     cell.textLabel.text = NSLocalizedString(@"Enanched Graphics Mode", @"");
   224                     cell.textLabel.text = NSLocalizedString(@"Enanched Graphics Mode", @"");
   241                     cell.detailTextLabel.text = NSLocalizedString(@"The game will use more memory so it could crash!", @"");
   225                     cell.detailTextLabel.text = NSLocalizedString(@"The game will use more memory so it could crash!", @"");
   242                     switchContent.on = [[self.settingsDictionary objectForKey:@"enhanced"] boolValue];
   226                     switchContent.on = [[self.settings objectForKey:@"enhanced"] boolValue];
   243                     switchContent.tag = 70;
   227                     switchContent.tag = 70;
   244                     break;
   228                     break;
   245                 case 2:
   229                 case 2:
   246                     cell.textLabel.text = NSLocalizedString(@"Multitasking Enabled", @"");
   230                     cell.textLabel.text = NSLocalizedString(@"Multitasking Enabled", @"");
   247                     cell.detailTextLabel.text = NSLocalizedString(@"Disable it in case of issues when returing in game", @"");
   231                     cell.detailTextLabel.text = NSLocalizedString(@"Disable it in case of issues when returing in game", @"");
   248                     switchContent.on = [[self.settingsDictionary objectForKey:@"multitasking"] boolValue];
   232                     switchContent.on = [[self.settings objectForKey:@"multitasking"] boolValue];
   249                     switchContent.tag = 80;
   233                     switchContent.tag = 80;
   250                     break;
   234                     break;
   251                 case 3:
   235                 case 3:
   252                     cell.textLabel.text = NSLocalizedString(@"Classic Ammo Menu", @"");
   236                     cell.textLabel.text = NSLocalizedString(@"Classic Ammo Menu", @"");
   253                     cell.detailTextLabel.text = NSLocalizedString(@"Select which style of ammo menu you prefer",@"");
   237                     cell.detailTextLabel.text = NSLocalizedString(@"Select which style of ammo menu you prefer",@"");
   254                     switchContent.on = [[self.settingsDictionary objectForKey:@"menu"] boolValue];
   238                     switchContent.on = [[self.settings objectForKey:@"classic_menu"] boolValue];
   255                     switchContent.tag = 60;
   239                     switchContent.tag = 60;
   256                     break;
   240                     break;
   257                 default:
   241                 default:
   258                     DLog(@"Nope");
   242                     DLog(@"Nope");
   259                     break;
   243                     break;
   285 -(void) didReceiveMemoryWarning {
   269 -(void) didReceiveMemoryWarning {
   286     [super didReceiveMemoryWarning];
   270     [super didReceiveMemoryWarning];
   287 }
   271 }
   288 
   272 
   289 -(void) viewDidUnload {
   273 -(void) viewDidUnload {
   290     self.settingsDictionary = nil;
   274     self.settings = nil;
   291     MSG_DIDUNLOAD();
   275     MSG_DIDUNLOAD();
   292     [super viewDidUnload];
   276     [super viewDidUnload];
   293 }
   277 }
   294 
   278 
   295 -(void) dealloc {
   279 -(void) dealloc {
   296     [settingsDictionary release];
   280     [settings release];
   297     [super dealloc];
   281     [super dealloc];
   298 }
   282 }
   299 
   283 
   300 @end
   284 @end