project_files/HedgewarsMobile/Classes/MissionTrainingViewController.m
branchios-develop
changeset 13191 0c1f3a8ba0bb
parent 13166 ba5c794adae3
child 13192 d6aea85111c7
equal deleted inserted replaced
13167:c129707eb1e6 13191:0c1f3a8ba0bb
    19 
    19 
    20 #import "MissionTrainingViewController.h"
    20 #import "MissionTrainingViewController.h"
    21 #import <QuartzCore/QuartzCore.h>
    21 #import <QuartzCore/QuartzCore.h>
    22 #import "GameInterfaceBridge.h"
    22 #import "GameInterfaceBridge.h"
    23 
    23 
       
    24 #define TRAINING_MISSION_TYPE @"Training"
    24 
    25 
    25 @implementation MissionTrainingViewController
    26 @implementation MissionTrainingViewController
    26 
    27 
    27 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    28 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    28     return rotationManager(interfaceOrientation);
    29     return rotationManager(interfaceOrientation);
    53 
    54 
    54     self.descriptionLabel.textColor = [UIColor lightYellowColor];
    55     self.descriptionLabel.textColor = [UIColor lightYellowColor];
    55 }
    56 }
    56 
    57 
    57 - (void)viewWillAppear:(BOOL)animated {
    58 - (void)viewWillAppear:(BOOL)animated {
    58     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:arc4random_uniform((int)[self.listOfMissionIDs count]) inSection:0];
    59     NSInteger randomSection = arc4random_uniform((int)[self.missionsTypes count]);
       
    60     NSString *type = self.missionsTypes[randomSection];
       
    61     NSArray *listOfIDs = [self listOfMissionsIDsForType:type];
       
    62     NSInteger randomRow = arc4random_uniform((int)[listOfIDs count]);
       
    63     
       
    64     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:randomRow inSection:randomSection];
    59     [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
    65     [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
    60     [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
    66     [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
    61     [super viewWillAppear:animated];
    67     [super viewWillAppear:animated];
    62 }
    68 }
    63 
    69 
    73     }
    79     }
    74 }
    80 }
    75 
    81 
    76 #pragma mark - Missions dictionaries methods
    82 #pragma mark - Missions dictionaries methods
    77 
    83 
    78 - (NSDictionary *)newLocalizedMissionsDictionary
    84 - (NSDictionary *)newLocalizedMissionsDictionaryForType: (NSString *)type
    79 {
    85 {
    80     NSString *languageID = [HWUtils languageID];
    86     NSString *languageID = [HWUtils languageID];
    81     
    87     
    82     NSString *missionsDescLocation = [[NSString alloc] initWithFormat:@"%@/missions_en.txt",LOCALE_DIRECTORY()];
    88     NSString *missionsDescLocation = [[NSString alloc] initWithFormat:@"%@/missions_en.txt",LOCALE_DIRECTORY()];
    83     NSString *localizedMissionsDescLocation = [[NSString alloc] initWithFormat:@"%@/missions_%@.txt", LOCALE_DIRECTORY(), languageID];
    89     NSString *localizedMissionsDescLocation = [[NSString alloc] initWithFormat:@"%@/missions_%@.txt", LOCALE_DIRECTORY(), languageID];
    84     
    90     
    85     if (![languageID isEqualToString:@"en"] && [[NSFileManager defaultManager] fileExistsAtPath:localizedMissionsDescLocation])
    91     if (![languageID isEqualToString:@"en"] && [[NSFileManager defaultManager] fileExistsAtPath:localizedMissionsDescLocation])
    86     {
    92     {
    87         NSDictionary *missionsDict = [self newMissionsDictionaryFromMissionsFile:missionsDescLocation];
    93         NSDictionary *missionsDict = [self newMissionsDictionaryForType:type fromMissionsFile:missionsDescLocation];
    88         NSDictionary *localizedMissionsDict = [self newMissionsDictionaryFromMissionsFile:localizedMissionsDescLocation];
    94         NSDictionary *localizedMissionsDict = [self newMissionsDictionaryForType:type fromMissionsFile:localizedMissionsDescLocation];
    89         
    95         
    90         
    96         
    91         NSMutableDictionary *tempMissionsDict = [[NSMutableDictionary alloc] init];
    97         NSMutableDictionary *tempMissionsDict = [[NSMutableDictionary alloc] init];
    92         
    98         
    93         for (NSString *key in [missionsDict allKeys])
    99         for (NSString *key in [missionsDict allKeys])
   105         
   111         
   106         return tempMissionsDict;
   112         return tempMissionsDict;
   107     }
   113     }
   108     else
   114     else
   109     {
   115     {
   110         NSDictionary *missionsDict = [self newMissionsDictionaryFromMissionsFile:missionsDescLocation];
   116         NSDictionary *missionsDict = [self newMissionsDictionaryForType:type fromMissionsFile:missionsDescLocation];
   111         
   117         
   112         
   118         
   113         return missionsDict;
   119         return missionsDict;
   114     }
   120     }
   115 }
   121 }
   116 
   122 
   117 - (NSDictionary *)newMissionsDictionaryFromMissionsFile:(NSString *)filePath
   123 - (NSDictionary *)newMissionsDictionaryForType:(NSString *)type fromMissionsFile:(NSString *)filePath
   118 {
   124 {
   119     NSMutableDictionary *missionsDict = [[NSMutableDictionary alloc] init];
   125     NSMutableDictionary *missionsDict = [[NSMutableDictionary alloc] init];
   120     
   126     
   121     NSString *missionsFileContents = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
   127     NSString *missionsFileContents = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
   122     NSArray *missionsLines = [missionsFileContents componentsSeparatedByString:@"\n"];
   128     NSArray *missionsLines = [missionsFileContents componentsSeparatedByString:@"\n"];
   123     
   129     
       
   130     NSString *directory = [self missionsDirectoryForType:type];
   124     for (NSString *line in missionsLines)
   131     for (NSString *line in missionsLines)
   125     {
   132     {
   126         if ([line length] > 0)
   133         if ([line length] > 0)
   127         {
   134         {
   128             NSUInteger firstDotLocation = [line rangeOfString:@"."].location;
   135             NSUInteger firstDotLocation = [line rangeOfString:@"."].location;
   129             
   136             
   130             NSString *missionID = [line substringToIndex:firstDotLocation];
   137             NSString *missionID = [line substringToIndex:firstDotLocation];
   131             
   138             
   132             NSString *missionFullPath = [NSString stringWithFormat:@"%@%@.lua", TRAININGS_DIRECTORY(), missionID];
   139             NSString *missionFullPath = [NSString stringWithFormat:@"%@%@.lua", directory, missionID];
   133             if (![[NSFileManager defaultManager] fileExistsAtPath:missionFullPath])
   140             if (![[NSFileManager defaultManager] fileExistsAtPath:missionFullPath])
   134             {
   141             {
   135                 continue;
   142                 continue;
   136             }
   143             }
   137             
   144             
   167 }
   174 }
   168 
   175 
   169 #pragma mark -
   176 #pragma mark -
   170 #pragma mark override setters/getters for better memory handling
   177 #pragma mark override setters/getters for better memory handling
   171 
   178 
   172 -(NSArray *)listOfMissionIDs
   179 - (NSArray *)missionsTypes
   173 {
   180 {
   174     if (!_listOfMissionIDs)
   181     if (!_missionsTypes)
   175     {
   182     {
   176         NSArray *sortedKeys = [[self.dictOfMissions allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
   183         _missionsTypes = @[ TRAINING_MISSION_TYPE ];
   177         _listOfMissionIDs = [[NSArray alloc] initWithArray:sortedKeys];
   184     }
   178     }
   185     
   179     
   186     return _missionsTypes;
   180     return _listOfMissionIDs;
   187 }
   181 }
   188 
   182 
   189 - (NSDictionary *)dictOfAllMissions
   183 - (NSDictionary *)dictOfMissions
   190 {
   184 {
   191     if (!_dictOfAllMissions)
   185     if (!_dictOfMissions)
   192     {
   186     {
   193         NSArray *types = [self missionsTypes];
   187         _dictOfMissions = [self newLocalizedMissionsDictionary];
   194         _dictOfAllMissions = @{ types[0] : [self dictOfTraining] };
   188     }
   195     }
   189     
   196     
   190     return _dictOfMissions;
   197     return _dictOfAllMissions;
       
   198 }
       
   199 
       
   200 - (NSArray *)listOfTrainingIDs
       
   201 {
       
   202     if (!_listOfTrainingIDs)
       
   203     {
       
   204         NSArray *sortedKeys = [[self.dictOfTraining allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
       
   205         _listOfTrainingIDs = [[NSArray alloc] initWithArray:sortedKeys];
       
   206     }
       
   207     
       
   208     return _listOfTrainingIDs;
       
   209 }
       
   210 
       
   211 - (NSDictionary *)dictOfTraining
       
   212 {
       
   213     if (!_dictOfTraining)
       
   214     {
       
   215         _dictOfTraining = [self newLocalizedMissionsDictionaryForType:TRAINING_MISSION_TYPE];
       
   216     }
       
   217     
       
   218     return _dictOfTraining;
       
   219 }
       
   220 
       
   221 #pragma mark -
       
   222 #pragma mark Missions types
       
   223 
       
   224 - (NSString *)missionsDirectoryForType:(NSString *)type
       
   225 {
       
   226     if ([type isEqualToString:TRAINING_MISSION_TYPE]) {
       
   227         return TRAININGS_DIRECTORY();
       
   228     }
       
   229     return nil;
       
   230 }
       
   231 
       
   232 - (NSArray *)listOfMissionsIDsForType:(NSString *)type
       
   233 {
       
   234     if ([type isEqualToString:TRAINING_MISSION_TYPE]) {
       
   235         return self.listOfTrainingIDs;
       
   236     }
       
   237     return nil;
       
   238 }
       
   239 
       
   240 - (NSDictionary *)dictOfMissionsForType:(NSString *)type
       
   241 {
       
   242     if ([type isEqualToString:TRAINING_MISSION_TYPE]) {
       
   243         return self.dictOfTraining;
       
   244     }
       
   245     return nil;
   191 }
   246 }
   192 
   247 
   193 #pragma mark -
   248 #pragma mark -
   194 #pragma mark Table view data source
   249 #pragma mark Table view data source
   195 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   250 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   196     return 1;
   251     return [self.missionsTypes count];
   197 }
   252 }
   198 
   253 
   199 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   254 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   200     return [self.listOfMissionIDs count];
   255     NSString *type = self.missionsTypes[section];
       
   256     NSArray *listOfIDs = [self listOfMissionsIDsForType:type];
       
   257     return [listOfIDs count];
   201 }
   258 }
   202 
   259 
   203 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   260 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   204     return (IS_IPAD()) ? self.tableView.rowHeight : 80;
   261     return (IS_IPAD()) ? self.tableView.rowHeight : 80;
   205 }
   262 }
   211     UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
   268     UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
   212     if (cell == nil)
   269     if (cell == nil)
   213         cell = [[UITableViewCell alloc] initWithStyle:(IS_IPAD()) ? UITableViewCellStyleDefault : UITableViewCellStyleSubtitle
   270         cell = [[UITableViewCell alloc] initWithStyle:(IS_IPAD()) ? UITableViewCellStyleDefault : UITableViewCellStyleSubtitle
   214                                        reuseIdentifier:CellIdentifier];
   271                                        reuseIdentifier:CellIdentifier];
   215     
   272     
   216     NSString *missionID = [self.listOfMissionIDs objectAtIndex:row];
   273     NSInteger section = [indexPath section];
   217     cell.textLabel.text = self.dictOfMissions[missionID][@"name"];
   274     NSString *type = self.missionsTypes[section];
       
   275     NSArray *listOfIDs = [self listOfMissionsIDsForType:type];
       
   276     NSDictionary *dict = [self dictOfMissionsForType:type];
       
   277     
       
   278     NSString *missionID = [listOfIDs objectAtIndex:row];
       
   279     cell.textLabel.text = dict[missionID][@"name"];
   218     
   280     
   219     cell.textLabel.textColor = [UIColor lightYellowColor];
   281     cell.textLabel.textColor = [UIColor lightYellowColor];
   220     //cell.textLabel.font = [UIFont fontWithName:@"Bradley Hand Bold" size:[UIFont labelFontSize]];
   282     //cell.textLabel.font = [UIFont fontWithName:@"Bradley Hand Bold" size:[UIFont labelFontSize]];
   221     cell.textLabel.textAlignment = (IS_IPAD()) ? NSTextAlignmentCenter : NSTextAlignmentLeft;
   283     cell.textLabel.textAlignment = (IS_IPAD()) ? NSTextAlignmentCenter : NSTextAlignmentLeft;
   222     cell.textLabel.backgroundColor = [UIColor clearColor];
   284     cell.textLabel.backgroundColor = [UIColor clearColor];
   223     cell.textLabel.adjustsFontSizeToFitWidth = YES;
   285     cell.textLabel.adjustsFontSizeToFitWidth = YES;
   224     cell.detailTextLabel.text = (IS_IPAD()) ? nil : self.dictOfMissions[missionID][@"desc"];
   286     cell.detailTextLabel.text = (IS_IPAD()) ? nil : dict[missionID][@"desc"];
   225     cell.detailTextLabel.textColor = [UIColor whiteColor];
   287     cell.detailTextLabel.textColor = [UIColor whiteColor];
   226     cell.detailTextLabel.backgroundColor = [UIColor clearColor];
   288     cell.detailTextLabel.backgroundColor = [UIColor clearColor];
   227     cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
   289     cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
   228     cell.detailTextLabel.numberOfLines = ([cell.detailTextLabel.text length] % 40);
   290     cell.detailTextLabel.numberOfLines = ([cell.detailTextLabel.text length] % 40);
   229     cell.detailTextLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
   291     cell.detailTextLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
   238 }
   300 }
   239 
   301 
   240 #pragma mark -
   302 #pragma mark -
   241 #pragma mark Table view delegate
   303 #pragma mark Table view delegate
   242 - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   304 - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       
   305     NSInteger section = [indexPath section];
       
   306     NSString *type = self.missionsTypes[section];
       
   307     NSArray *listOfIDs = [self listOfMissionsIDsForType:type];
       
   308     
   243     NSInteger row = [indexPath row];
   309     NSInteger row = [indexPath row];
   244 
   310     self.missionName = [listOfIDs objectAtIndex:row];
   245     self.missionName = [self.listOfMissionIDs objectAtIndex:row];
       
   246     NSString *size = IS_IPAD() ? @"@2x" : @"";
   311     NSString *size = IS_IPAD() ? @"@2x" : @"";
   247     NSString *filePath = [[NSString alloc] initWithFormat:@"%@/Missions/Training/%@%@.png",GRAPHICS_DIRECTORY(),self.missionName,size];
   312     NSString *filePath = [[NSString alloc] initWithFormat:@"%@/Missions/%@/%@%@.png",GRAPHICS_DIRECTORY(), type, self.missionName, size];
   248     UIImage *img = [[UIImage alloc] initWithContentsOfFile:filePath];
   313     UIImage *img = [[UIImage alloc] initWithContentsOfFile:filePath];
   249     [self.previewImage setImage:img];
   314     [self.previewImage setImage:img];
   250 
   315 
   251     self.descriptionLabel.text = self.dictOfMissions[self.missionName][@"desc"];
   316     NSDictionary *dict = [self dictOfMissionsForType:type];
       
   317     self.descriptionLabel.text = dict[self.missionName][@"desc"];
   252 }
   318 }
   253 
   319 
   254 #pragma mark -
   320 #pragma mark -
   255 #pragma mark Memory management
   321 #pragma mark Memory management
   256 
   322 
   257 - (void)didReceiveMemoryWarning
   323 - (void)didReceiveMemoryWarning
   258 {
   324 {
   259     self.missionName = nil;
   325     self.listOfTrainingIDs = nil;
   260     self.listOfMissionIDs = nil;
   326     self.dictOfTraining = nil;
   261     self.dictOfMissions = nil;
   327     self.dictOfAllMissions = nil;
       
   328     self.missionsTypes = nil;
   262     // if you nil this one it won't get updated anymore
   329     // if you nil this one it won't get updated anymore
   263     //self.previewImage = nil;
   330     //self.previewImage = nil;
   264     [super didReceiveMemoryWarning];
   331     [super didReceiveMemoryWarning];
   265 }
   332 }
   266 
   333