project_files/HedgewarsMobile/Classes/MainMenuViewController.m
author koda
Tue, 20 Jul 2010 03:14:43 +0200
changeset 3659 f8d5ac50e307
parent 3621 a8ddf681ba7d
child 3660 bc125bea5849
permissions -rw-r--r--
half reworking of the settings page with delegation some graphical enhancements, leaks and fixes
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];
3621
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3616
diff changeset
    97
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3616
diff changeset
    98
    // TODO: instead of this useless runtime initialization, check that all ammos remain compatible with engine
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    99
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   100
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   101
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   102
-(IBAction) switchViews:(id) sender {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   103
    UIButton *button = (UIButton *)sender;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   104
    UIAlertView *alert;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   105
    NSString *debugStr;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   106
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   107
    switch (button.tag) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   108
        case 0:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   109
            gameConfigViewController = [[GameConfigViewController alloc] initWithNibName:@"GameConfigViewController" bundle:nil];        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   110
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   111
            [self presentModalViewController:gameConfigViewController animated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   112
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   113
        case 2:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   114
            if (nil == settingsViewController) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   115
                settingsViewController = [[SplitViewRootController alloc] initWithNibName:nil bundle:nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   116
                settingsViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   117
            }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   118
            
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   119
            [self presentModalViewController:settingsViewController animated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   120
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   121
        case 3:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   122
            debugStr = [[NSString alloc] initWithContentsOfFile:DEBUG_FILE()];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   123
            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
   124
            scroll.text = debugStr;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   125
            [debugStr release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   126
            scroll.editable = NO;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   127
            
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   128
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   129
            [btn addTarget:scroll action:@selector(removeFromSuperview) forControlEvents:UIControlEventTouchUpInside];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   130
            btn.backgroundColor = [UIColor blackColor];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   131
            btn.frame = CGRectMake(self.view.frame.size.height-70, 0, 70, 70);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   132
            [scroll addSubview:btn];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   133
            [self.view addSubview:scroll];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   134
            [scroll release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   135
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   136
        default:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   137
            alert = [[UIAlertView alloc] initWithTitle:@"Not Yet Implemented"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   138
                                               message:@"Sorry, this feature is not yet implemented"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   139
                                              delegate:nil
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   140
                                     cancelButtonTitle:@"Well, don't worry"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   141
                                     otherButtonTitles:nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   142
            [alert show];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   143
            [alert release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   144
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   145
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   146
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   147
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   148
// allows child controllers to return to the main controller
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   149
-(void) dismissModalViewController {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   150
    [self dismissModalViewControllerAnimated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   151
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   152
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   153
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   154
-(void) viewDidUnload {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   155
    self.versionLabel = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   156
    gameConfigViewController = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   157
    settingsViewController = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   158
    [super viewDidUnload];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   159
    MSG_DIDUNLOAD();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   160
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   161
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   162
-(void) dealloc {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   163
    [versionLabel release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   164
    [settingsViewController release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   165
    [gameConfigViewController release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   166
    [super dealloc];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   167
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   168
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   169
@end