project_files/HedgewarsMobile/Classes/MainMenuViewController.m
author koda
Sat, 08 Oct 2011 21:17:24 +0200
changeset 6103 55ecfbf834e3
parent 6094 16b12a6417d1
child 6104 117bdf4e7af9
permissions -rw-r--r--
convert creation chamber functions to class methods
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3829
81db3c85784b headers ftw, also right project file
koda
parents: 3825
diff changeset
     1
/*
81db3c85784b headers ftw, also right project file
koda
parents: 3825
diff changeset
     2
 * Hedgewars-iOS, a Hedgewars port for iOS devices
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 4754
diff changeset
     3
 * Copyright (c) 2009-2011 Vittorio Giovara <vittorio.giovara@gmail.com>
3829
81db3c85784b headers ftw, also right project file
koda
parents: 3825
diff changeset
     4
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3825
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
81db3c85784b headers ftw, also right project file
koda
parents: 3825
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
81db3c85784b headers ftw, also right project file
koda
parents: 3825
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
81db3c85784b headers ftw, also right project file
koda
parents: 3825
diff changeset
     8
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3825
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
81db3c85784b headers ftw, also right project file
koda
parents: 3825
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
81db3c85784b headers ftw, also right project file
koda
parents: 3825
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
81db3c85784b headers ftw, also right project file
koda
parents: 3825
diff changeset
    12
 * GNU General Public License for more details.
81db3c85784b headers ftw, also right project file
koda
parents: 3825
diff changeset
    13
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3825
diff changeset
    14
 * You should have received a copy of the GNU General Public License
81db3c85784b headers ftw, also right project file
koda
parents: 3825
diff changeset
    15
 * along with this program; if not, write to the Free Software
81db3c85784b headers ftw, also right project file
koda
parents: 3825
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
81db3c85784b headers ftw, also right project file
koda
parents: 3825
diff changeset
    17
 *
81db3c85784b headers ftw, also right project file
koda
parents: 3825
diff changeset
    18
 * File created on 08/01/2010.
81db3c85784b headers ftw, also right project file
koda
parents: 3825
diff changeset
    19
 */
81db3c85784b headers ftw, also right project file
koda
parents: 3825
diff changeset
    20
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    21
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    22
#import "MainMenuViewController.h"
4281
e033cf015b2c redo once again file updating, moving stuff around
koda
parents: 4149
diff changeset
    23
#import "CreationChamber.h"
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    24
#import "GameConfigViewController.h"
6075
0173cd44c3bc rename the ios settings files into something more appropriate
koda
parents: 6020
diff changeset
    25
#import "SettingsContainerViewController.h"
3703
12d17c6e8855 halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents: 3701
diff changeset
    26
#import "AboutViewController.h"
3893
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents: 3891
diff changeset
    27
#import "SavedGamesViewController.h"
5220
76a2246f18f0 when the match is not completed (eg out of memory or crash) the game asks for restoring it as soon as it is opened again
koda
parents: 5219
diff changeset
    28
#import "RestoreViewController.h"
6083
72c882c0fd0f first pass for implementing missions/training selection on ipad (not yet running)
koda
parents: 6075
diff changeset
    29
#import "MissionTrainingViewController.h"
6020
c792d4b3e080 this finally fixes the interaction between RestoredGameViewController and StatsPagesViewController
koda
parents: 6008
diff changeset
    30
#import "GameInterfaceBridge.h"
5483
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5224
diff changeset
    31
#import "Appirater.h"
4547
b70004a576a3 move server connection stuff in its own class
koda
parents: 4463
diff changeset
    32
#import "ServerSetup.h"
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    33
6000
dbcebcd3d79f ios frontend: sounds and music have their own class now (with caching\!) instead of being spread here and there (exploiting class methods like a true oop pro)
koda
parents: 5984
diff changeset
    34
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    35
@implementation MainMenuViewController
6083
72c882c0fd0f first pass for implementing missions/training selection on ipad (not yet running)
koda
parents: 6075
diff changeset
    36
@synthesize gameConfigViewController, settingsViewController, aboutViewController, savedGamesViewController,
72c882c0fd0f first pass for implementing missions/training selection on ipad (not yet running)
koda
parents: 6075
diff changeset
    37
            restoreViewController, missionsViewController;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    38
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    39
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    40
    return rotationManager(interfaceOrientation);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    41
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    42
3884
d7479079a8a8 various fixes around, plus new 'ask for review' page
koda
parents: 3829
diff changeset
    43
// check if some configuration files are already set; if they are present it means that the current copy must be updated
d7479079a8a8 various fixes around, plus new 'ask for review' page
koda
parents: 3829
diff changeset
    44
-(void) createNecessaryFiles {
4284
57a501a69e5f update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents: 4281
diff changeset
    45
    NSString *resourcesDir = [[NSBundle mainBundle] resourcePath];
3923
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3903
diff changeset
    46
    DLog(@"Creating necessary files");
3737
2ba6ac8a114b reworked the initialization functions, now it should be safe to update and no more need of spinning wheel at first launch
koda
parents: 3703
diff changeset
    47
    
4281
e033cf015b2c redo once again file updating, moving stuff around
koda
parents: 4149
diff changeset
    48
    // SAVES - just delete and overwrite
3923
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3903
diff changeset
    49
    if ([[NSFileManager defaultManager] fileExistsAtPath:SAVES_DIRECTORY()])
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3903
diff changeset
    50
        [[NSFileManager defaultManager] removeItemAtPath:SAVES_DIRECTORY() error:NULL];
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3903
diff changeset
    51
    [[NSFileManager defaultManager] createDirectoryAtPath:SAVES_DIRECTORY() withIntermediateDirectories:NO attributes:nil error:NULL];
3752
73c2d7d5643b add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents: 3738
diff changeset
    52
    
5206
db775bddf771 settings.plist is no more, long live nsuserdefaults
koda
parents: 5002
diff changeset
    53
    // SETTINGS - nsuserdefaults ftw
6103
55ecfbf834e3 convert creation chamber functions to class methods
koda
parents: 6094
diff changeset
    54
    [CreationChamber createSettings];
4281
e033cf015b2c redo once again file updating, moving stuff around
koda
parents: 4149
diff changeset
    55
e033cf015b2c redo once again file updating, moving stuff around
koda
parents: 4149
diff changeset
    56
    // TEAMS - update exisiting teams with new format
e033cf015b2c redo once again file updating, moving stuff around
koda
parents: 4149
diff changeset
    57
    if ([[NSFileManager defaultManager] fileExistsAtPath:TEAMS_DIRECTORY()] == NO) {
e033cf015b2c redo once again file updating, moving stuff around
koda
parents: 4149
diff changeset
    58
        [[NSFileManager defaultManager] createDirectoryAtPath:TEAMS_DIRECTORY()
e033cf015b2c redo once again file updating, moving stuff around
koda
parents: 4149
diff changeset
    59
                                  withIntermediateDirectories:YES
e033cf015b2c redo once again file updating, moving stuff around
koda
parents: 4149
diff changeset
    60
                                                   attributes:nil
e033cf015b2c redo once again file updating, moving stuff around
koda
parents: 4149
diff changeset
    61
                                                        error:NULL];
e033cf015b2c redo once again file updating, moving stuff around
koda
parents: 4149
diff changeset
    62
        // we copy teams only the first time because it's unlikely that newer ones are going to be added
4284
57a501a69e5f update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents: 4281
diff changeset
    63
        NSString *baseTeamsDir = [[NSString alloc] initWithFormat:@"%@/Settings/Teams/",resourcesDir];
4281
e033cf015b2c redo once again file updating, moving stuff around
koda
parents: 4149
diff changeset
    64
        for (NSString *str in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:baseTeamsDir error:NULL]) {
4463
1fe77f2bc1d1 update schemes for ios
koda
parents: 4358
diff changeset
    65
            NSString *sourceFile = [baseTeamsDir stringByAppendingString:str];
1fe77f2bc1d1 update schemes for ios
koda
parents: 4358
diff changeset
    66
            NSString *destinationFile = [TEAMS_DIRECTORY() stringByAppendingString:str];
4284
57a501a69e5f update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents: 4281
diff changeset
    67
            [[NSFileManager defaultManager] removeItemAtPath:destinationFile error:NULL];
4281
e033cf015b2c redo once again file updating, moving stuff around
koda
parents: 4149
diff changeset
    68
            [[NSFileManager defaultManager] copyItemAtPath:sourceFile toPath:destinationFile error:NULL];
4284
57a501a69e5f update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents: 4281
diff changeset
    69
        }
57a501a69e5f update iFrontend with new schemes and weaps, fix up smaller glitches
koda
parents: 4281
diff changeset
    70
        [baseTeamsDir release];
4281
e033cf015b2c redo once again file updating, moving stuff around
koda
parents: 4149
diff changeset
    71
    }
4463
1fe77f2bc1d1 update schemes for ios
koda
parents: 4358
diff changeset
    72
    // merge not needed as format rarely changes
4281
e033cf015b2c redo once again file updating, moving stuff around
koda
parents: 4149
diff changeset
    73
4463
1fe77f2bc1d1 update schemes for ios
koda
parents: 4358
diff changeset
    74
    // SCHEMES - always overwrite and delete custom ones
1fe77f2bc1d1 update schemes for ios
koda
parents: 4358
diff changeset
    75
    if ([[NSFileManager defaultManager] fileExistsAtPath:SCHEMES_DIRECTORY()] == YES)
1fe77f2bc1d1 update schemes for ios
koda
parents: 4358
diff changeset
    76
        [[NSFileManager defaultManager] removeItemAtPath:SCHEMES_DIRECTORY() error:NULL];
6103
55ecfbf834e3 convert creation chamber functions to class methods
koda
parents: 6094
diff changeset
    77
    NSArray *schemeNames = [[NSArray alloc] initWithObjects:@"Default",@"Pro Mode",@"Shoppa",@"Clean Slate",@"Minefield",
55ecfbf834e3 convert creation chamber functions to class methods
koda
parents: 6094
diff changeset
    78
                            @"Barrel Mayhem",@"Tunnel Hogs",@"Fort Mode",@"Timeless",@"Thinking with Portals",@"King Mode",nil];
55ecfbf834e3 convert creation chamber functions to class methods
koda
parents: 6094
diff changeset
    79
    NSInteger index = 0;
55ecfbf834e3 convert creation chamber functions to class methods
koda
parents: 6094
diff changeset
    80
    for (NSString *name in schemeNames)
55ecfbf834e3 convert creation chamber functions to class methods
koda
parents: 6094
diff changeset
    81
        [CreationChamber createSchemeNamed:name ofType:index++];
55ecfbf834e3 convert creation chamber functions to class methods
koda
parents: 6094
diff changeset
    82
    [schemeNames release];
4281
e033cf015b2c redo once again file updating, moving stuff around
koda
parents: 4149
diff changeset
    83
6103
55ecfbf834e3 convert creation chamber functions to class methods
koda
parents: 6094
diff changeset
    84
    // WEAPONS - always overwrite as merge is not needed (missing weaps the set are 0ed automatically)
55ecfbf834e3 convert creation chamber functions to class methods
koda
parents: 6094
diff changeset
    85
    NSArray *weaponNames = [[NSArray alloc] initWithObjects:@"Default",@"Crazy",@"Pro Mode",@"Shoppa",@"Clean Slate",
55ecfbf834e3 convert creation chamber functions to class methods
koda
parents: 6094
diff changeset
    86
                            @"Minefield",@"Thinking with Portals",nil];
55ecfbf834e3 convert creation chamber functions to class methods
koda
parents: 6094
diff changeset
    87
    index = 0;
55ecfbf834e3 convert creation chamber functions to class methods
koda
parents: 6094
diff changeset
    88
    for (NSString *name in weaponNames)
55ecfbf834e3 convert creation chamber functions to class methods
koda
parents: 6094
diff changeset
    89
        [CreationChamber createWeaponNamed:name ofType:index++];
55ecfbf834e3 convert creation chamber functions to class methods
koda
parents: 6094
diff changeset
    90
    [weaponNames release];
3884
d7479079a8a8 various fixes around, plus new 'ask for review' page
koda
parents: 3829
diff changeset
    91
}
d7479079a8a8 various fixes around, plus new 'ask for review' page
koda
parents: 3829
diff changeset
    92
d7479079a8a8 various fixes around, plus new 'ask for review' page
koda
parents: 3829
diff changeset
    93
#pragma mark -
3923
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3903
diff changeset
    94
-(void) viewDidLoad {
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3903
diff changeset
    95
    [super viewDidLoad];
3884
d7479079a8a8 various fixes around, plus new 'ask for review' page
koda
parents: 3829
diff changeset
    96
3923
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3903
diff changeset
    97
    // get the app's version
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3903
diff changeset
    98
    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey];
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3903
diff changeset
    99
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3903
diff changeset
   100
    // get the version number that we've been tracking
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3903
diff changeset
   101
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3903
diff changeset
   102
    NSString *trackingVersion = [userDefaults stringForKey:@"HedgeVersion"];
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3903
diff changeset
   103
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3903
diff changeset
   104
    if (trackingVersion == nil || [trackingVersion isEqualToString:version] == NO) {
5486
e75f7c3c6275 ios: this should prevent at least one of the crashes zorg spotted; also added some callbacks in SDLh; also polished project file (from -O2 to -Os)
koda
parents: 5483
diff changeset
   105
        // remove any reminder of previous games as saves are going to be wiped out
e75f7c3c6275 ios: this should prevent at least one of the crashes zorg spotted; also added some callbacks in SDLh; also polished project file (from -O2 to -Os)
koda
parents: 5483
diff changeset
   106
        [userDefaults setObject:@"" forKey:@"savedGamePath"];
e75f7c3c6275 ios: this should prevent at least one of the crashes zorg spotted; also added some callbacks in SDLh; also polished project file (from -O2 to -Os)
koda
parents: 5483
diff changeset
   107
        // update the tracking version with the new one
3923
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3903
diff changeset
   108
        [userDefaults setObject:version forKey:@"HedgeVersion"];
5486
e75f7c3c6275 ios: this should prevent at least one of the crashes zorg spotted; also added some callbacks in SDLh; also polished project file (from -O2 to -Os)
koda
parents: 5483
diff changeset
   109
3923
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3903
diff changeset
   110
        [userDefaults synchronize];
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3903
diff changeset
   111
        [self createNecessaryFiles];
694e6f6e0e30 various classes updates (new version in mainmenu, opt in mapconfig, clear all in savegames)
koda
parents: 3903
diff changeset
   112
    }
4752
d65705a67c4e ios version bump and code cleanup
koda
parents: 4547
diff changeset
   113
5486
e75f7c3c6275 ios: this should prevent at least one of the crashes zorg spotted; also added some callbacks in SDLh; also polished project file (from -O2 to -Os)
koda
parents: 5483
diff changeset
   114
    // prompt for restoring any previous game
e75f7c3c6275 ios: this should prevent at least one of the crashes zorg spotted; also added some callbacks in SDLh; also polished project file (from -O2 to -Os)
koda
parents: 5483
diff changeset
   115
    NSString *saveString = [userDefaults objectForKey:@"savedGamePath"];
5220
76a2246f18f0 when the match is not completed (eg out of memory or crash) the game asks for restoring it as soon as it is opened again
koda
parents: 5219
diff changeset
   116
    if (saveString != nil && [saveString isEqualToString:@""] == NO) {
6020
c792d4b3e080 this finally fixes the interaction between RestoredGameViewController and StatsPagesViewController
koda
parents: 6008
diff changeset
   117
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(launchRestoredGame) name:@"launchRestoredGame" object:nil];
5220
76a2246f18f0 when the match is not completed (eg out of memory or crash) the game asks for restoring it as soon as it is opened again
koda
parents: 5219
diff changeset
   118
        if (self.restoreViewController == nil) {
5486
e75f7c3c6275 ios: this should prevent at least one of the crashes zorg spotted; also added some callbacks in SDLh; also polished project file (from -O2 to -Os)
koda
parents: 5483
diff changeset
   119
            NSString *xibName = [@"RestoreViewController-" stringByAppendingString:(IS_IPAD() ? @"iPad" : @"iPhone")];
5220
76a2246f18f0 when the match is not completed (eg out of memory or crash) the game asks for restoring it as soon as it is opened again
koda
parents: 5219
diff changeset
   120
            RestoreViewController *restored = [[RestoreViewController alloc] initWithNibName:xibName bundle:nil];
76a2246f18f0 when the match is not completed (eg out of memory or crash) the game asks for restoring it as soon as it is opened again
koda
parents: 5219
diff changeset
   121
            if ([restored respondsToSelector:@selector(setModalPresentationStyle:)])
76a2246f18f0 when the match is not completed (eg out of memory or crash) the game asks for restoring it as soon as it is opened again
koda
parents: 5219
diff changeset
   122
                restored.modalPresentationStyle = UIModalPresentationFormSheet;
76a2246f18f0 when the match is not completed (eg out of memory or crash) the game asks for restoring it as soon as it is opened again
koda
parents: 5219
diff changeset
   123
            self.restoreViewController = restored;
76a2246f18f0 when the match is not completed (eg out of memory or crash) the game asks for restoring it as soon as it is opened again
koda
parents: 5219
diff changeset
   124
            [restored release];
76a2246f18f0 when the match is not completed (eg out of memory or crash) the game asks for restoring it as soon as it is opened again
koda
parents: 5219
diff changeset
   125
        }
6020
c792d4b3e080 this finally fixes the interaction between RestoredGameViewController and StatsPagesViewController
koda
parents: 6008
diff changeset
   126
        [self performSelector:@selector(presentModalViewController:animated:) withObject:self.restoreViewController afterDelay:0.25];
5483
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5224
diff changeset
   127
    } else {
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5224
diff changeset
   128
        // let's not prompt for rating when app crashed >_>
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5224
diff changeset
   129
        [Appirater appLaunched];
5220
76a2246f18f0 when the match is not completed (eg out of memory or crash) the game asks for restoring it as soon as it is opened again
koda
parents: 5219
diff changeset
   130
    }
76a2246f18f0 when the match is not completed (eg out of memory or crash) the game asks for restoring it as soon as it is opened again
koda
parents: 5219
diff changeset
   131
5483
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5224
diff changeset
   132
4752
d65705a67c4e ios version bump and code cleanup
koda
parents: 4547
diff changeset
   133
    /*
4547
b70004a576a3 move server connection stuff in its own class
koda
parents: 4463
diff changeset
   134
    ServerSetup *setup = [[ServerSetup alloc] init];
5483
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 5224
diff changeset
   135
    if (isNetworkReachable()) {
4547
b70004a576a3 move server connection stuff in its own class
koda
parents: 4463
diff changeset
   136
        DLog(@"network is reachable");
b70004a576a3 move server connection stuff in its own class
koda
parents: 4463
diff changeset
   137
        [NSThread detachNewThreadSelector:@selector(serverProtocol)
b70004a576a3 move server connection stuff in its own class
koda
parents: 4463
diff changeset
   138
                                 toTarget:setup
b70004a576a3 move server connection stuff in its own class
koda
parents: 4463
diff changeset
   139
                               withObject:nil];
b70004a576a3 move server connection stuff in its own class
koda
parents: 4463
diff changeset
   140
    }
b70004a576a3 move server connection stuff in its own class
koda
parents: 4463
diff changeset
   141
    [setup release];
4752
d65705a67c4e ios version bump and code cleanup
koda
parents: 4547
diff changeset
   142
    */
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   143
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   144
6008
215f0c9719e3 fix a crash in getting statistics and a glitch that made background music start while in-game
koda
parents: 6000
diff changeset
   145
-(void) viewWillAppear:(BOOL)animated {
215f0c9719e3 fix a crash in getting statistics and a glitch that made background music start while in-game
koda
parents: 6000
diff changeset
   146
    [AudioManagerController playBackgroundMusic];
215f0c9719e3 fix a crash in getting statistics and a glitch that made background music start while in-game
koda
parents: 6000
diff changeset
   147
    [super viewWillAppear:animated];
215f0c9719e3 fix a crash in getting statistics and a glitch that made background music start while in-game
koda
parents: 6000
diff changeset
   148
}
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   149
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   150
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   151
-(IBAction) switchViews:(id) sender {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   152
    UIButton *button = (UIButton *)sender;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   153
    UIAlertView *alert;
3893
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents: 3891
diff changeset
   154
    NSString *xib = nil;
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents: 3891
diff changeset
   155
    NSString *debugStr = nil;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   156
6000
dbcebcd3d79f ios frontend: sounds and music have their own class now (with caching\!) instead of being spread here and there (exploiting class methods like a true oop pro)
koda
parents: 5984
diff changeset
   157
    [AudioManagerController playClickSound];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   158
    switch (button.tag) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   159
        case 0:
3701
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   160
            if (nil == self.gameConfigViewController) {
5486
e75f7c3c6275 ios: this should prevent at least one of the crashes zorg spotted; also added some callbacks in SDLh; also polished project file (from -O2 to -Os)
koda
parents: 5483
diff changeset
   161
                xib = IS_IPAD() ? nil : @"GameConfigViewController";
e75f7c3c6275 ios: this should prevent at least one of the crashes zorg spotted; also added some callbacks in SDLh; also polished project file (from -O2 to -Os)
koda
parents: 5483
diff changeset
   162
3737
2ba6ac8a114b reworked the initialization functions, now it should be safe to update and no more need of spinning wheel at first launch
koda
parents: 3703
diff changeset
   163
                GameConfigViewController *gcvc = [[GameConfigViewController alloc] initWithNibName:xib bundle:nil];
3752
73c2d7d5643b add the new flag, fix a couple of hicups in creating config files, swap animation between settings and lobby
koda
parents: 3738
diff changeset
   164
                gcvc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
3701
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   165
                self.gameConfigViewController = gcvc;
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   166
                [gcvc release];
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   167
            }
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   168
            [self presentModalViewController:self.gameConfigViewController animated:YES];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   169
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   170
        case 2:
3701
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   171
            if (nil == self.settingsViewController) {
6075
0173cd44c3bc rename the ios settings files into something more appropriate
koda
parents: 6020
diff changeset
   172
                SettingsContainerViewController *svrc = [[SettingsContainerViewController alloc] initWithNibName:nil bundle:nil];
3753
bb5a0252fb56 small update for the about page
koda
parents: 3752
diff changeset
   173
                svrc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
3701
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   174
                self.settingsViewController = svrc;
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   175
                [svrc release];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   176
            }
3701
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   177
            [self presentModalViewController:self.settingsViewController animated:YES];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   178
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   179
        case 3:
3893
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents: 3891
diff changeset
   180
#ifdef DEBUG
5486
e75f7c3c6275 ios: this should prevent at least one of the crashes zorg spotted; also added some callbacks in SDLh; also polished project file (from -O2 to -Os)
koda
parents: 5483
diff changeset
   181
            if ([[NSFileManager defaultManager] fileExistsAtPath:DEBUG_FILE()])
e75f7c3c6275 ios: this should prevent at least one of the crashes zorg spotted; also added some callbacks in SDLh; also polished project file (from -O2 to -Os)
koda
parents: 5483
diff changeset
   182
                debugStr = [[NSString alloc] initWithContentsOfFile:DEBUG_FILE()];
e75f7c3c6275 ios: this should prevent at least one of the crashes zorg spotted; also added some callbacks in SDLh; also polished project file (from -O2 to -Os)
koda
parents: 5483
diff changeset
   183
            else
e75f7c3c6275 ios: this should prevent at least one of the crashes zorg spotted; also added some callbacks in SDLh; also polished project file (from -O2 to -Os)
koda
parents: 5483
diff changeset
   184
                debugStr = [[NSString alloc] initWithString:@"Here be log"];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   185
            UITextView *scroll = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   186
            scroll.text = debugStr;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   187
            [debugStr release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   188
            scroll.editable = NO;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3667
diff changeset
   189
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   190
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   191
            [btn addTarget:scroll action:@selector(removeFromSuperview) forControlEvents:UIControlEventTouchUpInside];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   192
            btn.backgroundColor = [UIColor blackColor];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   193
            btn.frame = CGRectMake(self.view.frame.size.height-70, 0, 70, 70);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   194
            [scroll addSubview:btn];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   195
            [self.view addSubview:scroll];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   196
            [scroll release];
3893
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents: 3891
diff changeset
   197
#else
5486
e75f7c3c6275 ios: this should prevent at least one of the crashes zorg spotted; also added some callbacks in SDLh; also polished project file (from -O2 to -Os)
koda
parents: 5483
diff changeset
   198
            debugStr = debugStr; // prevent compiler warning
3893
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents: 3891
diff changeset
   199
            if (nil == self.aboutViewController) {
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents: 3891
diff changeset
   200
                AboutViewController *about = [[AboutViewController alloc] initWithNibName:@"AboutViewController" bundle:nil];
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents: 3891
diff changeset
   201
                about.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
4358
0445d807193e more backward compatibility
koda
parents: 4287
diff changeset
   202
                if ([about respondsToSelector:@selector(setModalPresentationStyle:)])
0445d807193e more backward compatibility
koda
parents: 4287
diff changeset
   203
                     about.modalPresentationStyle = UIModalPresentationFormSheet;
3893
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents: 3891
diff changeset
   204
                self.aboutViewController = about;
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents: 3891
diff changeset
   205
                [about release];
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents: 3891
diff changeset
   206
            }
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents: 3891
diff changeset
   207
            [self presentModalViewController:self.aboutViewController animated:YES];
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents: 3891
diff changeset
   208
#endif
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents: 3891
diff changeset
   209
            break;
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents: 3891
diff changeset
   210
        case 4:
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents: 3891
diff changeset
   211
            if (nil == self.savedGamesViewController) {
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents: 3891
diff changeset
   212
                SavedGamesViewController *savedgames = [[SavedGamesViewController alloc] initWithNibName:@"SavedGamesViewController" bundle:nil];
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents: 3891
diff changeset
   213
                savedgames.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
4358
0445d807193e more backward compatibility
koda
parents: 4287
diff changeset
   214
                if ([savedgames respondsToSelector:@selector(setModalPresentationStyle:)])
0445d807193e more backward compatibility
koda
parents: 4287
diff changeset
   215
                    savedgames.modalPresentationStyle = UIModalPresentationPageSheet;
3893
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents: 3891
diff changeset
   216
                self.savedGamesViewController = savedgames;
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents: 3891
diff changeset
   217
                [savedgames release];
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents: 3891
diff changeset
   218
            }
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents: 3891
diff changeset
   219
            [self presentModalViewController:self.savedGamesViewController animated:YES];
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   220
            break;
6083
72c882c0fd0f first pass for implementing missions/training selection on ipad (not yet running)
koda
parents: 6075
diff changeset
   221
        case 5:
72c882c0fd0f first pass for implementing missions/training selection on ipad (not yet running)
koda
parents: 6075
diff changeset
   222
            if (nil == self.missionsViewController) {
6094
16b12a6417d1 added iphone interface for missions/trainings page, setters/getters unlocked
koda
parents: 6093
diff changeset
   223
                xib = IS_IPAD() ? @"MissionTrainingViewController-iPad" : @"MissionTrainingViewController-iPhone";
16b12a6417d1 added iphone interface for missions/trainings page, setters/getters unlocked
koda
parents: 6093
diff changeset
   224
                MissionTrainingViewController *missions = [[MissionTrainingViewController alloc] initWithNibName:xib bundle:nil];
16b12a6417d1 added iphone interface for missions/trainings page, setters/getters unlocked
koda
parents: 6093
diff changeset
   225
                missions.modalTransitionStyle = IS_IPAD() ? UIModalTransitionStyleCoverVertical : UIModalTransitionStyleCrossDissolve;
6083
72c882c0fd0f first pass for implementing missions/training selection on ipad (not yet running)
koda
parents: 6075
diff changeset
   226
                if ([missions respondsToSelector:@selector(setModalPresentationStyle:)])
72c882c0fd0f first pass for implementing missions/training selection on ipad (not yet running)
koda
parents: 6075
diff changeset
   227
                    missions.modalPresentationStyle = UIModalPresentationPageSheet;
72c882c0fd0f first pass for implementing missions/training selection on ipad (not yet running)
koda
parents: 6075
diff changeset
   228
                self.missionsViewController = missions;
72c882c0fd0f first pass for implementing missions/training selection on ipad (not yet running)
koda
parents: 6075
diff changeset
   229
                [missions release];
72c882c0fd0f first pass for implementing missions/training selection on ipad (not yet running)
koda
parents: 6075
diff changeset
   230
            }
72c882c0fd0f first pass for implementing missions/training selection on ipad (not yet running)
koda
parents: 6075
diff changeset
   231
            [self presentModalViewController:self.missionsViewController animated:YES];
72c882c0fd0f first pass for implementing missions/training selection on ipad (not yet running)
koda
parents: 6075
diff changeset
   232
            break;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   233
        default:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   234
            alert = [[UIAlertView alloc] initWithTitle:@"Not Yet Implemented"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   235
                                               message:@"Sorry, this feature is not yet implemented"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   236
                                              delegate:nil
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   237
                                     cancelButtonTitle:@"Well, don't worry"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   238
                                     otherButtonTitles:nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   239
            [alert show];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   240
            [alert release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   241
            break;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   242
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   243
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   244
6020
c792d4b3e080 this finally fixes the interaction between RestoredGameViewController and StatsPagesViewController
koda
parents: 6008
diff changeset
   245
#pragma mark -
c792d4b3e080 this finally fixes the interaction between RestoredGameViewController and StatsPagesViewController
koda
parents: 6008
diff changeset
   246
-(void) launchRestoredGame {
c792d4b3e080 this finally fixes the interaction between RestoredGameViewController and StatsPagesViewController
koda
parents: 6008
diff changeset
   247
    [[NSNotificationCenter defaultCenter] removeObserver:self];
c792d4b3e080 this finally fixes the interaction between RestoredGameViewController and StatsPagesViewController
koda
parents: 6008
diff changeset
   248
    GameInterfaceBridge *bridge = [[GameInterfaceBridge alloc] initWithController:self];
c792d4b3e080 this finally fixes the interaction between RestoredGameViewController and StatsPagesViewController
koda
parents: 6008
diff changeset
   249
    [bridge startSaveGame:[[NSUserDefaults standardUserDefaults] objectForKey:@"savedGamePath"]];
c792d4b3e080 this finally fixes the interaction between RestoredGameViewController and StatsPagesViewController
koda
parents: 6008
diff changeset
   250
    [bridge release];
c792d4b3e080 this finally fixes the interaction between RestoredGameViewController and StatsPagesViewController
koda
parents: 6008
diff changeset
   251
}
c792d4b3e080 this finally fixes the interaction between RestoredGameViewController and StatsPagesViewController
koda
parents: 6008
diff changeset
   252
c792d4b3e080 this finally fixes the interaction between RestoredGameViewController and StatsPagesViewController
koda
parents: 6008
diff changeset
   253
#pragma mark -
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   254
-(void) viewDidUnload {
3701
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   255
    self.gameConfigViewController = nil;
8c449776ebe6 rewrite portions of some uiviewcontrollers
koda
parents: 3697
diff changeset
   256
    self.settingsViewController = nil;
3703
12d17c6e8855 halfway-through finishing the frontend with new graphics by Tiy + initial 'About' page set up
koda
parents: 3701
diff changeset
   257
    self.aboutViewController = nil;
3893
568bfd083465 allow more flexibility between viewcontrollers, also added stub pages for saved games
koda
parents: 3891
diff changeset
   258
    self.savedGamesViewController = nil;
5220
76a2246f18f0 when the match is not completed (eg out of memory or crash) the game asks for restoring it as soon as it is opened again
koda
parents: 5219
diff changeset
   259
    self.restoreViewController = nil;
6085
252e9a7d260f be a good memory aware citizen
koda
parents: 6083
diff changeset
   260
    self.missionsViewController = nil;
3662
a44406f4369b polish polish polish polish (also: panning horizontal fix, panning momentum, settings page reworked yet again, memory leaks, crashes, segfaults)
koda
parents: 3660
diff changeset
   261
    MSG_DIDUNLOAD();
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   262
    [super viewDidUnload];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   263
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   264
3971
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   265
-(void) didReceiveMemoryWarning {
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   266
    if (self.settingsViewController.view.superview == nil)
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   267
        self.settingsViewController = nil;
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   268
    if (self.gameConfigViewController.view.superview == nil)
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   269
        self.gameConfigViewController = nil;
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   270
    if (self.aboutViewController.view.superview == nil)
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   271
        self.aboutViewController = nil;
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   272
    if (self.savedGamesViewController.view.superview == nil)
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   273
        self.savedGamesViewController = nil;
5220
76a2246f18f0 when the match is not completed (eg out of memory or crash) the game asks for restoring it as soon as it is opened again
koda
parents: 5219
diff changeset
   274
    if (self.restoreViewController.view.superview == nil)
76a2246f18f0 when the match is not completed (eg out of memory or crash) the game asks for restoring it as soon as it is opened again
koda
parents: 5219
diff changeset
   275
        self.restoreViewController = nil;
6085
252e9a7d260f be a good memory aware citizen
koda
parents: 6083
diff changeset
   276
    if (self.missionsViewController.view.superview == nil)
252e9a7d260f be a good memory aware citizen
koda
parents: 6083
diff changeset
   277
        self.missionsViewController = nil;
3971
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   278
    MSG_MEMCLEAN();
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   279
    [super didReceiveMemoryWarning];
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   280
}
5c82ee165ed5 minor stuff
koda
parents: 3923
diff changeset
   281
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   282
-(void) dealloc {
5208
878e551f0b4a all together now! releaseAndNil ftw
koda
parents: 5206
diff changeset
   283
    releaseAndNil(settingsViewController);
878e551f0b4a all together now! releaseAndNil ftw
koda
parents: 5206
diff changeset
   284
    releaseAndNil(gameConfigViewController);
878e551f0b4a all together now! releaseAndNil ftw
koda
parents: 5206
diff changeset
   285
    releaseAndNil(aboutViewController);
878e551f0b4a all together now! releaseAndNil ftw
koda
parents: 5206
diff changeset
   286
    releaseAndNil(savedGamesViewController);
5220
76a2246f18f0 when the match is not completed (eg out of memory or crash) the game asks for restoring it as soon as it is opened again
koda
parents: 5219
diff changeset
   287
    releaseAndNil(restoreViewController);
6085
252e9a7d260f be a good memory aware citizen
koda
parents: 6083
diff changeset
   288
    releaseAndNil(missionsViewController);
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   289
    [super dealloc];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   290
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   291
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   292
@end