project_files/HedgewarsMobile/Classes/GameConfigViewController.m
branchios-develop
changeset 12872 00215a7ec5f5
parent 12870 350d8bde5ccd
equal deleted inserted replaced
12871:2c06b1120749 12872:00215a7ec5f5
    33 
    33 
    34 @implementation GameConfigViewController
    34 @implementation GameConfigViewController
    35 @synthesize imgContainer, titleImage, sliderBackground, helpPage,
    35 @synthesize imgContainer, titleImage, sliderBackground, helpPage,
    36             mapConfigViewController, teamConfigViewController, schemeWeaponConfigViewController;
    36             mapConfigViewController, teamConfigViewController, schemeWeaponConfigViewController;
    37 
    37 
    38 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    38 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    39     return rotationManager(interfaceOrientation);
    39     return rotationManager(interfaceOrientation);
    40 }
    40 }
    41 
    41 
    42 #pragma mark - Buttons
    42 #pragma mark - Buttons
    43 
    43 
    44 -(IBAction) buttonPressed:(id) sender {
    44 - (IBAction)buttonPressed:(id)sender {
    45     UIButton *theButton = (UIButton *)sender;
    45     UIButton *theButton = (UIButton *)sender;
    46 
    46 
    47     switch (theButton.tag) {
    47     switch (theButton.tag) {
    48         case 0:
    48         case 0:
    49             if ([self.mapConfigViewController busy]) {
    49             if ([self.mapConfigViewController busy]) {
    51                                                                 message:NSLocalizedString(@"Before returning the preview needs to be generated",@"")
    51                                                                 message:NSLocalizedString(@"Before returning the preview needs to be generated",@"")
    52                                                                delegate:nil
    52                                                                delegate:nil
    53                                                       cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"")
    53                                                       cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"")
    54                                                       otherButtonTitles:nil];
    54                                                       otherButtonTitles:nil];
    55                 [alert show];
    55                 [alert show];
    56                 [alert release];
       
    57             } else {
    56             } else {
    58                 [[AudioManagerController mainManager] playBackSound];
    57                 [[AudioManagerController mainManager] playBackSound];
    59                 [[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];
    58                 [[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];
    60             }
    59             }
    61             break;
    60             break;
    66             theButton.enabled = NO;
    65             theButton.enabled = NO;
    67             [self clearImgContainer];
    66             [self clearImgContainer];
    68             [self startGame:theButton];
    67             [self startGame:theButton];
    69 
    68 
    70             break;
    69             break;
    71         case 2:
    70         case 2: {
    72             [[AudioManagerController mainManager] playClickSound];
    71             [[AudioManagerController mainManager] playClickSound];
    73             if (self.helpPage == nil)
    72             if (self.helpPage == nil)
    74                 self.helpPage = [[HelpPageLobbyViewController alloc] initWithNibName:@"HelpPageLobbyViewController-iPad" bundle:nil];
    73                 self.helpPage = [[HelpPageLobbyViewController alloc] initWithNibName:@"HelpPageLobbyViewController-iPad" bundle:nil];
    75             self.helpPage.view.alpha = 0;
    74             self.helpPage.view.alpha = 0;
    76             self.helpPage.view.frame = self.view.frame;
    75             self.helpPage.view.frame = self.view.frame;
    77             [self.view addSubview:self.helpPage.view];
    76             [self.view addSubview:self.helpPage.view];
    78             [UIView animateWithDuration:0.5 animations:^{
    77             [UIView animateWithDuration:0.5 animations:^{
    79                 self.helpPage.view.alpha = 1;
    78                 self.helpPage.view.alpha = 1;
    80             }];
    79             }];
    81             break;
    80             break;
       
    81         }
    82         default:
    82         default:
    83             DLog(@"Nope");
    83             DLog(@"Nope");
    84             break;
    84             break;
    85     }
    85     }
    86 }
    86 }
    94         NSString *oldTitle = [self.tabsSegmentedControl titleForSegmentAtIndex:i];
    94         NSString *oldTitle = [self.tabsSegmentedControl titleForSegmentAtIndex:i];
    95         [self.tabsSegmentedControl setTitle:NSLocalizedString(oldTitle, nil) forSegmentAtIndex:i];
    95         [self.tabsSegmentedControl setTitle:NSLocalizedString(oldTitle, nil) forSegmentAtIndex:i];
    96     }
    96     }
    97 }
    97 }
    98 
    98 
    99 -(IBAction) segmentPressed:(id) sender {
    99 - (IBAction)segmentPressed:(id)sender {
   100 
   100 
   101     UISegmentedControl *theSegment = (UISegmentedControl *)sender;
   101     UISegmentedControl *theSegment = (UISegmentedControl *)sender;
   102 
   102 
   103     [[AudioManagerController mainManager] playSelectSound];
   103     [[AudioManagerController mainManager] playSelectSound];
   104     switch (theSegment.selectedSegmentIndex) {
   104     switch (theSegment.selectedSegmentIndex) {
   136 
   136 
   137 }
   137 }
   138 
   138 
   139 #pragma mark -
   139 #pragma mark -
   140 
   140 
   141 -(BOOL) isEverythingSet {
   141 - (BOOL)isEverythingSet {
   142     // don't start playing if the preview is in progress
   142     // don't start playing if the preview is in progress
   143     if ([self.mapConfigViewController busy]) {
   143     if ([self.mapConfigViewController busy]) {
   144         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Wait for the Preview",@"")
   144         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Wait for the Preview",@"")
   145                                                         message:NSLocalizedString(@"Before playing the preview needs to be generated",@"")
   145                                                         message:NSLocalizedString(@"Before playing the preview needs to be generated",@"")
   146                                                        delegate:nil
   146                                                        delegate:nil
   147                                               cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"")
   147                                               cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"")
   148                                               otherButtonTitles:nil];
   148                                               otherButtonTitles:nil];
   149         [alert show];
   149         [alert show];
   150         [alert release];
       
   151         return NO;
   150         return NO;
   152     }
   151     }
   153 
   152 
   154     // play only if there is more than one team
   153     // play only if there is more than one team
   155     if ([self.teamConfigViewController.listOfSelectedTeams count] < 2) {
   154     if ([self.teamConfigViewController.listOfSelectedTeams count] < 2) {
   157                                                         message:NSLocalizedString(@"Select at least two teams to play a game",@"")
   156                                                         message:NSLocalizedString(@"Select at least two teams to play a game",@"")
   158                                                        delegate:nil
   157                                                        delegate:nil
   159                                               cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"")
   158                                               cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"")
   160                                               otherButtonTitles:nil];
   159                                               otherButtonTitles:nil];
   161         [alert show];
   160         [alert show];
   162         [alert release];
       
   163         return NO;
   161         return NO;
   164     }
   162     }
   165 
   163 
   166     // play if there's room for enough hogs in the selected map
   164     // play if there's room for enough hogs in the selected map
   167     int hogs = 0;
   165     int hogs = 0;
   172                                                         message:NSLocalizedString(@"The map is too small for that many hogs",@"")
   170                                                         message:NSLocalizedString(@"The map is too small for that many hogs",@"")
   173                                                        delegate:nil
   171                                                        delegate:nil
   174                                               cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"")
   172                                               cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"")
   175                                               otherButtonTitles:nil];
   173                                               otherButtonTitles:nil];
   176         [alert show];
   174         [alert show];
   177         [alert release];
       
   178         return NO;
   175         return NO;
   179     }
   176     }
   180 
   177 
   181     // play if there aren't too many teams
   178     // play if there aren't too many teams
   182     if ((int)[self.teamConfigViewController.listOfSelectedTeams count] > HW_getMaxNumberOfTeams()) {
   179     if ((int)[self.teamConfigViewController.listOfSelectedTeams count] > HW_getMaxNumberOfTeams()) {
   184                                                         message:NSLocalizedString(@"You exceeded the maximum number of tems allowed in a game",@"")
   181                                                         message:NSLocalizedString(@"You exceeded the maximum number of tems allowed in a game",@"")
   185                                                        delegate:nil
   182                                                        delegate:nil
   186                                               cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"")
   183                                               cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"")
   187                                               otherButtonTitles:nil];
   184                                               otherButtonTitles:nil];
   188         [alert show];
   185         [alert show];
   189         [alert release];
       
   190         return NO;
   186         return NO;
   191     }
   187     }
   192 
   188 
   193     // play only if one scheme and one weapon are selected
   189     // play only if one scheme and one weapon are selected
   194     if ([self.schemeWeaponConfigViewController.selectedScheme length] == 0 || [self.schemeWeaponConfigViewController.selectedWeapon length] == 0 ) {
   190     if ([self.schemeWeaponConfigViewController.selectedScheme length] == 0 || [self.schemeWeaponConfigViewController.selectedWeapon length] == 0 ) {
   196                                                         message:NSLocalizedString(@"Select one Scheme and one Weapon for this game",@"")
   192                                                         message:NSLocalizedString(@"Select one Scheme and one Weapon for this game",@"")
   197                                                        delegate:nil
   193                                                        delegate:nil
   198                                               cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"")
   194                                               cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"")
   199                                               otherButtonTitles:nil];
   195                                               otherButtonTitles:nil];
   200         [alert show];
   196         [alert show];
   201         [alert release];
       
   202         return NO;
   197         return NO;
   203     }
   198     }
   204 
   199 
   205     // play if the gameflags are set correctly (divideteam works only with 2 teams)
   200     // play if the gameflags are set correctly (divideteam works only with 2 teams)
   206     NSString *schemePath = [[NSString alloc] initWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),self.schemeWeaponConfigViewController.selectedScheme];
   201     NSString *schemePath = [[NSString alloc] initWithFormat:@"%@/%@",SCHEMES_DIRECTORY(),self.schemeWeaponConfigViewController.selectedScheme];
   207     NSArray *gameFlags = [[NSDictionary dictionaryWithContentsOfFile:schemePath] objectForKey:@"gamemod"];
   202     NSArray *gameFlags = [[NSDictionary dictionaryWithContentsOfFile:schemePath] objectForKey:@"gamemod"];
   208     [schemePath release];
       
   209     if ([[gameFlags objectAtIndex:2] boolValue] && [self.teamConfigViewController.listOfSelectedTeams count] != 2) {
   203     if ([[gameFlags objectAtIndex:2] boolValue] && [self.teamConfigViewController.listOfSelectedTeams count] != 2) {
   210         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Scheme mismatch",@"")
   204         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Scheme mismatch",@"")
   211                                                         message:NSLocalizedString(@"The scheme you selected allows only for two teams",@"")
   205                                                         message:NSLocalizedString(@"The scheme you selected allows only for two teams",@"")
   212                                                        delegate:nil
   206                                                        delegate:nil
   213                                               cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"")
   207                                               cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"")
   214                                               otherButtonTitles:nil];
   208                                               otherButtonTitles:nil];
   215         [alert show];
   209         [alert show];
   216         [alert release];
       
   217         return NO;
   210         return NO;
   218     }
   211     }
   219 
   212 
   220     return YES;
   213     return YES;
   221 }
   214 }
   222 
   215 
   223 -(void) startGame:(UIButton *)button {
   216 - (void)startGame:(UIButton *)button {
   224     button.enabled = YES;
   217     button.enabled = YES;
   225 
   218 
   226     NSString *script = self.mapConfigViewController.missionCommand;
   219     NSString *script = self.mapConfigViewController.missionCommand;
   227     if ([script isEqualToString:@""])
   220     if ([script isEqualToString:@""])
   228         script = self.schemeWeaponConfigViewController.scriptCommand;
   221         script = self.schemeWeaponConfigViewController.scriptCommand;
   241                                     script,@"mission_command",
   234                                     script,@"mission_command",
   242                                     nil];
   235                                     nil];
   243 
   236 
   244     [GameInterfaceBridge registerCallingController:self];
   237     [GameInterfaceBridge registerCallingController:self];
   245     [GameInterfaceBridge startLocalGame:gameDictionary];
   238     [GameInterfaceBridge startLocalGame:gameDictionary];
   246     [gameDictionary release];
   239 }
   247 }
   240 
   248 
   241 - (void)loadNiceHogs
   249 -(void) loadNiceHogs
       
   250 {
   242 {
   251     @autoreleasepool
   243     @autoreleasepool
   252     {
   244     {
   253     
       
   254         NSString *filePath = [[NSString alloc] initWithFormat:@"%@/Hedgehog/Idle.png",GRAPHICS_DIRECTORY()];
   245         NSString *filePath = [[NSString alloc] initWithFormat:@"%@/Hedgehog/Idle.png",GRAPHICS_DIRECTORY()];
   255         UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:filePath];
   246         UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:filePath];
   256         [filePath release];
       
   257 
   247 
   258         NSArray *hatArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:HATS_DIRECTORY() error:NULL];
   248         NSArray *hatArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:HATS_DIRECTORY() error:NULL];
   259         NSUInteger numberOfHats = [hatArray count];
   249         NSUInteger numberOfHats = [hatArray count];
   260         int animationFrames = IS_VERY_POWERFUL([HWUtils modelType]) ? 16 : 1;
   250         int animationFrames = IS_VERY_POWERFUL([HWUtils modelType]) ? 16 : 1;
   261         
   251         
   276                 UIImage *hogWithHat = [hogSpriteFrame mergeWith:hatSpriteFrame atPoint:CGPointMake(0, 5)];
   266                 UIImage *hogWithHat = [hogSpriteFrame mergeWith:hatSpriteFrame atPoint:CGPointMake(0, 5)];
   277                 if (hogWithHat) {
   267                 if (hogWithHat) {
   278                     [animation addObject:hogWithHat];
   268                     [animation addObject:hogWithHat];
   279                 }
   269                 }
   280             }
   270             }
   281             [hatSprite release];
       
   282             [hatFile release];
       
   283 
   271 
   284             UIImageView *hog = [[UIImageView alloc] initWithImage:[animation firstObject]];
   272             UIImageView *hog = [[UIImageView alloc] initWithImage:[animation firstObject]];
   285             hog.animationImages = animation;
   273             hog.animationImages = animation;
   286             hog.animationDuration = 3;
   274             hog.animationDuration = 3;
   287             [animation release];
       
   288 
   275 
   289             int x = 20*i+arc4random_uniform(128);
   276             int x = 20*i+arc4random_uniform(128);
   290             while (x > 320 - 32)
   277             while (x > 320 - 32)
   291                 x = i*arc4random_uniform(32);
   278                 x = i*arc4random_uniform(32);
   292             
   279             
   293             hog.frame = CGRectMake(x, 25, hog.frame.size.width, hog.frame.size.height);
   280             hog.frame = CGRectMake(x, 25, hog.frame.size.width, hog.frame.size.height);
   294             [self.imgContainer addSubview:hog];
   281             [self.imgContainer addSubview:hog];
   295             [hog startAnimating];
   282             [hog startAnimating];
   296             [hog release];
       
   297         }
   283         }
   298         [hogSprite release];
       
   299         
   284         
   300         dispatch_async(dispatch_get_main_queue(), ^{
   285         dispatch_async(dispatch_get_main_queue(), ^{
   301             
   286             
   302             [self.view addSubview:self.imgContainer];
   287             [self.view addSubview:self.imgContainer];
   303             
   288             
   324     
   309     
   325     [self.imgContainer removeFromSuperview];
   310     [self.imgContainer removeFromSuperview];
   326     self.imgContainer = nil;
   311     self.imgContainer = nil;
   327 }
   312 }
   328 
   313 
   329 -(void) viewDidLoad
   314 - (void)viewDidLoad
   330 {
   315 {
   331     [super viewDidLoad];
   316     [super viewDidLoad];
   332     
   317     
   333     self.view.backgroundColor = [UIColor blackColor];
   318     self.view.backgroundColor = [UIColor blackColor];
   334 
   319 
   340         // the label for the filter slider
   325         // the label for the filter slider
   341         UILabel *backLabel = [[UILabel alloc] initWithFrame:CGRectMake(116, 714, 310, 40)
   326         UILabel *backLabel = [[UILabel alloc] initWithFrame:CGRectMake(116, 714, 310, 40)
   342                                                    andTitle:nil
   327                                                    andTitle:nil
   343                                             withBorderWidth:2.0f];
   328                                             withBorderWidth:2.0f];
   344         self.sliderBackground = backLabel;
   329         self.sliderBackground = backLabel;
   345         [backLabel release];
       
   346         [self.view addSubview:self.sliderBackground];
   330         [self.view addSubview:self.sliderBackground];
   347 
   331 
   348         // the label for max hogs
   332         // the label for max hogs
   349         UILabel *maxLabel = [[UILabel alloc] initWithFrame:CGRectMake(598, 714, 310, 40)
   333         UILabel *maxLabel = [[UILabel alloc] initWithFrame:CGRectMake(598, 714, 310, 40)
   350                                                   andTitle:NSLocalizedString(@"Loading...",@"")
   334                                                   andTitle:NSLocalizedString(@"Loading...",@"")
   352         maxLabel.font = [UIFont italicSystemFontOfSize:[UIFont labelFontSize]];
   336         maxLabel.font = [UIFont italicSystemFontOfSize:[UIFont labelFontSize]];
   353         maxLabel.textColor = [UIColor whiteColor];
   337         maxLabel.textColor = [UIColor whiteColor];
   354         maxLabel.textAlignment = NSTextAlignmentCenter;
   338         maxLabel.textAlignment = NSTextAlignmentCenter;
   355         [self.view addSubview:maxLabel];
   339         [self.view addSubview:maxLabel];
   356         self.mapConfigViewController.maxLabel = maxLabel;
   340         self.mapConfigViewController.maxLabel = maxLabel;
   357         [maxLabel release];
       
   358     }
   341     }
   359     else
   342     else
   360     {
   343     {
   361         [self localizeTabsSegmentedControl];
   344         [self localizeTabsSegmentedControl];
   362         
   345         
   368     
   351     
   369     [self.view addSubview:self.mapConfigViewController.view];
   352     [self.view addSubview:self.mapConfigViewController.view];
   370     [self.view bringSubviewToFront:self.mapConfigViewController.slider];
   353     [self.view bringSubviewToFront:self.mapConfigViewController.slider];
   371 }
   354 }
   372 
   355 
   373 -(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval) duration {
   356 - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval) duration {
   374     if (IS_IPAD() == NO)
   357     if (IS_IPAD() == NO)
   375         return;
   358         return;
   376 
   359 
   377     [self updateiPadUIForInterfaceOrientation:toInterfaceOrientation];
   360     [self updateiPadUIForInterfaceOrientation:toInterfaceOrientation];
   378 
   361 
   404         self.sliderBackground.frame = CGRectMake(465, 975, 200, 40);
   387         self.sliderBackground.frame = CGRectMake(465, 975, 200, 40);
   405         self.mapConfigViewController.slider.frame = CGRectMake(475, 983, 180, 23);
   388         self.mapConfigViewController.slider.frame = CGRectMake(475, 983, 180, 23);
   406     }
   389     }
   407 }
   390 }
   408 
   391 
   409 -(void) viewWillAppear:(BOOL)animated
   392 - (void)viewWillAppear:(BOOL)animated
   410 {
   393 {
   411     [super viewWillAppear:animated];
   394     [super viewWillAppear:animated];
   412     
   395     
   413     if (IS_IPAD() && !self.imgContainer && !self.isDrawingNiceHogs)
   396     if (IS_IPAD() && !self.imgContainer && !self.isDrawingNiceHogs)
   414     {
   397     {
   434         UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation];
   417         UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation];
   435         [self updateiPadUIForInterfaceOrientation:currentOrientation];
   418         [self updateiPadUIForInterfaceOrientation:currentOrientation];
   436     }
   419     }
   437 }
   420 }
   438 
   421 
   439 -(void) didReceiveMemoryWarning
   422 - (void)didReceiveMemoryWarning
   440 {
   423 {
   441     [self clearImgContainer];
   424     [self clearImgContainer];
   442 
   425 
   443     if (self.titleImage.superview == nil)
   426     if (self.titleImage.superview == nil)
   444         self.titleImage = nil;
   427         self.titleImage = nil;
   455         self.helpPage = nil;
   438         self.helpPage = nil;
   456     MSG_MEMCLEAN();
   439     MSG_MEMCLEAN();
   457     [super didReceiveMemoryWarning];
   440     [super didReceiveMemoryWarning];
   458 }
   441 }
   459 
   442 
   460 -(void) viewDidUnload {
       
   461     self.imgContainer = nil;
       
   462     self.titleImage = nil;
       
   463     self.sliderBackground = nil;
       
   464     self.schemeWeaponConfigViewController = nil;
       
   465     self.teamConfigViewController = nil;
       
   466     self.mapConfigViewController = nil;
       
   467     self.helpPage = nil;
       
   468     MSG_DIDUNLOAD();
       
   469     [super viewDidUnload];
       
   470 }
       
   471 
       
   472 -(void) dealloc {
       
   473     releaseAndNil(_tabsSegmentedControl);
       
   474     releaseAndNil(_backButton);
       
   475     releaseAndNil(_startButton);
       
   476     releaseAndNil(imgContainer);
       
   477     releaseAndNil(titleImage);
       
   478     releaseAndNil(sliderBackground);
       
   479     releaseAndNil(schemeWeaponConfigViewController);
       
   480     releaseAndNil(teamConfigViewController);
       
   481     releaseAndNil(mapConfigViewController);
       
   482     releaseAndNil(helpPage);
       
   483     [super dealloc];
       
   484 }
       
   485 
       
   486 @end
   443 @end