project_files/HedgewarsMobile/Classes/MissionTrainingViewController.m
branchqmlfrontend
changeset 11408 b894922d58cc
parent 11243 a954853e9c00
child 11350 1f517a461c33
equal deleted inserted replaced
11076:fcbdee9cdd74 11408:b894922d58cc
    21 #import <QuartzCore/QuartzCore.h>
    21 #import <QuartzCore/QuartzCore.h>
    22 #import "GameInterfaceBridge.h"
    22 #import "GameInterfaceBridge.h"
    23 
    23 
    24 
    24 
    25 @implementation MissionTrainingViewController
    25 @implementation MissionTrainingViewController
    26 @synthesize listOfMissions, listOfDescriptions, previewImage, tableView, descriptionLabel, missionName;
       
    27 
    26 
    28 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    27 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    29     return rotationManager(interfaceOrientation);
    28     return rotationManager(interfaceOrientation);
    30 }
    29 }
    31 
    30 
    32 #pragma mark -
    31 #pragma mark -
    33 #pragma mark View management
    32 #pragma mark View management
    34 -(void) viewDidLoad {
    33 -(void) viewDidLoad
       
    34 {
       
    35     [super viewDidLoad];
       
    36     
    35     self.previewImage.layer.borderColor = [[UIColor darkYellowColor] CGColor];
    37     self.previewImage.layer.borderColor = [[UIColor darkYellowColor] CGColor];
    36     self.previewImage.layer.borderWidth = 3.8f;
    38     self.previewImage.layer.borderWidth = 3.8f;
    37     self.previewImage.layer.cornerRadius = 14;
    39     self.previewImage.layer.cornerRadius = 14;
    38 
    40 
    39     if (IS_IPAD()) {
    41     if (IS_IPAD()) {
    48     self.tableView.layer.borderWidth = 2.4f;
    50     self.tableView.layer.borderWidth = 2.4f;
    49     self.tableView.layer.cornerRadius = 8;
    51     self.tableView.layer.cornerRadius = 8;
    50     self.tableView.separatorColor = [UIColor whiteColor];
    52     self.tableView.separatorColor = [UIColor whiteColor];
    51 
    53 
    52     self.descriptionLabel.textColor = [UIColor lightYellowColor];
    54     self.descriptionLabel.textColor = [UIColor lightYellowColor];
    53     [super viewDidLoad];
       
    54 }
    55 }
    55 
    56 
    56 -(void) viewWillAppear:(BOOL)animated {
    57 -(void) viewWillAppear:(BOOL)animated {
    57     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:random()%[self.listOfMissions count] inSection:0];
    58     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:arc4random_uniform((int)[self.listOfMissionIDs count]) inSection:0];
    58     [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
    59     [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
    59     [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
    60     [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
    60     [super viewWillAppear:animated];
    61     [super viewWillAppear:animated];
    61 }
    62 }
    62 
    63 
    63 -(IBAction) buttonPressed:(id) sender {
    64 -(IBAction) buttonPressed:(id) sender {
    64     UIButton *button = (UIButton *)sender;
    65     UIButton *button = (UIButton *)sender;
    65 
    66 
    66     if (button.tag == 0) {
    67     if (button.tag == 0) {
    67         [[AudioManagerController mainManager] playBackSound];
    68         [[AudioManagerController mainManager] playBackSound];
    68         [[self parentViewController] dismissModalViewControllerAnimated:YES];
    69         [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
    69     } else {
    70     } else {
    70         [GameInterfaceBridge registerCallingController:self];
    71         [GameInterfaceBridge registerCallingController:self];
    71         [GameInterfaceBridge startMissionGame:self.missionName];
    72         [GameInterfaceBridge startMissionGame:self.missionName];
    72     }
    73     }
    73 }
    74 }
    74 
    75 
       
    76 #pragma mark - Missions dictionaries methods
       
    77 
       
    78 - (NSDictionary *)newLocalizedMissionsDictionary
       
    79 {
       
    80     NSString *languageID = [HWUtils languageID];
       
    81     
       
    82     NSString *missionsDescLocation = [[NSString alloc] initWithFormat:@"%@/missions_en.txt",LOCALE_DIRECTORY()];
       
    83     NSString *localizedMissionsDescLocation = [[NSString alloc] initWithFormat:@"%@/missions_%@.txt", LOCALE_DIRECTORY(), languageID];
       
    84     
       
    85     if (![languageID isEqualToString:@"en"] && [[NSFileManager defaultManager] fileExistsAtPath:localizedMissionsDescLocation])
       
    86     {
       
    87         NSDictionary *missionsDict = [self newMissionsDictionaryFromMissionsFile:missionsDescLocation];
       
    88         NSDictionary *localizedMissionsDict = [self newMissionsDictionaryFromMissionsFile:localizedMissionsDescLocation];
       
    89         
       
    90         [missionsDescLocation release];
       
    91         [localizedMissionsDescLocation release];
       
    92         
       
    93         NSMutableDictionary *tempMissionsDict = [[NSMutableDictionary alloc] init];
       
    94         
       
    95         for (NSString *key in [missionsDict allKeys])
       
    96         {
       
    97             if ([localizedMissionsDict objectForKey:key])
       
    98             {
       
    99                 [tempMissionsDict setObject:[localizedMissionsDict objectForKey:key] forKey:key];
       
   100             }
       
   101             else
       
   102             {
       
   103                 [tempMissionsDict setObject:[missionsDict objectForKey:key] forKey:key];
       
   104             }
       
   105         }
       
   106         
       
   107         [missionsDict release];
       
   108         [localizedMissionsDict release];
       
   109         
       
   110         return tempMissionsDict;
       
   111     }
       
   112     else
       
   113     {
       
   114         NSDictionary *missionsDict = [self newMissionsDictionaryFromMissionsFile:missionsDescLocation];
       
   115         
       
   116         [missionsDescLocation release];
       
   117         [localizedMissionsDescLocation release];
       
   118         
       
   119         return missionsDict;
       
   120     }
       
   121 }
       
   122 
       
   123 - (NSDictionary *)newMissionsDictionaryFromMissionsFile:(NSString *)filePath
       
   124 {
       
   125     NSMutableDictionary *missionsDict = [[NSMutableDictionary alloc] init];
       
   126     
       
   127     NSString *missionsFileContents = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
       
   128     NSArray *missionsLines = [missionsFileContents componentsSeparatedByString:@"\n"];
       
   129     [missionsFileContents release];
       
   130     
       
   131     for (NSString *line in missionsLines)
       
   132     {
       
   133         if ([line length] > 0)
       
   134         {
       
   135             NSUInteger firstDotLocation = [line rangeOfString:@"."].location;
       
   136             
       
   137             NSString *missionID = [line substringToIndex:firstDotLocation];
       
   138             
       
   139             NSString *missionFullPath = [NSString stringWithFormat:@"%@%@.lua", TRAININGS_DIRECTORY(), missionID];
       
   140             if (![[NSFileManager defaultManager] fileExistsAtPath:missionFullPath])
       
   141             {
       
   142                 continue;
       
   143             }
       
   144             
       
   145             NSString *nameOrDesc = [line substringFromIndex:firstDotLocation+1];
       
   146             
       
   147             NSString *missionParsedName = ([nameOrDesc hasPrefix:@"name="]) ? [nameOrDesc stringByReplacingOccurrencesOfString:@"name=" withString:@""] : nil;
       
   148             NSString *missionParsedDesc = ([nameOrDesc hasPrefix:@"desc="]) ? [nameOrDesc stringByReplacingOccurrencesOfString:@"desc=" withString:@""] : nil;
       
   149             
       
   150             if (![missionsDict objectForKey:missionID])
       
   151             {
       
   152                 NSMutableDictionary *missionDict = [[NSMutableDictionary alloc] init];
       
   153                 [missionsDict setObject:missionDict forKey:missionID];
       
   154                 [missionDict release];
       
   155             }
       
   156             
       
   157             NSMutableDictionary *missionDict = [missionsDict objectForKey:missionID];
       
   158             
       
   159             if (missionParsedName)
       
   160             {
       
   161                 [missionDict setObject:missionParsedName forKey:@"name"];
       
   162             }
       
   163             
       
   164             if (missionParsedDesc)
       
   165             {
       
   166                 missionParsedDesc = [missionParsedDesc stringByReplacingOccurrencesOfString:@"\"" withString:@""];
       
   167                 [missionDict setObject:missionParsedDesc forKey:@"desc"];
       
   168             }
       
   169             
       
   170             [missionsDict setObject:missionDict forKey:missionID];
       
   171         }
       
   172     }
       
   173     
       
   174     return missionsDict;
       
   175 }
       
   176 
    75 #pragma mark -
   177 #pragma mark -
    76 #pragma mark override setters/getters for better memory handling
   178 #pragma mark override setters/getters for better memory handling
    77 -(NSArray *)listOfMissions {
   179 
    78     if (listOfMissions == nil)
   180 -(NSArray *)listOfMissionIDs
    79         self.listOfMissions = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:TRAININGS_DIRECTORY() error:NULL];
   181 {
    80     return listOfMissions;
   182     if (!_listOfMissionIDs)
    81 }
   183     {
    82 
   184         NSArray *sortedKeys = [[self.dictOfMissions allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
    83 -(NSArray *)listOfDescriptions {
   185         _listOfMissionIDs = [[NSArray alloc] initWithArray:sortedKeys];
    84     if (listOfDescriptions == nil) {
   186     }
    85         NSString *descLocation = [[NSString alloc] initWithFormat:@"%@/missions_en.txt",LOCALE_DIRECTORY()];
   187     
    86         NSString *descComplete = [[NSString alloc] initWithContentsOfFile:descLocation encoding:NSUTF8StringEncoding error:NULL];
   188     return _listOfMissionIDs;
    87         [descLocation release];
   189 }
    88         NSArray *descArray = [descComplete componentsSeparatedByString:@"\n"];
   190 
    89         NSMutableArray *filteredArray = [[NSMutableArray alloc] initWithCapacity:[descArray count]/3];
   191 - (NSDictionary *)dictOfMissions
    90         [descComplete release];
   192 {
    91         // sanity check to avoid having missions and descriptions conflicts
   193     if (!_dictOfMissions)
    92         for (NSUInteger i = 0; i < [self.listOfMissions count]; i++) {
   194     {
    93             NSString *desc = [[self.listOfMissions objectAtIndex:i] stringByDeletingPathExtension];
   195         _dictOfMissions = [self newLocalizedMissionsDictionary];
    94             for (NSString *str in descArray)
   196     }
    95                 if ([str hasPrefix:desc] && [str hasSuffix:@"\""]) {
   197     
    96                     NSArray *descriptionText = [str componentsSeparatedByString:@"\""];
   198     return _dictOfMissions;
    97                     [filteredArray insertObject:[descriptionText objectAtIndex:1] atIndex:i];
       
    98                     break;
       
    99                 }
       
   100         }
       
   101         self.listOfDescriptions = filteredArray;
       
   102         [filteredArray release];
       
   103     }
       
   104     return listOfDescriptions;
       
   105 }
   199 }
   106 
   200 
   107 #pragma mark -
   201 #pragma mark -
   108 #pragma mark Table view data source
   202 #pragma mark Table view data source
   109 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   203 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   110     return 1;
   204     return 1;
   111 }
   205 }
   112 
   206 
   113 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   207 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   114     return [self.listOfMissions count];
   208     return [self.listOfMissionIDs count];
   115 }
   209 }
   116 
   210 
   117 -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   211 -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   118     return (IS_IPAD()) ? self.tableView.rowHeight : 80;
   212     return (IS_IPAD()) ? self.tableView.rowHeight : 80;
   119 }
   213 }
   124 
   218 
   125     UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
   219     UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
   126     if (cell == nil)
   220     if (cell == nil)
   127         cell = [[[UITableViewCell alloc] initWithStyle:(IS_IPAD()) ? UITableViewCellStyleDefault : UITableViewCellStyleSubtitle
   221         cell = [[[UITableViewCell alloc] initWithStyle:(IS_IPAD()) ? UITableViewCellStyleDefault : UITableViewCellStyleSubtitle
   128                                        reuseIdentifier:CellIdentifier] autorelease];
   222                                        reuseIdentifier:CellIdentifier] autorelease];
   129 
   223     
   130     cell.textLabel.text = [[[self.listOfMissions objectAtIndex:row] stringByDeletingPathExtension]
   224     NSString *missionID = [self.listOfMissionIDs objectAtIndex:row];
   131                            stringByReplacingOccurrencesOfString:@"_" withString:@" "];
   225     cell.textLabel.text = self.dictOfMissions[missionID][@"name"];
       
   226     
   132     cell.textLabel.textColor = [UIColor lightYellowColor];
   227     cell.textLabel.textColor = [UIColor lightYellowColor];
   133     //cell.textLabel.font = [UIFont fontWithName:@"Bradley Hand Bold" size:[UIFont labelFontSize]];
   228     //cell.textLabel.font = [UIFont fontWithName:@"Bradley Hand Bold" size:[UIFont labelFontSize]];
   134     cell.textLabel.textAlignment = (IS_IPAD()) ? UITextAlignmentCenter : UITextAlignmentLeft;
   229     cell.textLabel.textAlignment = (IS_IPAD()) ? UITextAlignmentCenter : UITextAlignmentLeft;
   135     cell.textLabel.backgroundColor = [UIColor clearColor];
   230     cell.textLabel.backgroundColor = [UIColor clearColor];
   136     cell.textLabel.adjustsFontSizeToFitWidth = YES;
   231     cell.textLabel.adjustsFontSizeToFitWidth = YES;
   137     cell.detailTextLabel.text = (IS_IPAD()) ? nil : [self.listOfDescriptions objectAtIndex:row];
   232     cell.detailTextLabel.text = (IS_IPAD()) ? nil : self.dictOfMissions[missionID][@"desc"];
   138     cell.detailTextLabel.textColor = [UIColor whiteColor];
   233     cell.detailTextLabel.textColor = [UIColor whiteColor];
   139     cell.detailTextLabel.backgroundColor = [UIColor clearColor];
   234     cell.detailTextLabel.backgroundColor = [UIColor clearColor];
   140     cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
   235     cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
   141     cell.detailTextLabel.numberOfLines = ([cell.detailTextLabel.text length] % 40);
   236     cell.detailTextLabel.numberOfLines = ([cell.detailTextLabel.text length] % 40);
   142     cell.detailTextLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
   237     cell.detailTextLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
   143 
   238 
       
   239     UIView *bgColorView = [[UIView alloc] init];
       
   240     bgColorView.backgroundColor = [UIColor colorWithRed:(85.0/255.0) green:(15.0/255.0) blue:(106.0/255.0) alpha:1.0];
       
   241     bgColorView.layer.masksToBounds = YES;
       
   242     cell.selectedBackgroundView = bgColorView;
       
   243     [bgColorView release];
       
   244     
   144     cell.backgroundColor = [UIColor blackColorTransparent];
   245     cell.backgroundColor = [UIColor blackColorTransparent];
   145     return cell;
   246     return cell;
   146 }
   247 }
   147 
   248 
   148 #pragma mark -
   249 #pragma mark -
   149 #pragma mark Table view delegate
   250 #pragma mark Table view delegate
   150 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   251 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   151     NSInteger row = [indexPath row];
   252     NSInteger row = [indexPath row];
   152 
   253 
   153     self.missionName = [[self.listOfMissions objectAtIndex:row] stringByDeletingPathExtension];
   254     self.missionName = [self.listOfMissionIDs objectAtIndex:row];
   154     NSString *size = IS_IPAD() ? @"@2x" : @"";
   255     NSString *size = IS_IPAD() ? @"@2x" : @"";
   155     NSString *filePath = [[NSString alloc] initWithFormat:@"%@/Missions/Training/%@%@.png",GRAPHICS_DIRECTORY(),self.missionName,size];
   256     NSString *filePath = [[NSString alloc] initWithFormat:@"%@/Missions/Training/%@%@.png",GRAPHICS_DIRECTORY(),self.missionName,size];
   156     UIImage *img = [[UIImage alloc] initWithContentsOfFile:filePath];
   257     UIImage *img = [[UIImage alloc] initWithContentsOfFile:filePath];
   157     [filePath release];
   258     [filePath release];
   158     [self.previewImage setImage:img];
   259     [self.previewImage setImage:img];
   159     [img release];
   260     [img release];
   160 
   261 
   161     self.descriptionLabel.text = [self.listOfDescriptions objectAtIndex:row];
   262     self.descriptionLabel.text = self.dictOfMissions[self.missionName][@"desc"];
   162 }
   263 }
   163 
   264 
   164 #pragma mark -
   265 #pragma mark -
   165 #pragma mark Memory management
   266 #pragma mark Memory management
   166 -(void) didReceiveMemoryWarning {
   267 
       
   268 -(void) didReceiveMemoryWarning
       
   269 {
   167     self.previewImage = nil;
   270     self.previewImage = nil;
   168     self.missionName = nil;
   271     self.missionName = nil;
   169     self.listOfMissions = nil;
   272     self.listOfMissionIDs = nil;
   170     self.listOfDescriptions = nil;
   273     self.dictOfMissions = nil;
   171     // if you nil this one it won't get updated anymore
   274     // if you nil this one it won't get updated anymore
   172     //self.previewImage = nil;
   275     //self.previewImage = nil;
   173     [super didReceiveMemoryWarning];
   276     [super didReceiveMemoryWarning];
   174 }
   277 }
   175 
   278 
   176 -(void) viewDidUnload {
   279 -(void) viewDidUnload
   177     self.listOfMissions = nil;
   280 {
   178     self.listOfDescriptions = nil;
   281     self.listOfMissionIDs = nil;
       
   282     self.dictOfMissions = nil;
   179     self.previewImage = nil;
   283     self.previewImage = nil;
   180     self.tableView = nil;
   284     self.tableView = nil;
   181     self.descriptionLabel = nil;
   285     self.descriptionLabel = nil;
   182     self.missionName = nil;
   286     self.missionName = nil;
   183     MSG_DIDUNLOAD();
   287     MSG_DIDUNLOAD();
   184     [super viewDidUnload];
   288     [super viewDidUnload];
   185 }
   289 }
   186 
   290 
   187 
   291 
   188 -(void) dealloc {
   292 -(void) dealloc
   189     releaseAndNil(listOfMissions);
   293 {
   190     releaseAndNil(listOfDescriptions);
   294     releaseAndNil(_listOfMissionIDs);
   191     releaseAndNil(previewImage);
   295     releaseAndNil(_dictOfMissions);
   192     releaseAndNil(tableView);
   296     releaseAndNil(_previewImage);
   193     releaseAndNil(descriptionLabel);
   297     releaseAndNil(_tableView);
   194     releaseAndNil(missionName);
   298     releaseAndNil(_descriptionLabel);
       
   299     releaseAndNil(_missionName);
   195     [super dealloc];
   300     [super dealloc];
   196 }
   301 }
   197 
   302 
   198 
   303 
   199 @end
   304 @end