project_files/HedgewarsMobile/Classes/MapConfigViewController.m
changeset 3642 fb39fecca350
parent 3625 9f1d79e01a60
child 3660 bc125bea5849
equal deleted inserted replaced
3641:98319a621dc8 3642:fb39fecca350
    14 #import <pthread.h>
    14 #import <pthread.h>
    15 
    15 
    16 #define INDICATOR_TAG 7654
    16 #define INDICATOR_TAG 7654
    17 
    17 
    18 @implementation MapConfigViewController
    18 @implementation MapConfigViewController
    19 @synthesize previewButton, maxHogs, seedCommand, templateFilterCommand, mapGenCommand, mazeSizeCommand, themeCommand,
    19 @synthesize previewButton, maxHogs, seedCommand, templateFilterCommand, mapGenCommand, mazeSizeCommand, themeCommand, staticMapCommand,
    20             tableView, maxLabel, sizeLabel, segmentedControl, slider, lastIndexPath, themeArray, mapArray, busy;
    20             tableView, maxLabel, sizeLabel, segmentedControl, slider, lastIndexPath, themeArray, mapArray, busy;
    21 
    21 
    22 
    22 
    23 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    23 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    24     return rotationManager(interfaceOrientation);
    24     return rotationManager(interfaceOrientation);
   189     [self.tableView reloadData];
   189     [self.tableView reloadData];
   190     [self tableView:self.tableView didSelectRowAtIndexPath:theIndex];
   190     [self tableView:self.tableView didSelectRowAtIndexPath:theIndex];
   191     [self.tableView scrollToRowAtIndexPath:theIndex atScrollPosition:UITableViewScrollPositionNone animated:YES];
   191     [self.tableView scrollToRowAtIndexPath:theIndex atScrollPosition:UITableViewScrollPositionNone animated:YES];
   192 }
   192 }
   193 
   193 
       
   194 // instead of drawing a random map we load an image; this function is called by didSelectRowAtIndexPath only
   194 -(void) updatePreviewWithMap:(NSInteger) index {
   195 -(void) updatePreviewWithMap:(NSInteger) index {
   195     // change the preview button
   196     // change the preview button
   196     NSString *fileImage = [[NSString alloc] initWithFormat:@"%@/%@/preview.png", MAPS_DIRECTORY(),[self.mapArray objectAtIndex:index]];
   197     NSString *fileImage = [[NSString alloc] initWithFormat:@"%@/%@/preview.png", MAPS_DIRECTORY(),[self.mapArray objectAtIndex:index]];
   197     UIImage *image = [[UIImage alloc] initWithContentsOfFile:fileImage];
   198     UIImage *image = [[UIImage alloc] initWithContentsOfFile:fileImage];
   198     [fileImage release];
   199     [fileImage release];
   203     maxHogs = 18;
   204     maxHogs = 18;
   204     NSString *fileCfg = [[NSString alloc] initWithFormat:@"%@/%@/map.cfg", MAPS_DIRECTORY(),[self.mapArray objectAtIndex:index]];
   205     NSString *fileCfg = [[NSString alloc] initWithFormat:@"%@/%@/map.cfg", MAPS_DIRECTORY(),[self.mapArray objectAtIndex:index]];
   205     NSString *contents = [[NSString alloc] initWithContentsOfFile:fileCfg encoding:NSUTF8StringEncoding error:NULL];
   206     NSString *contents = [[NSString alloc] initWithContentsOfFile:fileCfg encoding:NSUTF8StringEncoding error:NULL];
   206     [fileCfg release];
   207     [fileCfg release];
   207     NSArray *split = [contents componentsSeparatedByString:@"\n"];
   208     NSArray *split = [contents componentsSeparatedByString:@"\n"];
   208 
   209     [contents release];
       
   210 
       
   211     // set the theme and map here
       
   212     self.themeCommand = [NSString stringWithFormat:@"etheme %@", [split objectAtIndex:0]];
       
   213     self.staticMapCommand = [NSString stringWithFormat:@"emap %@", [self.mapArray objectAtIndex:index]];
       
   214     
   209     // if the number is not set we keep 18 standard; 
   215     // if the number is not set we keep 18 standard; 
   210     // sometimes it's not set but there are trailing characters, we get around them with the second equation
   216     // sometimes it's not set but there are trailing characters, we get around them with the second equation
   211     if ([split count] > 1 && [[split objectAtIndex:1] intValue] > 0)
   217     if ([split count] > 1 && [[split objectAtIndex:1] intValue] > 0)
   212         maxHogs = [[split objectAtIndex:1] intValue];
   218         maxHogs = [[split objectAtIndex:1] intValue];
   213     [contents release];
       
   214     NSString *max = [[NSString alloc] initWithFormat:@"%d",maxHogs];
   219     NSString *max = [[NSString alloc] initWithFormat:@"%d",maxHogs];
   215     self.maxLabel.text = max;
   220     self.maxLabel.text = max;
   216     [max release];
   221     [max release];
   217 }
   222 }
   218 
   223 
   314     int newRow = [indexPath row];
   319     int newRow = [indexPath row];
   315     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
   320     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
   316     
   321     
   317     if (newRow != oldRow) {
   322     if (newRow != oldRow) {
   318         if (self.segmentedControl.selectedSegmentIndex != 1) {
   323         if (self.segmentedControl.selectedSegmentIndex != 1) {
   319             NSString *theme = [self.themeArray objectAtIndex:newRow];
   324             NSString *theme = [self.themeArray objectAtIndex:newRow];                
   320             self.themeCommand =  [NSString stringWithFormat:@"etheme %@", theme];
   325             self.themeCommand = [NSString stringWithFormat:@"etheme %@", theme];
   321         } else
   326         } else {
       
   327             // theme and map are set in the function below
   322             [self updatePreviewWithMap:newRow];
   328             [self updatePreviewWithMap:newRow];
   323 
   329         }
       
   330         
   324         UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath]; 
   331         UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath]; 
   325         newCell.accessoryType = UITableViewCellAccessoryCheckmark;
   332         newCell.accessoryType = UITableViewCellAccessoryCheckmark;
   326         UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:self.lastIndexPath];
   333         UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:self.lastIndexPath];
   327         oldCell.accessoryType = UITableViewCellAccessoryNone;
   334         oldCell.accessoryType = UITableViewCellAccessoryNone;
   328 
   335 
   419 
   426 
   420 // perform actions based on the activated section, then call updatePreview to visually update the selection 
   427 // perform actions based on the activated section, then call updatePreview to visually update the selection 
   421 // updatePreview will call didSelectRowAtIndexPath which will call the right update routine)
   428 // updatePreview will call didSelectRowAtIndexPath which will call the right update routine)
   422 // and if necessary update the table with a slide animation
   429 // and if necessary update the table with a slide animation
   423 -(IBAction) segmentedControlChanged:(id) sender {
   430 -(IBAction) segmentedControlChanged:(id) sender {
   424     NSString *mapgen;
   431     NSString *mapgen, *staticmap;
   425     NSInteger newPage = self.segmentedControl.selectedSegmentIndex;
   432     NSInteger newPage = self.segmentedControl.selectedSegmentIndex;
   426     
   433     
   427     switch (newPage) {
   434     switch (newPage) {
   428         case 0: // Random
   435         case 0: // Random
   429             mapgen = @"e$mapgen 0";
   436             mapgen = @"e$mapgen 0";
       
   437             staticmap = @"";
   430             [self sliderChanged:nil];
   438             [self sliderChanged:nil];
   431             self.slider.enabled = YES;
   439             self.slider.enabled = YES;
   432             break;
   440             break;
   433             
   441             
   434         case 1: // Map
   442         case 1: // Map
   435             mapgen = @"e$mapgen 0";
   443             mapgen = @"e$mapgen 0";
       
   444             // dummy value, everything is set by -updatePreview -> -didSelectRowAtIndexPath -> -updatePreviewWithMap
       
   445             staticmap = @"map Bamboo";
   436             self.slider.enabled = NO;
   446             self.slider.enabled = NO;
   437             self.sizeLabel.text = @".";
   447             self.sizeLabel.text = @".";
   438             [self restoreBackgroundImage];
   448             [self restoreBackgroundImage];
   439             break;
   449             break;
   440             
   450             
   441         case 2: // Maze
   451         case 2: // Maze
   442             mapgen = @"e$mapgen 1";
   452             mapgen = @"e$mapgen 1";
       
   453             staticmap = @"";
   443             [self sliderChanged:nil];
   454             [self sliderChanged:nil];
   444             self.slider.enabled = YES;
   455             self.slider.enabled = YES;
   445             break;
   456             break;
   446         
   457         
   447         default:
   458         default:
   448             mapgen = nil;
   459             mapgen = nil;
       
   460             staticmap = nil;
   449             break;
   461             break;
   450     }
   462     }
   451     self.mapGenCommand = mapgen;
   463     self.mapGenCommand = mapgen;
       
   464     self.staticMapCommand = staticmap;
   452     [self updatePreview];
   465     [self updatePreview];
   453     
   466     
   454     // nice animation for updating the table when appropriate (on iphone)
   467     // nice animation for updating the table when appropriate (on iphone)
   455     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
   468     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
   456         if (((oldPage == 0 || oldPage == 2) && newPage == 1) ||
   469         if (((oldPage == 0 || oldPage == 2) && newPage == 1) ||
   506     self.segmentedControl.selectedSegmentIndex = 0;
   519     self.segmentedControl.selectedSegmentIndex = 0;
   507 
   520 
   508     self.templateFilterCommand = @"e$template_filter 0";
   521     self.templateFilterCommand = @"e$template_filter 0";
   509     self.mazeSizeCommand = @"e$maze_size 0";
   522     self.mazeSizeCommand = @"e$maze_size 0";
   510     self.mapGenCommand = @"e$mapgen 0";
   523     self.mapGenCommand = @"e$mapgen 0";
       
   524     self.staticMapCommand = @"";
       
   525     
   511     self.lastIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
   526     self.lastIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
   512     
   527     
   513     oldValue = 5;
   528     oldValue = 5;
   514     oldPage = 0;
   529     oldPage = 0;
   515 }
   530 }
   530     self.seedCommand = nil;
   545     self.seedCommand = nil;
   531     self.templateFilterCommand = nil;
   546     self.templateFilterCommand = nil;
   532     self.mapGenCommand = nil;
   547     self.mapGenCommand = nil;
   533     self.mazeSizeCommand = nil;
   548     self.mazeSizeCommand = nil;
   534     self.themeCommand = nil;
   549     self.themeCommand = nil;
       
   550     self.staticMapCommand = nil;
   535     
   551     
   536     self.previewButton = nil;
   552     self.previewButton = nil;
   537     self.tableView = nil;
   553     self.tableView = nil;
   538     self.maxLabel = nil;
   554     self.maxLabel = nil;
   539     self.sizeLabel = nil;
   555     self.sizeLabel = nil;
   552     [seedCommand release];
   568     [seedCommand release];
   553     [templateFilterCommand release];
   569     [templateFilterCommand release];
   554     [mapGenCommand release];
   570     [mapGenCommand release];
   555     [mazeSizeCommand release];
   571     [mazeSizeCommand release];
   556     [themeCommand release];
   572     [themeCommand release];
       
   573     [staticMapCommand release];
   557     
   574     
   558     [previewButton release];
   575     [previewButton release];
   559     [tableView release];
   576     [tableView release];
   560     [maxLabel release];
   577     [maxLabel release];
   561     [sizeLabel release];
   578     [sizeLabel release];