cocoaTouch/GameConfigViewController.m
changeset 3516 a8c673657b79
parent 3510 23145a950eae
parent 3515 3e8635f43972
child 3529 0e968ba12a84
equal deleted inserted replaced
3510:23145a950eae 3516:a8c673657b79
     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 
       
    15 @implementation GameConfigViewController
       
    16 
       
    17 
       
    18 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
       
    19     return rotationManager(interfaceOrientation);
       
    20 }
       
    21 
       
    22 -(IBAction) buttonPressed:(id) sender {
       
    23     // works even if it's not actually a button
       
    24     UIButton *theButton = (UIButton *)sender;
       
    25     switch (theButton.tag) {
       
    26         case 0:
       
    27             [[self parentViewController] dismissModalViewControllerAnimated:YES];
       
    28             break;
       
    29         case 1:
       
    30             [self performSelector:@selector(startGame)
       
    31                        withObject:nil
       
    32                        afterDelay:0.25];
       
    33             break;
       
    34 
       
    35     }
       
    36 }
       
    37 
       
    38 -(IBAction) segmentPressed:(id) sender {
       
    39     UISegmentedControl *theSegment = (UISegmentedControl *)sender;
       
    40 
       
    41     switch (theSegment.selectedSegmentIndex) {
       
    42         case 0:
       
    43             // this init here is just aestetic as this controller was already set up in viewDidLoad
       
    44             if (mapConfigViewController == nil) {
       
    45                 mapConfigViewController = [[MapConfigViewController alloc] initWithNibName:@"MapConfigViewController-iPhone" bundle:nil];
       
    46                 [mapConfigViewController viewWillAppear:NO];  
       
    47             }
       
    48             activeController = mapConfigViewController;
       
    49             break;
       
    50         case 1:
       
    51             if (teamConfigViewController == nil) {
       
    52                 teamConfigViewController = [[TeamConfigViewController alloc] initWithStyle:UITableViewStyleGrouped];
       
    53                 // this message is compulsory otherwise the team table won't be loaded at all
       
    54                 [teamConfigViewController viewWillAppear:NO];  
       
    55             }
       
    56             activeController = teamConfigViewController;
       
    57             break;
       
    58         case 2:
       
    59             
       
    60             break;
       
    61     }
       
    62     
       
    63     [self.view addSubview:activeController.view];
       
    64 }
       
    65 
       
    66 -(void) startGame {
       
    67     // don't start playing if the preview is in progress
       
    68     if ([mapConfigViewController busy]) {
       
    69         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Wait for the Preview",@"")
       
    70                                                         message:NSLocalizedString(@"Before playing the preview needs to be generated",@"")
       
    71                                                        delegate:nil
       
    72                                               cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"")
       
    73                                               otherButtonTitles:nil];
       
    74         [alert show];
       
    75         [alert release];
       
    76         return;
       
    77     }
       
    78     
       
    79     // play only if there is more than one team
       
    80     if ([teamConfigViewController.listOfSelectedTeams count] < 2) {
       
    81         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Too few teams playing",@"")
       
    82                                                         message:NSLocalizedString(@"You need to select at least two teams to play a game",@"")
       
    83                                                        delegate:nil
       
    84                                               cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"")
       
    85                                               otherButtonTitles:nil];
       
    86         [alert show];
       
    87         [alert release];
       
    88         return;
       
    89     }
       
    90     
       
    91     // play if there's room for enough hogs in the selected map
       
    92     int hogs = 0;
       
    93     for (NSDictionary *teamData in teamConfigViewController.listOfSelectedTeams)
       
    94         hogs += [[teamData objectForKey:@"number"] intValue];
       
    95 
       
    96     if (hogs > mapConfigViewController.maxHogs) {
       
    97         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Too many hogs",@"")
       
    98                                                         message:NSLocalizedString(@"The map you selected is too small for that many hogs",@"")
       
    99                                                        delegate:nil
       
   100                                               cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"")
       
   101                                               otherButtonTitles:nil];
       
   102         [alert show];
       
   103         [alert release];
       
   104         return;
       
   105     }
       
   106     
       
   107     // create the configuration file that is going to be sent to engine
       
   108     NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:mapConfigViewController.seedCommand,@"seed_command",
       
   109                                                                       mapConfigViewController.templateFilterCommand,@"templatefilter_command",
       
   110                                                                       mapConfigViewController.mapGenCommand,@"mapgen_command",
       
   111                                                                       mapConfigViewController.mazeSizeCommand,@"mazesize_command",
       
   112                                                                       mapConfigViewController.themeCommand,@"theme_command",
       
   113                                                                       teamConfigViewController.listOfSelectedTeams,@"teams_list",nil];
       
   114     [dict writeToFile:GAMECONFIG_FILE() atomically:YES];
       
   115     [dict release];
       
   116 
       
   117     // finally launch game and remove this controller
       
   118     [[self parentViewController] dismissModalViewControllerAnimated:YES];
       
   119     [[SDLUIKitDelegate sharedAppDelegate] startSDLgame];
       
   120 }
       
   121 
       
   122 -(void) viewDidLoad {
       
   123     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
       
   124         mapConfigViewController = [[MapConfigViewController alloc] initWithNibName:@"MapConfigViewController-iPad" bundle:nil];
       
   125         teamConfigViewController = [[TeamConfigViewController alloc] initWithStyle:UITableViewStylePlain];
       
   126         teamConfigViewController.view.frame = CGRectMake(0, 224, 300, 500);
       
   127         teamConfigViewController.view.backgroundColor = [UIColor clearColor];
       
   128         [mapConfigViewController.view addSubview:teamConfigViewController.view];
       
   129     } else
       
   130         mapConfigViewController = [[MapConfigViewController alloc] initWithNibName:@"MapConfigViewController-iPhone" bundle:nil];
       
   131     activeController = mapConfigViewController;
       
   132     
       
   133     [self.view addSubview:mapConfigViewController.view];
       
   134     
       
   135     [super viewDidLoad];
       
   136 }
       
   137 
       
   138 -(void) viewWillAppear:(BOOL)animated {
       
   139     [mapConfigViewController viewWillAppear:animated];
       
   140     [teamConfigViewController viewWillAppear:animated];
       
   141     // ADD other controllers here
       
   142      
       
   143     [super viewWillAppear:animated];
       
   144 }
       
   145 
       
   146 -(void) viewDidAppear:(BOOL)animated {
       
   147     [mapConfigViewController viewDidAppear:animated];
       
   148     [teamConfigViewController viewDidAppear:animated];
       
   149     [super viewDidAppear:animated];
       
   150 }
       
   151 
       
   152 -(void) didReceiveMemoryWarning {
       
   153     // Releases the view if it doesn't have a superview.
       
   154     [super didReceiveMemoryWarning];
       
   155     // Release any cached data, images, etc that aren't in use.
       
   156     if (mapConfigViewController.view.superview == nil) 
       
   157         mapConfigViewController = nil;
       
   158     if (teamConfigViewController.view.superview == nil)
       
   159         teamConfigViewController = nil;
       
   160     activeController = nil;
       
   161     MSG_MEMCLEAN();
       
   162 }
       
   163 
       
   164 
       
   165 -(void) viewDidUnload {
       
   166     activeController = nil;
       
   167     mapConfigViewController = nil;
       
   168     teamConfigViewController = nil;
       
   169     [super viewDidUnload];
       
   170     MSG_DIDUNLOAD();
       
   171 }
       
   172 
       
   173 
       
   174 -(void) dealloc {
       
   175     [activeController release];
       
   176     [mapConfigViewController release];
       
   177     [teamConfigViewController release];
       
   178     [super dealloc];
       
   179 }
       
   180 
       
   181 @end