project_files/HedgewarsMobile/Classes/MainMenuViewController.m
author koda
Sat, 31 Jul 2010 10:42:40 +0200
changeset 3693 09892cdb8f95
parent 3667 9359a70df013
child 3697 d5b30d6373fc
permissions -rw-r--r--
merge
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"
3667
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
    15
#import "SDL_mixer.h"
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    16
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    17
@implementation MainMenuViewController
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    18
@synthesize versionLabel;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    19
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    20
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    21
    return rotationManager(interfaceOrientation);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    22
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    23
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    24
- (void)didReceiveMemoryWarning {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    25
    // Releases the view if it doesn't have a superview.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    26
    [super didReceiveMemoryWarning];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    27
    if (settingsViewController.view.superview == nil) 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    28
        settingsViewController = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    29
    if (gameConfigViewController.view.superview == nil) 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    30
        gameConfigViewController = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    31
    MSG_MEMCLEAN();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    32
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    33
3667
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
    34
// using a different thread for audio 'cos it's slow
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
    35
-(void) initAudioThread {
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
    36
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
    37
    Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 1, 512);
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
    38
    [pool release];
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
    39
}
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
    40
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    41
-(void) viewDidLoad {
3667
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
    42
    [NSThread detachNewThreadSelector:@selector(initAudioThread)
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
    43
                             toTarget:self
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
    44
                           withObject:nil];
9359a70df013 use external libs more consistently
koda
parents: 3662
diff changeset
    45
    
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    46
    char *ver;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    47
    HW_versionInfo(NULL, &ver);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    48
    NSString *versionNumber = [[NSString alloc] initWithCString:ver];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
    self.versionLabel.text = versionNumber;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    50
    [versionNumber release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    51
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
    // listen to request to remove the modalviewcontroller
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    53
    [[NSNotificationCenter defaultCenter] addObserver:self
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    54
                                             selector:@selector(dismissModalViewController)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
                                                 name: @"dismissModalView" 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    56
                                               object:nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    57
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    58
    // initialize some files the first time we load the game
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    59
    if (!([[NSFileManager defaultManager] fileExistsAtPath:SETTINGS_FILE()])) 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    60
        [NSThread detachNewThreadSelector:@selector(checkFirstRun) toTarget:self withObject:nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    61
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    62
    [super viewDidLoad];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    63
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    64
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    65
// this is called to verify whether it's the first time the app is launched
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    66
// if it is it blocks user interaction with an alertView until files are created
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    67
-(void) checkFirstRun {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    68
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
3660
bc125bea5849 complete settings page rework
koda
parents: 3621
diff changeset
    69
    DLog(@"First time run, creating settings files at %@", SETTINGS_FILE());
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    70
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    71
    // show a popup with an indicator to make the user wait
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    72
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Please wait",@"")
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    73
                                                    message:nil
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    74
                                                   delegate:nil
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    75
                                          cancelButtonTitle:nil
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    76
                                          otherButtonTitles:nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    77
    [alert show];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    78
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    79
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    80
                                          initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    81
    indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    82
    [indicator startAnimating];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    83
    [alert addSubview:indicator];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    84
    [indicator release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    85
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    86
    // create default files (teams/weapons/scheme)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    87
    createTeamNamed(@"Pirates");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    88
    createTeamNamed(@"Ninjas");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    89
    createWeaponNamed(@"Default");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    90
    createSchemeNamed(@"Default");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    91
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    92
    // create settings.plist
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    93
    NSMutableDictionary *saveDict = [[NSMutableDictionary alloc] init];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    94
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    95
    [saveDict setObject:@"" forKey:@"username"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    96
    [saveDict setObject:@"" forKey:@"password"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    97
    [saveDict setObject:[NSNumber numberWithBool:YES] forKey:@"music"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    98
    [saveDict setObject:[NSNumber numberWithBool:YES] forKey:@"sound"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    99
    [saveDict setObject:[NSNumber numberWithBool:NO] forKey:@"alternate"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   100
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   101
    [saveDict writeToFile:SETTINGS_FILE() atomically:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   102
    [saveDict release];    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   103
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   104
    // ok let the user take control
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   105
    [alert dismissWithClickedButtonIndex:0 animated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   106
    [alert release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   107
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   108
    [pool release];
3621
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3616
diff changeset
   109
a8ddf681ba7d initial support for customizing weaponset
koda
parents: 3616
diff changeset
   110
    // 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
   111
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   112
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   113
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   114
-(IBAction) switchViews:(id) sender {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   115
    UIButton *button = (UIButton *)sender;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   116
    UIAlertView *alert;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   117
    NSString *debugStr;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   118
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   119
    switch (button.tag) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   120
        case 0:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   121
            gameConfigViewController = [[GameConfigViewController alloc] initWithNibName:@"GameConfigViewController" bundle:nil];        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   122
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   123
            [self presentModalViewController:gameConfigViewController animated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   124
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   125
        case 2:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   126
            if (nil == settingsViewController) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   127
                settingsViewController = [[SplitViewRootController alloc] initWithNibName:nil bundle:nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   128
                settingsViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   129
            }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   130
            
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   131
            [self presentModalViewController:settingsViewController animated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   132
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   133
        case 3:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   134
            debugStr = [[NSString alloc] initWithContentsOfFile:DEBUG_FILE()];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   135
            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
   136
            scroll.text = debugStr;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   137
            [debugStr release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   138
            scroll.editable = NO;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   139
            
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   140
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   141
            [btn addTarget:scroll action:@selector(removeFromSuperview) forControlEvents:UIControlEventTouchUpInside];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   142
            btn.backgroundColor = [UIColor blackColor];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   143
            btn.frame = CGRectMake(self.view.frame.size.height-70, 0, 70, 70);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   144
            [scroll addSubview:btn];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   145
            [self.view addSubview:scroll];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   146
            [scroll release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   147
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   148
        default:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   149
            alert = [[UIAlertView alloc] initWithTitle:@"Not Yet Implemented"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   150
                                               message:@"Sorry, this feature is not yet implemented"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   151
                                              delegate:nil
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   152
                                     cancelButtonTitle:@"Well, don't worry"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   153
                                     otherButtonTitles:nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   154
            [alert show];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   155
            [alert release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   156
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   157
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   158
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   159
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   160
// allows child controllers to return to the main controller
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   161
-(void) dismissModalViewController {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   162
    [self dismissModalViewControllerAnimated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   163
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   164
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   165
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   166
-(void) viewDidUnload {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   167
    self.versionLabel = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   168
    gameConfigViewController = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   169
    settingsViewController = nil;
3662
a44406f4369b polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents: 3660
diff changeset
   170
    MSG_DIDUNLOAD();
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   171
    [super viewDidUnload];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   172
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   173
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   174
-(void) dealloc {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   175
    [versionLabel release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   176
    [settingsViewController release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   177
    [gameConfigViewController release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   178
    [super dealloc];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   179
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   180
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   181
@end