project_files/HedgewarsMobile/Classes/SingleSchemeViewController.m
changeset 3659 f8d5ac50e307
parent 3643 858b20bafb6e
child 3660 bc125bea5849
equal deleted inserted replaced
3658:113cb9345be1 3659:f8d5ac50e307
    13 
    13 
    14 #define LABEL_TAG  12345
    14 #define LABEL_TAG  12345
    15 #define SLIDER_TAG 54321
    15 #define SLIDER_TAG 54321
    16 
    16 
    17 @implementation SingleSchemeViewController
    17 @implementation SingleSchemeViewController
    18 @synthesize textFieldBeingEdited, schemeArray, basicSettingList, gameModifierArray;
    18 @synthesize schemeName, schemeArray, basicSettingList, gameModifierArray;
    19 
    19 
    20 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    20 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    21     return rotationManager(interfaceOrientation);
    21     return rotationManager(interfaceOrientation);
    22 }
    22 }
    23 
    23 
   102                               [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Explosives",@""),@"title",@"Damage",@"image",
   102                               [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Explosives",@""),@"title",@"Damage",@"image",
   103                                [NSNumber numberWithInt:2],@"default",[NSNumber numberWithInt:0],@"min",[NSNumber numberWithInt:40],@"max",nil],
   103                                [NSNumber numberWithInt:2],@"default",[NSNumber numberWithInt:0],@"min",[NSNumber numberWithInt:40],@"max",nil],
   104                               nil];
   104                               nil];
   105     self.basicSettingList = basicSettings;
   105     self.basicSettingList = basicSettings;
   106     [basicSettings release];
   106     [basicSettings release];
       
   107     
       
   108     self.title = NSLocalizedString(@"Edit scheme preferences",@"");
   107 }
   109 }
   108 
   110 
   109 // load from file
   111 // load from file
   110 -(void) viewWillAppear:(BOOL) animated {
   112 -(void) viewWillAppear:(BOOL) animated {
   111     [super viewWillAppear:animated];
   113     [super viewWillAppear:animated];
   112     
   114     
   113     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.title];
   115     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName];
   114     NSMutableArray *scheme = [[NSMutableArray alloc] initWithContentsOfFile:schemeFile];
   116     NSMutableArray *scheme = [[NSMutableArray alloc] initWithContentsOfFile:schemeFile];
   115     [schemeFile release];
   117     [schemeFile release];
   116     self.schemeArray = scheme;
   118     self.schemeArray = scheme;
   117     [scheme release];
   119     [scheme release];
   118     
   120     
   121 
   123 
   122 // save to file
   124 // save to file
   123 -(void) viewWillDisappear:(BOOL) animated {
   125 -(void) viewWillDisappear:(BOOL) animated {
   124     [super viewWillDisappear:animated];
   126     [super viewWillDisappear:animated];
   125     
   127     
   126     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.title];
   128     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName];
   127     [self.schemeArray writeToFile:schemeFile atomically:YES];
   129     [self.schemeArray writeToFile:schemeFile atomically:YES];
   128     [schemeFile release];
   130     [schemeFile release];
   129 }
   131 }
   130 
   132 
   131 #pragma mark -
   133 #pragma mark -
   132 #pragma mark textfield methods
   134 #pragma mark editableCellView delegate
   133 -(void) cancel:(id) sender {
       
   134     if (textFieldBeingEdited != nil)
       
   135         [self.textFieldBeingEdited resignFirstResponder];
       
   136 }
       
   137 
       
   138 // set the new value
   135 // set the new value
   139 -(BOOL) save:(id) sender {    
   136 -(void) saveTextFieldValue:(NSString *)textString {    
   140     if (textFieldBeingEdited != nil) {
   137     // delete old file
   141         if ([textFieldBeingEdited.text length] == 0) 
   138     [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName] error:NULL];
   142             textFieldBeingEdited.text = self.title;
   139     // update filename
   143         
   140     self.schemeName = textString;
   144         [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.title] error:NULL];
   141     // save new file
   145         self.title = self.textFieldBeingEdited.text;
   142     [self.schemeArray writeToFile:[NSString stringWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName] atomically:YES];
   146         [self.schemeArray writeToFile:[NSString stringWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.title] atomically:YES];
   143 }
   147         [self.textFieldBeingEdited resignFirstResponder];
       
   148         return YES;
       
   149     }
       
   150     return NO;
       
   151 }
       
   152 
       
   153 // the textfield is being modified, update the navigation controller
       
   154 -(void) textFieldDidBeginEditing:(UITextField *)aTextField{   
       
   155     self.textFieldBeingEdited = aTextField;
       
   156 
       
   157     UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel",@"from schemes table")
       
   158                                                                      style:UIBarButtonItemStylePlain
       
   159                                                                     target:self
       
   160                                                                     action:@selector(cancel:)];
       
   161     self.navigationItem.leftBarButtonItem = cancelButton;
       
   162     [cancelButton release];
       
   163     
       
   164     UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Save",@"from schemes table")
       
   165                                                                      style:UIBarButtonItemStyleDone
       
   166                                                                     target:self
       
   167                                                                     action:@selector(save:)];
       
   168     self.navigationItem.rightBarButtonItem = saveButton;
       
   169     [saveButton release];
       
   170 }
       
   171 
       
   172 // the textfield has been modified, check for empty strings and restore original navigation bar
       
   173 -(void) textFieldDidEndEditing:(UITextField *)aTextField{
       
   174     if ([textFieldBeingEdited.text length] == 0) 
       
   175         textFieldBeingEdited.text = [NSString stringWithFormat:@"New Scheme"];
       
   176 
       
   177     self.textFieldBeingEdited = nil;
       
   178     self.navigationItem.rightBarButtonItem = self.navigationItem.backBarButtonItem;
       
   179     self.navigationItem.leftBarButtonItem = nil;
       
   180 }
       
   181 
       
   182 // limit the size of the field to 64 characters like in original frontend
       
   183 -(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
       
   184     int limit = 64;
       
   185     return !([textField.text length] > limit && [string length] > range.length);
       
   186 }
       
   187 
       
   188 
   144 
   189 #pragma mark -
   145 #pragma mark -
   190 #pragma mark Table view data source
   146 #pragma mark Table view data source
   191 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   147 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   192     return 3;
   148     return 3;
   212     static NSString *CellIdentifier0 = @"Cell0";
   168     static NSString *CellIdentifier0 = @"Cell0";
   213     static NSString *CellIdentifier1 = @"Cell1";
   169     static NSString *CellIdentifier1 = @"Cell1";
   214     static NSString *CellIdentifier2 = @"Cell2";
   170     static NSString *CellIdentifier2 = @"Cell2";
   215     
   171     
   216     UITableViewCell *cell = nil;
   172     UITableViewCell *cell = nil;
       
   173     EditableCellView *editableCell = nil;
   217     NSInteger row = [indexPath row];
   174     NSInteger row = [indexPath row];
   218     
   175     
   219     switch ([indexPath section]) {
   176     switch ([indexPath section]) {
   220         case 0:
   177         case 0:
   221             cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier0];
   178             editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier0];
   222             if (cell == nil) {
   179             if (editableCell == nil) {
   223                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
   180                 editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault 
   224                                                reuseIdentifier:CellIdentifier0] autorelease];
   181                                                reuseIdentifier:CellIdentifier0] autorelease];
   225                 // create a uitextfield for each row, expand it to take the maximum size
   182                 editableCell.delegate = self;
   226                 UITextField *aTextField = [[UITextField alloc] 
       
   227                                            initWithFrame:CGRectMake(5, 12, (cell.frame.size.width + cell.frame.size.width/3) - 42, 25)];
       
   228                 aTextField.clearsOnBeginEditing = NO;
       
   229                 aTextField.returnKeyType = UIReturnKeyDone;
       
   230                 aTextField.adjustsFontSizeToFitWidth = YES;
       
   231                 aTextField.delegate = self;
       
   232                 aTextField.tag = [indexPath row];
       
   233                 aTextField.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize] + 2];
       
   234                 aTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
       
   235                 [aTextField addTarget:self action:@selector(save:) forControlEvents:UIControlEventEditingDidEndOnExit];
       
   236                 [cell.contentView addSubview:aTextField];
       
   237                 [aTextField release];
       
   238             }
   183             }
   239             
   184             
   240             for (UIView *oneView in cell.contentView.subviews) {
   185             editableCell.textField.text = self.schemeName;
   241                 if ([oneView isMemberOfClass:[UITextField class]]) {
   186             editableCell.detailTextLabel.text = nil;
   242                     // we find the uitextfied and we'll use its tag to understand which one is being edited
   187             editableCell.imageView.image = nil;
   243                     UITextField *textFieldFound = (UITextField *)oneView;
   188             editableCell.selectionStyle = UITableViewCellSelectionStyleNone;
   244                     textFieldFound.text = self.title;
   189             cell = editableCell;
   245                 }
       
   246             }
       
   247             cell.detailTextLabel.text = nil;
       
   248             cell.imageView.image = nil;
       
   249             cell.selectionStyle = UITableViewCellSelectionStyleNone;
       
   250             break;
   190             break;
   251         case 1:
   191         case 1:
   252             cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier1];
   192             cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier1];
   253             NSDictionary *detail = [self.basicSettingList objectAtIndex:row];
   193             NSDictionary *detail = [self.basicSettingList objectAtIndex:row];
   254             // need to offset this section (see format in CommodityFunctions.m and above)
   194             // need to offset this section (see format in CommodityFunctions.m and above)
   257                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 
   197                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 
   258                                                reuseIdentifier:CellIdentifier1] autorelease];
   198                                                reuseIdentifier:CellIdentifier1] autorelease];
   259                 
   199                 
   260                 int offset = 0;
   200                 int offset = 0;
   261                 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
   201                 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
   262                     offset = 45;
   202                     offset = 50;
   263                 
   203                 
   264                 UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(offset+260, 12, offset+150, 23)];
   204                 UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(offset+260, 12, offset+150, 23)];
   265                 slider.maximumValue = [[detail objectForKey:@"max"] floatValue];
   205                 slider.maximumValue = [[detail objectForKey:@"max"] floatValue];
   266                 slider.minimumValue = [[detail objectForKey:@"min"] floatValue];
   206                 slider.minimumValue = [[detail objectForKey:@"min"] floatValue];
   267                 slider.tag = row+gmSize;
   207                 slider.tag = row+gmSize;
   347 
   287 
   348 #pragma mark -
   288 #pragma mark -
   349 #pragma mark Table view delegate
   289 #pragma mark Table view delegate
   350 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   290 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   351     UITableViewCell *cell = [aTableView cellForRowAtIndexPath:indexPath];
   291     UITableViewCell *cell = [aTableView cellForRowAtIndexPath:indexPath];
   352     UISwitch *sw = nil;
   292     EditableCellView *editableCell = nil;
   353     UISlider *cellSlider = nil;
   293     UISlider *cellSlider = nil;
   354     
   294     
   355     switch ([indexPath section]) {
   295     switch ([indexPath section]) {
   356         case 0:
   296         case 0:
   357             for (UIView *oneView in cell.contentView.subviews) {
   297             editableCell = (EditableCellView *)cell;
   358                 if ([oneView isMemberOfClass:[UITextField class]]) {
   298             [editableCell replyKeyboard];
   359                     textFieldBeingEdited = (UITextField *)oneView;
       
   360                     [textFieldBeingEdited becomeFirstResponder];
       
   361                 }
       
   362             }
       
   363             break;
   299             break;
   364         case 1:
   300         case 1:
   365             cellSlider = (UISlider *)[cell.contentView viewWithTag:[indexPath row]+[self.gameModifierArray count]];
   301             cellSlider = (UISlider *)[cell.contentView viewWithTag:[indexPath row]+[self.gameModifierArray count]];
   366             [cellSlider setValue:[[[self.basicSettingList objectAtIndex:[indexPath row]] objectForKey:@"default"] floatValue] animated:YES];
   302             [cellSlider setValue:[[[self.basicSettingList objectAtIndex:[indexPath row]] objectForKey:@"default"] floatValue] animated:YES];
   367             [self sliderChanged:cellSlider];
   303             [self sliderChanged:cellSlider];
   377     }
   313     }
   378     
   314     
   379     [aTableView deselectRowAtIndexPath:indexPath animated:YES];
   315     [aTableView deselectRowAtIndexPath:indexPath animated:YES];
   380 }
   316 }
   381 
   317 
       
   318 -(NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section {
       
   319     NSString *sectionTitle = nil;
       
   320     switch (section) {
       
   321         case 0:
       
   322             sectionTitle = NSLocalizedString(@"Scheme Name", @"");
       
   323             break;
       
   324         case 1:
       
   325             sectionTitle = NSLocalizedString(@"Game Settings", @"");
       
   326             break;
       
   327         case 2:
       
   328             sectionTitle = NSLocalizedString(@"Game Modifiers", @"");
       
   329             break;
       
   330         default:
       
   331             DLog(@"nope");
       
   332             break;
       
   333     }
       
   334     return sectionTitle;
       
   335 }
   382 
   336 
   383 #pragma mark -
   337 #pragma mark -
   384 #pragma mark Memory management
   338 #pragma mark Memory management
   385 -(void) didReceiveMemoryWarning {
   339 -(void) didReceiveMemoryWarning {
   386     [super didReceiveMemoryWarning];
   340     [super didReceiveMemoryWarning];
   387 }
   341 }
   388 
   342 
   389 -(void) viewDidUnload {
   343 -(void) viewDidUnload {
   390     self.textFieldBeingEdited = nil;
   344     self.schemeName = nil;
   391     self.schemeArray = nil;
   345     self.schemeArray = nil;
   392     self.basicSettingList = nil;
   346     self.basicSettingList = nil;
   393     self.gameModifierArray = nil;
   347     self.gameModifierArray = nil;
   394     [super viewDidUnload];
   348     [super viewDidUnload];
   395     MSG_DIDUNLOAD();
   349     MSG_DIDUNLOAD();
   396 }
   350 }
   397 
   351 
   398 -(void) dealloc {
   352 -(void) dealloc {
   399     [textFieldBeingEdited release];
   353     [schemeName release];
   400     [schemeArray release];
   354     [schemeArray release];
   401     [basicSettingList release];
   355     [basicSettingList release];
   402     [gameModifierArray release];
   356     [gameModifierArray release];
   403     [super dealloc];
   357     [super dealloc];
   404 }
   358 }
   405 
   359 
   406 
       
   407 @end
   360 @end
   408