project_files/HedgewarsMobile/Classes/GeneralSettingsViewController.m
changeset 5207 4c9ae0f484da
parent 5206 db775bddf771
child 5224 6e8fbbfb0de5
equal deleted inserted replaced
5206:db775bddf771 5207:4c9ae0f484da
    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 settings;
       
    27 
    26 
    28 
    27 
    29 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    28 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    30     return rotationManager(interfaceOrientation);
    29     return rotationManager(interfaceOrientation);
    31 }
    30 }
    36     [super viewDidLoad];
    35     [super viewDidLoad];
    37 }
    36 }
    38 
    37 
    39 -(void) viewWillAppear:(BOOL)animated {
    38 -(void) viewWillAppear:(BOOL)animated {
    40     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    39     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    41 
       
    42     self.settings = [NSUserDefaults standardUserDefaults];
       
    43 
       
    44     [super viewWillAppear:animated];
    40     [super viewWillAppear:animated];
    45 }
    41 }
    46 
    42 
    47 -(void) viewWillDisappear:(BOOL)animated {
    43 -(void) viewWillDisappear:(BOOL)animated {
       
    44     [[NSUserDefaults standardUserDefaults] synchronize];
    48     [super viewWillDisappear:animated];
    45     [super viewWillDisappear:animated];
    49     [self.settings synchronize];
       
    50 }
    46 }
    51 
    47 
    52 #pragma mark -
    48 #pragma mark -
    53 -(void) switchValueChanged:(id) sender {
    49 -(void) switchValueChanged:(id) sender {
    54     UISwitch *theSwitch = (UISwitch *)sender;
    50     UISwitch *theSwitch = (UISwitch *)sender;
    55     UISwitch *theOtherSwitch = nil;
    51     UISwitch *theOtherSwitch = nil;
       
    52     NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    56 
    53 
    57     switch (theSwitch.tag) {
    54     switch (theSwitch.tag) {
    58         case 10:    //soundSwitch
    55         case 10:    //soundSwitch
    59             // this turn off also the switch below
    56             // this turn off also the switch below
    60             [self.settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"sound"];
    57             [settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"sound"];
    61             [self.settings setObject:[NSNumber numberWithBool:NO] forKey:@"music"];
    58             [settings setObject:[NSNumber numberWithBool:NO] forKey:@"music"];
    62             theOtherSwitch = (UISwitch *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]].accessoryView;
    59             theOtherSwitch = (UISwitch *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]].accessoryView;
    63             [theOtherSwitch setOn:NO animated:YES];
    60             [theOtherSwitch setOn:NO animated:YES];
    64             break;
    61             break;
    65         case 20:    //musicSwitch
    62         case 20:    //musicSwitch
    66             // if switch above is off, never turn on
    63             // if switch above is off, never turn on
    67             if (NO == [[self.settings objectForKey:@"sound"] boolValue]) {
    64             if (NO == [[settings objectForKey:@"sound"] boolValue]) {
    68                 [self.settings setObject:[NSNumber numberWithBool:NO] forKey:@"music"];
    65                 [settings setObject:[NSNumber numberWithBool:NO] forKey:@"music"];
    69                 theOtherSwitch = (UISwitch *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]].accessoryView;
    66                 theOtherSwitch = (UISwitch *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]].accessoryView;
    70                 [theOtherSwitch setOn:NO animated:YES];
    67                 [theOtherSwitch setOn:NO animated:YES];
    71             } else
    68             } else
    72                 [self.settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"music"];
    69                 [settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"music"];
    73             break;
    70             break;
    74         case 30:    //alternateSwitch
    71         case 30:    //alternateSwitch
    75             [self.settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"alternate"];
    72             [settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"alternate"];
    76             break;
    73             break;
    77         case 70:    //enhanced graphics
    74         case 70:    //enhanced graphics
    78             [self.settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"enhanced"];
    75             [settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"enhanced"];
    79             break;
    76             break;
    80         case 80:    //nomultitasking
    77         case 80:    //nomultitasking
    81             [self.settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"multitasking"];
    78             [settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"multitasking"];
    82             break;
    79             break;
    83         case 60:    //classic menu
    80         case 60:    //classic menu
    84             [self.settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"classic_menu"];
    81             [settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"classic_menu"];
    85             break;
    82             break;
    86         default:
    83         default:
    87             DLog(@"Wrong tag");
    84             DLog(@"Wrong tag");
    88             break;
    85             break;
    89     }
    86     }
    90 }
    87 }
    91 
    88 
    92 -(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue {
    89 -(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue {
       
    90     NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
       
    91 
    93     if (tagValue == 40)
    92     if (tagValue == 40)
    94         [self.settings setObject:textString forKey:@"username"];
    93         [settings setObject:textString forKey:@"username"];
    95     else
    94     else
    96         [self.settings setObject:[textString MD5hash] forKey:@"password"];
    95         [settings setObject:[textString MD5hash] forKey:@"password"];
    97 }
    96 }
    98 
    97 
    99 #pragma mark -
    98 #pragma mark -
   100 #pragma mark TableView Methods
    99 #pragma mark TableView Methods
   101 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   100 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   146     static NSString *cellIdentifier0 = @"Cell0";
   145     static NSString *cellIdentifier0 = @"Cell0";
   147     static NSString *cellIdentifier1 = @"Cell1";
   146     static NSString *cellIdentifier1 = @"Cell1";
   148     static NSString *cellIdentifier2 = @"Cell2";
   147     static NSString *cellIdentifier2 = @"Cell2";
   149     NSInteger row = [indexPath row];
   148     NSInteger row = [indexPath row];
   150     NSInteger section = [indexPath section];
   149     NSInteger section = [indexPath section];
       
   150     NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
   151 
   151 
   152     UITableViewCell *cell = nil;
   152     UITableViewCell *cell = nil;
   153     EditableCellView *editableCell = nil;
   153     EditableCellView *editableCell = nil;
   154     UISwitch *switchContent = nil;
   154     UISwitch *switchContent = nil;
   155     switch(section) {
   155     switch(section) {
   164             }
   164             }
   165             
   165             
   166             if (row == 0) {
   166             if (row == 0) {
   167                 editableCell.titleLabel.text = NSLocalizedString(@"Nickname","from the settings table");
   167                 editableCell.titleLabel.text = NSLocalizedString(@"Nickname","from the settings table");
   168                 editableCell.textField.placeholder = NSLocalizedString(@"Insert your username (if you have one)",@"");
   168                 editableCell.textField.placeholder = NSLocalizedString(@"Insert your username (if you have one)",@"");
   169                 editableCell.textField.text = [self.settings objectForKey:@"username"];
   169                 editableCell.textField.text = [settings objectForKey:@"username"];
   170                 editableCell.textField.secureTextEntry = NO;
   170                 editableCell.textField.secureTextEntry = NO;
   171                 editableCell.tag = 40;
   171                 editableCell.tag = 40;
   172             } else {
   172             } else {
   173                 NSString *pwd = [self.settings objectForKey:@"password"];
       
   174                 editableCell.titleLabel.text = NSLocalizedString(@"Password","from the settings table");
   173                 editableCell.titleLabel.text = NSLocalizedString(@"Password","from the settings table");
   175                 editableCell.textField.placeholder = NSLocalizedString(@"Insert your password",@"");
   174                 editableCell.textField.placeholder = NSLocalizedString(@"Insert your password",@"");
   176                 editableCell.textField.text = ([pwd length] == 0) ? @"0123456789" : pwd;
   175                 editableCell.textField.text = [settings objectForKey:@"password"];
   177                 editableCell.textField.secureTextEntry = YES;
   176                 editableCell.textField.secureTextEntry = YES;
   178                 editableCell.tag = 50;
   177                 editableCell.tag = 50;
   179             }
   178             }
   180             
   179             
   181             editableCell.accessoryView = nil;
   180             editableCell.accessoryView = nil;
   192             }
   191             }
   193             
   192             
   194             switchContent = (UISwitch *)cell.accessoryView;
   193             switchContent = (UISwitch *)cell.accessoryView;
   195             if (row == 0) {
   194             if (row == 0) {
   196                 cell.textLabel.text = NSLocalizedString(@"Sound", @"");
   195                 cell.textLabel.text = NSLocalizedString(@"Sound", @"");
   197                 switchContent.on = [[self.settings objectForKey:@"sound"] boolValue];
   196                 switchContent.on = [[settings objectForKey:@"sound"] boolValue];
   198                 switchContent.tag = 10;
   197                 switchContent.tag = 10;
   199             } else {
   198             } else {
   200                 cell.textLabel.text = NSLocalizedString(@"Music", @"");
   199                 cell.textLabel.text = NSLocalizedString(@"Music", @"");
   201                 switchContent.on = [[self.settings objectForKey:@"music"] boolValue];
   200                 switchContent.on = [[settings objectForKey:@"music"] boolValue];
   202                 switchContent.tag = 20;
   201                 switchContent.tag = 20;
   203             }
   202             }
   204             break;
   203             break;
   205         case 2:
   204         case 2:
   206             cell = [aTableView dequeueReusableCellWithIdentifier:cellIdentifier2];
   205             cell = [aTableView dequeueReusableCellWithIdentifier:cellIdentifier2];
   215             switchContent = (UISwitch *)cell.accessoryView;
   214             switchContent = (UISwitch *)cell.accessoryView;
   216             switch (row) {
   215             switch (row) {
   217                 case 0:
   216                 case 0:
   218                     cell.textLabel.text = NSLocalizedString(@"Alternate Damage", @"");
   217                     cell.textLabel.text = NSLocalizedString(@"Alternate Damage", @"");
   219                     cell.detailTextLabel.text = NSLocalizedString(@"Damage popups will notify you on every single hit", @"");
   218                     cell.detailTextLabel.text = NSLocalizedString(@"Damage popups will notify you on every single hit", @"");
   220                     switchContent.on = [[self.settings objectForKey:@"alternate"] boolValue];
   219                     switchContent.on = [[settings objectForKey:@"alternate"] boolValue];
   221                     switchContent.tag = 30;
   220                     switchContent.tag = 30;
   222                     break;
   221                     break;
   223                 case 1:
   222                 case 1:
   224                     cell.textLabel.text = NSLocalizedString(@"Enanched Graphics Mode", @"");
   223                     cell.textLabel.text = NSLocalizedString(@"Enanched Graphics Mode", @"");
   225                     cell.detailTextLabel.text = NSLocalizedString(@"The game will use more memory so it could crash!", @"");
   224                     cell.detailTextLabel.text = NSLocalizedString(@"The game will use more memory so it could crash!", @"");
   226                     switchContent.on = [[self.settings objectForKey:@"enhanced"] boolValue];
   225                     switchContent.on = [[settings objectForKey:@"enhanced"] boolValue];
   227                     switchContent.tag = 70;
   226                     switchContent.tag = 70;
   228                     break;
   227                     break;
   229                 case 2:
   228                 case 2:
   230                     cell.textLabel.text = NSLocalizedString(@"Multitasking Enabled", @"");
   229                     cell.textLabel.text = NSLocalizedString(@"Multitasking Enabled", @"");
   231                     cell.detailTextLabel.text = NSLocalizedString(@"Disable it in case of issues when returing in game", @"");
   230                     cell.detailTextLabel.text = NSLocalizedString(@"Disable it in case of issues when returing in game", @"");
   232                     switchContent.on = [[self.settings objectForKey:@"multitasking"] boolValue];
   231                     switchContent.on = [[settings objectForKey:@"multitasking"] boolValue];
   233                     switchContent.tag = 80;
   232                     switchContent.tag = 80;
   234                     break;
   233                     break;
   235                 case 3:
   234                 case 3:
   236                     cell.textLabel.text = NSLocalizedString(@"Classic Ammo Menu", @"");
   235                     cell.textLabel.text = NSLocalizedString(@"Classic Ammo Menu", @"");
   237                     cell.detailTextLabel.text = NSLocalizedString(@"Select which style of ammo menu you prefer",@"");
   236                     cell.detailTextLabel.text = NSLocalizedString(@"Select which style of ammo menu you prefer",@"");
   238                     switchContent.on = [[self.settings objectForKey:@"classic_menu"] boolValue];
   237                     switchContent.on = [[settings objectForKey:@"classic_menu"] boolValue];
   239                     switchContent.tag = 60;
   238                     switchContent.tag = 60;
   240                     break;
   239                     break;
   241                 default:
   240                 default:
   242                     DLog(@"Nope");
   241                     DLog(@"Nope");
   243                     break;
   242                     break;
   269 -(void) didReceiveMemoryWarning {
   268 -(void) didReceiveMemoryWarning {
   270     [super didReceiveMemoryWarning];
   269     [super didReceiveMemoryWarning];
   271 }
   270 }
   272 
   271 
   273 -(void) viewDidUnload {
   272 -(void) viewDidUnload {
   274     self.settings = nil;
       
   275     MSG_DIDUNLOAD();
       
   276     [super viewDidUnload];
   273     [super viewDidUnload];
   277 }
   274 }
   278 
   275 
   279 -(void) dealloc {
   276 -(void) dealloc {
   280     [settings release];
       
   281     [super dealloc];
   277     [super dealloc];
   282 }
   278 }
   283 
   279 
   284 @end
   280 @end