project_files/HedgewarsMobile/Classes/MainMenuViewController.m
changeset 3514 59dbd31e9953
parent 3513 f589230fa21b
child 3522 156c04c6a3d8
equal deleted inserted replaced
3513:f589230fa21b 3514:59dbd31e9953
       
     1 //
       
     2 //  MainMenuViewController.m
       
     3 //  hwengine
       
     4 //
       
     5 //  Created by Vittorio on 08/01/10.
       
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
       
     7 //
       
     8 
       
     9 #import "MainMenuViewController.h"
       
    10 #import "SDL_uikitappdelegate.h"
       
    11 #import "PascalImports.h"
       
    12 #import "GameConfigViewController.h"
       
    13 #import "SplitViewRootController.h"
       
    14 #import "CommodityFunctions.h"
       
    15 
       
    16 @implementation MainMenuViewController
       
    17 @synthesize versionLabel;
       
    18 
       
    19 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
       
    20 	return rotationManager(interfaceOrientation);
       
    21 }
       
    22 
       
    23 - (void)didReceiveMemoryWarning {
       
    24 	// Releases the view if it doesn't have a superview.
       
    25 	[super didReceiveMemoryWarning];
       
    26     if (settingsViewController.view.superview == nil) 
       
    27         settingsViewController = nil;
       
    28     if (gameConfigViewController.view.superview == nil) 
       
    29         gameConfigViewController = nil;
       
    30     MSG_MEMCLEAN();
       
    31 }
       
    32 
       
    33 -(void) viewDidLoad {
       
    34     char *ver;
       
    35     HW_versionInfo(NULL, &ver);
       
    36     NSString *versionNumber = [[NSString alloc] initWithCString:ver];
       
    37     self.versionLabel.text = versionNumber;
       
    38     [versionNumber release];
       
    39 
       
    40     // listen to request to remove the modalviewcontroller
       
    41     [[NSNotificationCenter defaultCenter] addObserver:self
       
    42                                              selector:@selector(dismissModalViewController)
       
    43                                                  name: @"dismissModalView" 
       
    44                                                object:nil];
       
    45     
       
    46     // initialize some files the first time we load the game
       
    47 	if (!([[NSFileManager defaultManager] fileExistsAtPath:SETTINGS_FILE()])) 
       
    48         [NSThread detachNewThreadSelector:@selector(checkFirstRun) toTarget:self withObject:nil];
       
    49     
       
    50 	[super viewDidLoad];
       
    51 }
       
    52 
       
    53 // this is called to verify whether it's the first time the app is launched
       
    54 // if it is it blocks user interaction with an alertView until files are created
       
    55 -(void) checkFirstRun {
       
    56 	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
       
    57     NSLog(@"First time run, creating settings files at %@", SETTINGS_FILE());
       
    58     
       
    59     // show a popup with an indicator to make the user wait
       
    60     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Please wait",@"")
       
    61                                                     message:nil
       
    62                                                    delegate:nil
       
    63                                           cancelButtonTitle:nil
       
    64                                           otherButtonTitles:nil];
       
    65     [alert show];
       
    66     
       
    67     UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] 
       
    68                                           initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
       
    69     indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50);
       
    70     [indicator startAnimating];
       
    71     [alert addSubview:indicator];
       
    72     [indicator release];
       
    73     
       
    74     // create a team
       
    75     createTeamNamed(@"Pirates");
       
    76     createTeamNamed(@"Ninjas");
       
    77     
       
    78     createSchemeNamed(@"Default");
       
    79     
       
    80     // create settings.plist
       
    81     NSMutableDictionary *saveDict = [[NSMutableDictionary alloc] init];
       
    82 
       
    83     [saveDict setObject:@"" forKey:@"username"];
       
    84     [saveDict setObject:@"" forKey:@"password"];
       
    85     [saveDict setObject:[NSNumber numberWithBool:YES] forKey:@"music"];
       
    86     [saveDict setObject:[NSNumber numberWithBool:YES] forKey:@"sound"];
       
    87     [saveDict setObject:[NSNumber numberWithBool:NO] forKey:@"alternate"];
       
    88 
       
    89     [saveDict writeToFile:SETTINGS_FILE() atomically:YES];
       
    90     [saveDict release];    
       
    91     
       
    92     // ok let the user take control
       
    93     [alert dismissWithClickedButtonIndex:0 animated:YES];
       
    94     [alert release];
       
    95 
       
    96 	[pool release];
       
    97 	[NSThread exit];
       
    98 }
       
    99 
       
   100 #pragma mark -
       
   101 -(IBAction) switchViews:(id) sender {
       
   102     UIButton *button = (UIButton *)sender;
       
   103     UIAlertView *alert;
       
   104     NSString *debugStr, *configNibName;
       
   105 
       
   106     switch (button.tag) {
       
   107         case 0:
       
   108             // bug in UIModalTransitionStylePartialCurl, displays the controller awkwardly if it is not allocated every time
       
   109             if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
       
   110                 configNibName = @"GameConfigViewController-iPad";
       
   111             else
       
   112                 configNibName = @"GameConfigViewController-iPhone";
       
   113             
       
   114             gameConfigViewController = [[GameConfigViewController alloc] initWithNibName:configNibName bundle:nil];        
       
   115 #ifdef __IPHONE_3_2
       
   116             if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
       
   117                 gameConfigViewController.modalTransitionStyle = UIModalTransitionStylePartialCurl;
       
   118 #endif
       
   119             [self presentModalViewController:gameConfigViewController animated:YES];
       
   120             break;
       
   121         case 2:
       
   122             if (nil == settingsViewController) {
       
   123                 settingsViewController = [[SplitViewRootController alloc] initWithNibName:nil bundle:nil];
       
   124                 settingsViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
       
   125             }
       
   126             
       
   127             [self presentModalViewController:settingsViewController animated:YES];
       
   128             break;
       
   129         case 3:
       
   130             debugStr = [[NSString alloc] initWithContentsOfFile:DEBUG_FILE()];
       
   131             UITextView *scroll = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width)];
       
   132             scroll.text = debugStr;
       
   133             [debugStr release];
       
   134             scroll.editable = NO;
       
   135             
       
   136             UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
       
   137             [btn addTarget:scroll action:@selector(removeFromSuperview) forControlEvents:UIControlEventTouchUpInside];
       
   138             btn.backgroundColor = [UIColor blackColor];
       
   139             btn.frame = CGRectMake(self.view.frame.size.height-70, 0, 70, 70);
       
   140             [scroll addSubview:btn];
       
   141             [self.view addSubview:scroll];
       
   142             [scroll release];
       
   143             break;
       
   144         default:
       
   145             alert = [[UIAlertView alloc] initWithTitle:@"Not Yet Implemented"
       
   146                                                message:@"Sorry, this feature is not yet implemented"
       
   147                                               delegate:nil
       
   148                                      cancelButtonTitle:@"Well, don't worry"
       
   149                                      otherButtonTitles:nil];
       
   150             [alert show];
       
   151             [alert release];
       
   152             break;
       
   153     }
       
   154 }
       
   155 
       
   156 // allows child controllers to return to the main controller
       
   157 -(void) dismissModalViewController {
       
   158     [self dismissModalViewControllerAnimated:YES];
       
   159 }
       
   160 
       
   161 
       
   162 -(void) viewDidUnload {
       
   163     self.versionLabel = nil;
       
   164     gameConfigViewController = nil;
       
   165     settingsViewController = nil;
       
   166 	[super viewDidUnload];
       
   167     MSG_DIDUNLOAD();
       
   168 }
       
   169 
       
   170 -(void) dealloc {
       
   171     [versionLabel release];
       
   172     [settingsViewController release];
       
   173     [gameConfigViewController release];
       
   174 	[super dealloc];
       
   175 }
       
   176 
       
   177 @end