project_files/HedgewarsMobile/Classes/GameConfigViewController.m
branchqmlfrontend
changeset 11408 b894922d58cc
parent 11232 e1bbd85a53b7
child 11554 893722a2a1f9
--- a/project_files/HedgewarsMobile/Classes/GameConfigViewController.m	Tue Sep 08 19:20:58 2015 +0300
+++ b/project_files/HedgewarsMobile/Classes/GameConfigViewController.m	Mon Nov 16 22:57:24 2015 +0300
@@ -22,16 +22,25 @@
 #import "TeamConfigViewController.h"
 #import "SchemeWeaponConfigViewController.h"
 #import "GameInterfaceBridge.h"
+#import "HelpPageLobbyViewController.h"
 
+@interface GameConfigViewController ()
+@property (nonatomic, retain) IBOutlet UISegmentedControl *tabsSegmentedControl; //iPhone only
+
+@property (nonatomic, retain) IBOutlet UIBarButtonItem *backButton; //iPhone only
+@property (nonatomic, retain) IBOutlet UIBarButtonItem *startButton; //iPhone only
+@end
 
 @implementation GameConfigViewController
-@synthesize imgContainer, titleImage, sliderBackground, //helpPage,
+@synthesize imgContainer, titleImage, sliderBackground, helpPage,
             mapConfigViewController, teamConfigViewController, schemeWeaponConfigViewController;
 
 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
     return rotationManager(interfaceOrientation);
 }
 
+#pragma mark - Buttons
+
 -(IBAction) buttonPressed:(id) sender {
     UIButton *theButton = (UIButton *)sender;
 
@@ -47,7 +56,7 @@
                 [alert release];
             } else {
                 [[AudioManagerController mainManager] playBackSound];
-                [[self parentViewController] dismissModalViewControllerAnimated:YES];
+                [[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];
             }
             break;
         case 1:
@@ -55,26 +64,20 @@
             if ([self isEverythingSet] == NO)
                 return;
             theButton.enabled = NO;
-            for (UIView *oneView in self.imgContainer.subviews) {
-                if ([oneView isMemberOfClass:[UIImageView class]]) {
-                    UIImageView *anImageView = (UIImageView *)oneView;
-                    [anImageView removeFromSuperview];
-                }
-            }
+            [self clearImgContainer];
             [self startGame:theButton];
 
             break;
         case 2:
             [[AudioManagerController mainManager] playClickSound];
-            /*
             if (self.helpPage == nil)
                 self.helpPage = [[HelpPageLobbyViewController alloc] initWithNibName:@"HelpPageLobbyViewController-iPad" bundle:nil];
             self.helpPage.view.alpha = 0;
+            self.helpPage.view.frame = self.view.frame;
             [self.view addSubview:self.helpPage.view];
-            [UIView beginAnimations:@"helplobby" context:NULL];
-            self.helpPage.view.alpha = 1;
-            [UIView commitAnimations];
-            */
+            [UIView animateWithDuration:0.5 animations:^{
+                self.helpPage.view.alpha = 1;
+            }];
             break;
         default:
             DLog(@"Nope");
@@ -82,6 +85,17 @@
     }
 }
 
+#pragma mark - Tabs Segmented Control
+
+- (void)localizeTabsSegmentedControl
+{
+    for (NSUInteger i = 0; i < self.tabsSegmentedControl.numberOfSegments; i++)
+    {
+        NSString *oldTitle = [self.tabsSegmentedControl titleForSegmentAtIndex:i];
+        [self.tabsSegmentedControl setTitle:NSLocalizedString(oldTitle, nil) forSegmentAtIndex:i];
+    }
+}
+
 -(IBAction) segmentPressed:(id) sender {
 
     UISegmentedControl *theSegment = (UISegmentedControl *)sender;
@@ -104,15 +118,16 @@
             [self.view bringSubviewToFront:schemeWeaponConfigViewController.view];
             break;
         case 3:
-            /*
             if (helpPage == nil) {
                 helpPage = [[HelpPageLobbyViewController alloc] initWithNibName:@"HelpPageLobbyViewController-iPhone" bundle:nil];
+                CGRect helpPageFrame = self.view.frame;
+                helpPageFrame.size.height -= 44; //toolbar height
+                self.helpPage.view.frame = helpPageFrame;
                 [self.view addSubview:helpPage.view];
             }
             // this message is compulsory otherwise the table won't be loaded at all
             [helpPage viewWillAppear:NO];
             [self.view bringSubviewToFront:helpPage.view];
-            */
             break;
         default:
             DLog(@"Nope");
@@ -121,6 +136,8 @@
 
 }
 
+#pragma mark -
+
 -(BOOL) isEverythingSet {
     // don't start playing if the preview is in progress
     if ([self.mapConfigViewController busy]) {
@@ -229,71 +246,95 @@
     [gameDictionary release];
 }
 
--(void) loadNiceHogs {
-    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-    srand(time(NULL));
-    NSString *filePath = [[NSString alloc] initWithFormat:@"%@/Hedgehog/Idle.png",GRAPHICS_DIRECTORY()];
-    UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:filePath];
-    [filePath release];
+-(void) loadNiceHogs
+{
+    @autoreleasepool
+    {
+    
+        NSString *filePath = [[NSString alloc] initWithFormat:@"%@/Hedgehog/Idle.png",GRAPHICS_DIRECTORY()];
+        UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:filePath];
+        [filePath release];
 
-    NSArray *hatArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:HATS_DIRECTORY() error:NULL];
-    int numberOfHats = [hatArray count];
-    int animationFrames = IS_VERY_POWERFUL([HWUtils modelType]) ? 18 : 1;
-
-    if (self.imgContainer != nil)
-        [self.imgContainer removeFromSuperview];
-
-    self.imgContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 40)];
-    NSInteger numberOfHogs = 1 + random() % 20;
-    DLog(@"Drawing %d nice hedgehogs", numberOfHogs);
-    for (int i = 0; i < numberOfHogs; i++) {
-        NSString *hat = [hatArray objectAtIndex:random()%numberOfHats];
+        NSArray *hatArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:HATS_DIRECTORY() error:NULL];
+        NSUInteger numberOfHats = [hatArray count];
+        int animationFrames = IS_VERY_POWERFUL([HWUtils modelType]) ? 16 : 1;
+        
+        self.imgContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 40)];
+        NSInteger numberOfHogs = 1 + arc4random_uniform(15);
+        DLog(@"Drawing %ld nice hedgehogs", (long)numberOfHogs);
+        for (int i = 0; i < numberOfHogs; i++) {
+            NSString *hat = [hatArray objectAtIndex:arc4random_uniform((int)numberOfHats)];
 
-        NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", HATS_DIRECTORY(), hat];
-        UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile:hatFile];
-        NSMutableArray *animation = [[NSMutableArray alloc] initWithCapacity:animationFrames];
-        for (int j = 0; j < animationFrames; j++) {
-            int x = ((j*32)/(int)hatSprite.size.height)*32;
-            int y = (j*32)%(int)hatSprite.size.height;
-            UIImage *hatSpriteFrame = [hatSprite cutAt:CGRectMake(x, y, 32, 32)];
-            UIImage *hogSpriteFrame = [hogSprite cutAt:CGRectMake(x, y, 32, 32)];
-            UIImage *hogWithHat = [hogSpriteFrame mergeWith:hatSpriteFrame atPoint:CGPointMake(0, 5)];
-            [animation addObject:hogWithHat];
-        }
-        [hatSprite release];
-        [hatFile release];
+            NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", HATS_DIRECTORY(), hat];
+            UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile:hatFile];
+            NSMutableArray *animation = [[NSMutableArray alloc] initWithCapacity:animationFrames];
+            for (int j = 0; j < animationFrames; j++) {
+                int x = ((j*32)/(int)hatSprite.size.height)*32;
+                int y = (j*32)%(int)hatSprite.size.height;
+                UIImage *hatSpriteFrame = [hatSprite cutAt:CGRectMake(x, y, 32, 32)];
+                UIImage *hogSpriteFrame = [hogSprite cutAt:CGRectMake(x, y, 32, 32)];
+                UIImage *hogWithHat = [hogSpriteFrame mergeWith:hatSpriteFrame atPoint:CGPointMake(0, 5)];
+                [animation addObject:hogWithHat];
+            }
+            [hatSprite release];
+            [hatFile release];
+
+            UIImageView *hog = [[UIImageView alloc] initWithImage:[animation objectAtIndex:0]];
+            hog.animationImages = animation;
+            hog.animationDuration = 3;
+            [animation release];
 
-        UIImageView *hog = [[UIImageView alloc] initWithImage:[animation objectAtIndex:0]];
-        hog.animationImages = animation;
-        hog.animationDuration = 3;
-        [animation release];
-
-        int x = 20*i+random()%128;
-        if (x > 320 - 32)
-            x = i*random()%32;
-        hog.frame = CGRectMake(x, 25, hog.frame.size.width, hog.frame.size.height);
-        [self.imgContainer addSubview:hog];
-        [hog startAnimating];
-        [hog release];
+            int x = 20*i+arc4random_uniform(128);
+            while (x > 320 - 32)
+                x = i*arc4random_uniform(32);
+            
+            hog.frame = CGRectMake(x, 25, hog.frame.size.width, hog.frame.size.height);
+            [self.imgContainer addSubview:hog];
+            [hog startAnimating];
+            [hog release];
+        }
+        [hogSprite release];
+        
+        dispatch_async(dispatch_get_main_queue(), ^{
+            
+            [self.view addSubview:self.imgContainer];
+            
+            // don't place the nice hogs if there is no space for them
+            if ((self.interfaceOrientation == UIInterfaceOrientationPortrait ||
+                 self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))
+                self.imgContainer.alpha = 0;
+            
+            self.isDrawingNiceHogs = NO;
+        });
     }
-
-    // don't place the nice hogs if there is no space for them
-    if ((self.interfaceOrientation == UIInterfaceOrientationPortrait ||
-         self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))
-        self.imgContainer.alpha = 0;
-
-    [self.view addSubview:self.imgContainer];
-    [hogSprite release];
-    [pool drain];
 }
 
--(void) viewDidLoad {
+- (void)clearImgContainer
+{
+    for (UIView *oneView in [self.imgContainer subviews])
+    {
+        if ([oneView isMemberOfClass:[UIImageView class]])
+        {
+            UIImageView *anImageView = (UIImageView *)oneView;
+            [anImageView removeFromSuperview];
+        }
+    }
+    
+    [self.imgContainer removeFromSuperview];
+    self.imgContainer = nil;
+}
+
+-(void) viewDidLoad
+{
+    [super viewDidLoad];
+    
     self.view.backgroundColor = [UIColor blackColor];
 
     CGRect screenRect = [[UIScreen mainScreen] safeBounds];
     self.view.frame = screenRect;
 
-    if (IS_IPAD()) {
+    if (IS_IPAD())
+    {
         // the label for the filter slider
         UILabel *backLabel = [[UILabel alloc] initWithFrame:CGRectMake(116, 714, 310, 40)
                                                    andTitle:nil
@@ -312,21 +353,37 @@
         [self.view addSubview:maxLabel];
         self.mapConfigViewController.maxLabel = maxLabel;
         [maxLabel release];
-    } else {
+    }
+    else
+    {
+        [self localizeTabsSegmentedControl];
+        
+        [self.backButton setTitle:NSLocalizedString(@"Back", nil)];
+        [self.startButton setTitle:NSLocalizedString(@"Start", nil)];
+        
         self.mapConfigViewController.view.frame = CGRectMake(0, 0, screenRect.size.width, screenRect.size.height-44);
     }
+    
     [self.view addSubview:self.mapConfigViewController.view];
     [self.view bringSubviewToFront:self.mapConfigViewController.slider];
-
-    [super viewDidLoad];
 }
 
 -(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval) duration {
     if (IS_IPAD() == NO)
         return;
 
-    if ((toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
-         toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)) {
+    [self updateiPadUIForInterfaceOrientation:toInterfaceOrientation];
+
+    if (self.helpPage)
+    {
+        self.helpPage.view.frame = self.view.frame;
+    }
+}
+
+- (void)updateiPadUIForInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
+{
+    if ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
+         interfaceOrientation == UIInterfaceOrientationLandscapeRight)) {
         self.imgContainer.alpha = 1;
         self.titleImage.frame = CGRectMake(357, 17, 309, 165);
         self.schemeWeaponConfigViewController.view.frame = CGRectMake(0, 60, 320, 620);
@@ -345,46 +402,41 @@
         self.sliderBackground.frame = CGRectMake(465, 975, 200, 40);
         self.mapConfigViewController.slider.frame = CGRectMake(475, 983, 180, 23);
     }
-
-    [self.schemeWeaponConfigViewController willAnimateRotationToInterfaceOrientation:toInterfaceOrientation
-                                                                            duration:duration];
-}
-
--(void) viewWillAppear:(BOOL)animated {
-    if (IS_IPAD())
-        [NSThread detachNewThreadSelector:@selector(loadNiceHogs) toTarget:self withObject:nil];
-
-    [self.mapConfigViewController viewWillAppear:animated];
-    [self.teamConfigViewController viewWillAppear:animated];
-    [self.schemeWeaponConfigViewController viewWillAppear:animated];
-    // add other controllers here and below
-
-    [super viewWillAppear:animated];
 }
 
--(void) viewDidAppear:(BOOL)animated {
-    [self.mapConfigViewController viewDidAppear:animated];
-    [self.teamConfigViewController viewDidAppear:animated];
-    [self.schemeWeaponConfigViewController viewDidAppear:animated];
-    [super viewDidAppear:animated];
+-(void) viewWillAppear:(BOOL)animated
+{
+    [super viewWillAppear:animated];
+    
+    if (IS_IPAD() && !self.imgContainer && !self.isDrawingNiceHogs)
+    {
+        self.isDrawingNiceHogs = YES;
+        [NSThread detachNewThreadSelector:@selector(loadNiceHogs) toTarget:self withObject:nil];
+    }
+    
+    if (IS_IPAD())
+    {
+        // we assume here what 'statusBarOrientation' will never be changed manually!
+        UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation];
+        [self updateiPadUIForInterfaceOrientation:currentOrientation];
+    }
 }
 
--(void) viewWillDisappear:(BOOL)animated {
-    [self.mapConfigViewController viewWillDisappear:animated];
-    [self.teamConfigViewController viewWillDisappear:animated];
-    [self.schemeWeaponConfigViewController viewWillDisappear:animated];
-    [super viewWillDisappear:animated];
+- (void)viewDidAppear:(BOOL)animated
+{
+    [super viewDidAppear:animated];
+
+    if (IS_IPAD())
+    {
+        // need to call this again in order to fix layout on iOS 9 when going back from rotated stats page
+        UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation];
+        [self updateiPadUIForInterfaceOrientation:currentOrientation];
+    }
 }
 
--(void) viewDidDisappear:(BOOL)animated {
-    [self.mapConfigViewController viewDidDisappear:animated];
-    [self.teamConfigViewController viewDidDisappear:animated];
-    [self.schemeWeaponConfigViewController viewDidDisappear:animated];
-    [super viewDidDisappear:animated];
-}
-
--(void) didReceiveMemoryWarning {
-    self.imgContainer = nil;
+-(void) didReceiveMemoryWarning
+{
+    [self clearImgContainer];
 
     if (self.titleImage.superview == nil)
         self.titleImage = nil;
@@ -397,8 +449,8 @@
         self.teamConfigViewController = nil;
     if (self.schemeWeaponConfigViewController.view.superview == nil)
         self.schemeWeaponConfigViewController = nil;
-    //if (self.helpPage.view.superview == nil)
-    //    self.helpPage = nil;
+    if (self.helpPage.view.superview == nil)
+        self.helpPage = nil;
     MSG_MEMCLEAN();
     [super didReceiveMemoryWarning];
 }
@@ -410,19 +462,22 @@
     self.schemeWeaponConfigViewController = nil;
     self.teamConfigViewController = nil;
     self.mapConfigViewController = nil;
-    //self.helpPage = nil;
+    self.helpPage = nil;
     MSG_DIDUNLOAD();
     [super viewDidUnload];
 }
 
 -(void) dealloc {
+    releaseAndNil(_tabsSegmentedControl);
+    releaseAndNil(_backButton);
+    releaseAndNil(_startButton);
     releaseAndNil(imgContainer);
     releaseAndNil(titleImage);
     releaseAndNil(sliderBackground);
     releaseAndNil(schemeWeaponConfigViewController);
     releaseAndNil(teamConfigViewController);
     releaseAndNil(mapConfigViewController);
-    //releaseAndNil(helpPage);
+    releaseAndNil(helpPage);
     [super dealloc];
 }