cocoaTouch/GameConfigViewController.m
changeset 3365 37ac593e9027
parent 3364 e5403e2bf02c
child 3369 c7289e42f0ee
equal deleted inserted replaced
3364:e5403e2bf02c 3365:37ac593e9027
     7 //
     7 //
     8 
     8 
     9 #import "GameConfigViewController.h"
     9 #import "GameConfigViewController.h"
    10 #import "SDL_uikitappdelegate.h"
    10 #import "SDL_uikitappdelegate.h"
    11 #import "CommodityFunctions.h"
    11 #import "CommodityFunctions.h"
       
    12 #import "MapConfigViewController.h"
    12 #import "TeamConfigViewController.h"
    13 #import "TeamConfigViewController.h"
    13 
    14 
    14 @implementation GameConfigViewController
    15 @implementation GameConfigViewController
    15 @synthesize availableTeamsTableView, weaponsButton, schemesButton, mapButton, randomButton, startButton;
    16 @synthesize availableTeamsTableView, weaponsButton, schemesButton, mapButton, randomButton, startButton;
    16 
    17 
    22 -(IBAction) buttonPressed:(id) sender {
    23 -(IBAction) buttonPressed:(id) sender {
    23     // works even if it's not actually a button
    24     // works even if it's not actually a button
    24     UIButton *theButton = (UIButton *)sender;
    25     UIButton *theButton = (UIButton *)sender;
    25     switch (theButton.tag) {
    26     switch (theButton.tag) {
    26         case 0:
    27         case 0:
    27             [[NSNotificationCenter defaultCenter] postNotificationName:@"dismissModalView" object:nil];
    28             [[self parentViewController] dismissModalViewControllerAnimated:YES];
    28             break;
    29             break;
    29         case 1:
    30         case 1:
    30             [self performSelector:@selector(startGame)
    31             [self performSelector:@selector(startGame)
    31                        withObject:nil
    32                        withObject:nil
    32                        afterDelay:0.25];
    33                        afterDelay:0.25];
    33             break;
    34             break;
       
    35 
    34     }
    36     }
    35 }
    37 }
    36 
    38 
       
    39 -(IBAction) segmentPressed:(id) sender {
       
    40     UISegmentedControl *theSegment = (UISegmentedControl *)sender;
       
    41     NSLog(@"%d", theSegment.selectedSegmentIndex);
       
    42     switch (theSegment.selectedSegmentIndex) {
       
    43         case 0:
       
    44             // this init here is just aestetic as this controller was already set up in viewDidLoad
       
    45             if (mapConfigViewController == nil) {
       
    46                 mapConfigViewController = [[MapConfigViewController alloc] initWithNibName:@"MapConfigViewController-iPhone" bundle:nil];
       
    47                 [mapConfigViewController viewWillAppear:NO];  
       
    48             }
       
    49             activeController = mapConfigViewController;
       
    50             break;
       
    51         case 1:
       
    52             if (teamConfigViewController == nil) {
       
    53                 teamConfigViewController = [[TeamConfigViewController alloc] initWithStyle:UITableViewStyleGrouped];
       
    54                 // this message is compulsory otherwise the team table won't be loaded at all
       
    55                 [teamConfigViewController viewWillAppear:NO];  
       
    56             }
       
    57             activeController = teamConfigViewController;
       
    58             break;
       
    59         case 2:
       
    60             
       
    61             break;
       
    62     }
       
    63     
       
    64     [self.view addSubview:activeController.view];
       
    65 }
       
    66 
    37 -(void) startGame {
    67 -(void) startGame {
       
    68     // play only if there is more than one team
    38     if ([teamConfigViewController.listOfSelectedTeams count] < 2) {
    69     if ([teamConfigViewController.listOfSelectedTeams count] < 2) {
    39         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Too few teams playing",@"")
    70         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Too few teams playing",@"")
    40                                                         message:NSLocalizedString(@"You need to select at least two teams to play a Game",@"")
    71                                                         message:NSLocalizedString(@"You need to select at least two teams to play a game",@"")
    41                                                        delegate:nil
    72                                                        delegate:nil
    42                                               cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"")
    73                                               cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"")
    43                                               otherButtonTitles:nil];
    74                                               otherButtonTitles:nil];
    44         [alert show];
    75         [alert show];
    45         [alert release];
    76         [alert release];
    46     } else {
    77         return;
    47         [teamConfigViewController.listOfSelectedTeams writeToFile:GAMECONFIG_FILE() atomically:YES];
       
    48         [[NSNotificationCenter defaultCenter] postNotificationName:@"dismissModalView" object:nil];
       
    49         [[SDLUIKitDelegate sharedAppDelegate] startSDLgame];
       
    50     }
    78     }
       
    79     
       
    80     // play if there's room for enough hogs in the selected map
       
    81     int hogs = 0;
       
    82     for (NSDictionary *teamData in teamConfigViewController.listOfSelectedTeams)
       
    83         hogs += [[teamData objectForKey:@"number"] intValue];
       
    84 
       
    85     if (hogs > mapConfigViewController.maxHogs) {
       
    86         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Too many hogs",@"")
       
    87                                                         message:NSLocalizedString(@"The map you selected is too small for that many hogs",@"")
       
    88                                                        delegate:nil
       
    89                                               cancelButtonTitle:NSLocalizedString(@"Ok, got it",@"")
       
    90                                               otherButtonTitles:nil];
       
    91         [alert show];
       
    92         [alert release];
       
    93         return;
       
    94     } 
       
    95     NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:mapConfigViewController.seedCommand,@"seed_command",
       
    96                                                                       teamConfigViewController.listOfSelectedTeams,@"teams_list",nil];
       
    97     [dict writeToFile:GAMECONFIG_FILE() atomically:YES];
       
    98     [dict release];
       
    99     [[self parentViewController] dismissModalViewControllerAnimated:YES];
       
   100     [[SDLUIKitDelegate sharedAppDelegate] startSDLgame];
    51 }
   101 }
    52 
   102 
    53 -(void) viewDidLoad {
   103 -(void) viewDidLoad {
    54     teamConfigViewController = [[TeamConfigViewController alloc] initWithStyle:UITableViewStyleGrouped];
   104     mapConfigViewController = [[MapConfigViewController alloc] initWithNibName:@"MapConfigViewController-iPhone" bundle:nil];
    55     activeController = teamConfigViewController;
   105     activeController = mapConfigViewController;
    56     
   106     
    57     [self.view insertSubview:teamConfigViewController.view atIndex:0];
   107     [self.view insertSubview:mapConfigViewController.view atIndex:0];
    58     
   108     
    59     [super viewDidLoad];
   109     [super viewDidLoad];
       
   110 }
       
   111 
       
   112 -(void) viewWillAppear:(BOOL)animated {
       
   113     [mapConfigViewController viewWillAppear:animated];
       
   114     [teamConfigViewController viewWillAppear:animated];
       
   115     // ADD other controllers here
       
   116      
       
   117     [super viewWillAppear:animated];
    60 }
   118 }
    61 
   119 
    62 -(void) didReceiveMemoryWarning {
   120 -(void) didReceiveMemoryWarning {
    63     // Releases the view if it doesn't have a superview.
   121     // Releases the view if it doesn't have a superview.
    64     [super didReceiveMemoryWarning];
   122     [super didReceiveMemoryWarning];
    65     // Release any cached data, images, etc that aren't in use.
   123     // Release any cached data, images, etc that aren't in use.
    66 }
   124 }
    67 
   125 
    68 
   126 
    69 -(void) viewDidUnload {
   127 -(void) viewDidUnload {
       
   128     NSLog(@"unloading");
    70     activeController = nil;
   129     activeController = nil;
       
   130     mapConfigViewController = nil;
    71     teamConfigViewController = nil;
   131     teamConfigViewController = nil;
    72     self.availableTeamsTableView = nil;
   132     self.availableTeamsTableView = nil;
    73     self.weaponsButton = nil;
   133     self.weaponsButton = nil;
    74     self.schemesButton = nil;
   134     self.schemesButton = nil;
    75     self.mapButton = nil;
   135     self.mapButton = nil;
    79 }
   139 }
    80 
   140 
    81 
   141 
    82 -(void) dealloc {
   142 -(void) dealloc {
    83     [activeController release];
   143     [activeController release];
       
   144     [mapConfigViewController release];
    84     [teamConfigViewController release];
   145     [teamConfigViewController release];
    85     [availableTeamsTableView release];
   146     [availableTeamsTableView release];
    86     [weaponsButton release];
   147     [weaponsButton release];
    87     [schemesButton release];
   148     [schemesButton release];
    88     [mapButton release];
   149     [mapButton release];
    89     [randomButton release];
   150     [randomButton release];
    90     [startButton release];
   151     [startButton release];
    91     [super dealloc];
   152     [super dealloc];
    92 }
   153 }
    93 
   154 
    94 -(void) viewWillAppear:(BOOL)animated {
       
    95     [super viewWillAppear:animated];
       
    96     [activeController viewWillAppear:animated];
       
    97 }
       
    98 
       
    99 -(void) viewWillDisappear:(BOOL)animated {
       
   100     [super viewWillDisappear:animated];
       
   101     [activeController viewWillDisappear:animated];
       
   102 }
       
   103 
       
   104 -(void) viewDidAppear:(BOOL)animated {
       
   105     [super viewDidLoad];
       
   106     [activeController viewDidAppear:animated];
       
   107 }
       
   108 
       
   109 -(void) viewDidDisappear:(BOOL)animated {
       
   110     [super viewDidUnload];
       
   111     [activeController viewDidDisappear:animated];
       
   112 }
       
   113 
       
   114 @end
   155 @end