cocoaTouch/iPad/MainMenuViewController.m
author koda
Sat, 27 Mar 2010 16:57:18 +0000
changeset 3113 2829ea0dd47c
child 3122 e005359efc59
permissions -rw-r--r--
* port fadeout to opengles * better notification mechanism * add a confirmation message before ending game * new structure for custom settings (splitview)
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
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    21
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    22
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    23
	return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    24
}
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    25
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    26
- (void)didReceiveMemoryWarning {
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    27
	// Releases the view if it doesn't have a superview.
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    28
	[super didReceiveMemoryWarning];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    29
}
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    30
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    31
- (void)dealloc {
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    32
	[super dealloc];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    33
}
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    34
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    35
-(void) viewDidLoad {
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    36
    // initialize some files the first time we load the game
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    37
	[NSThread detachNewThreadSelector:@selector(checkFirstRun) toTarget:self withObject:nil];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    38
    // listet to request to remove the modalviewcontroller
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    39
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissModalViewController) name: @"dismissModalView" object:nil];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    40
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    41
	[super viewDidLoad];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    42
}
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    43
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    44
// this is called to verify whether it's the first time the app is launched
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    45
// if it is it blocks user interaction with an alertView until files are created
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    46
-(void) checkFirstRun {
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    47
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    48
	
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    49
	NSString *filePath = [[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    50
	if (!([[NSFileManager defaultManager] fileExistsAtPath:filePath])) {
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    51
		// file not present, means that also other files are absent
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    52
		NSLog(@"First time run, creating settings files");
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    53
		
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    54
		// show a popup with an indicator to make the user wait
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    55
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"One-time Preferences Configuration",@"")
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    56
                                                        message:nil
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    57
                                                       delegate:nil
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    58
                                              cancelButtonTitle:nil
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    59
                                              otherButtonTitles:nil];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    60
		[alert show];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    61
		[alert release];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    62
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    63
		UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    64
		indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50);
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    65
		[indicator startAnimating];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    66
		[alert addSubview:indicator];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    67
		[indicator release];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    68
		
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    69
		// create settings.plist
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    70
		NSMutableDictionary *saveDict = [[NSMutableDictionary alloc] init];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    71
	
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    72
		[saveDict setObject:@"" forKey:@"username"];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    73
		[saveDict setObject:@"" forKey:@"password"];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    74
		[saveDict setObject:@"1" forKey:@"music"];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    75
		[saveDict setObject:@"1" forKey:@"sounds"];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    76
		[saveDict setObject:@"0" forKey:@"alternate"];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    77
	
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    78
		[saveDict writeToFile:filePath atomically:YES];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    79
		[saveDict release];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    80
		
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    81
		// create other files
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    82
		
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    83
		[alert dismissWithClickedButtonIndex:0 animated:YES];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    84
	}
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    85
	[pool release];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    86
	[NSThread exit];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    87
}
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    88
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    89
#pragma mark -
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    90
-(void) appear {
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    91
    [[SDLUIKitDelegate sharedAppDelegate].uiwindow addSubview:self.view];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    92
    [self release];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    93
    
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    94
    [UIView beginAnimations:@"inserting main controller" context:NULL];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    95
	[UIView setAnimationDuration:1];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    96
	self.view.alpha = 1;
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    97
	[UIView commitAnimations];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    98
}
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
    99
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   100
-(void) disappear {
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   101
    [UIView beginAnimations:@"removing main controller" context:NULL];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   102
	[UIView setAnimationDuration:1];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   103
	self.view.alpha = 0;
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   104
	[UIView commitAnimations];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   105
    
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   106
    [self retain];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   107
    [self.view removeFromSuperview];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   108
}
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   109
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   110
#pragma mark -
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   111
-(IBAction) switchViews:(id) sender {
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   112
    UIButton *button = (UIButton *)sender;
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   113
    SplitViewRootController *splitViewController;
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   114
    UIAlertView *alert;
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   115
    
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   116
    switch (button.tag) {
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   117
        case 0:
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   118
            [[SDLUIKitDelegate sharedAppDelegate] startSDLgame];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   119
            break;
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   120
        case 2:
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   121
            // for now this controller is just to simplify code management
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   122
            splitViewController = [[SplitViewRootController alloc] init];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   123
            splitViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   124
            [self presentModalViewController:splitViewController animated:YES];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   125
            break;
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   126
        default:
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   127
            alert = [[UIAlertView alloc] initWithTitle:@"Not Yet Implemented"
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   128
                                               message:@"Sorry, this feature is not yet implemented"
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   129
                                              delegate:nil
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   130
                                     cancelButtonTitle:@"Well, don't worry"
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   131
                                     otherButtonTitles:nil];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   132
            [alert show];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   133
            [alert release];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   134
            break;
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   135
    }
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   136
}
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   137
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   138
-(void) dismissModalViewController {
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   139
    [self dismissModalViewControllerAnimated:YES];
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   140
}
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   141
2829ea0dd47c * port fadeout to opengles
koda
parents:
diff changeset
   142
@end