project_files/HedgewarsMobile/Classes/GeneralSettingsViewController.m
changeset 3662 a44406f4369b
parent 3660 bc125bea5849
child 3663 8c28abf427f5
equal deleted inserted replaced
3661:2378ada8a6ee 3662:a44406f4369b
     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"
       
    12 
    11 
    13 @implementation GeneralSettingsViewController
    12 @implementation GeneralSettingsViewController
    14 @synthesize settingsDictionary, textFieldBeingEdited, musicSwitch, soundSwitch, altDamageSwitch;
    13 @synthesize settingsDictionary;
    15 
    14 
    16 
    15 
    17 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    16 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    18     return rotationManager(interfaceOrientation);
    17     return rotationManager(interfaceOrientation);
    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 -(void) 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         [self.textFieldBeingEdited resignFirstResponder];
       
    38     }
       
    39 }
       
    40 
       
    41 // the textfield is being modified, update the navigation controller
       
    42 -(void) textFieldDidBeginEditing:(UITextField *)aTextField{   
       
    43     self.textFieldBeingEdited = aTextField;
       
    44     UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"from the settings table")
       
    45                                                                      style:UIBarButtonItemStylePlain
       
    46                                                                     target:self
       
    47                                                                     action:@selector(cancel:)];
       
    48     self.navigationItem.leftBarButtonItem = cancelButton;
       
    49     [cancelButton release];
       
    50     
       
    51     UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save",@"from the settings table")
       
    52                                                                      style:UIBarButtonItemStyleDone
       
    53                                                                     target:self
       
    54                                                                     action:@selector(save:)];
       
    55     self.navigationItem.rightBarButtonItem = saveButton;
       
    56     [saveButton release];
       
    57 }
       
    58 
       
    59 // the textfield has been modified, check for empty strings and restore original navigation bar
       
    60 -(void) textFieldDidEndEditing:(UITextField *)aTextField{
       
    61     self.textFieldBeingEdited = nil;
       
    62     self.navigationItem.rightBarButtonItem = self.navigationItem.backBarButtonItem;
       
    63     self.navigationItem.leftBarButtonItem = nil;
       
    64 }
       
    65 
       
    66 // limit the size of the field to 64 characters like in original frontend
       
    67 -(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
       
    68     return !([textField.text length] > MAX_STRING_LENGTH && [string length] > range.length);
       
    69 }
    18 }
    70 
    19 
    71 #pragma mark -
    20 #pragma mark -
    72 #pragma mark View Lifecycle
    21 #pragma mark View Lifecycle
    73 -(void) viewDidLoad {
    22 -(void) viewDidLoad {
    74     [super viewDidLoad];
    23     [super viewDidLoad];
    75     self.musicSwitch = [[UISwitch alloc] init];
       
    76     self.soundSwitch = [[UISwitch alloc] init];
       
    77     self.altDamageSwitch = [[UISwitch alloc] init];
       
    78     [self.soundSwitch addTarget:self action:@selector(alsoTurnOffMusic:) forControlEvents:UIControlEventValueChanged];
       
    79     [self.musicSwitch addTarget:self action:@selector(dontTurnOnMusic:) forControlEvents:UIControlEventValueChanged];
       
    80     [self.altDamageSwitch addTarget:self action:@selector(justUpdateDictionary:) forControlEvents:UIControlEventValueChanged];
       
    81     
    24     
    82     NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()];
    25     NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()];
    83     self.settingsDictionary = dictionary;
    26     self.settingsDictionary = dictionary;
    84     [dictionary release];
    27     [dictionary release];
    85 }
    28 }
    86 
    29 
    87 -(void) viewWillAppear:(BOOL)animated {
    30 -(void) viewWillAppear:(BOOL)animated {
    88     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    31     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    89     
    32     
    90     musicSwitch.on = [[settingsDictionary objectForKey:@"music"] boolValue];
       
    91     soundSwitch.on = [[settingsDictionary objectForKey:@"sound"] boolValue];
       
    92     altDamageSwitch.on = [[settingsDictionary objectForKey:@"alternate"] boolValue];
       
    93     
       
    94     [super viewWillAppear:animated];
    33     [super viewWillAppear:animated];
    95 }
    34 }
    96 
    35 
    97 -(void) viewWillDisappear:(BOOL)animated {
    36 -(void) viewWillDisappear:(BOOL)animated {
    98     [super viewWillDisappear:animated];    
    37     [super viewWillDisappear:animated];    
    99     [self.settingsDictionary writeToFile:SETTINGS_FILE() atomically:YES];
    38     [self.settingsDictionary writeToFile:SETTINGS_FILE() atomically:YES];
   100 }
    39 }
   101 
    40 
   102 #pragma mark -
    41 #pragma mark -
   103 // if the sound system is off, turn off also the background music 
    42 -(void) switchValueChanged:(id) sender {
   104 -(void) alsoTurnOffMusic:(id) sender {
    43     UISwitch *theSwitch = (UISwitch *)sender;
   105     [self.settingsDictionary setObject:[NSNumber numberWithBool:soundSwitch.on] forKey:@"sound"];
    44     UISwitch *theOtherSwitch = nil;
   106     if (YES == self.musicSwitch.on) {
    45     
   107         [musicSwitch setOn:NO animated:YES];
    46     switch (theSwitch.tag) {
   108         [self.settingsDictionary setObject:[NSNumber numberWithBool:musicSwitch.on] forKey:@"music"];
    47         case 10:    //soundSwitch
   109     }
    48             // this turn off also the switch below
   110 }
    49             [self.settingsDictionary setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"sound"];
   111 
    50             [self.settingsDictionary setObject:[NSNumber numberWithBool:NO] forKey:@"music"];
   112 // if the sound system is off, don't enable background music 
    51             theOtherSwitch = (UISwitch *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]].accessoryView;
   113 -(void) dontTurnOnMusic:(id) sender {
    52             [theOtherSwitch setOn:NO animated:YES];
   114     if (NO == self.soundSwitch.on)
    53             break;
   115         [musicSwitch setOn:NO animated:YES];
    54         case 20:    //musicSwitch
       
    55             // if switch above is off, never turn on
       
    56             if (NO == [[self.settingsDictionary objectForKey:@"sound"] boolValue]) {
       
    57                 [self.settingsDictionary setObject:[NSNumber numberWithBool:NO] forKey:@"music"];
       
    58                 theOtherSwitch = (UISwitch *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]].accessoryView;
       
    59                 [theOtherSwitch setOn:NO animated:YES];
       
    60             } else
       
    61                 [self.settingsDictionary setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"music"];
       
    62             break;
       
    63         case 30:    //alternateSwitch
       
    64             [self.settingsDictionary setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"alternate"];
       
    65             break;
       
    66         default:
       
    67             DLog(@"Wrong tag");
       
    68             break;
       
    69     }
       
    70 }
       
    71 
       
    72 -(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue {
       
    73     if (tagValue == 40)
       
    74         [self.settingsDictionary setObject:textString forKey:@"username"];
   116     else
    75     else
   117         [self.settingsDictionary setObject:[NSNumber numberWithBool:musicSwitch.on] forKey:@"music"];
    76         [self.settingsDictionary setObject:textString forKey:@"password"];
   118 }
       
   119 
       
   120 -(void) justUpdateDictionary:(id) sender {
       
   121     UISwitch *theSwitch = (UISwitch *)sender;
       
   122     [self.settingsDictionary setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"alternate"];
       
   123 }
    77 }
   124 
    78 
   125 #pragma mark -
    79 #pragma mark -
   126 #pragma mark TableView Methods
    80 #pragma mark TableView Methods
   127 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    81 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   141             break;
    95             break;
   142         default:
    96         default:
   143             break;
    97             break;
   144     }
    98     }
   145     return 0;
    99     return 0;
   146 }
       
   147 
       
   148 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       
   149     static NSString *cellIdentifier = @"systemSettingsCell";
       
   150     NSInteger row = [indexPath row];
       
   151     NSInteger section = [indexPath section];
       
   152     
       
   153     UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:cellIdentifier];    
       
   154     UITextField *aTextField;
       
   155     switch (section) {
       
   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             }
       
   184             for (UIView *oneView in cell.contentView.subviews) 
       
   185                 if ([oneView isMemberOfClass:[UITextField class]]) 
       
   186                     aTextField = (UITextField *)oneView;
       
   187 
       
   188             switch (row) {
       
   189                 case 0:                    
       
   190                     aTextField.placeholder = NSLocalizedString(@"Insert your username (if you have one)",@"");
       
   191                     aTextField.text = [self.settingsDictionary objectForKey:@"username"];
       
   192                     aTextField.secureTextEntry = NO;
       
   193                     break;
       
   194                 case 1:                    
       
   195                     aTextField.placeholder = NSLocalizedString(@"Insert your password",@"");
       
   196                     aTextField.text = [self.settingsDictionary objectForKey:@"password"];
       
   197                     aTextField.secureTextEntry = YES;
       
   198                     break;
       
   199                 default:
       
   200                     break;
       
   201             }
       
   202             break;
       
   203             
       
   204             cell.accessoryView = nil;
       
   205         case 1:
       
   206             if (nil == cell)
       
   207                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
       
   208 
       
   209             switch (row) {
       
   210                 case 0:
       
   211                     cell.textLabel.text = NSLocalizedString(@"Sound", @"");
       
   212                     cell.accessoryView = soundSwitch;
       
   213                     break;
       
   214                 case 1:
       
   215                     cell.textLabel.text = NSLocalizedString(@"Music", @"");
       
   216                     cell.accessoryView = musicSwitch;
       
   217                     break;
       
   218                 default:
       
   219                     break;
       
   220             }
       
   221             break;
       
   222             
       
   223         case 2:
       
   224             if (nil == cell)
       
   225                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
       
   226 
       
   227             cell.textLabel.text = NSLocalizedString(@"Alternate Damage", @"");
       
   228             cell.accessoryView = altDamageSwitch;
       
   229             break;
       
   230         default:
       
   231             break;
       
   232     }
       
   233     
       
   234     cell.accessoryType = UITableViewCellAccessoryNone;
       
   235     cell.selectionStyle = UITableViewCellSelectionStyleNone;
       
   236     cell.imageView.image = nil;
       
   237     
       
   238     return cell;
       
   239 }
   100 }
   240 
   101 
   241 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
   102 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
   242     NSString *sectionTitle = nil;
   103     NSString *sectionTitle = nil;
   243     switch (section) {
   104     switch (section) {
   253         default:
   114         default:
   254             DLog(@"Nope");
   115             DLog(@"Nope");
   255             break;
   116             break;
   256     }
   117     }
   257     return sectionTitle;
   118     return sectionTitle;
       
   119 }
       
   120 
       
   121 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       
   122     static NSString *cellIdentifier0 = @"Cell0";
       
   123     static NSString *cellIdentifier1 = @"Cell1";
       
   124     NSInteger row = [indexPath row];
       
   125     NSInteger section = [indexPath section];
       
   126     
       
   127     UITableViewCell *cell = nil;
       
   128     EditableCellView *editableCell = nil;
       
   129     if (section == 0) {
       
   130         editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:cellIdentifier0];
       
   131         if (nil == editableCell) {
       
   132             editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier0] autorelease];
       
   133             editableCell.minimumCharacters = 0;
       
   134             editableCell.delegate = self;
       
   135             editableCell.textField.font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
       
   136             editableCell.textField.textColor = [UIColor lightGrayColor];
       
   137         }
       
   138         
       
   139         if (row == 0) {
       
   140             editableCell.titleLabel.text = NSLocalizedString(@"Nickname","from the settings table");
       
   141             editableCell.textField.placeholder = NSLocalizedString(@"Insert your username (if you have one)",@"");
       
   142             editableCell.textField.text = [self.settingsDictionary objectForKey:@"username"];
       
   143             editableCell.textField.secureTextEntry = NO;
       
   144             editableCell.tag = 40;
       
   145         } else {
       
   146             editableCell.titleLabel.text = NSLocalizedString(@"Password","from the settings table");
       
   147             editableCell.textField.placeholder = NSLocalizedString(@"Insert your password",@"");
       
   148             editableCell.textField.text = [self.settingsDictionary objectForKey:@"password"];
       
   149             editableCell.textField.secureTextEntry = YES;
       
   150             editableCell.tag = 50;
       
   151         }
       
   152         
       
   153         editableCell.accessoryView = nil;
       
   154         cell = editableCell;
       
   155     } else {
       
   156         cell = [aTableView dequeueReusableCellWithIdentifier:cellIdentifier1];
       
   157         if (nil == cell) {
       
   158             cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier1] autorelease];
       
   159             UISwitch *theSwitch = [[UISwitch alloc] init];
       
   160             [theSwitch addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
       
   161             cell.accessoryView = theSwitch;
       
   162             [theSwitch release];
       
   163         }
       
   164         
       
   165         UISwitch *switchContent = (UISwitch *)cell.accessoryView;
       
   166         if (section == 1) {
       
   167             if (row == 0) {
       
   168                 cell.textLabel.text = NSLocalizedString(@"Sound", @"");
       
   169                 switchContent.on = [[self.settingsDictionary objectForKey:@"sound"] boolValue];
       
   170                 switchContent.tag = 10;
       
   171             } else {
       
   172                 cell.textLabel.text = NSLocalizedString(@"Music", @"");
       
   173                 switchContent.on = [[self.settingsDictionary objectForKey:@"music"] boolValue];
       
   174                 switchContent.tag = 20;
       
   175             }
       
   176         } else {
       
   177             cell.textLabel.text = NSLocalizedString(@"Alternate Damage", @"");
       
   178             switchContent.on = [[self.settingsDictionary objectForKey:@"alternate"] boolValue];
       
   179             switchContent.tag = 30;
       
   180         }
       
   181     }
       
   182     
       
   183     cell.accessoryType = UITableViewCellAccessoryNone;
       
   184     cell.selectionStyle = UITableViewCellSelectionStyleNone;
       
   185     cell.imageView.image = nil;
       
   186     
       
   187     return cell;
   258 }
   188 }
   259 
   189 
   260 /*
   190 /*
   261 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
   191 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
   262     UIView *containerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)] autorelease];
   192     UIView *containerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)] autorelease];
   297 -(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
   227 -(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
   298     return 57.0;
   228     return 57.0;
   299 }
   229 }
   300 */
   230 */
   301 
   231 
   302 /*
       
   303  causes segfault if pressing twice cancel
       
   304 #pragma mark -
   232 #pragma mark -
   305 #pragma mark Table view delegate
   233 #pragma mark Table view delegate
   306 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   234 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   307     UITableViewCell *cell;
       
   308     if (0 == [indexPath section]) {
   235     if (0 == [indexPath section]) {
   309         cell = [aTableView cellForRowAtIndexPath:indexPath];
   236         EditableCellView *cell = (EditableCellView *)[aTableView cellForRowAtIndexPath:indexPath];
   310         for (UIView *oneView in cell.contentView.subviews) {
   237         [cell replyKeyboard];
   311             if ([oneView isMemberOfClass:[UITextField class]]) {
   238     }
   312                 textFieldBeingEdited = (UITextField *)oneView;
   239 }
   313                 [textFieldBeingEdited becomeFirstResponder];
   240 
   314             }
       
   315         }
       
   316         [aTableView deselectRowAtIndexPath:indexPath animated:NO];
       
   317     }
       
   318 }
       
   319 */
       
   320 
   241 
   321 #pragma mark -
   242 #pragma mark -
   322 #pragma mark Memory management
   243 #pragma mark Memory management
   323 -(void) didReceiveMemoryWarning {
   244 -(void) didReceiveMemoryWarning {
   324     [super didReceiveMemoryWarning];
   245     [super didReceiveMemoryWarning];
   325 }
   246 }
   326 
   247 
   327 -(void) viewDidUnload {
   248 -(void) viewDidUnload {
   328     self.settingsDictionary = nil;
   249     self.settingsDictionary = nil;
   329     self.textFieldBeingEdited = nil;
   250     MSG_DIDUNLOAD();
   330     self.musicSwitch = nil;
       
   331     self.soundSwitch = nil;
       
   332     self.altDamageSwitch = nil;
       
   333     [super viewDidUnload];
   251     [super viewDidUnload];
   334     MSG_DIDUNLOAD();
       
   335 }
   252 }
   336 
   253 
   337 -(void) dealloc {
   254 -(void) dealloc {
   338     [settingsDictionary release];
   255     [settingsDictionary release];
   339     [textFieldBeingEdited release];
       
   340     [musicSwitch release];
       
   341     [soundSwitch release];
       
   342     [altDamageSwitch release];
       
   343     [super dealloc];
   256     [super dealloc];
   344 }
   257 }
   345 
   258 
   346 
       
   347 @end
   259 @end