cocoaTouch/iPad/MainMenuViewController.m
author palewolf
Sun, 28 Mar 2010 23:41:58 +0000
changeset 3151 3f23113d9df4
parent 3122 e005359efc59
child 3165 3ec07a7d8456
permissions -rw-r--r--
AfterAttack was issued twice
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3113
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
     1
//
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
     2
//  MainMenuViewController.m
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
     3
//  hwengine
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
     4
//
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
     5
//  Created by Vittorio on 08/01/10.
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
     6
//  Copyright 2010 __MyCompanyName__. All rights reserved.
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
     7
//
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
     8
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
     9
#import "MainMenuViewController.h"
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    10
#import "SDL_uikitappdelegate.h"
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    11
#import "PascalImports.h"
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    12
#import "SplitViewRootController.h"
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    13
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    14
// in case we don't want SDL_mixer...
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    15
//#import "SoundEffect.h"	
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    16
//SoundEffect *erasingSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"Erase" ofType:@"caf"]];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    17
//SoundEffect *selectSound = [[SoundEffect alloc] initWithContentsOfFile:[mainBundle pathForResource:@"Select" ofType:@"caf"]];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    18
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    19
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    20
@implementation MainMenuViewController
3122
e005359efc59 hide the background gl context that remains active (might be worth freeing ithide the background gl context that remains active (might be worth freeing it))
koda
parents: 3113
diff changeset
    21
@synthesize cover;
3113
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    22
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    23
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    24
	return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    25
}
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    26
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    27
- (void)didReceiveMemoryWarning {
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    28
	// Releases the view if it doesn't have a superview.
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    29
	[super didReceiveMemoryWarning];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    30
}
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    31
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    32
- (void)dealloc {
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    33
	[super dealloc];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    34
}
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    35
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    36
-(void) viewDidLoad {
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    37
    // initialize some files the first time we load the game
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    38
	[NSThread detachNewThreadSelector:@selector(checkFirstRun) toTarget:self withObject:nil];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    39
    // listet to request to remove the modalviewcontroller
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    40
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissModalViewController) name: @"dismissModalView" object:nil];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    41
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    42
	[super viewDidLoad];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    43
}
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    44
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    45
// this is called to verify whether it's the first time the app is launched
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    46
// if it is it blocks user interaction with an alertView until files are created
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    47
-(void) checkFirstRun {
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    48
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    49
	
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    50
	NSString *filePath = [[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    51
	if (!([[NSFileManager defaultManager] fileExistsAtPath:filePath])) {
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    52
		// file not present, means that also other files are absent
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    53
		NSLog(@"First time run, creating settings files");
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    54
		
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    55
		// show a popup with an indicator to make the user wait
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    56
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"One-time Preferences Configuration",@"")
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    57
                                                        message:nil
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    58
                                                       delegate:nil
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    59
                                              cancelButtonTitle:nil
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    60
                                              otherButtonTitles:nil];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    61
		[alert show];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    62
		[alert release];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    63
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    64
		UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    65
		indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50);
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    66
		[indicator startAnimating];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    67
		[alert addSubview:indicator];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    68
		[indicator release];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    69
		
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    70
		// create settings.plist
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    71
		NSMutableDictionary *saveDict = [[NSMutableDictionary alloc] init];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    72
	
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    73
		[saveDict setObject:@"" forKey:@"username"];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    74
		[saveDict setObject:@"" forKey:@"password"];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    75
		[saveDict setObject:@"1" forKey:@"music"];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    76
		[saveDict setObject:@"1" forKey:@"sounds"];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    77
		[saveDict setObject:@"0" forKey:@"alternate"];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    78
	
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    79
		[saveDict writeToFile:filePath atomically:YES];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    80
		[saveDict release];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    81
		
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    82
		// create other files
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    83
		
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    84
		[alert dismissWithClickedButtonIndex:0 animated:YES];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    85
	}
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    86
	[pool release];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    87
	[NSThread exit];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    88
}
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    89
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    90
#pragma mark -
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    91
-(void) appear {
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    92
    [[SDLUIKitDelegate sharedAppDelegate].uiwindow addSubview:self.view];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    93
    [self release];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    94
    
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    95
    [UIView beginAnimations:@"inserting main controller" context:NULL];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    96
	[UIView setAnimationDuration:1];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    97
	self.view.alpha = 1;
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    98
	[UIView commitAnimations];
3122
e005359efc59 hide the background gl context that remains active (might be worth freeing ithide the background gl context that remains active (might be worth freeing it))
koda
parents: 3113
diff changeset
    99
    
e005359efc59 hide the background gl context that remains active (might be worth freeing ithide the background gl context that remains active (might be worth freeing it))
koda
parents: 3113
diff changeset
   100
    // this is a silly way to hide the sdl contex that remained active
e005359efc59 hide the background gl context that remains active (might be worth freeing ithide the background gl context that remains active (might be worth freeing it))
koda
parents: 3113
diff changeset
   101
    if (nil == cover) {
e005359efc59 hide the background gl context that remains active (might be worth freeing ithide the background gl context that remains active (might be worth freeing it))
koda
parents: 3113
diff changeset
   102
        cover= [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
e005359efc59 hide the background gl context that remains active (might be worth freeing ithide the background gl context that remains active (might be worth freeing it))
koda
parents: 3113
diff changeset
   103
        cover.backgroundColor = [UIColor blackColor];
e005359efc59 hide the background gl context that remains active (might be worth freeing ithide the background gl context that remains active (might be worth freeing it))
koda
parents: 3113
diff changeset
   104
    }
e005359efc59 hide the background gl context that remains active (might be worth freeing ithide the background gl context that remains active (might be worth freeing it))
koda
parents: 3113
diff changeset
   105
    [[SDLUIKitDelegate sharedAppDelegate].uiwindow insertSubview:cover belowSubview:self.view];
3113
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   106
}
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   107
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   108
-(void) disappear {
3122
e005359efc59 hide the background gl context that remains active (might be worth freeing ithide the background gl context that remains active (might be worth freeing it))
koda
parents: 3113
diff changeset
   109
    if (nil != cover)
e005359efc59 hide the background gl context that remains active (might be worth freeing ithide the background gl context that remains active (might be worth freeing it))
koda
parents: 3113
diff changeset
   110
        [cover release];
e005359efc59 hide the background gl context that remains active (might be worth freeing ithide the background gl context that remains active (might be worth freeing it))
koda
parents: 3113
diff changeset
   111
    
3113
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   112
    [UIView beginAnimations:@"removing main controller" context:NULL];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   113
	[UIView setAnimationDuration:1];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   114
	self.view.alpha = 0;
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   115
	[UIView commitAnimations];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   116
    
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   117
    [self retain];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   118
    [self.view removeFromSuperview];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   119
}
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   120
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   121
#pragma mark -
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   122
-(IBAction) switchViews:(id) sender {
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   123
    UIButton *button = (UIButton *)sender;
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   124
    SplitViewRootController *splitViewController;
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   125
    UIAlertView *alert;
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   126
    
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   127
    switch (button.tag) {
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   128
        case 0:
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   129
            [[SDLUIKitDelegate sharedAppDelegate] startSDLgame];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   130
            break;
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   131
        case 2:
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   132
            // for now this controller is just to simplify code management
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   133
            splitViewController = [[SplitViewRootController alloc] init];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   134
            splitViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   135
            [self presentModalViewController:splitViewController animated:YES];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   136
            break;
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   137
        default:
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   138
            alert = [[UIAlertView alloc] initWithTitle:@"Not Yet Implemented"
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   139
                                               message:@"Sorry, this feature is not yet implemented"
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   140
                                              delegate:nil
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   141
                                     cancelButtonTitle:@"Well, don't worry"
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   142
                                     otherButtonTitles:nil];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   143
            [alert show];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   144
            [alert release];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   145
            break;
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   146
    }
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   147
}
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   148
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   149
-(void) dismissModalViewController {
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   150
    [self dismissModalViewControllerAnimated:YES];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   151
}
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   152
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   153
@end