project_files/HedgewarsMobile/Classes/GameConfigViewController.m
branchios-revival
changeset 11222 bd45676ab966
parent 11221 4d8ab775bad3
child 11228 e1bbd85a53b7
equal deleted inserted replaced
11221:4d8ab775bad3 11222:bd45676ab966
    53         case 1:
    53         case 1:
    54             [[AudioManagerController mainManager] playClickSound];
    54             [[AudioManagerController mainManager] playClickSound];
    55             if ([self isEverythingSet] == NO)
    55             if ([self isEverythingSet] == NO)
    56                 return;
    56                 return;
    57             theButton.enabled = NO;
    57             theButton.enabled = NO;
    58             for (UIView *oneView in self.imgContainer.subviews) {
    58             [self clearImgContainer];
    59                 if ([oneView isMemberOfClass:[UIImageView class]]) {
       
    60                     UIImageView *anImageView = (UIImageView *)oneView;
       
    61                     [anImageView removeFromSuperview];
       
    62                 }
       
    63             }
       
    64             [self startGame:theButton];
    59             [self startGame:theButton];
    65 
    60 
    66             break;
    61             break;
    67         case 2:
    62         case 2:
    68             [[AudioManagerController mainManager] playClickSound];
    63             [[AudioManagerController mainManager] playClickSound];
   227     [GameInterfaceBridge registerCallingController:self];
   222     [GameInterfaceBridge registerCallingController:self];
   228     [GameInterfaceBridge startLocalGame:gameDictionary];
   223     [GameInterfaceBridge startLocalGame:gameDictionary];
   229     [gameDictionary release];
   224     [gameDictionary release];
   230 }
   225 }
   231 
   226 
   232 -(void) loadNiceHogs {
   227 -(void) loadNiceHogs
   233     @autoreleasepool {
   228 {
       
   229     @autoreleasepool
       
   230     {
   234     
   231     
   235     NSString *filePath = [[NSString alloc] initWithFormat:@"%@/Hedgehog/Idle.png",GRAPHICS_DIRECTORY()];
   232         NSString *filePath = [[NSString alloc] initWithFormat:@"%@/Hedgehog/Idle.png",GRAPHICS_DIRECTORY()];
   236     UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:filePath];
   233         UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:filePath];
   237     [filePath release];
   234         [filePath release];
   238 
   235 
   239     NSArray *hatArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:HATS_DIRECTORY() error:NULL];
   236         NSArray *hatArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:HATS_DIRECTORY() error:NULL];
   240     NSUInteger numberOfHats = [hatArray count];
   237         NSUInteger numberOfHats = [hatArray count];
   241     int animationFrames = IS_VERY_POWERFUL([HWUtils modelType]) ? 16 : 1;
   238         int animationFrames = IS_VERY_POWERFUL([HWUtils modelType]) ? 16 : 1;
   242 
   239         
   243     if (self.imgContainer != nil)
   240         self.imgContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 40)];
   244         [self.imgContainer removeFromSuperview];
   241         NSInteger numberOfHogs = 1 + arc4random_uniform(15);
   245 
   242         DLog(@"Drawing %ld nice hedgehogs", (long)numberOfHogs);
   246     self.imgContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 40)];
   243         for (int i = 0; i < numberOfHogs; i++) {
   247     NSInteger numberOfHogs = 1 + arc4random_uniform(15);
   244             NSString *hat = [hatArray objectAtIndex:arc4random_uniform((int)numberOfHats)];
   248     DLog(@"Drawing %ld nice hedgehogs", (long)numberOfHogs);
   245 
   249     for (int i = 0; i < numberOfHogs; i++) {
   246             NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", HATS_DIRECTORY(), hat];
   250         NSString *hat = [hatArray objectAtIndex:arc4random_uniform((int)numberOfHats)];
   247             UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile:hatFile];
   251 
   248             NSMutableArray *animation = [[NSMutableArray alloc] initWithCapacity:animationFrames];
   252         NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", HATS_DIRECTORY(), hat];
   249             for (int j = 0; j < animationFrames; j++) {
   253         UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile:hatFile];
   250                 int x = ((j*32)/(int)hatSprite.size.height)*32;
   254         NSMutableArray *animation = [[NSMutableArray alloc] initWithCapacity:animationFrames];
   251                 int y = (j*32)%(int)hatSprite.size.height;
   255         for (int j = 0; j < animationFrames; j++) {
   252                 UIImage *hatSpriteFrame = [hatSprite cutAt:CGRectMake(x, y, 32, 32)];
   256             int x = ((j*32)/(int)hatSprite.size.height)*32;
   253                 UIImage *hogSpriteFrame = [hogSprite cutAt:CGRectMake(x, y, 32, 32)];
   257             int y = (j*32)%(int)hatSprite.size.height;
   254                 UIImage *hogWithHat = [hogSpriteFrame mergeWith:hatSpriteFrame atPoint:CGPointMake(0, 5)];
   258             UIImage *hatSpriteFrame = [hatSprite cutAt:CGRectMake(x, y, 32, 32)];
   255                 [animation addObject:hogWithHat];
   259             UIImage *hogSpriteFrame = [hogSprite cutAt:CGRectMake(x, y, 32, 32)];
   256             }
   260             UIImage *hogWithHat = [hogSpriteFrame mergeWith:hatSpriteFrame atPoint:CGPointMake(0, 5)];
   257             [hatSprite release];
   261             [animation addObject:hogWithHat];
   258             [hatFile release];
       
   259 
       
   260             UIImageView *hog = [[UIImageView alloc] initWithImage:[animation objectAtIndex:0]];
       
   261             hog.animationImages = animation;
       
   262             hog.animationDuration = 3;
       
   263             [animation release];
       
   264 
       
   265             int x = 20*i+arc4random_uniform(128);
       
   266             while (x > 320 - 32)
       
   267                 x = i*arc4random_uniform(32);
       
   268             
       
   269             hog.frame = CGRectMake(x, 25, hog.frame.size.width, hog.frame.size.height);
       
   270             [self.imgContainer addSubview:hog];
       
   271             [hog startAnimating];
       
   272             [hog release];
   262         }
   273         }
   263         [hatSprite release];
   274         [hogSprite release];
   264         [hatFile release];
       
   265 
       
   266         UIImageView *hog = [[UIImageView alloc] initWithImage:[animation objectAtIndex:0]];
       
   267         hog.animationImages = animation;
       
   268         hog.animationDuration = 3;
       
   269         [animation release];
       
   270 
       
   271         int x = 20*i+arc4random_uniform(128);
       
   272         while (x > 320 - 32)
       
   273             x = i*arc4random_uniform(32);
       
   274         
   275         
   275         hog.frame = CGRectMake(x, 25, hog.frame.size.width, hog.frame.size.height);
   276         dispatch_async(dispatch_get_main_queue(), ^{
   276         [self.imgContainer addSubview:hog];
   277             
   277         [hog startAnimating];
   278             [self.view addSubview:self.imgContainer];
   278         [hog release];
   279             
   279     }
   280             // don't place the nice hogs if there is no space for them
   280 
   281             if ((self.interfaceOrientation == UIInterfaceOrientationPortrait ||
   281     // don't place the nice hogs if there is no space for them
   282                  self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))
   282     if ((self.interfaceOrientation == UIInterfaceOrientationPortrait ||
   283                 self.imgContainer.alpha = 0;
   283          self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))
   284             
   284         self.imgContainer.alpha = 0;
   285             self.isDrawingNiceHogs = NO;
   285 
   286         });
   286     [self.view addSubview:self.imgContainer];
   287     }
   287     [hogSprite release];
   288 }
   288         
   289 
   289     }
   290 - (void)clearImgContainer
       
   291 {
       
   292     for (UIView *oneView in [self.imgContainer subviews])
       
   293     {
       
   294         if ([oneView isMemberOfClass:[UIImageView class]])
       
   295         {
       
   296             UIImageView *anImageView = (UIImageView *)oneView;
       
   297             [anImageView removeFromSuperview];
       
   298         }
       
   299     }
       
   300     
       
   301     [self.imgContainer removeFromSuperview];
       
   302     self.imgContainer = nil;
   290 }
   303 }
   291 
   304 
   292 -(void) viewDidLoad {
   305 -(void) viewDidLoad {
   293     self.view.backgroundColor = [UIColor blackColor];
   306     self.view.backgroundColor = [UIColor blackColor];
   294 
   307 
   359     }
   372     }
   360 }
   373 }
   361 
   374 
   362 -(void) viewWillAppear:(BOOL)animated
   375 -(void) viewWillAppear:(BOOL)animated
   363 {
   376 {
   364     [super viewDidAppear:animated];
   377     [super viewWillAppear:animated];
   365     
   378     
   366     if (IS_IPAD())
   379     if (IS_IPAD() && !self.imgContainer && !self.isDrawingNiceHogs)
       
   380     {
       
   381         self.isDrawingNiceHogs = YES;
   367         [NSThread detachNewThreadSelector:@selector(loadNiceHogs) toTarget:self withObject:nil];
   382         [NSThread detachNewThreadSelector:@selector(loadNiceHogs) toTarget:self withObject:nil];
       
   383     }
   368     
   384     
   369     if (IS_IPAD())
   385     if (IS_IPAD())
   370     {
   386     {
   371         // we assume here what 'statusBarOrientation' will never be changed manually!
   387         // we assume here what 'statusBarOrientation' will never be changed manually!
   372         UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation];
   388         UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation];
   373         [self updateiPadUIForInterfaceOrientation:currentOrientation];
   389         [self updateiPadUIForInterfaceOrientation:currentOrientation];
   374     }
   390     }
   375 
       
   376     [super viewWillAppear:animated];
       
   377 }
   391 }
   378 
   392 
   379 - (void)viewDidAppear:(BOOL)animated
   393 - (void)viewDidAppear:(BOOL)animated
   380 {
   394 {
   381     [super viewDidAppear:animated];
   395     [super viewDidAppear:animated];
   388     }
   402     }
   389 }
   403 }
   390 
   404 
   391 -(void) didReceiveMemoryWarning
   405 -(void) didReceiveMemoryWarning
   392 {
   406 {
   393     [self.imgContainer removeFromSuperview];
   407     [self clearImgContainer];
   394     self.imgContainer = nil;
       
   395 
   408 
   396     if (self.titleImage.superview == nil)
   409     if (self.titleImage.superview == nil)
   397         self.titleImage = nil;
   410         self.titleImage = nil;
   398     if (self.sliderBackground.superview == nil)
   411     if (self.sliderBackground.superview == nil)
   399         self.sliderBackground = nil;
   412         self.sliderBackground = nil;