cocoaTouch/iPad/MainMenuViewController.m
changeset 3305 91074496d5c9
parent 3304 8690a3aa93b5
child 3306 fcdf6d85b042
equal deleted inserted replaced
3304:8690a3aa93b5 3305:91074496d5c9
     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 
       
    15 @implementation MainMenuViewController
       
    16 @synthesize cover;
       
    17 
       
    18 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
       
    19 	return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
       
    20 }
       
    21 
       
    22 - (void)didReceiveMemoryWarning {
       
    23 	// Releases the view if it doesn't have a superview.
       
    24 	[super didReceiveMemoryWarning];
       
    25 }
       
    26 
       
    27 - (void)dealloc {
       
    28     [cover release];
       
    29 	[super dealloc];
       
    30 }
       
    31 
       
    32 -(void) viewDidUnload {
       
    33     self.cover = nil;
       
    34 	[super viewDidUnload];
       
    35 }
       
    36 
       
    37 -(void) viewDidLoad {
       
    38     // initialize some files the first time we load the game
       
    39 	[NSThread detachNewThreadSelector:@selector(checkFirstRun) toTarget:self withObject:nil];
       
    40     // listen to request to remove the modalviewcontroller
       
    41     [[NSNotificationCenter defaultCenter] addObserver:self
       
    42                                              selector:@selector(dismissModalViewController)
       
    43                                                  name: @"dismissModalView" 
       
    44                                                object:nil];
       
    45 
       
    46 	[super viewDidLoad];
       
    47 }
       
    48 
       
    49 // this is called to verify whether it's the first time the app is launched
       
    50 // if it is it blocks user interaction with an alertView until files are created
       
    51 -(void) checkFirstRun {
       
    52 	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
       
    53 	
       
    54 	NSString *filePath = [[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"];
       
    55 	if (!([[NSFileManager defaultManager] fileExistsAtPath:filePath])) {
       
    56 		// file not present, means that also other files are absent
       
    57 		NSLog(@"First time run, creating settings files");
       
    58 		
       
    59 		// show a popup with an indicator to make the user wait
       
    60 		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Please wait",@"")
       
    61                                                         message:nil
       
    62                                                        delegate:nil
       
    63                                               cancelButtonTitle:nil
       
    64                                               otherButtonTitles:nil];
       
    65 		[alert show];
       
    66 		[alert release];
       
    67 
       
    68 		UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] 
       
    69                                               initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
       
    70 		indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50);
       
    71 		[indicator startAnimating];
       
    72 		[alert addSubview:indicator];
       
    73 		[indicator release];
       
    74 		
       
    75         // create Default Team.plist
       
    76         NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
       
    77         NSString *teamsDirectory = [[paths objectAtIndex:0] stringByAppendingString:@"Teams/"];
       
    78 		[[NSFileManager defaultManager] createDirectoryAtPath:teamsDirectory 
       
    79                                   withIntermediateDirectories:NO 
       
    80                                                    attributes:nil 
       
    81                                                         error:NULL];
       
    82 
       
    83         NSMutableArray *hedgehogs = [[NSMutableArray alloc] init];
       
    84 
       
    85         for (int i = 0; i < 8; i++) {
       
    86             NSString *hogName = [[NSString alloc] initWithFormat:@"hedgehog %d",i];
       
    87             NSDictionary *hog = [[NSDictionary alloc] initWithObjectsAndKeys:@"100",@"health",@"0",@"level",
       
    88                                  hogName,@"hogname",@"NoHat",@"hat",nil];
       
    89             [hogName release];
       
    90             [hedgehogs addObject:hog];
       
    91             [hog release];
       
    92         }
       
    93         
       
    94         NSDictionary *defaultTeam = [[NSDictionary alloc] initWithObjectsAndKeys:@"4421353",@"color",@"0",@"hash",
       
    95                                      @"Default Team",@"teamname",@"Statue",@"grave",@"Plane",@"fort",
       
    96                                      @"Default",@"voicepack",@"hedgewars",@"flag",hedgehogs,@"hedgehogs",nil];
       
    97         [hedgehogs release];
       
    98         NSString *defaultTeamFile = [teamsDirectory stringByAppendingString:@"Default Team.plist"];
       
    99         [defaultTeam writeToFile:defaultTeamFile atomically:YES];
       
   100         [defaultTeam release];
       
   101         
       
   102 		// create settings.plist
       
   103 		NSMutableDictionary *saveDict = [[NSMutableDictionary alloc] init];
       
   104 	
       
   105 		[saveDict setObject:@"" forKey:@"username"];
       
   106 		[saveDict setObject:@"" forKey:@"password"];
       
   107 		[saveDict setObject:@"1" forKey:@"music"];
       
   108 		[saveDict setObject:@"1" forKey:@"sounds"];
       
   109 		[saveDict setObject:@"0" forKey:@"alternate"];
       
   110 	
       
   111 		[saveDict writeToFile:filePath atomically:YES];
       
   112 		[saveDict release];
       
   113 		
       
   114 		// create other files
       
   115         
       
   116         // ok let the user take control
       
   117 		[alert dismissWithClickedButtonIndex:0 animated:YES];
       
   118 	}
       
   119 	[pool release];
       
   120 	[NSThread exit];
       
   121 }
       
   122 
       
   123 #pragma mark -
       
   124 -(void) appear {
       
   125     [[SDLUIKitDelegate sharedAppDelegate].uiwindow addSubview:self.view];
       
   126     [self release];
       
   127     
       
   128     [UIView beginAnimations:@"inserting main controller" context:NULL];
       
   129 	[UIView setAnimationDuration:1];
       
   130 	self.view.alpha = 1;
       
   131 	[UIView commitAnimations];
       
   132     
       
   133     [NSTimer scheduledTimerWithTimeInterval:0.7 target:self selector:@selector(hideBehind) userInfo:nil repeats:NO];
       
   134 }
       
   135 
       
   136 -(void) disappear {
       
   137     if (nil != cover)
       
   138         [cover release];
       
   139     
       
   140     [UIView beginAnimations:@"removing main controller" context:NULL];
       
   141 	[UIView setAnimationDuration:1];
       
   142 	self.view.alpha = 0;
       
   143 	[UIView commitAnimations];
       
   144     
       
   145     [self retain];
       
   146     [self.view removeFromSuperview];
       
   147 }
       
   148 
       
   149 // this is a silly way to hide the sdl contex that remained active
       
   150 -(void) hideBehind {
       
   151     if (nil == cover) {
       
   152         cover= [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
       
   153         cover.backgroundColor = [UIColor blackColor];
       
   154     }
       
   155     [[SDLUIKitDelegate sharedAppDelegate].uiwindow insertSubview:cover belowSubview:self.view];
       
   156 }
       
   157 
       
   158 #pragma mark -
       
   159 -(IBAction) switchViews:(id) sender {
       
   160     UIButton *button = (UIButton *)sender;
       
   161     SplitViewRootController *splitViewController;
       
   162     UIAlertView *alert;
       
   163     
       
   164     switch (button.tag) {
       
   165         case 0:
       
   166             [[SDLUIKitDelegate sharedAppDelegate] startSDLgame];
       
   167             break;
       
   168         case 2:
       
   169             // for now this controller is just to simplify code management
       
   170             splitViewController = [[SplitViewRootController alloc] initWithNibName:nil bundle:nil];
       
   171             splitViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
       
   172             [self presentModalViewController:splitViewController animated:YES];
       
   173             break;
       
   174         default:
       
   175             alert = [[UIAlertView alloc] initWithTitle:@"Not Yet Implemented"
       
   176                                                message:@"Sorry, this feature is not yet implemented"
       
   177                                               delegate:nil
       
   178                                      cancelButtonTitle:@"Well, don't worry"
       
   179                                      otherButtonTitles:nil];
       
   180             [alert show];
       
   181             [alert release];
       
   182             break;
       
   183     }
       
   184 }
       
   185 
       
   186 -(void) dismissModalViewController {
       
   187     [self dismissModalViewControllerAnimated:YES];
       
   188 }
       
   189 
       
   190 @end