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