project_files/HedgewarsMobile/Classes/MapConfigViewController.m
changeset 6115 485cfecadc9a
parent 6109 f6726ec81e64
child 6634 e00762923086
equal deleted inserted replaced
6114:443cce9e060f 6115:485cfecadc9a
    29 #define isRandomness()  (segmentedControl.selectedSegmentIndex == 0 || segmentedControl.selectedSegmentIndex == 2)
    29 #define isRandomness()  (segmentedControl.selectedSegmentIndex == 0 || segmentedControl.selectedSegmentIndex == 2)
    30 
    30 
    31 @implementation MapConfigViewController
    31 @implementation MapConfigViewController
    32 @synthesize previewButton, maxHogs, seedCommand, templateFilterCommand, mapGenCommand, mazeSizeCommand, themeCommand, staticMapCommand,
    32 @synthesize previewButton, maxHogs, seedCommand, templateFilterCommand, mapGenCommand, mazeSizeCommand, themeCommand, staticMapCommand,
    33             missionCommand, tableView, maxLabel, sizeLabel, segmentedControl, slider, lastIndexPath, dataSourceArray, busy,
    33             missionCommand, tableView, maxLabel, sizeLabel, segmentedControl, slider, lastIndexPath, dataSourceArray, busy,
    34             externalController, parentController;
    34             oldPage, oldValue;
    35 
    35 
    36 
    36 
    37 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    37 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    38     return rotationManager(interfaceOrientation);
    38     return rotationManager(interfaceOrientation);
    39 }
    39 }
    43     [self updatePreview];
    43     [self updatePreview];
    44 }
    44 }
    45 
    45 
    46 -(void) updatePreview {
    46 -(void) updatePreview {
    47     // don't generate a new preview while it's already generating one
    47     // don't generate a new preview while it's already generating one
    48     if (busy)
    48     if (self.busy)
    49         return;
    49         return;
    50 
    50 
    51     // generate a seed
    51     // generate a seed
    52     CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
    52     CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
    53     NSString *seed = (NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid);
    53     NSString *seed = (NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid);
    74 
    74 
    75 -(void) turnOffWidgets {
    75 -(void) turnOffWidgets {
    76     busy = YES;
    76     busy = YES;
    77     self.previewButton.alpha = 0.5f;
    77     self.previewButton.alpha = 0.5f;
    78     self.previewButton.enabled = NO;
    78     self.previewButton.enabled = NO;
    79     self.maxLabel.text = @"...";
    79     self.maxLabel.text = NSLocalizedString(@"Loading...",@"");;
    80     self.segmentedControl.enabled = NO;
    80     self.segmentedControl.enabled = NO;
    81     self.slider.enabled = NO;
    81     self.slider.enabled = NO;
    82 }
    82 }
    83 
    83 
       
    84 #pragma mark -
       
    85 #pragma mark MapPreviewButtonView delegate methods
    84 -(void) turnOnWidgets {
    86 -(void) turnOnWidgets {
    85     self.previewButton.alpha = 1.0f;
    87     self.previewButton.alpha = 1.0f;
    86     self.previewButton.enabled = YES;
    88     self.previewButton.enabled = YES;
    87     self.segmentedControl.enabled = YES;
    89     self.segmentedControl.enabled = YES;
    88     self.slider.enabled = YES;
    90     self.slider.enabled = YES;
    89     busy = NO;
    91     self.busy = NO;
    90 }
    92 }
    91 
    93 
    92 -(void) setLabelText:(NSString *)str {
    94 -(void) setMaxLabelText:(NSString *)str {
    93     self.maxHogs = [str intValue];
    95     self.maxHogs = [str intValue];
    94     self.maxLabel.text = str;
    96     self.maxLabel.text = [NSString stringWithFormat:@"%@ %@",NSLocalizedString(@"Max Hogs:",@""),str];
    95 }
    97 }
    96 
    98 
    97 -(NSDictionary *)getDataForEngine {
    99 -(NSDictionary *)getDataForEngine {
    98     NSDictionary *dictForEngine = [NSDictionary dictionaryWithObjectsAndKeys:
   100     NSDictionary *dictForEngine = [NSDictionary dictionaryWithObjectsAndKeys:
    99                                    self.seedCommand,@"seedCommand",
   101                                    self.seedCommand,@"seedCommand",
   160     NSArray *split = [contents componentsSeparatedByString:@"\n"];
   162     NSArray *split = [contents componentsSeparatedByString:@"\n"];
   161     [contents release];
   163     [contents release];
   162 
   164 
   163     // if the number is not set we keep 18 standard;
   165     // if the number is not set we keep 18 standard;
   164     // sometimes it's not set but there are trailing characters, we get around them with the second equation
   166     // sometimes it's not set but there are trailing characters, we get around them with the second equation
       
   167     NSString *max;
   165     if ([split count] > 1 && [[split objectAtIndex:1] intValue] > 0)
   168     if ([split count] > 1 && [[split objectAtIndex:1] intValue] > 0)
   166         maxHogs = [[split objectAtIndex:1] intValue];
   169         max = [split objectAtIndex:1];
   167     else
   170     else
   168         maxHogs = 18;
   171         max = @"18";
   169     NSString *max = [[NSString alloc] initWithFormat:@"%d",maxHogs];
   172     [self setMaxLabelText:max];
   170     self.maxLabel.text = max;
       
   171     [max release];
       
   172     
   173     
   173     self.themeCommand = [NSString stringWithFormat:@"etheme %@", [split objectAtIndex:0]];
   174     self.themeCommand = [NSString stringWithFormat:@"etheme %@", [split objectAtIndex:0]];
   174     self.staticMapCommand = [NSString stringWithFormat:@"emap %@", [source objectAtIndex:index]];
   175     self.staticMapCommand = [NSString stringWithFormat:@"emap %@", [source objectAtIndex:index]];
   175 
   176 
   176     if (scIndex != 3)
   177     if (scIndex != 3)
   308             mapgen = @"e$mapgen 0";
   309             mapgen = @"e$mapgen 0";
   309             staticmap = @"";
   310             staticmap = @"";
   310             mission = @"";
   311             mission = @"";
   311             [self sliderChanged:nil];
   312             [self sliderChanged:nil];
   312             self.slider.enabled = YES;
   313             self.slider.enabled = YES;
   313             [externalController fillSections];
   314             [SchemeWeaponConfigViewController fillInstanceSections];
   314             break;
   315             break;
   315 
   316 
   316         case 1: // Map
   317         case 1: // Map
   317             mapgen = @"e$mapgen 0";
   318             mapgen = @"e$mapgen 0";
   318             // dummy values, these are set by -updatePreview -> -didSelectRowAtIndexPath -> -setDetailsForStaticMap
   319             // dummy values, these are set by -updatePreview -> -didSelectRowAtIndexPath -> -setDetailsForStaticMap
   319             staticmap = @"map Bamboo";
   320             staticmap = @"map Bamboo";
   320             mission = @"";
   321             mission = @"";
   321             self.slider.enabled = NO;
   322             self.slider.enabled = NO;
   322             self.sizeLabel.text = NSLocalizedString(@"No filter",@"");
   323             self.sizeLabel.text = NSLocalizedString(@"No filter",@"");
   323             [externalController fillSections];
   324             [SchemeWeaponConfigViewController fillInstanceSections];
   324             break;
   325             break;
   325 
   326 
   326         case 2: // Maze
   327         case 2: // Maze
   327             mapgen = @"e$mapgen 1";
   328             mapgen = @"e$mapgen 1";
   328             staticmap = @"";
   329             staticmap = @"";
   329             mission = @"";
   330             mission = @"";
   330             [self sliderChanged:nil];
   331             [self sliderChanged:nil];
   331             self.slider.enabled = YES;
   332             self.slider.enabled = YES;
   332             [externalController fillSections];
   333             [SchemeWeaponConfigViewController fillInstanceSections];
   333             break;
   334             break;
   334 
   335 
   335         case 3: // Mission
   336         case 3: // Mission
   336             mapgen = @"e$mapgen 0";
   337             mapgen = @"e$mapgen 0";
   337             // dummy values, these are set by -updatePreview -> -didSelectRowAtIndexPath -> -setDetailsForStaticMap
   338             // dummy values, these are set by -updatePreview -> -didSelectRowAtIndexPath -> -setDetailsForStaticMap
   338             staticmap = @"map Bamboo";
   339             staticmap = @"map Bamboo";
   339             mission = @"";
   340             mission = @"";
   340             self.slider.enabled = NO;
   341             self.slider.enabled = NO;
   341             self.sizeLabel.text = NSLocalizedString(@"No filter",@"");
   342             self.sizeLabel.text = NSLocalizedString(@"No filter",@"");
   342             [externalController emptySections];
   343             [SchemeWeaponConfigViewController emptyInstanceSections];
   343             break;
   344             break;
   344 
   345 
   345         default:
   346         default:
   346             mapgen = nil;
   347             mapgen = nil;
   347             staticmap = nil;
   348             staticmap = nil;
   353     self.missionCommand = mission;
   354     self.missionCommand = mission;
   354 
   355 
   355     [self.tableView reloadData];
   356     [self.tableView reloadData];
   356     [self updatePreview];
   357     [self updatePreview];
   357     oldPage = newPage;
   358     oldPage = newPage;
   358 }
       
   359 
       
   360 -(IBAction) buttonPressed:(id) sender {
       
   361     [self.parentController buttonPressed:sender];
       
   362 }
   359 }
   363 
   360 
   364 #pragma mark -
   361 #pragma mark -
   365 #pragma mark view management
   362 #pragma mark view management
   366 -(NSArray *) dataSourceArray {
   363 -(NSArray *) dataSourceArray {
   410     return dataSourceArray;
   407     return dataSourceArray;
   411 }
   408 }
   412 
   409 
   413 -(MapPreviewButtonView *)previewButton {
   410 -(MapPreviewButtonView *)previewButton {
   414     if (previewButton == nil) {
   411     if (previewButton == nil) {
   415         MapPreviewButtonView *preview = [[MapPreviewButtonView alloc] initWithFrame:CGRectMake(736, 26, 256, 128)];
   412         MapPreviewButtonView *preview = [[MapPreviewButtonView alloc] initWithFrame:CGRectMake(32, 26, 256, 128)];
   416         preview.delegate = self;
   413         preview.delegate = self;
   417         [preview addTarget:self action:@selector(mapButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
   414         [preview addTarget:self action:@selector(mapButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
   418         [self.view addSubview:preview];
   415         [self.view addSubview:preview];
   419         self.previewButton = preview;
   416         self.previewButton = preview;
   420         [preview release];
   417         [preview release];
   425 -(void) viewDidLoad {
   422 -(void) viewDidLoad {
   426     [super viewDidLoad];
   423     [super viewDidLoad];
   427 
   424 
   428     srandom(time(NULL));
   425     srandom(time(NULL));
   429 
   426 
       
   427     /*
   430     CGSize screenSize = [[UIScreen mainScreen] bounds].size;
   428     CGSize screenSize = [[UIScreen mainScreen] bounds].size;
   431     self.view.frame = CGRectMake(0, 0, screenSize.height, screenSize.width - 44);
   429     self.view.frame = CGRectMake(0, 0, screenSize.height, screenSize.width - 44);
       
   430     */
   432     
   431     
   433     // initialize some "default" values
   432     // initialize some "default" values
   434     self.sizeLabel.text = NSLocalizedString(@"All",@"");
       
   435     self.slider.value = 0.05f;
   433     self.slider.value = 0.05f;
   436     oldValue = 5;
   434     self.slider.enabled = NO;
   437     
   435     self.sizeLabel.text = NSLocalizedString(@"No filter",@"");
   438     busy = NO;
   436     self.oldValue = 5;
   439     self.lastIndexPath = [NSIndexPath indexPathForRow:-1 inSection:0];
   437     self.busy = NO;
   440     
   438     self.oldPage = self.segmentedControl.selectedSegmentIndex;
   441     // select a map at first because it's faster - done in IB
       
   442     oldPage = 1;
       
   443     if (self.segmentedControl.selectedSegmentIndex == 1) {
       
   444         self.slider.enabled = NO;
       
   445         self.sizeLabel.text = NSLocalizedString(@"No filter",@"");
       
   446     }
       
   447 
   439 
   448     self.templateFilterCommand = @"e$template_filter 0";
   440     self.templateFilterCommand = @"e$template_filter 0";
   449     self.mazeSizeCommand = @"e$maze_size 0";
   441     self.mazeSizeCommand = @"e$maze_size 0";
   450     self.mapGenCommand = @"e$mapgen 0";
   442     self.mapGenCommand = @"e$mapgen 0";
   451     self.staticMapCommand = @"";
   443     self.staticMapCommand = @"";
   455         [self.tableView setBackgroundColorForAnyTable:[UIColor darkBlueColorTransparent]];
   447         [self.tableView setBackgroundColorForAnyTable:[UIColor darkBlueColorTransparent]];
   456         self.tableView.layer.borderColor = [[UIColor darkYellowColor] CGColor];
   448         self.tableView.layer.borderColor = [[UIColor darkYellowColor] CGColor];
   457         self.tableView.layer.borderWidth = 2.7f;
   449         self.tableView.layer.borderWidth = 2.7f;
   458         self.tableView.layer.cornerRadius = 8;
   450         self.tableView.layer.cornerRadius = 8;
   459         self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 10, 0);
   451         self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 10, 0);
       
   452 
       
   453         UILabel *backLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 14, 300, 190) andTitle:nil withBorderWidth:2.3f];
       
   454         [self.view insertSubview:backLabel belowSubview:self.segmentedControl];
       
   455         [backLabel release];
   460     }
   456     }
   461     self.tableView.separatorColor = [UIColor whiteColor];
   457     self.tableView.separatorColor = [UIColor whiteColor];
   462     self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
   458     self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
   463 }
   459 }
   464 
   460