project_files/HedgewarsMobile/Classes/MainMenuViewController.m
author koda
Sun, 01 Aug 2010 17:58:09 +0200
changeset 3701 8c449776ebe6
parent 3697 d5b30d6373fc
child 3703 12d17c6e8855
permissions -rw-r--r--
rewrite portions of some uiviewcontrollers
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
3701
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    18
@synthesize versionLabel, gameConfigViewController, settingsViewController;
3547
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];
3701
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    27
    if (self.settingsViewController.view.superview == nil)
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    28
        self.settingsViewController = nil;
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    29
    if (self.gameConfigViewController.view.superview == nil)
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
    30
        self.gameConfigViewController = nil;
3547
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];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
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)
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    55
                                                 name: @"dismissModalView"
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    56
                                               object:nil];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    57
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    58
    // initialize some files the first time we load the game
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    59
    if (!([[NSFileManager defaultManager] fileExistsAtPath:SETTINGS_FILE()]))
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    60
        [NSThread detachNewThreadSelector:@selector(checkFirstRun) toTarget:self withObject:nil];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    61
3547
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());
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    70
3547
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];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    78
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    79
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc]
3547
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];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    85
3547
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");
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
    91
3547
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];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   102
    [saveDict release];
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   103
3547
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:
3701
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   121
            if (nil == self.gameConfigViewController) {
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   122
                GameConfigViewController *gcvc = [[GameConfigViewController alloc] initWithNibName:@"GameConfigViewController" bundle:nil];
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   123
                self.gameConfigViewController = gcvc;
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   124
                [gcvc release];
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   125
            }
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   126
3701
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   127
            [self presentModalViewController:self.gameConfigViewController animated:YES];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   128
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   129
        case 2:
3701
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   130
            if (nil == self.settingsViewController) {
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   131
                SplitViewRootController *svrc = [[SplitViewRootController alloc] initWithNibName:nil bundle:nil];
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   132
                svrc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   133
                self.settingsViewController = svrc;
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   134
                [svrc release];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   135
            }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   136
3701
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   137
            [self presentModalViewController:self.settingsViewController animated:YES];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   138
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   139
        case 3:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   140
            debugStr = [[NSString alloc] initWithContentsOfFile:DEBUG_FILE()];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   141
            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
   142
            scroll.text = debugStr;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   143
            [debugStr release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   144
            scroll.editable = NO;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   145
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   146
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   147
            [btn addTarget:scroll action:@selector(removeFromSuperview) forControlEvents:UIControlEventTouchUpInside];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   148
            btn.backgroundColor = [UIColor blackColor];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   149
            btn.frame = CGRectMake(self.view.frame.size.height-70, 0, 70, 70);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   150
            [scroll addSubview:btn];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   151
            [self.view addSubview:scroll];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   152
            [scroll release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   153
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   154
        default:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   155
            alert = [[UIAlertView alloc] initWithTitle:@"Not Yet Implemented"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   156
                                               message:@"Sorry, this feature is not yet implemented"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   157
                                              delegate:nil
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   158
                                     cancelButtonTitle:@"Well, don't worry"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   159
                                     otherButtonTitles:nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   160
            [alert show];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   161
            [alert release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   162
            break;
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
// allows child controllers to return to the main controller
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   167
-(void) dismissModalViewController {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   168
    [self dismissModalViewControllerAnimated:YES];
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
-(void) viewDidUnload {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   172
    self.versionLabel = nil;
3701
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   173
    self.gameConfigViewController = nil;
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   174
    self.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
   175
    MSG_DIDUNLOAD();
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   176
    [super viewDidUnload];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   177
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   178
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   179
-(void) dealloc {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   180
    [versionLabel release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   181
    [settingsViewController release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   182
    [gameConfigViewController release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   183
    [super dealloc];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   184
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   185
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   186
@end