project_files/HedgewarsMobile/Classes/MainMenuViewController.m
branchqmlfrontend
changeset 11408 b894922d58cc
parent 11309 402baa6d4f64
child 11563 38996f467b23
equal deleted inserted replaced
11076:fcbdee9cdd74 11408:b894922d58cc
    18 
    18 
    19 
    19 
    20 #import "MainMenuViewController.h"
    20 #import "MainMenuViewController.h"
    21 #import <QuartzCore/QuartzCore.h>
    21 #import <QuartzCore/QuartzCore.h>
    22 #import "GameConfigViewController.h"
    22 #import "GameConfigViewController.h"
    23 #import "SettingsContainerViewController.h"
    23 #import "MGSplitViewController.h"
       
    24 #import "SettingsBaseViewController.h"
    24 #import "AboutViewController.h"
    25 #import "AboutViewController.h"
    25 #import "SavedGamesViewController.h"
    26 #import "SavedGamesViewController.h"
    26 #import "RestoreViewController.h"
    27 #import "RestoreViewController.h"
    27 #import "MissionTrainingViewController.h"
    28 #import "MissionTrainingViewController.h"
    28 #import "Appirater.h"
    29 #import "Appirater.h"
    29 #import "ServerProtocolNetwork.h"
    30 #import "ServerProtocolNetwork.h"
    30 #import "GameInterfaceBridge.h"
    31 #import "GameInterfaceBridge.h"
    31 
    32 
       
    33 #import "SettingsBaseViewController.h"
       
    34 #import "GeneralSettingsViewController.h"
       
    35 #import "TeamSettingsViewController.h"
       
    36 #import "WeaponSettingsViewController.h"
       
    37 #import "SchemeSettingsViewController.h"
       
    38 #import "SupportViewController.h"
       
    39 
       
    40 #ifdef DEBUG
       
    41 #import "GameLogViewController.h"
       
    42 #endif
       
    43 
       
    44 @interface MainMenuViewController ()
       
    45 @property (retain, nonatomic) IBOutlet UIButton *simpleGameButton;
       
    46 @property (retain, nonatomic) IBOutlet UIButton *missionsButton;
       
    47 @end
    32 
    48 
    33 @implementation MainMenuViewController
    49 @implementation MainMenuViewController
    34 @synthesize gameConfigViewController, settingsViewController, aboutViewController, savedGamesViewController,
       
    35             restoreViewController, missionsViewController;
       
    36 
    50 
    37 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    51 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    38     return rotationManager(interfaceOrientation);
    52     return rotationManager(interfaceOrientation);
    39 }
    53 }
    40 
    54 
    41 #pragma mark -
    55 #pragma mark -
    42 -(void) viewDidLoad {
    56 -(void) viewDidLoad {
    43     self.view.frame = [[UIScreen mainScreen] safeBounds];
    57     self.view.frame = [[UIScreen mainScreen] safeBounds];
    44     [super viewDidLoad];
    58     [super viewDidLoad];
    45 
    59     
       
    60     [self.simpleGameButton setTitle:NSLocalizedString(@"Simple", nil) forState:UIControlStateNormal];
       
    61     [self.missionsButton setTitle:NSLocalizedString(@"Missions", nil) forState:UIControlStateNormal];
       
    62     
       
    63     [self.simpleGameButton applyDarkBlueQuickStyle];
       
    64     [self.missionsButton applyDarkBlueQuickStyle];
       
    65     
    46     // get the app's version
    66     // get the app's version
    47     NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey];
    67     NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey];
    48 
    68 
    49     // get the version number that we've been tracking
    69     // get the version number that we've been tracking
    50     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    70     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    60         [CreationChamber createFirstLaunch];
    80         [CreationChamber createFirstLaunch];
    61     }
    81     }
    62 
    82 
    63     // prompt for restoring any previous game
    83     // prompt for restoring any previous game
    64     NSString *saveString = [userDefaults objectForKey:@"savedGamePath"];
    84     NSString *saveString = [userDefaults objectForKey:@"savedGamePath"];
    65     if (saveString != nil && [saveString isEqualToString:@""] == NO && [[userDefaults objectForKey:@"saveIsValid"] boolValue]) {
    85     if (saveString != nil && [saveString isEqualToString:@""] == NO && [[userDefaults objectForKey:@"saveIsValid"] boolValue])
    66         if (self.restoreViewController == nil) {
    86     {
    67             NSString *xibName = [@"RestoreViewController-" stringByAppendingString:(IS_IPAD() ? @"iPad" : @"iPhone")];
    87         NSString *xibName = [@"RestoreViewController-" stringByAppendingString:(IS_IPAD() ? @"iPad" : @"iPhone")];
    68             RestoreViewController *restored = [[RestoreViewController alloc] initWithNibName:xibName bundle:nil];
    88         RestoreViewController *restored = [[RestoreViewController alloc] initWithNibName:xibName bundle:nil];
    69             if ([restored respondsToSelector:@selector(setModalPresentationStyle:)])
    89         if ([restored respondsToSelector:@selector(setModalPresentationStyle:)])
    70                 restored.modalPresentationStyle = UIModalPresentationFormSheet;
    90             restored.modalPresentationStyle = UIModalPresentationFormSheet;
    71             self.restoreViewController = restored;
    91 
    72             [restored release];
    92         [self performSelector:@selector(presentViewController:) withObject:restored afterDelay:0.25];
    73         }
    93     }
    74         [self performSelector:@selector(presentModalViewController:animated:) withObject:self.restoreViewController afterDelay:0.25];
    94     else
    75     } else {
    95     {
    76         // let's not prompt for rating when app crashed >_>
    96         // let's not prompt for rating when app crashed >_>
    77         [Appirater appLaunched];
    97         [Appirater appLaunched:YES];
    78     }
    98     }
    79 
    99 
    80     /*
   100     /*
    81     [ServerProtocolNetwork openServerConnection];
   101     [ServerProtocolNetwork openServerConnection];
    82     */
   102     */
       
   103 }
       
   104 
       
   105 - (void) presentViewController:(UIViewController *)vc
       
   106 {
       
   107     [self presentViewController:vc animated:NO completion:nil];
       
   108     [vc release];
    83 }
   109 }
    84 
   110 
    85 -(void) viewWillAppear:(BOOL)animated {
   111 -(void) viewWillAppear:(BOOL)animated {
    86     [[AudioManagerController mainManager] playBackgroundMusic];
   112     [[AudioManagerController mainManager] playBackgroundMusic];
    87     [super viewWillAppear:animated];
   113     [super viewWillAppear:animated];
    90 #pragma mark -
   116 #pragma mark -
    91 -(IBAction) switchViews:(id) sender {
   117 -(IBAction) switchViews:(id) sender {
    92     UIButton *button = (UIButton *)sender;
   118     UIButton *button = (UIButton *)sender;
    93     UIAlertView *alert;
   119     UIAlertView *alert;
    94     NSString *xib = nil;
   120     NSString *xib = nil;
    95     NSString *debugStr = nil;
       
    96 
   121 
    97     [[AudioManagerController mainManager] playClickSound];
   122     [[AudioManagerController mainManager] playClickSound];
    98     switch (button.tag) {
   123     switch (button.tag) {
    99         case 0:
   124         case 0:
   100             if (nil == self.gameConfigViewController) {
   125             xib = IS_IPAD() ? @"GameConfigViewController-iPad" : @"GameConfigViewController-iPhone";
   101                 xib = IS_IPAD() ? @"GameConfigViewController-iPad" : @"GameConfigViewController-iPhone";
   126 
   102 
   127             GameConfigViewController *gcvc = [[GameConfigViewController alloc] initWithNibName:xib bundle:nil];
   103                 GameConfigViewController *gcvc = [[GameConfigViewController alloc] initWithNibName:xib bundle:nil];
   128             gcvc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
   104                 gcvc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
   129 
   105                 self.gameConfigViewController = gcvc;
   130             [self presentViewController:gcvc animated:YES completion:nil];
   106                 [gcvc release];
   131             [gcvc release];
   107             }
       
   108             [self presentModalViewController:self.gameConfigViewController animated:YES];
       
   109             break;
   132             break;
   110         case 2:
   133         case 2:
   111             if (nil == self.settingsViewController) {
   134             if (IS_IPAD())
   112                 SettingsContainerViewController *svrc = [[SettingsContainerViewController alloc] initWithNibName:nil bundle:nil];
   135             {
   113                 svrc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
   136                 // the contents on the right of the splitview, setting targetController to nil to avoid creating the table
   114                 self.settingsViewController = svrc;
   137                 SettingsBaseViewController *rightController = [[SettingsBaseViewController alloc] init];
   115                 [svrc release];
   138                 rightController.targetController = nil;
   116             }
   139                 UINavigationController *rightNavController = [[UINavigationController alloc] initWithRootViewController:rightController];
   117             [self presentModalViewController:self.settingsViewController animated:YES];
   140                 [rightController release];
       
   141                 
       
   142                 // the contens on the left of the splitview, setting targetController that will receive push/pop actions
       
   143                 SettingsBaseViewController *leftController = [[SettingsBaseViewController alloc] init];
       
   144                 leftController.targetController = rightNavController.topViewController;
       
   145                 UINavigationController *leftNavController = [[UINavigationController alloc] initWithRootViewController:leftController];
       
   146                 [leftController release];
       
   147                 
       
   148                 MGSplitViewController *splitViewRootController = [[MGSplitViewController alloc] init];
       
   149                 splitViewRootController.delegate = nil;
       
   150                 splitViewRootController.showsMasterInPortrait = YES;
       
   151                 splitViewRootController.viewControllers = [NSArray arrayWithObjects:leftNavController, rightNavController, nil];
       
   152                 [leftNavController release];
       
   153                 [rightNavController release];
       
   154 
       
   155                 [self presentViewController:splitViewRootController animated:YES completion:nil];
       
   156                 [splitViewRootController release];
       
   157             }
       
   158             else
       
   159             {
       
   160                 NSMutableArray *tabBarNavigationControllers = [[NSMutableArray alloc] initWithCapacity:5];
       
   161                 
       
   162                 UIViewController *generalSettingsViewController = [[GeneralSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
       
   163                 generalSettingsViewController.tabBarItem = [self tabBarItemWithTitle:NSLocalizedString(@"General",@"") imageName:@"flower" selectedImageName:@"flower_filled"];
       
   164                 UINavigationController *generalNavController = [[UINavigationController alloc] initWithRootViewController:generalSettingsViewController];
       
   165                 [generalSettingsViewController release];
       
   166                 [tabBarNavigationControllers addObject:generalNavController];
       
   167                 [generalNavController release];
       
   168                 
       
   169                 UIViewController *teamSettingsViewController = [[TeamSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
       
   170                 teamSettingsViewController.tabBarItem = [self tabBarItemWithTitle:NSLocalizedString(@"Teams",@"") imageName:@"teams" selectedImageName:@"teams_filled"];
       
   171                 UINavigationController *teamNavController = [[UINavigationController alloc] initWithRootViewController:teamSettingsViewController];
       
   172                 [teamSettingsViewController release];
       
   173                 [tabBarNavigationControllers addObject:teamNavController];
       
   174                 [teamNavController release];
       
   175                 
       
   176                 UIViewController *weaponSettingsViewController = [[WeaponSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
       
   177                 weaponSettingsViewController.tabBarItem = [self tabBarItemWithTitle:NSLocalizedString(@"Weapons",@"") imageName:@"bullet" selectedImageName:@"bullet_filled"];
       
   178                 UINavigationController *weaponNavController = [[UINavigationController alloc] initWithRootViewController:weaponSettingsViewController];
       
   179                 [weaponSettingsViewController release];
       
   180                 [tabBarNavigationControllers addObject:weaponNavController];
       
   181                 [weaponNavController release];
       
   182                 
       
   183                 UIViewController *schemeSettingsViewController = [[SchemeSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
       
   184                 schemeSettingsViewController.tabBarItem = [self tabBarItemWithTitle:NSLocalizedString(@"Schemes",@"") imageName:@"target" selectedImageName:@"target_filled"];
       
   185                 UINavigationController *schemeNavController = [[UINavigationController alloc] initWithRootViewController:schemeSettingsViewController];
       
   186                 [schemeSettingsViewController release];
       
   187                 [tabBarNavigationControllers addObject:schemeNavController];
       
   188                 [schemeNavController release];
       
   189                 
       
   190                 UIViewController *supportViewController = [[SupportViewController alloc] initWithStyle:UITableViewStyleGrouped];
       
   191                 supportViewController.tabBarItem = [self tabBarItemWithTitle:NSLocalizedString(@"Support",@"") imageName:@"heart" selectedImageName:@"heart_filled"];
       
   192                 UINavigationController *supportNavController = [[UINavigationController alloc] initWithRootViewController:supportViewController];
       
   193                 [supportViewController release];
       
   194                 [tabBarNavigationControllers addObject:supportNavController];
       
   195                 [supportNavController release];
       
   196                 
       
   197                 UITabBarController *settingsTabController = [[UITabBarController alloc] init];
       
   198                 settingsTabController.viewControllers = tabBarNavigationControllers;
       
   199                 [tabBarNavigationControllers release];
       
   200                 
       
   201                 [self presentViewController:settingsTabController animated:YES completion:nil];
       
   202                 [settingsTabController release];
       
   203             }
   118             break;
   204             break;
   119         case 3:
   205         case 3:
   120 #ifdef DEBUG
   206 #ifdef DEBUG
   121             if ([[NSFileManager defaultManager] fileExistsAtPath:DEBUG_FILE()])
   207             {
   122                 debugStr = [[NSString alloc] initWithContentsOfFile:DEBUG_FILE()];
   208                 GameLogViewController *gameLogVC = [[GameLogViewController alloc] init];
   123             else
   209                 UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:gameLogVC];
   124                 debugStr = [[NSString alloc] initWithString:@"Here be log"];
   210                 [gameLogVC release];
   125             UITextView *scroll = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width)];
   211                 
   126             scroll.text = debugStr;
   212                 [self presentViewController:navController animated:YES completion:nil];
   127             [debugStr release];
   213                 [navController release];
   128             scroll.editable = NO;
   214             }
   129             scroll.alpha = 0;
       
   130 
       
   131             UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
       
   132             [btn addTarget:scroll action:@selector(removeFromSuperview) forControlEvents:UIControlEventTouchUpInside];
       
   133             [btn addTarget:btn action:@selector(removeFromSuperview) forControlEvents:UIControlEventTouchUpInside];
       
   134             btn.frame = CGRectMake(self.view.frame.size.height-58, -6, 64, 64);
       
   135             btn.backgroundColor = [UIColor blackColor];
       
   136             btn.titleLabel.textColor = [UIColor whiteColor];
       
   137             btn.titleLabel.textAlignment = UITextAlignmentCenter;
       
   138             btn.titleLabel.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
       
   139             [btn setTitle:@"Close" forState:UIControlStateNormal];
       
   140             btn.alpha = 0;
       
   141             [btn.layer setCornerRadius:10.0f];
       
   142             [btn.layer setMasksToBounds:YES];
       
   143 
       
   144             [self.view addSubview:scroll];
       
   145             [self.view addSubview:btn];
       
   146 
       
   147             [UIView beginAnimations:@"fadein" context:NULL];
       
   148             [UIView setAnimationDuration:0.25f];
       
   149             btn.alpha = 1;
       
   150             scroll.alpha = 1;
       
   151             [UIView commitAnimations];
       
   152             [scroll release];
       
   153 #else
   215 #else
   154             debugStr = debugStr; // prevent compiler warning
   216             {
   155             if (nil == self.aboutViewController) {
       
   156                 AboutViewController *about = [[AboutViewController alloc] initWithNibName:@"AboutViewController" bundle:nil];
   217                 AboutViewController *about = [[AboutViewController alloc] initWithNibName:@"AboutViewController" bundle:nil];
   157                 about.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
   218                 about.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
   158                 if ([about respondsToSelector:@selector(setModalPresentationStyle:)])
   219                 if ([about respondsToSelector:@selector(setModalPresentationStyle:)])
   159                      about.modalPresentationStyle = UIModalPresentationFormSheet;
   220                      about.modalPresentationStyle = UIModalPresentationFormSheet;
   160                 self.aboutViewController = about;
   221                 
       
   222                 [self presentViewController:about animated:YES completion:nil];
   161                 [about release];
   223                 [about release];
   162             }
   224             }
   163             [self presentModalViewController:self.aboutViewController animated:YES];
       
   164 #endif
   225 #endif
   165             break;
   226             break;
   166         case 4:
   227         case 4:
   167             if (nil == self.savedGamesViewController) {
   228             {
   168                 SavedGamesViewController *savedgames = [[SavedGamesViewController alloc] initWithNibName:@"SavedGamesViewController" bundle:nil];
   229                 SavedGamesViewController *savedgames = [[SavedGamesViewController alloc] initWithNibName:@"SavedGamesViewController" bundle:nil];
   169                 savedgames.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
   230                 savedgames.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
   170                 if ([savedgames respondsToSelector:@selector(setModalPresentationStyle:)])
   231                 if ([savedgames respondsToSelector:@selector(setModalPresentationStyle:)])
   171                     savedgames.modalPresentationStyle = UIModalPresentationPageSheet;
   232                     savedgames.modalPresentationStyle = UIModalPresentationPageSheet;
   172                 self.savedGamesViewController = savedgames;
   233                 
       
   234                 [self presentViewController:savedgames animated:YES completion:nil];
   173                 [savedgames release];
   235                 [savedgames release];
   174             }
   236             }
   175             [self presentModalViewController:self.savedGamesViewController animated:YES];
       
   176             break;
   237             break;
   177         case 5:
   238         case 5:
   178             if (nil == self.missionsViewController) {
   239             {
   179                 xib = IS_IPAD() ? @"MissionTrainingViewController-iPad" : @"MissionTrainingViewController-iPhone";
   240                 xib = IS_IPAD() ? @"MissionTrainingViewController-iPad" : @"MissionTrainingViewController-iPhone";
   180                 MissionTrainingViewController *missions = [[MissionTrainingViewController alloc] initWithNibName:xib bundle:nil];
   241                 MissionTrainingViewController *missions = [[MissionTrainingViewController alloc] initWithNibName:xib bundle:nil];
   181                 missions.modalTransitionStyle = IS_IPAD() ? UIModalTransitionStyleCoverVertical : UIModalTransitionStyleCrossDissolve;
   242                 missions.modalTransitionStyle = IS_IPAD() ? UIModalTransitionStyleCoverVertical : UIModalTransitionStyleCrossDissolve;
   182                 if ([missions respondsToSelector:@selector(setModalPresentationStyle:)])
   243                 if ([missions respondsToSelector:@selector(setModalPresentationStyle:)])
   183                     missions.modalPresentationStyle = UIModalPresentationPageSheet;
   244                     missions.modalPresentationStyle = UIModalPresentationPageSheet;
   184                 self.missionsViewController = missions;
   245                 
       
   246                 [self presentViewController:missions animated:YES completion:nil];
   185                 [missions release];
   247                 [missions release];
   186             }
   248             }
   187             [self presentModalViewController:self.missionsViewController animated:YES];
       
   188             break;
   249             break;
   189         case 6:
   250         case 6:
   190             [GameInterfaceBridge registerCallingController:self];
   251             [GameInterfaceBridge registerCallingController:self];
   191             [GameInterfaceBridge startSimpleGame];
   252             [GameInterfaceBridge startSimpleGame];
   192             break;
   253             break;
   200             [alert release];
   261             [alert release];
   201             break;
   262             break;
   202     }
   263     }
   203 }
   264 }
   204 
   265 
       
   266 - (UITabBarItem *)tabBarItemWithTitle: (NSString *)title
       
   267                             imageName: (NSString *)imageName
       
   268                     selectedImageName: (NSString *)selectedImageName
       
   269 {
       
   270     return [[[UITabBarItem alloc] initWithTitle:title
       
   271                                           image:[UIImage imageNamed:imageName]
       
   272                                   selectedImage:[UIImage imageNamed:selectedImageName]] autorelease];
       
   273 }
       
   274 
   205 #pragma mark -
   275 #pragma mark -
   206 -(void) viewDidUnload {
   276 -(void) viewDidUnload {
   207     self.gameConfigViewController = nil;
       
   208     self.settingsViewController = nil;
       
   209     self.aboutViewController = nil;
       
   210     self.savedGamesViewController = nil;
       
   211     self.restoreViewController = nil;
       
   212     self.missionsViewController = nil;
       
   213     MSG_DIDUNLOAD();
   277     MSG_DIDUNLOAD();
   214     [super viewDidUnload];
   278     [super viewDidUnload];
   215 }
   279 }
   216 
   280 
   217 -(void) didReceiveMemoryWarning {
   281 -(void) didReceiveMemoryWarning {
   218     if (self.settingsViewController.view.superview == nil)
       
   219         self.settingsViewController = nil;
       
   220     if (self.gameConfigViewController.view.superview == nil)
       
   221         self.gameConfigViewController = nil;
       
   222     if (self.aboutViewController.view.superview == nil)
       
   223         self.aboutViewController = nil;
       
   224     if (self.savedGamesViewController.view.superview == nil)
       
   225         self.savedGamesViewController = nil;
       
   226     if (self.restoreViewController.view.superview == nil)
       
   227         self.restoreViewController = nil;
       
   228     if (self.missionsViewController.view.superview == nil)
       
   229         self.missionsViewController = nil;
       
   230     MSG_MEMCLEAN();
   282     MSG_MEMCLEAN();
   231     [super didReceiveMemoryWarning];
   283     [super didReceiveMemoryWarning];
   232 }
   284 }
   233 
   285 
   234 -(void) dealloc {
   286 -(void) dealloc {
   235     releaseAndNil(settingsViewController);
   287     [_simpleGameButton release];
   236     releaseAndNil(gameConfigViewController);
   288     [_missionsButton release];
   237     releaseAndNil(aboutViewController);
       
   238     releaseAndNil(savedGamesViewController);
       
   239     releaseAndNil(restoreViewController);
       
   240     releaseAndNil(missionsViewController);
       
   241     [super dealloc];
   289     [super dealloc];
   242 }
   290 }
   243 
   291 
   244 @end
   292 @end