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