some code refactoring of the ios game config
authorkoda
Sun, 09 Oct 2011 00:24:58 +0200
changeset 6107 0741c0f0203e
parent 6106 ba098945bd72
child 6108 7a8da11a6144
some code refactoring of the ios game config
project_files/HedgewarsMobile/Classes/MapConfigViewController.h
project_files/HedgewarsMobile/Classes/MapConfigViewController.m
project_files/HedgewarsMobile/Classes/SchemeWeaponConfigViewController.m
project_files/HedgewarsMobile/Classes/TeamConfigViewController.m
--- a/project_files/HedgewarsMobile/Classes/MapConfigViewController.h	Sat Oct 08 23:23:25 2011 +0200
+++ b/project_files/HedgewarsMobile/Classes/MapConfigViewController.h	Sun Oct 09 00:24:58 2011 +0200
@@ -93,6 +93,5 @@
 -(void) turnOffWidgets;
 -(void) setLabelText:(NSString *)str;
 -(void) updatePreview;
--(void) loadDataSourceArray;
 
 @end
--- a/project_files/HedgewarsMobile/Classes/MapConfigViewController.m	Sat Oct 08 23:23:25 2011 +0200
+++ b/project_files/HedgewarsMobile/Classes/MapConfigViewController.m	Sun Oct 09 00:24:58 2011 +0200
@@ -55,8 +55,6 @@
     self.seedCommand = seedCmd;
     [seedCmd release];
 
-    if (self.dataSourceArray == nil)
-        [self loadDataSourceArray];
     NSArray *source = [self.dataSourceArray objectAtIndex:scIndex];
     NSIndexPath *theIndex;
     if (isRandomness()) {
@@ -115,8 +113,6 @@
 }
 
 -(NSInteger) tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger) section {
-    if (self.dataSourceArray == nil)
-        [self loadDataSourceArray];
     return [[self.dataSourceArray objectAtIndex:scIndex] count];
 }
 
@@ -128,8 +124,6 @@
     if (cell == nil)
         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
 
-    if (self.dataSourceArray == nil)
-        [self loadDataSourceArray];
     NSArray *source = [self.dataSourceArray objectAtIndex:scIndex];
 
     NSString *labelString = [source objectAtIndex:row];
@@ -159,8 +153,6 @@
 
 // this set details for a static map (called by didSelectRowAtIndexPath)
 -(void) setDetailsForStaticMap:(NSInteger) index {
-    if (self.dataSourceArray == nil)
-        [self loadDataSourceArray];
     NSArray *source = [self.dataSourceArray objectAtIndex:scIndex];
     
     NSString *fileCfg = [[NSString alloc] initWithFormat:@"%@/%@/map.cfg", 
@@ -196,8 +188,6 @@
     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
 
     if (newRow != oldRow) {
-        if (self.dataSourceArray == nil)
-            [self loadDataSourceArray];
         NSArray *source = [self.dataSourceArray objectAtIndex:scIndex];
         if (isRandomness()) {
             // just change the theme, don't update preview
@@ -375,48 +365,51 @@
 
 #pragma mark -
 #pragma mark view management
--(void) loadDataSourceArray {
-    NSString *model = [HWUtils modelType];
-
-    // only folders containing icon.png are a valid theme
-    NSArray *themeArrayFull = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:THEMES_DIRECTORY() error:NULL];
-    NSMutableArray *themeArray = [[NSMutableArray alloc] init];
-    for (NSString *themeName in themeArrayFull) {
-        NSString *checkPath = [[NSString alloc] initWithFormat:@"%@/%@/icon.png",THEMES_DIRECTORY(),themeName];
-        if ([[NSFileManager defaultManager] fileExistsAtPath:checkPath])
-            [themeArray addObject:themeName];
-        [checkPath release];
+-(NSArray *) dataSourceArray {
+    if (dataSourceArray == nil) {
+        NSString *model = [HWUtils modelType];
+        
+        // only folders containing icon.png are a valid theme
+        NSArray *themeArrayFull = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:THEMES_DIRECTORY() error:NULL];
+        NSMutableArray *themeArray = [[NSMutableArray alloc] init];
+        for (NSString *themeName in themeArrayFull) {
+            NSString *checkPath = [[NSString alloc] initWithFormat:@"%@/%@/icon.png",THEMES_DIRECTORY(),themeName];
+            if ([[NSFileManager defaultManager] fileExistsAtPath:checkPath])
+                [themeArray addObject:themeName];
+            [checkPath release];
+        }
+        
+        // remove images that are too big for certain devices without loading the whole image
+        NSArray *mapArrayFull = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:MAPS_DIRECTORY() error:NULL];
+        NSMutableArray *mapArray = [[NSMutableArray alloc] init];
+        for (NSString *str in mapArrayFull) {
+            CGSize imgSize = [UIImage imageSizeFromMetadataOf:[MAPS_DIRECTORY() stringByAppendingFormat:@"%@/map.png",str]];
+            if (IS_NOT_POWERFUL(model) && imgSize.height > 1024.0f)
+                continue;
+            if (IS_NOT_VERY_POWERFUL(model) && imgSize.height > 1280.0f)
+                continue;
+            [mapArray addObject:str];
+        }
+        
+        NSArray *missionArrayFull = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:MISSIONS_DIRECTORY() error:NULL];
+        NSMutableArray *missionArray = [[NSMutableArray alloc] init];
+        for (NSString *str in missionArrayFull) {
+            CGSize imgSize = [UIImage imageSizeFromMetadataOf:[MISSIONS_DIRECTORY() stringByAppendingFormat:@"%@/map.png",str]];
+            if (IS_NOT_POWERFUL(model) && imgSize.height > 1024.0f)
+                continue;
+            if (IS_NOT_VERY_POWERFUL(model) && imgSize.height > 1280.0f)
+                continue;
+            [missionArray addObject:str];
+        }
+        NSArray *array = [[NSArray alloc] initWithObjects:themeArray,mapArray,themeArray,missionArray,nil];
+        [missionArray release];
+        [themeArray release];
+        [mapArray release];
+        
+        self.dataSourceArray = array;
+        [array release];
     }
-
-    // remove images that are too big for certain devices without loading the whole image
-    NSArray *mapArrayFull = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:MAPS_DIRECTORY() error:NULL];
-    NSMutableArray *mapArray = [[NSMutableArray alloc] init];
-    for (NSString *str in mapArrayFull) {
-        CGSize imgSize = [UIImage imageSizeFromMetadataOf:[MAPS_DIRECTORY() stringByAppendingFormat:@"%@/map.png",str]];
-        if (IS_NOT_POWERFUL(model) && imgSize.height > 1024.0f)
-            continue;
-        if (IS_NOT_VERY_POWERFUL(model) && imgSize.height > 1280.0f)
-            continue;
-        [mapArray addObject:str];
-    }
-    
-    NSArray *missionArrayFull = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:MISSIONS_DIRECTORY() error:NULL];
-    NSMutableArray *missionArray = [[NSMutableArray alloc] init];
-    for (NSString *str in missionArrayFull) {
-        CGSize imgSize = [UIImage imageSizeFromMetadataOf:[MISSIONS_DIRECTORY() stringByAppendingFormat:@"%@/map.png",str]];
-        if (IS_NOT_POWERFUL(model) && imgSize.height > 1024.0f)
-            continue;
-        if (IS_NOT_VERY_POWERFUL(model) && imgSize.height > 1280.0f)
-            continue;
-        [missionArray addObject:str];
-    }
-    NSArray *array = [[NSArray alloc] initWithObjects:themeArray,mapArray,themeArray,missionArray,nil];
-    [missionArray release];
-    [themeArray release];
-    [mapArray release];
-
-    self.dataSourceArray = array;
-    [array release];
+    return dataSourceArray;
 }
 
 -(void) viewDidLoad {
@@ -433,7 +426,6 @@
     oldValue = 5;
     
     busy = NO;
-    [self loadDataSourceArray];
     self.lastIndexPath = [NSIndexPath indexPathForRow:-1 inSection:0];
     
     // select a map at first because it's faster - done in IB
@@ -458,8 +450,6 @@
 }
 
 -(void) viewWillAppear:(BOOL)animated {
-    if (self.dataSourceArray == nil)
-        [self loadDataSourceArray];
     [super viewWillAppear:animated];
 }
 
--- a/project_files/HedgewarsMobile/Classes/SchemeWeaponConfigViewController.m	Sat Oct 08 23:23:25 2011 +0200
+++ b/project_files/HedgewarsMobile/Classes/SchemeWeaponConfigViewController.m	Sun Oct 09 00:24:58 2011 +0200
@@ -33,6 +33,73 @@
 }
 
 #pragma mark -
+#pragma mark custom setters/getters
+-(NSString *)selectedScheme {
+    if (selectedScheme == nil)
+        self.selectedScheme = @"Default.plist";
+    return selectedScheme;
+}
+
+-(NSString *)selectedWeapon {
+    if (selectedWeapon == nil)
+        self.selectedWeapon = @"Default.plist";
+    return selectedWeapon;
+}
+
+-(NSString *)selectedScript {
+    if (selectedScript == nil)
+        self.selectedScript = @"Normal.plist";
+    return selectedScript;
+}
+
+-(NSString *)scriptCommand {
+    if (scriptCommand == nil)
+        self.scriptCommand = @"";
+    return scriptCommand;
+}
+
+-(NSArray *)listOfSchemes {
+    if (listOfSchemes == nil)
+        self.listOfSchemes = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SCHEMES_DIRECTORY() error:NULL];
+    return listOfSchemes;
+}
+
+-(NSArray *)listOfWeapons {
+    if (listOfWeapons == nil)
+        self.listOfWeapons = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:WEAPONS_DIRECTORY() error:NULL];
+    return listOfWeapons;
+}
+
+-(NSArray *)listOfScripts {
+    if (listOfScripts == nil)
+        self.listOfScripts = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SCRIPTS_DIRECTORY() error:NULL];
+    return listOfScripts;
+}
+
+-(UISegmentedControl *)topControl {
+    if (topControl == nil) {
+        NSArray *array = [[NSArray alloc] initWithObjects:
+                          NSLocalizedString(@"Scheme",@""),
+                          NSLocalizedString(@"Weapon",@""),
+                          NSLocalizedString(@"Style",@""),nil];
+        UISegmentedControl *controller = [[UISegmentedControl alloc] initWithItems:array];
+        [array release];
+        [controller addTarget:self.tableView action:@selector(reloadData) forControlEvents:UIControlEventValueChanged];
+        controller.segmentedControlStyle = UISegmentedControlStyleBar;
+        controller.tintColor = [UIColor lightGrayColor];
+        controller.selectedSegmentIndex = 0;
+        self.topControl = controller;
+        [controller release];
+    }
+    return topControl;
+}
+
+-(void) viewWillAppear:(BOOL) animated {
+    [super viewWillAppear:animated];
+    [self.tableView reloadData];
+}
+
+#pragma mark -
 #pragma mark View lifecycle
 -(void) viewDidLoad {
     [super viewDidLoad];
@@ -40,11 +107,6 @@
     CGSize screenSize = [[UIScreen mainScreen] bounds].size;
     self.view.frame = CGRectMake(0, 0, screenSize.height, screenSize.width - 44);
 
-    self.selectedScheme = nil;
-    self.selectedWeapon = nil;
-    self.selectedScript = nil;
-    self.scriptCommand = nil;
-
     if ([self.tableView respondsToSelector:@selector(setBackgroundView:)]) {
         if (IS_IPAD())
             [self.tableView setBackgroundView:nil];
@@ -63,37 +125,10 @@
     self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
 }
 
--(void) viewWillAppear:(BOOL) animated {
-    [super viewWillAppear:animated];
-
-    NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SCHEMES_DIRECTORY() error:NULL];
-    self.listOfSchemes = contentsOfDir;
-
-    if (self.selectedScheme == nil && [listOfSchemes containsObject:@"Default.plist"])
-        self.selectedScheme = @"Default.plist";
-    
-    contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:WEAPONS_DIRECTORY() error:NULL];
-    self.listOfWeapons = contentsOfDir;
-    
-    if (self.selectedWeapon == nil && [listOfWeapons containsObject:@"Default.plist"])
-        self.selectedWeapon = @"Default.plist";
-
-    contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SCRIPTS_DIRECTORY() error:NULL];
-    self.listOfScripts = contentsOfDir;
-    self.selectedScript = @"Normal.plist";
-    self.scriptCommand = @"";
-
-    [self.tableView reloadData];
-}
-
-
 #pragma mark -
 #pragma mark Table view data source
 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
-    if (hideSections)
-        return 0;
-    else
-        return 1;
+    return (self.hideSections ? 0 : 1);
 }
 
 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
@@ -167,20 +202,9 @@
 }
 
 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
-    if (self.topControl == nil) {
-        NSArray *array = [[NSArray alloc] initWithObjects:NSLocalizedString(@"Scheme",@""),NSLocalizedString(@"Weapon",@""),
-                          NSLocalizedString(@"Style",@""),nil];
-        self.topControl = [[UISegmentedControl alloc] initWithItems:array];
-        [array release];
-        [self.topControl addTarget:self.tableView action:@selector(reloadData) forControlEvents:UIControlEventValueChanged];
-        self.topControl.segmentedControlStyle = UISegmentedControlStyleBar;
-        self.topControl.frame = CGRectMake(0, 0, self.view.frame.size.width * 80/100, 30);
-        self.topControl.center = CGPointMake(self.view.frame.size.width/2, 24);
-        self.topControl.tintColor = [UIColor lightGrayColor];
-        self.topControl.selectedSegmentIndex = 0;
-    }
-
     UIView *theView = [[[UIView alloc] init] autorelease];
+    self.topControl.frame = CGRectMake(0, 0, self.view.frame.size.width * 80/100, 30);
+    self.topControl.center = CGPointMake(self.view.frame.size.width/2, 24);
     [theView addSubview:self.topControl];
     return theView;
 }
@@ -213,6 +237,7 @@
             self.lastIndexPath_sc = indexPath;
             self.selectedScheme = [self.listOfSchemes objectAtIndex:newRow];
 
+            // also set weaponset when selecting scheme, if set
             NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
             if ([[settings objectForKey:@"sync_ws"] boolValue]) {
                 for (NSString *str in self.listOfWeapons) {
@@ -232,7 +257,8 @@
             self.lastIndexPath_lu = indexPath;
             self.selectedScript = [self.listOfScripts objectAtIndex:newRow];
 
-            NSString *path = [[NSString alloc] initWithFormat:@"%@/%@",SCRIPTS_DIRECTORY(),selectedScript];
+            // some styles disable or force the choice of a particular scheme/weaponset
+            NSString *path = [[NSString alloc] initWithFormat:@"%@/%@",SCRIPTS_DIRECTORY(),self.selectedScript];
             NSDictionary *scriptDict = [[NSDictionary alloc] initWithContentsOfFile:path];
             [path release];
             self.scriptCommand = [scriptDict objectForKey:@"command"];
@@ -262,18 +288,16 @@
     [aTableView deselectRowAtIndexPath:indexPath animated:YES];
 }
 
+#pragma mark -
+#pragma mark called externally to empty or fill the sections completely
 -(void) fillSections {
-    if (hideSections == YES) {
-        hideSections = NO;
+    if (self.hideSections == YES) {
+        self.hideSections = NO;
         NSRange range;
         range.location = 0;
         range.length = 1;
         NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range];
         [self.tableView insertSections:sections withRowAnimation:UITableViewRowAnimationFade];
-        self.selectedScheme = @"Default.plist";
-        self.selectedWeapon = @"Default.plist";
-        self.selectedScript = @"Normal.plist";
-
         self.tableView.scrollEnabled = YES;
 
         [[self.view viewWithTag:LABEL_TAG] removeFromSuperview];
@@ -287,10 +311,6 @@
     range.length = 1;
     NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range];
     [self.tableView deleteSections:sections withRowAnimation:UITableViewRowAnimationFade];
-    self.selectedScheme = @"Default.plist";
-    self.selectedWeapon = @"Default.plist";
-    self.selectedScript = @"Normal.plist";
-
     self.tableView.scrollEnabled = NO;
 
     CGRect frame = CGRectMake(0, 0, self.view.frame.size.width * 80/100, 60);
@@ -311,12 +331,16 @@
         self.lastIndexPath_sc = nil;
         self.lastIndexPath_we = nil;
         self.lastIndexPath_lu = nil;
-        self.listOfSchemes = nil;
-        self.listOfWeapons = nil;
-        self.listOfScripts = nil;
+        self.selectedScheme = nil;
+        self.selectedWeapon = nil;
+        self.selectedScript = nil;
+        self.scriptCommand = nil;
         self.topControl = nil;
-        MSG_MEMCLEAN();
     }
+    self.listOfSchemes = nil;
+    self.listOfWeapons = nil;
+    self.listOfScripts = nil;
+    MSG_MEMCLEAN();
     [super didReceiveMemoryWarning];
 }
 
@@ -336,7 +360,6 @@
     [super viewDidUnload];
 }
 
-
 -(void) dealloc {
     releaseAndNil(listOfSchemes);
     releaseAndNil(listOfWeapons);
--- a/project_files/HedgewarsMobile/Classes/TeamConfigViewController.m	Sat Oct 08 23:23:25 2011 +0200
+++ b/project_files/HedgewarsMobile/Classes/TeamConfigViewController.m	Sun Oct 09 00:24:58 2011 +0200
@@ -137,10 +137,7 @@
 }
 
 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
-    if (section == 0)
-        return selectedTeamsCount;
-    else
-        return allTeamsCount;
+    return (section == 0 ? selectedTeamsCount : allTeamsCount);
 }
 
 // Customize the appearance of table view cells.
@@ -177,7 +174,7 @@
         if (cell == nil)
             cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
 
-        cell.textLabel.text = [[[listOfTeams objectAtIndex:[indexPath row]] objectForKey:@"team"] stringByDeletingPathExtension];
+        cell.textLabel.text = [[[self.listOfTeams objectAtIndex:[indexPath row]] objectForKey:@"team"] stringByDeletingPathExtension];
         cell.textLabel.backgroundColor = [UIColor clearColor];
         
         NSString *teamPath = [NSString stringWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),cell.textLabel.text];
@@ -299,7 +296,10 @@
 #pragma mark -
 #pragma mark Memory management
 -(void) didReceiveMemoryWarning {
-    // Relinquish ownership any cached data, images, etc that aren't in use.
+    if ([[HedgewarsAppDelegate sharedAppDelegate] isInGame]) {
+        self.listOfSelectedTeams = nil;
+        self.listOfTeams = nil;
+    }
     self.cachedContentsOfDir = nil;
     MSG_MEMCLEAN();
     [super didReceiveMemoryWarning];