project_files/HedgewarsMobile/Classes/MissionTrainingViewController.m
changeset 6094 16b12a6417d1
parent 6084 e692c0348e74
child 6100 e6426c6b2882
equal deleted inserted replaced
6093:42c9ac754460 6094:16b12a6417d1
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    17  *
    17  *
    18  * File created on 03/10/2011.
    18  * File created on 03/10/2011.
    19  */
    19  */
    20 
    20 
       
    21 
    21 #import "MissionTrainingViewController.h"
    22 #import "MissionTrainingViewController.h"
    22 #import <QuartzCore/QuartzCore.h>
    23 #import <QuartzCore/QuartzCore.h>
    23 #import "GameInterfaceBridge.h"
    24 #import "GameInterfaceBridge.h"
    24 
    25 
       
    26 
    25 @implementation MissionTrainingViewController
    27 @implementation MissionTrainingViewController
    26 @synthesize listOfMissions, previewImage, tableView, descriptionLabel, missionName;
    28 @synthesize listOfMissions, listOfDescriptions, previewImage, tableView, descriptionLabel, missionName;
    27 
    29 
    28 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    30 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    29     return rotationManager(interfaceOrientation);
    31     return rotationManager(interfaceOrientation);
    30 }
    32 }
    31 
    33 
    32 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    34 #pragma mark -
       
    35 #pragma mark View management
    33 -(void) viewDidLoad {
    36 -(void) viewDidLoad {
    34     NSString *imgName = (IS_IPAD()) ? @"mediumBackground~ipad.png" : @"smallerBackground~iphone.png";
    37     NSString *imgName = (IS_IPAD()) ? @"mediumBackground~ipad.png" : @"smallerBackground~iphone.png";
    35     UIImage *img = [[UIImage alloc] initWithContentsOfFile:imgName];
    38     UIImage *img = [[UIImage alloc] initWithContentsOfFile:imgName];
    36     self.view.backgroundColor = [UIColor colorWithPatternImage:img];
    39     self.view.backgroundColor = [UIColor colorWithPatternImage:img];
    37     [img release];
    40     [img release];
    38     
    41     
    39     NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:TRAININGS_DIRECTORY() error:NULL];
       
    40     self.listOfMissions = array;
       
    41     self.previewImage.layer.borderColor = [[UIColor darkYellowColor] CGColor];
    42     self.previewImage.layer.borderColor = [[UIColor darkYellowColor] CGColor];
    42     self.previewImage.layer.borderWidth = 3.8f;
    43     self.previewImage.layer.borderWidth = 3.8f;
    43     self.previewImage.layer.cornerRadius = 14;
    44     self.previewImage.layer.cornerRadius = 14;
    44 
    45 
    45     UIView *backView = [[UIView alloc] initWithFrame:self.tableView.frame];
    46     UIView *backView = [[UIView alloc] initWithFrame:self.tableView.frame];
    46     backView.backgroundColor = [UIColor darkBlueColorTransparent];
    47     backView.backgroundColor = IS_IPAD() ? [UIColor darkBlueColorTransparent] : [UIColor blackColorTransparent];
    47     [self.tableView setBackgroundView:backView];
    48     [self.tableView setBackgroundView:backView];
    48     [backView release];
    49     [backView release];
    49     self.tableView.backgroundColor = [UIColor clearColor];
    50     self.tableView.backgroundColor = [UIColor clearColor];
    50     self.tableView.layer.borderColor = [[UIColor darkYellowColor] CGColor];
    51     self.tableView.layer.borderColor = IS_IPAD() ? [[UIColor darkYellowColor] CGColor] : [[UIColor whiteColor] CGColor];
    51     self.tableView.layer.borderWidth = 2;
    52     self.tableView.layer.borderWidth = 2.4f;
    52     self.tableView.layer.cornerRadius = 8;
    53     self.tableView.layer.cornerRadius = 8;
       
    54     self.tableView.separatorColor = [UIColor whiteColor];
       
    55     self.tableView.separatorStyle = IS_IPAD() ? UITableViewCellSeparatorStyleNone : UITableViewCellSeparatorStyleSingleLine;
    53 
    56 
    54     self.descriptionLabel.textColor = [UIColor lightYellowColor];
    57     self.descriptionLabel.textColor = [UIColor lightYellowColor];
    55     [super viewDidLoad];
    58     [super viewDidLoad];
    56 }
    59 }
    57 
    60 
    73         [bridge startMissionGame:self.missionName];
    76         [bridge startMissionGame:self.missionName];
    74         [bridge release];
    77         [bridge release];
    75     }
    78     }
    76 }
    79 }
    77 
    80 
       
    81 #pragma mark -
       
    82 #pragma mark override setters/getters for better memory handling
       
    83 -(NSArray *)listOfMissions {
       
    84     if (listOfMissions == nil)
       
    85         self.listOfMissions = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:TRAININGS_DIRECTORY() error:NULL];
       
    86     return listOfMissions;
       
    87 }
       
    88 
       
    89 -(NSArray *)listOfDescriptions {
       
    90     if (listOfDescriptions == nil) {
       
    91         NSString *descLocation = [[NSString alloc] initWithFormat:@"%@/missions_en.txt",LOCALE_DIRECTORY()];
       
    92         NSString *descComplete = [[NSString alloc] initWithContentsOfFile:descLocation encoding:NSUTF8StringEncoding error:NULL];
       
    93         [descLocation release];
       
    94         NSArray *descArray = [descComplete componentsSeparatedByString:@"\n"];
       
    95         NSMutableArray *filteredArray = [[NSMutableArray alloc] initWithCapacity:[descArray count]];
       
    96         [descComplete release];
       
    97         // sanity check to avoid having missions and descriptions conflicts
       
    98         for (int i = 0; i < [self.listOfMissions count]; i++) {
       
    99             NSString *desc = [[self.listOfMissions objectAtIndex:i] stringByDeletingPathExtension];
       
   100             for (NSString *str in descArray)
       
   101                 if ([str hasPrefix:desc]) {
       
   102                     NSArray *descriptionText = [str componentsSeparatedByString:@"\""];
       
   103                     [filteredArray insertObject:[descriptionText objectAtIndex:1] atIndex:i];
       
   104                     break;
       
   105                 }
       
   106         }
       
   107         self.listOfDescriptions = filteredArray;
       
   108         [filteredArray release];
       
   109     }
       
   110     return listOfDescriptions;
       
   111 }
       
   112 
       
   113 #pragma mark -
    78 #pragma mark Table view data source
   114 #pragma mark Table view data source
    79 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   115 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    80     return 1;
   116     return 1;
    81 }
   117 }
    82 
   118 
    83 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   119 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    84     return [self.listOfMissions count];
   120     return [self.listOfMissions count];
    85 }
   121 }
    86 
   122 
       
   123 -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
       
   124     return (IS_IPAD()) ? self.tableView.rowHeight : 80;
       
   125 }
       
   126 
    87 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   127 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    88     static NSString *CellIdentifier = @"CellTr";
   128     static NSString *CellIdentifier = @"CellTr";
       
   129     NSInteger row = [indexPath row];
    89 
   130 
    90     UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
   131     UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    91     if (cell == nil)
   132     if (cell == nil)
    92         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
   133         cell = [[[UITableViewCell alloc] initWithStyle:(IS_IPAD()) ? UITableViewCellStyleDefault : UITableViewCellStyleSubtitle
    93 
   134                                        reuseIdentifier:CellIdentifier] autorelease];
    94     cell.textLabel.text = [[[self.listOfMissions objectAtIndex:[indexPath row]] stringByDeletingPathExtension] stringByReplacingOccurrencesOfString:@"_" withString:@" "];
   135 
       
   136     cell.textLabel.text = [[[self.listOfMissions objectAtIndex:row] stringByDeletingPathExtension]
       
   137                            stringByReplacingOccurrencesOfString:@"_" withString:@" "];
    95     cell.textLabel.textColor = [UIColor lightYellowColor];
   138     cell.textLabel.textColor = [UIColor lightYellowColor];
    96     //cell.textLabel.font = [UIFont fontWithName:@"Bradley Hand Bold" size:[UIFont labelFontSize]];
   139     //cell.textLabel.font = [UIFont fontWithName:@"Bradley Hand Bold" size:[UIFont labelFontSize]];
    97     cell.textLabel.textAlignment = UITextAlignmentCenter;
   140     cell.textLabel.textAlignment = (IS_IPAD()) ? UITextAlignmentCenter : UITextAlignmentLeft;
    98     cell.textLabel.backgroundColor = [UIColor clearColor];
   141     cell.textLabel.backgroundColor = [UIColor clearColor];
       
   142     cell.textLabel.adjustsFontSizeToFitWidth = YES;
       
   143     cell.detailTextLabel.text = (IS_IPAD()) ? nil : [self.listOfDescriptions objectAtIndex:row];
       
   144     cell.detailTextLabel.textColor = [UIColor whiteColor];
       
   145     cell.detailTextLabel.backgroundColor = [UIColor clearColor];
       
   146     cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
       
   147     cell.detailTextLabel.numberOfLines = ([cell.detailTextLabel.text length] % 40);
       
   148     cell.detailTextLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
       
   149 
    99     cell.backgroundColor = [UIColor blackColorTransparent];
   150     cell.backgroundColor = [UIColor blackColorTransparent];
   100     return cell;
   151     return cell;
   101 }
   152 }
   102 
   153 
   103 #pragma mark -
   154 #pragma mark -
   104 #pragma mark Table view delegate
   155 #pragma mark Table view delegate
   105 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   156 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   106     self.missionName = [[self.listOfMissions objectAtIndex:[indexPath row]] stringByDeletingPathExtension];
   157     NSInteger row = [indexPath row];
       
   158 
       
   159     self.missionName = [[self.listOfMissions objectAtIndex:row] stringByDeletingPathExtension];
   107     NSString *filePath = [[NSString alloc] initWithFormat:@"%@/Missions/Training/%@@2x.png",GRAPHICS_DIRECTORY(),self.missionName];
   160     NSString *filePath = [[NSString alloc] initWithFormat:@"%@/Missions/Training/%@@2x.png",GRAPHICS_DIRECTORY(),self.missionName];
   108     UIImage *img = [[UIImage alloc] initWithContentsOfFile:filePath];
   161     UIImage *img = [[UIImage alloc] initWithContentsOfFile:filePath];
   109     [filePath release];
   162     [filePath release];
   110     [self.previewImage setImage:img];
   163     [self.previewImage setImage:img];
   111     [img release];
   164     [img release];
   112 
   165 
   113     self.descriptionLabel.text = nil;
   166     self.descriptionLabel.text = [self.listOfDescriptions objectAtIndex:row];
   114     NSString *descLocation = [[NSString alloc] initWithFormat:@"%@/missions_en.txt",LOCALE_DIRECTORY()];
       
   115     NSString *descComplete = [[NSString alloc] initWithContentsOfFile:descLocation encoding:NSUTF8StringEncoding error:NULL];
       
   116     [descLocation release];
       
   117     NSArray *descArray =  [descComplete componentsSeparatedByString:@"\n"];
       
   118     [descComplete release];
       
   119     for (NSString *str in descArray) {
       
   120         if ([str hasPrefix:missionName]) {
       
   121             NSArray *descriptionText = [str componentsSeparatedByString:@"\""];
       
   122             self.descriptionLabel.text = [descriptionText objectAtIndex:1];
       
   123         }
       
   124     }
       
   125 }
   167 }
   126 
   168 
   127 #pragma mark -
   169 #pragma mark -
   128 #pragma mark Memory management
   170 #pragma mark Memory management
   129 -(void) didReceiveMemoryWarning {
   171 -(void) didReceiveMemoryWarning {
   130     previewImage = nil;
   172     self.previewImage = nil;
   131     missionName = nil;
   173     self.missionName = nil;
       
   174     self.listOfMissions = nil;
       
   175     self.listOfDescriptions = nil;
       
   176     // if you nil this one it won't get updated anymore
       
   177     //self.previewImage = nil;
   132     [super didReceiveMemoryWarning];
   178     [super didReceiveMemoryWarning];
   133 }
   179 }
   134 
   180 
   135 -(void) viewDidUnload {
   181 -(void) viewDidUnload {
   136     self.listOfMissions = nil;
   182     self.listOfMissions = nil;
       
   183     self.listOfDescriptions = nil;
   137     self.previewImage = nil;
   184     self.previewImage = nil;
   138     self.tableView = nil;
   185     self.tableView = nil;
   139     self.descriptionLabel = nil;
   186     self.descriptionLabel = nil;
   140     self.missionName = nil;
   187     self.missionName = nil;
   141     MSG_DIDUNLOAD();
   188     MSG_DIDUNLOAD();
   143 }
   190 }
   144 
   191 
   145 
   192 
   146 -(void) dealloc {
   193 -(void) dealloc {
   147     releaseAndNil(listOfMissions);
   194     releaseAndNil(listOfMissions);
       
   195     releaseAndNil(listOfDescriptions);
   148     releaseAndNil(previewImage);
   196     releaseAndNil(previewImage);
   149     releaseAndNil(tableView);
   197     releaseAndNil(tableView);
   150     releaseAndNil(descriptionLabel);
   198     releaseAndNil(descriptionLabel);
   151     releaseAndNil(missionName);
   199     releaseAndNil(missionName);
   152     [super dealloc];
   200     [super dealloc];