project_files/HedgewarsMobile/Classes/MasterViewController.m
changeset 6074 047eaed35cbb
parent 6000 dbcebcd3d79f
equal deleted inserted replaced
6073:e6ce1a337e0f 6074:047eaed35cbb
    26 #import "WeaponSettingsViewController.h"
    26 #import "WeaponSettingsViewController.h"
    27 #import "SchemeSettingsViewController.h"
    27 #import "SchemeSettingsViewController.h"
    28 #import "SupportViewController.h"
    28 #import "SupportViewController.h"
    29 
    29 
    30 @implementation MasterViewController
    30 @implementation MasterViewController
    31 @synthesize rootController, targetController, controllerNames, lastIndexPath;
    31 @synthesize targetController, controllerNames, lastIndexPath;
    32 
    32 
    33 
    33 
    34 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    34 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    35     return rotationManager(interfaceOrientation);
    35     return rotationManager(interfaceOrientation);
    36 }
    36 }
    37 
    37 
    38 
    38 
    39 #pragma mark -
    39 #pragma mark -
    40 #pragma mark View lifecycle
    40 #pragma mark View lifecycle
    41 -(void) viewDidLoad {
    41 -(void) viewDidLoad {
    42     [super viewDidLoad];
    42     // the list of available controllers
    43 
       
    44     // the list of selectable controllers
       
    45     NSArray *array = [[NSArray alloc] initWithObjects:NSLocalizedString(@"General",@""),
    43     NSArray *array = [[NSArray alloc] initWithObjects:NSLocalizedString(@"General",@""),
    46                                                       NSLocalizedString(@"Teams",@""),
    44                                                       NSLocalizedString(@"Teams",@""),
    47                                                       NSLocalizedString(@"Weapons",@""),
    45                                                       NSLocalizedString(@"Weapons",@""),
    48                                                       NSLocalizedString(@"Schemes",@""),
    46                                                       NSLocalizedString(@"Schemes",@""),
    49                                                       NSLocalizedString(@"Support",@""),
    47                                                       NSLocalizedString(@"Support",@""),
    50                                                       nil];
    48                                                       nil];
    51     self.controllerNames = array;
    49     self.controllerNames = array;
    52     [array release];
    50     [array release];
    53 
    51 
    54     // targetControllers tells whether we're on the right or left side of the splitview -- on iphone we only use the right side
    52     UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
    55     if (targetController == nil && IS_IPAD()) {
    53                                                                                 target:self
    56         if (nil == generalSettingsViewController)
    54                                                                                 action:@selector(dismissSplitView)];
       
    55     if (IS_IPAD()) {
       
    56         // this class gets loaded twice, we tell the difference by looking at targetController
       
    57         if (self.targetController != nil) {
       
    58             UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
       
    59             tableView.delegate = self;
       
    60             tableView.dataSource = self;
       
    61             [tableView reloadData];
       
    62             [self.view addSubview:tableView];
       
    63             [self tableView:tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
       
    64             [tableView release];
       
    65             self.navigationItem.leftBarButtonItem = doneButton;
       
    66         }
       
    67     } else {
       
    68         // this class just loads all controllers and set up tabbar and navigation controllers
       
    69         NSMutableArray *tabBarNavigationControllers = [[NSMutableArray alloc] initWithCapacity:5];
       
    70         UINavigationController *navController = nil;
       
    71 
       
    72         if (nil == generalSettingsViewController) {
    57             generalSettingsViewController = [[GeneralSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
    73             generalSettingsViewController = [[GeneralSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
    58         generalSettingsViewController.navigationItem.hidesBackButton = YES;
    74             generalSettingsViewController.tabBarItem.title = [self.controllerNames objectAtIndex:0];
    59         [generalSettingsViewController viewWillAppear:YES];
    75             generalSettingsViewController.tabBarItem.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/TargetBee.png",GRAPHICS_DIRECTORY()]];
    60         [self.navigationController pushViewController:generalSettingsViewController animated:NO];
    76             navController = [[UINavigationController alloc] initWithRootViewController:generalSettingsViewController];
    61     } else {
    77             generalSettingsViewController.navigationItem.backBarButtonItem = doneButton;
    62         self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
    78             generalSettingsViewController.navigationItem.leftBarButtonItem = doneButton;
    63                                                                                               target:self
    79             [generalSettingsViewController release];
    64                                                                                               action:@selector(dismissSplitView)];
    80             [tabBarNavigationControllers addObject:navController];
       
    81             releaseAndNil(navController);
       
    82         }
       
    83         if (nil == teamSettingsViewController) {
       
    84             teamSettingsViewController = [[TeamSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
       
    85             teamSettingsViewController.tabBarItem.title = [self.controllerNames objectAtIndex:1];
       
    86             teamSettingsViewController.tabBarItem.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/Egg.png",GRAPHICS_DIRECTORY()]];
       
    87             navController = [[UINavigationController alloc] initWithRootViewController:teamSettingsViewController];
       
    88             teamSettingsViewController.navigationItem.backBarButtonItem = doneButton;
       
    89             teamSettingsViewController.navigationItem.leftBarButtonItem = doneButton;
       
    90             [tabBarNavigationControllers addObject:navController];
       
    91             releaseAndNil(navController);
       
    92         }
       
    93         if (nil == weaponSettingsViewController) {
       
    94             weaponSettingsViewController = [[WeaponSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
       
    95             weaponSettingsViewController.tabBarItem.title = [self.controllerNames objectAtIndex:2];
       
    96             weaponSettingsViewController.tabBarItem.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/cheese.png",GRAPHICS_DIRECTORY()]];
       
    97             navController = [[UINavigationController alloc] initWithRootViewController:weaponSettingsViewController];
       
    98             weaponSettingsViewController.navigationItem.backBarButtonItem = doneButton;
       
    99             weaponSettingsViewController.navigationItem.leftBarButtonItem = doneButton;
       
   100             [tabBarNavigationControllers addObject:navController];
       
   101             releaseAndNil(navController);
       
   102         }
       
   103         if (nil == schemeSettingsViewController) {
       
   104             schemeSettingsViewController = [[SchemeSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
       
   105             schemeSettingsViewController.tabBarItem.title = [self.controllerNames objectAtIndex:3];
       
   106             schemeSettingsViewController.tabBarItem.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/Targetp.png",GRAPHICS_DIRECTORY()]];
       
   107             navController = [[UINavigationController alloc] initWithRootViewController:schemeSettingsViewController];
       
   108             schemeSettingsViewController.navigationItem.backBarButtonItem = doneButton;
       
   109             schemeSettingsViewController.navigationItem.leftBarButtonItem = doneButton;
       
   110             [tabBarNavigationControllers addObject:navController];
       
   111             releaseAndNil(navController);
       
   112         }
       
   113         if (nil == supportViewController) {
       
   114             supportViewController = [[SupportViewController alloc] initWithStyle:UITableViewStyleGrouped];
       
   115             supportViewController.tabBarItem.title = [self.controllerNames objectAtIndex:4];
       
   116             supportViewController.tabBarItem.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/Seduction.png",GRAPHICS_DIRECTORY()]];
       
   117             navController = [[UINavigationController alloc] initWithRootViewController:supportViewController];
       
   118             supportViewController.navigationItem.backBarButtonItem = doneButton;
       
   119             supportViewController.navigationItem.leftBarButtonItem = doneButton;
       
   120             [tabBarNavigationControllers addObject:navController];
       
   121             releaseAndNil(navController);
       
   122         }
       
   123 
       
   124         UITabBarController *tabController = [[UITabBarController alloc] init];
       
   125         tabController.viewControllers = tabBarNavigationControllers;
       
   126         tabController.delegate = self;
       
   127 
       
   128         [self.view addSubview:tabController.view];
    65     }
   129     }
       
   130     [doneButton release];
       
   131     [super viewDidLoad];
       
   132 }
       
   133 
       
   134 -(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
       
   135     [viewController viewWillAppear:NO];
       
   136 }
       
   137 
       
   138 -(void) dismissSplitView {
       
   139     [AudioManagerController playBackSound];
       
   140     [[[HedgewarsAppDelegate sharedAppDelegate] mainViewController] dismissModalViewControllerAnimated:YES];
    66 }
   141 }
    67 
   142 
    68 #pragma mark -
   143 #pragma mark -
    69 #pragma mark Table view data source
   144 #pragma mark Table view data source
    70 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   145 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    71     return 1;
   146     return 1;
    72 }
   147 }
    73 
   148 
    74 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   149 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    75     return [controllerNames count];
   150     return [self.controllerNames count];
    76 }
   151 }
    77 
   152 
    78 // Customize the appearance of table view cells.
   153 // Customize the appearance of table view cells.
    79 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   154 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    80     static NSString *CellIdentifier = @"Cell";
   155     static NSString *CellIdentifier = @"Cell";
   104             //seduction.png for support page
   179             //seduction.png for support page
   105             DLog(@"Nope");
   180             DLog(@"Nope");
   106             break;
   181             break;
   107     }
   182     }
   108     
   183     
   109     if (nil == targetController)
   184     cell.accessoryType = UITableViewCellAccessoryNone;
   110         cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
       
   111     else
       
   112         cell.accessoryType = UITableViewCellAccessoryNone;
       
   113 
       
   114     cell.textLabel.text = [controllerNames objectAtIndex:[indexPath row]];
   185     cell.textLabel.text = [controllerNames objectAtIndex:[indexPath row]];
   115     UIImage *icon = [[UIImage alloc] initWithContentsOfFile:iconStr];
   186     UIImage *icon = [[UIImage alloc] initWithContentsOfFile:iconStr];
   116     cell.imageView.image = icon;
   187     cell.imageView.image = icon;
   117     [icon release];
   188     [icon release];
   118 
   189 
   125     int newRow = [indexPath row];
   196     int newRow = [indexPath row];
   126     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
   197     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
   127     UIViewController *nextController = nil;
   198     UIViewController *nextController = nil;
   128 
   199 
   129     if (newRow != oldRow) {
   200     if (newRow != oldRow) {
   130         [self.tableView deselectRowAtIndexPath:lastIndexPath animated:YES];
   201         [tableView deselectRowAtIndexPath:lastIndexPath animated:YES];
   131         [targetController.navigationController popToRootViewControllerAnimated:NO];
   202         [targetController.navigationController popToRootViewControllerAnimated:NO];
   132 
   203 
   133         switch (newRow) {
   204         switch (newRow) {
   134             case 0:
   205             case 0:
   135                 if (nil == generalSettingsViewController)
   206                 if (nil == generalSettingsViewController)
   156                     supportViewController = [[SupportViewController alloc] initWithStyle:UITableViewStyleGrouped];
   227                     supportViewController = [[SupportViewController alloc] initWithStyle:UITableViewStyleGrouped];
   157                 nextController = supportViewController;
   228                 nextController = supportViewController;
   158                 break;
   229                 break;
   159         }
   230         }
   160 
   231 
   161         nextController.title = [controllerNames objectAtIndex:newRow];
       
   162         self.lastIndexPath = indexPath;
   232         self.lastIndexPath = indexPath;
   163         [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
   233         [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
   164 
   234 
   165         if (nil == targetController) {
   235         nextController.navigationItem.hidesBackButton = YES;
   166             nextController.navigationItem.hidesBackButton = NO;
   236         [nextController viewWillAppear:NO];
   167             [self.navigationController pushViewController:nextController animated:YES];
   237         [targetController.navigationController pushViewController:nextController animated:NO];
   168         } else {
   238         [AudioManagerController playClickSound];
   169             [AudioManagerController playClickSound];
       
   170             nextController.navigationItem.hidesBackButton = YES;
       
   171             [targetController.navigationController pushViewController:nextController animated:NO];
       
   172         }
       
   173     }
   239     }
   174 }
   240 }
   175 
   241 
   176 
   242 
   177 #pragma mark -
   243 #pragma mark -
   190     MSG_MEMCLEAN();
   256     MSG_MEMCLEAN();
   191     [super didReceiveMemoryWarning];
   257     [super didReceiveMemoryWarning];
   192 }
   258 }
   193 
   259 
   194 -(void) viewDidUnload {
   260 -(void) viewDidUnload {
   195     //self.rootController = nil;
       
   196     //self.targetController = nil;
       
   197     self.controllerNames = nil;
   261     self.controllerNames = nil;
   198     self.lastIndexPath = nil;
   262     self.lastIndexPath = nil;
   199     generalSettingsViewController = nil;
   263     generalSettingsViewController = nil;
   200     teamSettingsViewController = nil;
   264     teamSettingsViewController = nil;
   201     weaponSettingsViewController = nil;
   265     weaponSettingsViewController = nil;
   204     MSG_DIDUNLOAD();
   268     MSG_DIDUNLOAD();
   205     [super viewDidUnload];
   269     [super viewDidUnload];
   206 }
   270 }
   207 
   271 
   208 -(void) dealloc {
   272 -(void) dealloc {
   209     releaseAndNil(rootController);
       
   210     releaseAndNil(targetController);
   273     releaseAndNil(targetController);
   211     releaseAndNil(controllerNames);
   274     releaseAndNil(controllerNames);
   212     releaseAndNil(lastIndexPath);
   275     releaseAndNil(lastIndexPath);
   213     releaseAndNil(generalSettingsViewController);
   276     releaseAndNil(generalSettingsViewController);
   214     releaseAndNil(teamSettingsViewController);
   277     releaseAndNil(teamSettingsViewController);
   216     releaseAndNil(schemeSettingsViewController);
   279     releaseAndNil(schemeSettingsViewController);
   217     releaseAndNil(supportViewController);
   280     releaseAndNil(supportViewController);
   218     [super dealloc];
   281     [super dealloc];
   219 }
   282 }
   220 
   283 
   221 -(IBAction) dismissSplitView {
       
   222     [self.rootController dismissModalViewControllerAnimated:YES];
       
   223 }
       
   224 
       
   225 @end
   284 @end
   226 
   285