project_files/HedgewarsMobile/Classes/GeneralSettingsViewController.m
changeset 3660 bc125bea5849
parent 3659 f8d5ac50e307
child 3662 a44406f4369b
equal deleted inserted replaced
3659:f8d5ac50e307 3660:bc125bea5849
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
     7 //
     7 //
     8 
     8 
     9 #import "GeneralSettingsViewController.h"
     9 #import "GeneralSettingsViewController.h"
    10 #import "CommodityFunctions.h"
    10 #import "CommodityFunctions.h"
       
    11 #import "EditableCellView.h"
    11 
    12 
    12 @implementation GeneralSettingsViewController
    13 @implementation GeneralSettingsViewController
    13 @synthesize settingsDictionary, textFieldBeingEdited, musicSwitch, soundSwitch, altDamageSwitch;
    14 @synthesize settingsDictionary, textFieldBeingEdited, musicSwitch, soundSwitch, altDamageSwitch;
    14 
    15 
    15 
    16 
    24     if (textFieldBeingEdited != nil)
    25     if (textFieldBeingEdited != nil)
    25         [self.textFieldBeingEdited resignFirstResponder];
    26         [self.textFieldBeingEdited resignFirstResponder];
    26 }
    27 }
    27 
    28 
    28 // set the new value
    29 // set the new value
    29 -(BOOL) save:(id) sender {
    30 -(void) save:(id) sender {
    30     if (textFieldBeingEdited != nil) {
    31     if (textFieldBeingEdited != nil) {
    31         if (textFieldBeingEdited.tag == 0) {
    32         if (textFieldBeingEdited.tag == 0)
    32             [self.settingsDictionary setObject:textFieldBeingEdited.text forKey:@"username"];
    33             [self.settingsDictionary setObject:textFieldBeingEdited.text forKey:@"username"];
    33         } else {
    34         else
    34             [self.settingsDictionary setObject:textFieldBeingEdited.text forKey:@"password"];
    35             [self.settingsDictionary setObject:textFieldBeingEdited.text forKey:@"password"];
    35         }
       
    36         
    36         
    37         isWriteNeeded = YES;
       
    38         [self.textFieldBeingEdited resignFirstResponder];
    37         [self.textFieldBeingEdited resignFirstResponder];
    39         return YES;
    38     }
    40     }
       
    41     return NO;
       
    42 }
    39 }
    43 
    40 
    44 // the textfield is being modified, update the navigation controller
    41 // the textfield is being modified, update the navigation controller
    45 -(void) textFieldDidBeginEditing:(UITextField *)aTextField{   
    42 -(void) textFieldDidBeginEditing:(UITextField *)aTextField{   
    46     self.textFieldBeingEdited = aTextField;
    43     self.textFieldBeingEdited = aTextField;
    66     self.navigationItem.leftBarButtonItem = nil;
    63     self.navigationItem.leftBarButtonItem = nil;
    67 }
    64 }
    68 
    65 
    69 // limit the size of the field to 64 characters like in original frontend
    66 // limit the size of the field to 64 characters like in original frontend
    70 -(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    67 -(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    71     int limit = 64;
    68     return !([textField.text length] > MAX_STRING_LENGTH && [string length] > range.length);
    72     return !([textField.text length] > limit && [string length] > range.length);
    69 }
    73 }
       
    74 
       
    75 
    70 
    76 #pragma mark -
    71 #pragma mark -
    77 #pragma mark View Lifecycle
    72 #pragma mark View Lifecycle
    78 -(void) viewDidLoad {
    73 -(void) viewDidLoad {
    79     [super viewDidLoad];
    74     [super viewDidLoad];
    89     [dictionary release];
    84     [dictionary release];
    90 }
    85 }
    91 
    86 
    92 -(void) viewWillAppear:(BOOL)animated {
    87 -(void) viewWillAppear:(BOOL)animated {
    93     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    88     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    94     isWriteNeeded = NO;
       
    95     
    89     
    96     musicSwitch.on = [[settingsDictionary objectForKey:@"music"] boolValue];
    90     musicSwitch.on = [[settingsDictionary objectForKey:@"music"] boolValue];
    97     soundSwitch.on = [[settingsDictionary objectForKey:@"sound"] boolValue];
    91     soundSwitch.on = [[settingsDictionary objectForKey:@"sound"] boolValue];
    98     altDamageSwitch.on = [[settingsDictionary objectForKey:@"alternate"] boolValue];
    92     altDamageSwitch.on = [[settingsDictionary objectForKey:@"alternate"] boolValue];
    99     
    93     
   100     [super viewWillAppear:animated];
    94     [super viewWillAppear:animated];
   101 }
    95 }
   102 
    96 
   103 -(void) viewWillDisappear:(BOOL)animated {
    97 -(void) viewWillDisappear:(BOOL)animated {
   104     [super viewWillDisappear:animated];
    98     [super viewWillDisappear:animated];    
   105     
    99     [self.settingsDictionary writeToFile:SETTINGS_FILE() atomically:YES];
   106     if (isWriteNeeded) {
       
   107         NSLog(@"writing preferences to file");
       
   108         [self.settingsDictionary writeToFile:SETTINGS_FILE() atomically:YES];
       
   109         isWriteNeeded = NO;
       
   110     }
       
   111 }
   100 }
   112 
   101 
   113 #pragma mark -
   102 #pragma mark -
   114 // if the sound system is off, turn off also the background music 
   103 // if the sound system is off, turn off also the background music 
   115 -(void) alsoTurnOffMusic:(id) sender {
   104 -(void) alsoTurnOffMusic:(id) sender {
   116     [self.settingsDictionary setObject:[NSNumber numberWithBool:soundSwitch.on] forKey:@"sound"];
   105     [self.settingsDictionary setObject:[NSNumber numberWithBool:soundSwitch.on] forKey:@"sound"];
   117     if (YES == self.musicSwitch.on) {
   106     if (YES == self.musicSwitch.on) {
   118         [musicSwitch setOn:NO animated:YES];
   107         [musicSwitch setOn:NO animated:YES];
   119         [self.settingsDictionary setObject:[NSNumber numberWithBool:musicSwitch.on] forKey:@"music"];
   108         [self.settingsDictionary setObject:[NSNumber numberWithBool:musicSwitch.on] forKey:@"music"];
   120     }
   109     }
   121     isWriteNeeded = YES;
       
   122 }
   110 }
   123 
   111 
   124 // if the sound system is off, don't enable background music 
   112 // if the sound system is off, don't enable background music 
   125 -(void) dontTurnOnMusic:(id) sender {
   113 -(void) dontTurnOnMusic:(id) sender {
   126     if (NO == self.soundSwitch.on) {
   114     if (NO == self.soundSwitch.on)
   127         [musicSwitch setOn:NO animated:YES];
   115         [musicSwitch setOn:NO animated:YES];
   128     } else {
   116     else
   129         [self.settingsDictionary setObject:[NSNumber numberWithBool:musicSwitch.on] forKey:@"music"];
   117         [self.settingsDictionary setObject:[NSNumber numberWithBool:musicSwitch.on] forKey:@"music"];
   130         isWriteNeeded = YES;
       
   131     }
       
   132 }
   118 }
   133 
   119 
   134 -(void) justUpdateDictionary:(id) sender {
   120 -(void) justUpdateDictionary:(id) sender {
   135     UISwitch *theSwitch = (UISwitch *)sender;
   121     UISwitch *theSwitch = (UISwitch *)sender;
   136     [self.settingsDictionary setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"alternate"];
   122     [self.settingsDictionary setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"alternate"];
   137     isWriteNeeded = YES;
       
   138 }
   123 }
   139 
   124 
   140 #pragma mark -
   125 #pragma mark -
   141 #pragma mark TableView Methods
   126 #pragma mark TableView Methods
   142 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   127 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   143     return 3;
   128     return 3;
   144 }
   129 }
   145 
   130 
   146 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   131 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   147     switch (section) {
   132     switch (section) {
   148         case kNetworkFields:
   133         case 0:     // user and pass
   149             return 2;
   134             return 2;
   150             break;
   135             break;
   151         case kAudioFields:
   136         case 1:     // audio
   152             return 2;
   137             return 2;
   153             break;
   138             break;
   154         case kOtherFields:
   139         case 2:     // alternate damage
   155             return 1;
   140             return 1;
   156             break;
   141             break;
   157         default:
   142         default:
   158             break;
   143             break;
   159     }
   144     }
   163 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   148 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   164     static NSString *cellIdentifier = @"systemSettingsCell";
   149     static NSString *cellIdentifier = @"systemSettingsCell";
   165     NSInteger row = [indexPath row];
   150     NSInteger row = [indexPath row];
   166     NSInteger section = [indexPath section];
   151     NSInteger section = [indexPath section];
   167     
   152     
   168     UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:cellIdentifier];
   153     UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:cellIdentifier];    
   169     if (nil == cell) {
       
   170         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
       
   171         if (section == kNetworkFields) {
       
   172             UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(-15, 10, 100, 25)];
       
   173             label.textAlignment = UITextAlignmentRight;
       
   174             label.backgroundColor = [UIColor clearColor];
       
   175             label.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize] + 2];
       
   176             if (row == 0) 
       
   177                 label.text = NSLocalizedString(@"Nickname","from the settings table");
       
   178             else 
       
   179                 label.text = NSLocalizedString(@"Password","from the settings table");
       
   180             [cell.contentView addSubview:label];
       
   181             [label release];
       
   182             
       
   183             UITextField *aTextField = [[UITextField alloc] initWithFrame:
       
   184                                        CGRectMake(110, 12, (cell.frame.size.width + cell.frame.size.width/3) - 90, 25)];
       
   185             aTextField.clearsOnBeginEditing = NO;
       
   186             aTextField.returnKeyType = UIReturnKeyDone;
       
   187             aTextField.adjustsFontSizeToFitWidth = YES;
       
   188             aTextField.delegate = self;
       
   189             aTextField.tag = row;
       
   190             aTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
       
   191             [aTextField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit];
       
   192             [cell.contentView addSubview:aTextField];
       
   193             [aTextField release];
       
   194         }
       
   195     }
       
   196     
       
   197     cell.accessoryType = UITableViewCellAccessoryNone;
       
   198     cell.selectionStyle = UITableViewCellSelectionStyleNone;
       
   199     cell.imageView.image = nil;
       
   200     
       
   201     UITextField *aTextField;
   154     UITextField *aTextField;
   202     switch (section) {
   155     switch (section) {
   203         case kNetworkFields:
   156         case 0:
       
   157             if (nil == cell) {
       
   158                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
       
   159                 if (section == 0) {
       
   160                     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(-9, 10, 100, [UIFont labelFontSize] + 4)];
       
   161                     label.textAlignment = UITextAlignmentRight;
       
   162                     label.backgroundColor = [UIColor clearColor];
       
   163                     label.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]];
       
   164                     if (row == 0) 
       
   165                         label.text = NSLocalizedString(@"Nickname","from the settings table");
       
   166                     else 
       
   167                         label.text = NSLocalizedString(@"Password","from the settings table");
       
   168                     [cell.contentView addSubview:label];
       
   169                     [label release];
       
   170                     
       
   171                     UITextField *aTextField = [[UITextField alloc] initWithFrame:
       
   172                                                CGRectMake(110, 10, (cell.frame.size.width + cell.frame.size.width/3) - 90, [UIFont labelFontSize] + 4)];
       
   173                     aTextField.clearsOnBeginEditing = NO;
       
   174                     aTextField.returnKeyType = UIReturnKeyDone;
       
   175                     aTextField.adjustsFontSizeToFitWidth = YES;
       
   176                     aTextField.delegate = self;
       
   177                     aTextField.tag = row;
       
   178                     aTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
       
   179                     [aTextField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit];
       
   180                     [cell.contentView addSubview:aTextField];
       
   181                     [aTextField release];
       
   182                 }
       
   183             }
   204             for (UIView *oneView in cell.contentView.subviews) 
   184             for (UIView *oneView in cell.contentView.subviews) 
   205                 if ([oneView isMemberOfClass:[UITextField class]]) 
   185                 if ([oneView isMemberOfClass:[UITextField class]]) 
   206                     aTextField = (UITextField *)oneView;
   186                     aTextField = (UITextField *)oneView;
   207 
   187 
   208             switch (row) {
   188             switch (row) {
   209                 case 0:                    
   189                 case 0:                    
   210                     aTextField.placeholder = NSLocalizedString(@"Insert your username (if you have one)",@"");
   190                     aTextField.placeholder = NSLocalizedString(@"Insert your username (if you have one)",@"");
   211                     aTextField.text = [self.settingsDictionary objectForKey:@"username"];
   191                     aTextField.text = [self.settingsDictionary objectForKey:@"username"];
       
   192                     aTextField.secureTextEntry = NO;
   212                     break;
   193                     break;
   213                 case 1:                    
   194                 case 1:                    
   214                     aTextField.placeholder = NSLocalizedString(@"Insert your password",@"");
   195                     aTextField.placeholder = NSLocalizedString(@"Insert your password",@"");
   215                     aTextField.text = [self.settingsDictionary objectForKey:@"password"];
   196                     aTextField.text = [self.settingsDictionary objectForKey:@"password"];
   216                     aTextField.secureTextEntry = YES;
   197                     aTextField.secureTextEntry = YES;
   218                 default:
   199                 default:
   219                     break;
   200                     break;
   220             }
   201             }
   221             break;
   202             break;
   222             
   203             
   223         case kAudioFields:
   204             cell.accessoryView = nil;
       
   205         case 1:
       
   206             if (nil == cell)
       
   207                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
       
   208 
   224             switch (row) {
   209             switch (row) {
   225                 case 0:
   210                 case 0:
   226                     cell.textLabel.text = NSLocalizedString(@"Sound", @"");
   211                     cell.textLabel.text = NSLocalizedString(@"Sound", @"");
   227                     cell.accessoryView = soundSwitch;
   212                     cell.accessoryView = soundSwitch;
   228                     break;
   213                     break;
   233                 default:
   218                 default:
   234                     break;
   219                     break;
   235             }
   220             }
   236             break;
   221             break;
   237             
   222             
   238         case kOtherFields:
   223         case 2:
   239             cell.selectionStyle = UITableViewCellSelectionStyleNone;
   224             if (nil == cell)
       
   225                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
       
   226 
   240             cell.textLabel.text = NSLocalizedString(@"Alternate Damage", @"");
   227             cell.textLabel.text = NSLocalizedString(@"Alternate Damage", @"");
   241             cell.accessoryView = altDamageSwitch;
   228             cell.accessoryView = altDamageSwitch;
   242             break;
   229             break;
   243         default:
   230         default:
   244             break;
   231             break;
   245     }
   232     }
       
   233     
       
   234     cell.accessoryType = UITableViewCellAccessoryNone;
       
   235     cell.selectionStyle = UITableViewCellSelectionStyleNone;
       
   236     cell.imageView.image = nil;
       
   237     
   246     return cell;
   238     return cell;
   247 }
   239 }
   248 
   240 
   249 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
   241 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
   250     NSString *sectionTitle = nil;
   242     NSString *sectionTitle = nil;
   251     switch (section) {
   243     switch (section) {
   252         case kNetworkFields:
   244         case 0:
   253             sectionTitle = NSLocalizedString(@"Network Configuration", @"");
   245             sectionTitle = NSLocalizedString(@"Network Configuration", @"");
   254             break;
   246             break;
   255         case kAudioFields:
   247         case 1:
   256             sectionTitle = NSLocalizedString(@"Audio Preferences", @"");
   248             sectionTitle = NSLocalizedString(@"Audio Preferences", @"");
   257             break;
   249             break;
   258         case kOtherFields:
   250         case 2:
   259             sectionTitle = NSLocalizedString(@"Other Settings", @"");
   251             sectionTitle = NSLocalizedString(@"Other Settings", @"");
   260             break;
   252             break;
   261         default:
   253         default:
   262             NSLog(@"Nope");
   254             DLog(@"Nope");
   263             break;
   255             break;
   264     }
   256     }
   265     return sectionTitle;
   257     return sectionTitle;
   266 }
   258 }
   267 
   259 
   311  causes segfault if pressing twice cancel
   303  causes segfault if pressing twice cancel
   312 #pragma mark -
   304 #pragma mark -
   313 #pragma mark Table view delegate
   305 #pragma mark Table view delegate
   314 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   306 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   315     UITableViewCell *cell;
   307     UITableViewCell *cell;
   316     if (kNetworkFields == [indexPath section]) {
   308     if (0 == [indexPath section]) {
   317         cell = [aTableView cellForRowAtIndexPath:indexPath];
   309         cell = [aTableView cellForRowAtIndexPath:indexPath];
   318         for (UIView *oneView in cell.contentView.subviews) {
   310         for (UIView *oneView in cell.contentView.subviews) {
   319             if ([oneView isMemberOfClass:[UITextField class]]) {
   311             if ([oneView isMemberOfClass:[UITextField class]]) {
   320                 textFieldBeingEdited = (UITextField *)oneView;
   312                 textFieldBeingEdited = (UITextField *)oneView;
   321                 [textFieldBeingEdited becomeFirstResponder];
   313                 [textFieldBeingEdited becomeFirstResponder];