project_files/HedgewarsMobile/Classes/MainMenuViewController.m
changeset 3546 ccf4854df294
parent 3535 9e78c1f3d8d8
child 3547 02875b1145b7
equal deleted inserted replaced
3545:b07ee704f35d 3546:ccf4854df294
     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 default files (teams/weapons/scheme)
       
    75     createTeamNamed(@"Pirates");
       
    76     createTeamNamed(@"Ninjas");
       
    77     createWeaponNamed(@"Default");
       
    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;
       
   105 
       
   106     switch (button.tag) {
       
   107         case 0:
       
   108             gameConfigViewController = [[GameConfigViewController alloc] initWithNibName:@"GameConfigViewController" bundle:nil];        
       
   109 
       
   110             // bug in UIModalTransitionStylePartialCurl, displays the controller awkwardly if it is not allocated every time            
       
   111             if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
       
   112                 gameConfigViewController.modalTransitionStyle = UIModalTransitionStylePartialCurl;
       
   113             [self presentModalViewController:gameConfigViewController animated:YES];
       
   114             break;
       
   115         case 2:
       
   116             if (nil == settingsViewController) {
       
   117                 settingsViewController = [[SplitViewRootController alloc] initWithNibName:nil bundle:nil];
       
   118                 settingsViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
       
   119             }
       
   120             
       
   121             [self presentModalViewController:settingsViewController animated:YES];
       
   122             break;
       
   123         case 3:
       
   124             debugStr = [[NSString alloc] initWithContentsOfFile:DEBUG_FILE()];
       
   125             UITextView *scroll = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width)];
       
   126             scroll.text = debugStr;
       
   127             [debugStr release];
       
   128             scroll.editable = NO;
       
   129             
       
   130             UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
       
   131             [btn addTarget:scroll action:@selector(removeFromSuperview) forControlEvents:UIControlEventTouchUpInside];
       
   132             btn.backgroundColor = [UIColor blackColor];
       
   133             btn.frame = CGRectMake(self.view.frame.size.height-70, 0, 70, 70);
       
   134             [scroll addSubview:btn];
       
   135             [self.view addSubview:scroll];
       
   136             [scroll release];
       
   137             break;
       
   138         default:
       
   139             alert = [[UIAlertView alloc] initWithTitle:@"Not Yet Implemented"
       
   140                                                message:@"Sorry, this feature is not yet implemented"
       
   141                                               delegate:nil
       
   142                                      cancelButtonTitle:@"Well, don't worry"
       
   143                                      otherButtonTitles:nil];
       
   144             [alert show];
       
   145             [alert release];
       
   146             break;
       
   147     }
       
   148 }
       
   149 
       
   150 // allows child controllers to return to the main controller
       
   151 -(void) dismissModalViewController {
       
   152     [self dismissModalViewControllerAnimated:YES];
       
   153 }
       
   154 
       
   155 
       
   156 -(void) viewDidUnload {
       
   157     self.versionLabel = nil;
       
   158     gameConfigViewController = nil;
       
   159     settingsViewController = nil;
       
   160 	[super viewDidUnload];
       
   161     MSG_DIDUNLOAD();
       
   162 }
       
   163 
       
   164 -(void) dealloc {
       
   165     [versionLabel release];
       
   166     [settingsViewController release];
       
   167     [gameConfigViewController release];
       
   168 	[super dealloc];
       
   169 }
       
   170 
       
   171 @end