project_files/HedgewarsMobile/Classes/MissionTrainingViewController.m
changeset 6094 16b12a6417d1
parent 6084 e692c0348e74
child 6100 e6426c6b2882
--- a/project_files/HedgewarsMobile/Classes/MissionTrainingViewController.m	Sat Oct 08 00:04:18 2011 +0200
+++ b/project_files/HedgewarsMobile/Classes/MissionTrainingViewController.m	Sat Oct 08 02:57:30 2011 +0200
@@ -18,38 +18,41 @@
  * File created on 03/10/2011.
  */
 
+
 #import "MissionTrainingViewController.h"
 #import <QuartzCore/QuartzCore.h>
 #import "GameInterfaceBridge.h"
 
+
 @implementation MissionTrainingViewController
-@synthesize listOfMissions, previewImage, tableView, descriptionLabel, missionName;
+@synthesize listOfMissions, listOfDescriptions, previewImage, tableView, descriptionLabel, missionName;
 
 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
     return rotationManager(interfaceOrientation);
 }
 
-// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
+#pragma mark -
+#pragma mark View management
 -(void) viewDidLoad {
     NSString *imgName = (IS_IPAD()) ? @"mediumBackground~ipad.png" : @"smallerBackground~iphone.png";
     UIImage *img = [[UIImage alloc] initWithContentsOfFile:imgName];
     self.view.backgroundColor = [UIColor colorWithPatternImage:img];
     [img release];
     
-    NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:TRAININGS_DIRECTORY() error:NULL];
-    self.listOfMissions = array;
     self.previewImage.layer.borderColor = [[UIColor darkYellowColor] CGColor];
     self.previewImage.layer.borderWidth = 3.8f;
     self.previewImage.layer.cornerRadius = 14;
 
     UIView *backView = [[UIView alloc] initWithFrame:self.tableView.frame];
-    backView.backgroundColor = [UIColor darkBlueColorTransparent];
+    backView.backgroundColor = IS_IPAD() ? [UIColor darkBlueColorTransparent] : [UIColor blackColorTransparent];
     [self.tableView setBackgroundView:backView];
     [backView release];
     self.tableView.backgroundColor = [UIColor clearColor];
-    self.tableView.layer.borderColor = [[UIColor darkYellowColor] CGColor];
-    self.tableView.layer.borderWidth = 2;
+    self.tableView.layer.borderColor = IS_IPAD() ? [[UIColor darkYellowColor] CGColor] : [[UIColor whiteColor] CGColor];
+    self.tableView.layer.borderWidth = 2.4f;
     self.tableView.layer.cornerRadius = 8;
+    self.tableView.separatorColor = [UIColor whiteColor];
+    self.tableView.separatorStyle = IS_IPAD() ? UITableViewCellSeparatorStyleNone : UITableViewCellSeparatorStyleSingleLine;
 
     self.descriptionLabel.textColor = [UIColor lightYellowColor];
     [super viewDidLoad];
@@ -75,6 +78,39 @@
     }
 }
 
+#pragma mark -
+#pragma mark override setters/getters for better memory handling
+-(NSArray *)listOfMissions {
+    if (listOfMissions == nil)
+        self.listOfMissions = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:TRAININGS_DIRECTORY() error:NULL];
+    return listOfMissions;
+}
+
+-(NSArray *)listOfDescriptions {
+    if (listOfDescriptions == nil) {
+        NSString *descLocation = [[NSString alloc] initWithFormat:@"%@/missions_en.txt",LOCALE_DIRECTORY()];
+        NSString *descComplete = [[NSString alloc] initWithContentsOfFile:descLocation encoding:NSUTF8StringEncoding error:NULL];
+        [descLocation release];
+        NSArray *descArray = [descComplete componentsSeparatedByString:@"\n"];
+        NSMutableArray *filteredArray = [[NSMutableArray alloc] initWithCapacity:[descArray count]];
+        [descComplete release];
+        // sanity check to avoid having missions and descriptions conflicts
+        for (int i = 0; i < [self.listOfMissions count]; i++) {
+            NSString *desc = [[self.listOfMissions objectAtIndex:i] stringByDeletingPathExtension];
+            for (NSString *str in descArray)
+                if ([str hasPrefix:desc]) {
+                    NSArray *descriptionText = [str componentsSeparatedByString:@"\""];
+                    [filteredArray insertObject:[descriptionText objectAtIndex:1] atIndex:i];
+                    break;
+                }
+        }
+        self.listOfDescriptions = filteredArray;
+        [filteredArray release];
+    }
+    return listOfDescriptions;
+}
+
+#pragma mark -
 #pragma mark Table view data source
 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
     return 1;
@@ -84,18 +120,33 @@
     return [self.listOfMissions count];
 }
 
+-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
+    return (IS_IPAD()) ? self.tableView.rowHeight : 80;
+}
+
 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     static NSString *CellIdentifier = @"CellTr";
+    NSInteger row = [indexPath row];
 
     UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if (cell == nil)
-        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
+        cell = [[[UITableViewCell alloc] initWithStyle:(IS_IPAD()) ? UITableViewCellStyleDefault : UITableViewCellStyleSubtitle
+                                       reuseIdentifier:CellIdentifier] autorelease];
 
-    cell.textLabel.text = [[[self.listOfMissions objectAtIndex:[indexPath row]] stringByDeletingPathExtension] stringByReplacingOccurrencesOfString:@"_" withString:@" "];
+    cell.textLabel.text = [[[self.listOfMissions objectAtIndex:row] stringByDeletingPathExtension]
+                           stringByReplacingOccurrencesOfString:@"_" withString:@" "];
     cell.textLabel.textColor = [UIColor lightYellowColor];
     //cell.textLabel.font = [UIFont fontWithName:@"Bradley Hand Bold" size:[UIFont labelFontSize]];
-    cell.textLabel.textAlignment = UITextAlignmentCenter;
+    cell.textLabel.textAlignment = (IS_IPAD()) ? UITextAlignmentCenter : UITextAlignmentLeft;
     cell.textLabel.backgroundColor = [UIColor clearColor];
+    cell.textLabel.adjustsFontSizeToFitWidth = YES;
+    cell.detailTextLabel.text = (IS_IPAD()) ? nil : [self.listOfDescriptions objectAtIndex:row];
+    cell.detailTextLabel.textColor = [UIColor whiteColor];
+    cell.detailTextLabel.backgroundColor = [UIColor clearColor];
+    cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
+    cell.detailTextLabel.numberOfLines = ([cell.detailTextLabel.text length] % 40);
+    cell.detailTextLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
+
     cell.backgroundColor = [UIColor blackColorTransparent];
     return cell;
 }
@@ -103,37 +154,33 @@
 #pragma mark -
 #pragma mark Table view delegate
 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
-    self.missionName = [[self.listOfMissions objectAtIndex:[indexPath row]] stringByDeletingPathExtension];
+    NSInteger row = [indexPath row];
+
+    self.missionName = [[self.listOfMissions objectAtIndex:row] stringByDeletingPathExtension];
     NSString *filePath = [[NSString alloc] initWithFormat:@"%@/Missions/Training/%@@2x.png",GRAPHICS_DIRECTORY(),self.missionName];
     UIImage *img = [[UIImage alloc] initWithContentsOfFile:filePath];
     [filePath release];
     [self.previewImage setImage:img];
     [img release];
 
-    self.descriptionLabel.text = nil;
-    NSString *descLocation = [[NSString alloc] initWithFormat:@"%@/missions_en.txt",LOCALE_DIRECTORY()];
-    NSString *descComplete = [[NSString alloc] initWithContentsOfFile:descLocation encoding:NSUTF8StringEncoding error:NULL];
-    [descLocation release];
-    NSArray *descArray =  [descComplete componentsSeparatedByString:@"\n"];
-    [descComplete release];
-    for (NSString *str in descArray) {
-        if ([str hasPrefix:missionName]) {
-            NSArray *descriptionText = [str componentsSeparatedByString:@"\""];
-            self.descriptionLabel.text = [descriptionText objectAtIndex:1];
-        }
-    }
+    self.descriptionLabel.text = [self.listOfDescriptions objectAtIndex:row];
 }
 
 #pragma mark -
 #pragma mark Memory management
 -(void) didReceiveMemoryWarning {
-    previewImage = nil;
-    missionName = nil;
+    self.previewImage = nil;
+    self.missionName = nil;
+    self.listOfMissions = nil;
+    self.listOfDescriptions = nil;
+    // if you nil this one it won't get updated anymore
+    //self.previewImage = nil;
     [super didReceiveMemoryWarning];
 }
 
 -(void) viewDidUnload {
     self.listOfMissions = nil;
+    self.listOfDescriptions = nil;
     self.previewImage = nil;
     self.tableView = nil;
     self.descriptionLabel = nil;
@@ -145,6 +192,7 @@
 
 -(void) dealloc {
     releaseAndNil(listOfMissions);
+    releaseAndNil(listOfDescriptions);
     releaseAndNil(previewImage);
     releaseAndNil(tableView);
     releaseAndNil(descriptionLabel);