// // ttMainMenuViewController.m // timetornado // // Created by Editor on 9/30/09. // Copyright 2009 Bennett Innovations. All rights reserved. // #import "ttMainMenuViewController.h" #import "timetornadoAppDelegate.h" @implementation ttMainMenuViewController @synthesize menuBG; //gameViewController @synthesize btnPlay, btnScores, mmVignette, mmHSReturn, mmHSText, mmHSText2,splashState; @synthesize ttMenuMusic, flickSoundID; #define kSplashStateNormal 1 #define kSplashStateDispScores 2 #define kSplashStateScrollingScores 3 #define khsPageLength 15 /* // The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { // Custom initialization } return self; } */ -(void)fadeMusicOut{ // pthread_mutex_lock( &(self->lock)); if (ttMenuMusic.volume<=0.0){ [ttMenuMusic stop]; // pthread_mutex_unlock(&(self->lock)); } else { ttMenuMusic.volume-=.05; [self performSelector:@selector(fadeMusicOut) withObject:nil afterDelay: 0.1]; } } -(void)viewDidLoad{ // pthread_mutex_init(&(self->lock),NULL); NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: @"tt-theme" ofType: @"caf"]; NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath]; ttMenuMusic = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil]; [fileURL release]; ttMenuMusic.numberOfLoops = -1; // Loop indefinately ttMenuMusic.currentTime = 0; // start at beginning ttMenuMusic.volume = 1.0; [ttMenuMusic play]; } - (void)loadView { NSString *tmpSoundPath = [[NSBundle mainBundle] pathForResource:@"flickSound" ofType:@"caf"]; CFURLRef tmpSoundURL = (CFURLRef ) [NSURL fileURLWithPath:tmpSoundPath]; AudioServicesCreateSystemSoundID (tmpSoundURL, & flickSoundID); CGRect appFrame=[[UIScreen mainScreen] applicationFrame]; UIView *view=[[UIView alloc] initWithFrame:appFrame]; view.autoresizingMask=UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; self.view=view; [view release]; menuBG=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mainMenuBG.png"]]; menuBG.frame=CGRectMake(0,0,320,480); [self.view addSubview:menuBG]; // gameViewController=[[timetornadoViewController alloc] initWithNibName:@"timetornadoViewController" bundle:[NSBundle mainBundle]]; // gameViewController.view.alpha=0.0; // [self.view addSubview:gameViewController.view]; btnPlay=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mainMenuBtnPlay.png"]]; btnPlay.frame=CGRectMake(0,0,132,59); btnPlay.center=CGPointMake(100,350); [self.view addSubview:btnPlay]; btnPlay.userInteractionEnabled=YES; btnScores=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mainMenuBtnScores.png"]]; btnScores.frame=CGRectMake(0,0,193,62); btnScores.center=CGPointMake(100,410); [self.view addSubview:btnScores]; btnScores.userInteractionEnabled=YES; mmVignette=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mainMenu-vignette.png"]]; mmVignette.frame=CGRectMake(0,0,320,480); mmVignette.center=CGPointMake(160,240); [self.view addSubview:mmVignette]; mmVignette.hidden=YES; mmHSReturn=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mainMenuHSReturn.png"]]; mmHSReturn.frame=CGRectMake(0,0,80,20); mmHSReturn.center=CGPointMake(440,20); [self.view addSubview:mmHSReturn]; mmHSReturn.userInteractionEnabled=YES; mmHSText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 420)]; [self.view addSubview:mmHSText]; mmHSText.backgroundColor = [UIColor clearColor]; mmHSText.font = [UIFont fontWithName:@"Trebuchet MS" size: 22.0]; mmHSText.textColor = [UIColor whiteColor]; mmHSText.lineBreakMode = UILineBreakModeWordWrap; mmHSText.textAlignment = UITextAlignmentLeft; mmHSText.shadowColor = [UIColor blackColor]; mmHSText.shadowOffset = CGSizeMake(1,1); mmHSText.numberOfLines = 0; // 2 lines ; 0 - dynamical number of lines mmHSText.center=CGPointMake(500,240); mmHSText2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 420)]; [self.view addSubview:mmHSText2]; mmHSText2.backgroundColor = [UIColor clearColor]; mmHSText2.font = [UIFont fontWithName:@"Trebuchet MS" size: 22.0]; mmHSText2.textColor = [UIColor whiteColor]; mmHSText2.textAlignment = UITextAlignmentLeft; mmHSText.shadowColor = [UIColor blackColor]; mmHSText.shadowOffset = CGSizeMake(1,1); mmHSText2.lineBreakMode = UILineBreakModeWordWrap; mmHSText2.numberOfLines = 0; // 2 lines ; 0 - dynamical number of lines mmHSText2.center=CGPointMake(600,240); splashState=kSplashStateNormal; } -(void)readHighScores{ hsNumItems=0; NSInteger myLimitStart=hsPage*khsPageLength; splashState=kSplashStateDispScores; mmHSText.text=@""; if (hsPageDir==0){ mmHSText.center=CGPointMake(-200,240); mmHSText2.center=CGPointMake(600,240); } else if (hsPageDir==1){ mmHSText2.center=CGPointMake(500,240); mmHSText.center=CGPointMake(500,240); } else { mmHSText2.center=CGPointMake(-200,240); mmHSText.center=CGPointMake(-200,240); } NSString *myURL=[NSString stringWithFormat:@"http://ridiculopathy.com/projects/flash/hs_get_list.php?gametext=timetornado&colums=2&limitStart=%i&limit=%i",myLimitStart,khsPageLength]; NSURL *url = [NSURL URLWithString:myURL]; NSString *myString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; NSArray *myArray = [[NSArray alloc] initWithArray:[myString componentsSeparatedByString:@"|"]]; NSString *nmText=[myArray objectAtIndex:0]; NSString *scrText=[myArray objectAtIndex:1]; mmHSText.text=nmText; mmHSText2.text=scrText; NSString *tmpString=[nmText stringByReplacingOccurrencesOfString:@"\n" withString:@"\r"]; NSArray *tmpArray=[tmpString componentsSeparatedByString:@"\r"]; hsNumItems=[tmpArray count]; hsNumItems--; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.4]; [UIView setAnimationDelegate:self]; mmHSText2.center=CGPointMake(300,240); mmHSText.center=CGPointMake(170,240); [UIView commitAnimations]; } -(void)fadeMenuInAgain{ menuBG.alpha=0.0; btnScores.center=CGPointMake(-160,410); btnPlay.center=CGPointMake(-160,350); mmHSReturn.center=CGPointMake(440,20); mmHSText.center=CGPointMake(500,240); mmHSText2.center=CGPointMake(600,240); mmVignette.alpha=0.0; menuBG.hidden=NO; btnScores.hidden=NO; btnPlay.hidden=NO; mmHSReturn.hidden=NO; mmHSText.hidden=NO; mmHSText2.hidden=NO; mmVignette.hidden=NO; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.4]; [UIView setAnimationDelegate:self]; // [UIView setAnimationDidStopSelector:@selector(menuFaded)]; menuBG.alpha=1.0; btnScores.center=CGPointMake(100,410); btnPlay.center=CGPointMake(100,350); [UIView commitAnimations]; } -(void)menuFaded{ timetornadoAppDelegate *mainDelegate = [[UIApplication sharedApplication] delegate]; [mainDelegate fromMenuToGame]; menuBG.hidden=YES; // init game btnScores.hidden=YES; btnPlay.hidden=YES; mmHSReturn.hidden=YES; mmHSText.hidden=YES; mmHSText2.hidden=YES; mmVignette.hidden=YES; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.4]; [UIView setAnimationDelegate:self]; // [UIView setAnimationDidStopSelector:@selector(menuFaded)]; self.view.alpha=1.0; // gameViewController.view.alpha=1.0; [UIView commitAnimations]; } -(void)afterHSReturn{ splashState=kSplashStateNormal; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject]; if (splashState==kSplashStateDispScores){ if ([touch view]==mmHSReturn){ } else { splashState=kSplashStateScrollingScores; touchStartX=[touch locationInView:self.view].x; } } } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject]; if (splashState==kSplashStateScrollingScores){ splashState=99; UITouch *touch = [touches anyObject]; CGPoint location=[touch locationInView:self.view]; if (location.x>touchStartX){ // page back hsPageDir=2; if (hsPage>0){ hsPage--; } else { hsPage=0; // calc last page? } AudioServicesPlaySystemSound(flickSoundID); [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.3]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(readHighScores)]; mmHSText.center=CGPointMake(450,240); mmHSText2.center=CGPointMake(450,240); [UIView commitAnimations]; } else { hsPageDir=1; if (hsNumItems