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