project_files/HedgewarsMobile/Classes/GameConfigViewController.m
changeset 3948 24daa33a3114
parent 3930 8b00b4f93242
child 3971 5c82ee165ed5
equal deleted inserted replaced
3947:709fdb89f76c 3948:24daa33a3114
    28 #import "CommodityFunctions.h"
    28 #import "CommodityFunctions.h"
    29 #import "UIImageExtra.h"
    29 #import "UIImageExtra.h"
    30 #import "PascalImports.h"
    30 #import "PascalImports.h"
    31 
    31 
    32 @implementation GameConfigViewController
    32 @implementation GameConfigViewController
    33 @synthesize hedgehogImage, imgContainer, helpPage, mapConfigViewController, teamConfigViewController, schemeWeaponConfigViewController;
    33 @synthesize imgContainer, helpPage, mapConfigViewController, teamConfigViewController, schemeWeaponConfigViewController;
    34 
    34 
    35 
    35 
    36 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    36 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    37     return rotationManager(interfaceOrientation);
    37     return rotationManager(interfaceOrientation);
    38 }
    38 }
   200                                                                       mapConfigViewController.staticMapCommand,@"staticmap_command",
   200                                                                       mapConfigViewController.staticMapCommand,@"staticmap_command",
   201                                                                       mapConfigViewController.missionCommand,@"mission_command",  
   201                                                                       mapConfigViewController.missionCommand,@"mission_command",  
   202                                                                       teamConfigViewController.listOfSelectedTeams,@"teams_list",
   202                                                                       teamConfigViewController.listOfSelectedTeams,@"teams_list",
   203                                                                       schemeWeaponConfigViewController.selectedScheme,@"scheme",
   203                                                                       schemeWeaponConfigViewController.selectedScheme,@"scheme",
   204                                                                       schemeWeaponConfigViewController.selectedWeapon,@"weapon",
   204                                                                       schemeWeaponConfigViewController.selectedWeapon,@"weapon",
       
   205                                                                       [NSNumber numberWithInt:self.interfaceOrientation],@"orientation",
   205                                                                       nil];
   206                                                                       nil];
   206 
   207 
   207     // finally launch game and remove this controller
   208     // finally launch game and remove this controller
   208     DLog(@"sending config %@", gameDictionary);
   209     DLog(@"sending config %@", gameDictionary);
   209 
   210 
   210     if ([[gameDictionary allKeys] count] == 10) {
   211     if ([[gameDictionary allKeys] count] == 11) {
   211         NSDictionary *allDataNecessary = [NSDictionary dictionaryWithObjectsAndKeys:gameDictionary,@"game_dictionary", @"",@"savefile",
   212         NSDictionary *allDataNecessary = [NSDictionary dictionaryWithObjectsAndKeys:gameDictionary,@"game_dictionary", @"",@"savefile",
   212                                                                                     [NSNumber numberWithBool:NO],@"netgame", nil];
   213                                                                                     [NSNumber numberWithBool:NO],@"netgame", nil];
   213         [[SDLUIKitDelegate sharedAppDelegate] startSDLgame:allDataNecessary];
   214         [[SDLUIKitDelegate sharedAppDelegate] startSDLgame:allDataNecessary];
   214         
   215         
   215         // tell controllers that they're being reloaded
   216         // tell controllers that they're being reloaded
   240         [alert release];
   241         [alert release];
   241     }
   242     }
   242 
   243 
   243 }
   244 }
   244 
   245 
       
   246 -(void) loadNiceHogs {
       
   247     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
       
   248     NSString *filePath = [NSString stringWithFormat:@"%@/Hedgehog.png",GRAPHICS_DIRECTORY()];
       
   249     UIImage *sprite = [[UIImage alloc] initWithContentsOfFile:filePath andCutAt:CGRectMake(96, 0, 32, 32)];
       
   250     
       
   251     NSArray *hatArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:HATS_DIRECTORY() error:NULL];
       
   252     int numberOfHats = [hatArray count];
       
   253 
       
   254     if (self.imgContainer != nil)
       
   255         [self.imgContainer removeFromSuperview];
       
   256     
       
   257     self.imgContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 40)];
       
   258     for (int i = 0; i < 1 + random()%40; i++) {
       
   259         NSString *hat = [hatArray objectAtIndex:random()%numberOfHats];
       
   260         
       
   261         NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", HATS_DIRECTORY(), hat];
       
   262         UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)];
       
   263         [hatFile release];
       
   264         UIImage *hogWithHat = [sprite mergeWith:hatSprite atPoint:CGPointMake(0, 5)];
       
   265         [hatSprite release];
       
   266         
       
   267         UIImageView *hog = [[UIImageView alloc] initWithImage:hogWithHat];
       
   268         hog.frame = CGRectMake(10*(i+1)+random()%30, 30, 32, 32);
       
   269         [self.imgContainer addSubview:hog];
       
   270         [hog release];
       
   271     }
       
   272     [self.view addSubview:self.imgContainer];
       
   273     [sprite release];
       
   274     [pool drain];
       
   275 }
       
   276 
   245 -(void) viewDidLoad {
   277 -(void) viewDidLoad {
   246     self.view.backgroundColor = [UIColor blackColor];
   278     self.view.backgroundColor = [UIColor blackColor];
   247 
   279 
   248     CGRect screen = [[UIScreen mainScreen] bounds];
   280     CGRect screen = [[UIScreen mainScreen] bounds];
   249     self.view.frame = CGRectMake(0, 0, screen.size.height, screen.size.width);
   281     self.view.frame = CGRectMake(0, 0, screen.size.height, screen.size.width);
   252         [[NSNotificationCenter defaultCenter] addObserver:self
   284         [[NSNotificationCenter defaultCenter] addObserver:self
   253                                                  selector:@selector(buttonPressed:)
   285                                                  selector:@selector(buttonPressed:)
   254                                                      name:@"buttonPressed"
   286                                                      name:@"buttonPressed"
   255                                                    object:nil];
   287                                                    object:nil];
   256         srandom(time(NULL));
   288         srandom(time(NULL));
   257         self.hedgehogImage = nil;
       
   258         
   289         
   259         // load other controllers
   290         // load other controllers
   260         if (self.mapConfigViewController == nil)
   291         if (self.mapConfigViewController == nil)
   261             self.mapConfigViewController = [[MapConfigViewController alloc] initWithNibName:@"MapConfigViewController-iPad" bundle:nil];
   292             self.mapConfigViewController = [[MapConfigViewController alloc] initWithNibName:@"MapConfigViewController-iPad" bundle:nil];
   262         if (self.teamConfigViewController == nil)
   293         if (self.teamConfigViewController == nil)
   269         self.teamConfigViewController.view.frame = CGRectMake(348, 200, 328, 480);
   300         self.teamConfigViewController.view.frame = CGRectMake(348, 200, 328, 480);
   270         self.schemeWeaponConfigViewController.view.frame = CGRectMake(10, 70, 300, 600);
   301         self.schemeWeaponConfigViewController.view.frame = CGRectMake(10, 70, 300, 600);
   271         
   302         
   272     } else {
   303     } else {
   273         // this is the visible controller
   304         // this is the visible controller
   274         if (mapConfigViewController == nil)
   305         if (self.mapConfigViewController == nil)
   275             mapConfigViewController = [[MapConfigViewController alloc] initWithNibName:@"MapConfigViewController-iPhone" bundle:nil];
   306             self.mapConfigViewController = [[MapConfigViewController alloc] initWithNibName:@"MapConfigViewController-iPhone" bundle:nil];
   276         // this must be loaded & added to auto set default scheme and ammo
   307         // this must be loaded & added in order to auto set default scheme and ammo
   277         schemeWeaponConfigViewController = [[SchemeWeaponConfigViewController alloc] initWithStyle:UITableViewStyleGrouped];
   308         self.schemeWeaponConfigViewController = [[SchemeWeaponConfigViewController alloc] initWithStyle:UITableViewStyleGrouped];
   278         [self.view addSubview:schemeWeaponConfigViewController.view];
   309         [self.view addSubview:self.schemeWeaponConfigViewController.view];
   279     }
   310     }
   280     [self.view addSubview:mapConfigViewController.view];
   311     [self.view addSubview:self.mapConfigViewController.view];
   281 
   312 
   282     [super viewDidLoad];
   313     [super viewDidLoad];
   283 }
   314 }
   284 
   315 
   285 -(void) viewWillAppear:(BOOL)animated {
   316 -(void) viewWillAppear:(BOOL)animated {
   286     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
   317     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
   287         // load a base image that will be updated in viewWill Load
   318         [NSThread detachNewThreadSelector:@selector(loadNiceHogs) toTarget:self withObject:nil];
   288         if (self.hedgehogImage == nil) {
       
   289             NSString *filePath = [NSString stringWithFormat:@"%@/Hedgehog.png",GRAPHICS_DIRECTORY()];
       
   290             UIImage *sprite = [[UIImage alloc] initWithContentsOfFile:filePath andCutAt:CGRectMake(96, 0, 32, 32)];
       
   291             self.hedgehogImage = sprite;
       
   292             [sprite release];
       
   293         }
       
   294         
       
   295         NSArray *hatArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:HATS_DIRECTORY() error:NULL];
       
   296         int numberOfHats = [hatArray count];
       
   297         if (self.imgContainer == nil)
       
   298             self.imgContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 40)];
       
   299         
       
   300         for (int i=0; i < 1 + random()%40; i++) {
       
   301             NSString *hat = [hatArray objectAtIndex:random()%numberOfHats];
       
   302             
       
   303             NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", HATS_DIRECTORY(), hat];
       
   304             UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)];
       
   305             [hatFile release];
       
   306             UIImage *hogWithHat = [self.hedgehogImage mergeWith:hatSprite atPoint:CGPointMake(0, -5)];
       
   307             [hatSprite release];
       
   308             
       
   309             UIImageView *hog = [[UIImageView alloc] initWithImage:hogWithHat];
       
   310             hog.frame = CGRectMake(10*(i+1)+random()%30, 30, 32, 32);
       
   311             [self.imgContainer addSubview:hog];
       
   312             [hog release];
       
   313         }
       
   314         [self.view addSubview:self.imgContainer];
       
   315     }
       
   316 
   319 
   317     [mapConfigViewController viewWillAppear:animated];
   320     [mapConfigViewController viewWillAppear:animated];
   318     [teamConfigViewController viewWillAppear:animated];
   321     [teamConfigViewController viewWillAppear:animated];
   319     [schemeWeaponConfigViewController viewWillAppear:animated];
   322     [schemeWeaponConfigViewController viewWillAppear:animated];
   320     // add other controllers here and below
   323     // add other controllers here and below
   335     [schemeWeaponConfigViewController viewWillDisappear:animated];
   338     [schemeWeaponConfigViewController viewWillDisappear:animated];
   336     [super viewWillDisappear:animated];
   339     [super viewWillDisappear:animated];
   337 }
   340 }
   338 
   341 
   339 -(void) viewDidDisappear:(BOOL)animated {
   342 -(void) viewDidDisappear:(BOOL)animated {
   340     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
       
   341         [self.imgContainer removeFromSuperview];
       
   342         releaseAndNil(self.imgContainer);
       
   343     }
       
   344     
       
   345     [mapConfigViewController viewDidDisappear:animated];
   343     [mapConfigViewController viewDidDisappear:animated];
   346     [teamConfigViewController viewDidDisappear:animated];
   344     [teamConfigViewController viewDidDisappear:animated];
   347     [schemeWeaponConfigViewController viewDidDisappear:animated];
   345     [schemeWeaponConfigViewController viewDidDisappear:animated];
   348     [super viewDidDisappear:animated];
   346     [super viewDidDisappear:animated];
   349 }
   347 }
   359     if (helpPage.view.superview == nil)
   357     if (helpPage.view.superview == nil)
   360         helpPage = nil;
   358         helpPage = nil;
   361 
   359 
   362     // Release any cached data, images, etc that aren't in use.
   360     // Release any cached data, images, etc that aren't in use.
   363     self.imgContainer = nil;
   361     self.imgContainer = nil;
   364     self.hedgehogImage = nil;
       
   365     MSG_MEMCLEAN();
   362     MSG_MEMCLEAN();
   366     [super didReceiveMemoryWarning];
   363     [super didReceiveMemoryWarning];
   367 }
   364 }
   368 
   365 
   369 -(void) viewDidUnload {
   366 -(void) viewDidUnload {
   370     [[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:@"buttonPressed"];
   367     [[NSNotificationCenter defaultCenter] removeObserver:self];
   371     self.hedgehogImage = nil;
       
   372     self.imgContainer = nil;
   368     self.imgContainer = nil;
   373     self.mapConfigViewController = nil;
   369     self.mapConfigViewController = nil;
   374     self.teamConfigViewController = nil;
   370     self.teamConfigViewController = nil;
   375     self.schemeWeaponConfigViewController = nil;
   371     self.schemeWeaponConfigViewController = nil;
   376     self.helpPage = nil;
   372     self.helpPage = nil;
   377     MSG_DIDUNLOAD();
   373     MSG_DIDUNLOAD();
   378     [super viewDidUnload];
   374     [super viewDidUnload];
   379 }
   375 }
   380 
   376 
   381 -(void) dealloc {
   377 -(void) dealloc {
   382     [hedgehogImage release];
       
   383     [imgContainer release];
   378     [imgContainer release];
   384     [mapConfigViewController release];
   379     [mapConfigViewController release];
   385     [teamConfigViewController release];
   380     [teamConfigViewController release];
   386     [schemeWeaponConfigViewController release];
   381     [schemeWeaponConfigViewController release];
   387     [helpPage release];
   382     [helpPage release];