project_files/HedgewarsMobile/Classes/GameConfigViewController.m
changeset 3547 02875b1145b7
parent 3546 ccf4854df294
child 3548 4d220ee7c75f
equal deleted inserted replaced
3546:ccf4854df294 3547:02875b1145b7
       
     1     //
       
     2 //  GameConfigViewController.m
       
     3 //  HedgewarsMobile
       
     4 //
       
     5 //  Created by Vittorio on 18/04/10.
       
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
       
     7 //
       
     8 
       
     9 #import "GameConfigViewController.h"
       
    10 #import "SDL_uikitappdelegate.h"
       
    11 #import "CommodityFunctions.h"
       
    12 #import "MapConfigViewController.h"
       
    13 #import "TeamConfigViewController.h"
       
    14 #import "SchemeWeaponConfigViewController.h"
       
    15 
       
    16 @implementation GameConfigViewController
       
    17 
       
    18 
       
    19 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
       
    20     return rotationManager(interfaceOrientation);
       
    21 }
       
    22 
       
    23 -(IBAction) buttonPressed:(id) sender {
       
    24     // works even if it's not actually a button
       
    25     UIButton *theButton = (UIButton *)sender;
       
    26     switch (theButton.tag) {
       
    27         case 0:
       
    28             [[self parentViewController] dismissModalViewControllerAnimated:YES];
       
    29             break;
       
    30         case 1:
       
    31             theButton.enabled = NO;
       
    32             [self performSelector:@selector(startGame:)
       
    33                        withObject:theButton
       
    34                        afterDelay:0.25];
       
    35             break;
       
    36         default:
       
    37             break;
       
    38     }
       
    39 }
       
    40 
       
    41 -(IBAction) segmentPressed:(id) sender {
       
    42     UISegmentedControl *theSegment = (UISegmentedControl *)sender;
       
    43 
       
    44     switch (theSegment.selectedSegmentIndex) {
       
    45         case 0:
       
    46             // this init here is just aestetic as this controller was already set up in viewDidLoad
       
    47             if (mapConfigViewController == nil) {
       
    48                 mapConfigViewController = [[MapConfigViewController alloc] initWithNibName:@"MapConfigViewController-iPhone" bundle:nil];
       
    49             }
       
    50             activeController = mapConfigViewController;
       
    51             break;
       
    52         case 1:
       
    53             if (teamConfigViewController == nil) {
       
    54                 teamConfigViewController = [[TeamConfigViewController alloc] initWithStyle:UITableViewStyleGrouped];
       
    55                 // this message is compulsory otherwise the table won't be loaded at all
       
    56             }
       
    57             activeController = teamConfigViewController;
       
    58             break;
       
    59         case 2:
       
    60             if (schemeWeaponConfigViewController == nil) {
       
    61                 schemeWeaponConfigViewController = [[SchemeWeaponConfigViewController alloc] initWithStyle:UITableViewStyleGrouped];
       
    62             }
       
    63             activeController = schemeWeaponConfigViewController;
       
    64             break;
       
    65     }
       
    66     
       
    67     // this message is compulsory otherwise the table won't be loaded at all
       
    68     [activeController viewWillAppear:NO];      
       
    69     [self.view addSubview:activeController.view];
       
    70 }
       
    71 
       
    72 -(void) startGame:(UIButton *)button {
       
    73     button.enabled = YES;
       
    74 
       
    75     // don't start playing if the preview is in progress
       
    76     if ([mapConfigViewController busy]) {
       
    77         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Wait for the Preview",@"")
       
    78                                                         message:NSLocalizedString(@"Before playing the preview needs to be generated",@"")
       
    79                                                        delegate:nil
       
    80                                               cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"")
       
    81                                               otherButtonTitles:nil];
       
    82         [alert show];
       
    83         [alert release];
       
    84         return;
       
    85     }
       
    86     
       
    87     // play only if there is more than one team
       
    88     if ([teamConfigViewController.listOfSelectedTeams count] < 2) {
       
    89         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Too few teams playing",@"")
       
    90                                                         message:NSLocalizedString(@"You need to select at least two teams to play a game",@"")
       
    91                                                        delegate:nil
       
    92                                               cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"")
       
    93                                               otherButtonTitles:nil];
       
    94         [alert show];
       
    95         [alert release];
       
    96         return;
       
    97     }
       
    98     
       
    99     // play if there's room for enough hogs in the selected map
       
   100     int hogs = 0;
       
   101     for (NSDictionary *teamData in teamConfigViewController.listOfSelectedTeams)
       
   102         hogs += [[teamData objectForKey:@"number"] intValue];
       
   103 
       
   104     if (hogs > mapConfigViewController.maxHogs) {
       
   105         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Too many hogs",@"")
       
   106                                                         message:NSLocalizedString(@"The map you selected is too small for that many hogs",@"")
       
   107                                                        delegate:nil
       
   108                                               cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"")
       
   109                                               otherButtonTitles:nil];
       
   110         [alert show];
       
   111         [alert release];
       
   112         return;
       
   113     }
       
   114     
       
   115     // create the configuration file that is going to be sent to engine
       
   116     NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:mapConfigViewController.seedCommand,@"seed_command",
       
   117                                                                       mapConfigViewController.templateFilterCommand,@"templatefilter_command",
       
   118                                                                       mapConfigViewController.mapGenCommand,@"mapgen_command",
       
   119                                                                       mapConfigViewController.mazeSizeCommand,@"mazesize_command",
       
   120                                                                       mapConfigViewController.themeCommand,@"theme_command",
       
   121                                                                       teamConfigViewController.listOfSelectedTeams,@"teams_list",
       
   122                                                                       schemeWeaponConfigViewController.selectedScheme,@"scheme",
       
   123                                                                       schemeWeaponConfigViewController.selectedWeapon,@"weapon",
       
   124                                                                       nil];
       
   125     [dict writeToFile:GAMECONFIG_FILE() atomically:YES];
       
   126     [dict release];
       
   127 
       
   128     // finally launch game and remove this controller
       
   129     [[self parentViewController] dismissModalViewControllerAnimated:YES];
       
   130     [[SDLUIKitDelegate sharedAppDelegate] startSDLgame];
       
   131 }
       
   132 
       
   133 -(void) viewDidLoad {
       
   134     CGRect screen = [[UIScreen mainScreen] bounds];
       
   135     self.view.frame = CGRectMake(0, 0, screen.size.height, screen.size.width);
       
   136 
       
   137     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
       
   138         if (mapConfigViewController == nil)
       
   139             mapConfigViewController = [[MapConfigViewController alloc] initWithNibName:@"MapConfigViewController-iPad" bundle:nil];
       
   140         if (teamConfigViewController == nil)
       
   141             teamConfigViewController = [[TeamConfigViewController alloc] initWithStyle:UITableViewStylePlain];
       
   142         teamConfigViewController.view.frame = CGRectMake(0, 224, 300, 500);
       
   143         teamConfigViewController.view.backgroundColor = [UIColor clearColor];
       
   144         [mapConfigViewController.view addSubview:teamConfigViewController.view];
       
   145         if (schemeWeaponConfigViewController == nil)
       
   146             schemeWeaponConfigViewController = [[SchemeWeaponConfigViewController alloc] initWithStyle:UITableViewStyleGrouped];
       
   147         schemeWeaponConfigViewController.view.frame = CGRectMake(362, 224, 300, 500);
       
   148         schemeWeaponConfigViewController.view.backgroundColor = [UIColor clearColor];
       
   149         [mapConfigViewController.view addSubview:schemeWeaponConfigViewController.view];
       
   150         for (UIView *oneView in self.view.subviews) {
       
   151             if ([oneView isMemberOfClass:[UIToolbar class]]) {
       
   152                 [[oneView viewWithTag:12345] setHidden:YES];
       
   153                 break;
       
   154             }
       
   155         }
       
   156     } else
       
   157         mapConfigViewController = [[MapConfigViewController alloc] initWithNibName:@"MapConfigViewController-iPhone" bundle:nil];
       
   158     activeController = mapConfigViewController;
       
   159     
       
   160     [self.view addSubview:mapConfigViewController.view];
       
   161     
       
   162     [super viewDidLoad];
       
   163 }
       
   164 
       
   165 -(void) viewWillAppear:(BOOL)animated {
       
   166     [mapConfigViewController viewWillAppear:animated];
       
   167     [teamConfigViewController viewWillAppear:animated];
       
   168     [schemeWeaponConfigViewController viewWillAppear:animated];
       
   169     // ADD other controllers here
       
   170      
       
   171     [super viewWillAppear:animated];
       
   172 }
       
   173 
       
   174 -(void) viewDidAppear:(BOOL)animated {
       
   175     [mapConfigViewController viewDidAppear:animated];
       
   176     [teamConfigViewController viewDidAppear:animated];
       
   177     [schemeWeaponConfigViewController viewDidAppear:animated];
       
   178     [super viewDidAppear:animated];
       
   179 }
       
   180 
       
   181 -(void) didReceiveMemoryWarning {
       
   182     // Releases the view if it doesn't have a superview.
       
   183     [super didReceiveMemoryWarning];
       
   184     // Release any cached data, images, etc that aren't in use.
       
   185     if (mapConfigViewController.view.superview == nil) 
       
   186         mapConfigViewController = nil;
       
   187     if (teamConfigViewController.view.superview == nil)
       
   188         teamConfigViewController = nil;
       
   189     if (schemeWeaponConfigViewController.view.superview == nil)
       
   190         schemeWeaponConfigViewController = nil;
       
   191     activeController = nil;
       
   192     MSG_MEMCLEAN();
       
   193 }
       
   194 
       
   195 -(void) viewDidUnload {
       
   196     activeController = nil;
       
   197     mapConfigViewController = nil;
       
   198     teamConfigViewController = nil;
       
   199     schemeWeaponConfigViewController = nil;
       
   200     [super viewDidUnload];
       
   201     MSG_DIDUNLOAD();
       
   202 }
       
   203 
       
   204 -(void) dealloc {
       
   205     [activeController release];
       
   206     [mapConfigViewController release];
       
   207     [teamConfigViewController release];
       
   208     [schemeWeaponConfigViewController release];
       
   209     [super dealloc];
       
   210 }
       
   211 
       
   212 @end