project_files/HedgewarsMobile/Classes/SingleSchemeViewController.m
changeset 3781 2bfda544ae48
parent 3753 bb5a0252fb56
child 3782 dc3531e49e4c
--- a/project_files/HedgewarsMobile/Classes/SingleSchemeViewController.m	Sat Aug 28 02:35:26 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/SingleSchemeViewController.m	Sat Aug 28 05:03:26 2010 +0200
@@ -13,9 +13,10 @@
 
 #define LABEL_TAG  12345
 #define SLIDER_TAG 54321
+#define SWITCH_TAG 67890
 
 @implementation SingleSchemeViewController
-@synthesize schemeName, schemeArray, basicSettingList, gameModifierArray;
+@synthesize schemeName, schemeDictionary, basicSettingList, gameModifierArray;
 
 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
     return rotationManager(interfaceOrientation);
@@ -118,9 +119,9 @@
     [super viewWillAppear:animated];
 
     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName];
-    NSMutableArray *scheme = [[NSMutableArray alloc] initWithContentsOfFile:schemeFile];
+    NSMutableDictionary *scheme = [[NSMutableDictionary alloc] initWithContentsOfFile:schemeFile];
     [schemeFile release];
-    self.schemeArray = scheme;
+    self.schemeDictionary = scheme;
     [scheme release];
 
     [self.tableView reloadData];
@@ -131,7 +132,7 @@
     [super viewWillDisappear:animated];
 
     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName];
-    [self.schemeArray writeToFile:schemeFile atomically:YES];
+    [self.schemeDictionary writeToFile:schemeFile atomically:YES];
     [schemeFile release];
 }
 
@@ -144,7 +145,7 @@
     // update filename
     self.schemeName = textString;
     // save new file
-    [self.schemeArray writeToFile:[NSString stringWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName] atomically:YES];
+    [self.schemeDictionary writeToFile:[NSString stringWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName] atomically:YES];
 }
 
 #pragma mark -
@@ -197,7 +198,6 @@
             cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier1];
             NSDictionary *detail = [self.basicSettingList objectAtIndex:row];
             // need to offset this section (see format in CommodityFunctions.m and above)
-            NSInteger gmSize = [self.gameModifierArray count];
             if (cell == nil) {
                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                                reuseIdentifier:CellIdentifier1] autorelease];
@@ -209,7 +209,7 @@
                 UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(offset+260, 12, offset+150, 23)];
                 slider.maximumValue = [[detail objectForKey:@"max"] floatValue];
                 slider.minimumValue = [[detail objectForKey:@"min"] floatValue];
-                slider.tag = row+gmSize;
+                slider.tag = SLIDER_TAG+row;
                 [slider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
                 [cell.contentView addSubview:slider];
                 [slider release];
@@ -229,11 +229,11 @@
             UILabel *cellLabel = (UILabel *)[cell.contentView viewWithTag:LABEL_TAG];
             cellLabel.text = [[self.basicSettingList objectAtIndex:row] objectForKey:@"title"];
 
-            UISlider *cellSlider = (UISlider *)[cell.contentView viewWithTag:row+gmSize];
-            cellSlider.value = [[self.schemeArray objectAtIndex:row+gmSize] floatValue];
+            UISlider *cellSlider = (UISlider *)[cell.contentView viewWithTag:SLIDER_TAG+row];
+            cellSlider.value = [[[self.schemeDictionary objectForKey:@"basic"] objectAtIndex:row] floatValue];
 
             // forced to use this weird format otherwise the label disappears when size of the text is bigger than the original
-            NSString *prestring = [NSString stringWithFormat:@"%d",[[self.schemeArray objectAtIndex:row+gmSize] intValue]];
+            NSString *prestring = [NSString stringWithFormat:@"%d",(NSInteger) cellSlider.value];
             while ([prestring length] <= 4)
                 prestring = [NSString stringWithFormat:@" %@",prestring];
             cell.detailTextLabel.text = prestring;
@@ -246,7 +246,7 @@
                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                                reuseIdentifier:CellIdentifier2] autorelease];
                 UISwitch *onOff = [[UISwitch alloc] init];
-                onOff.tag = row;
+                onOff.tag = SWITCH_TAG+row;
                 [onOff addTarget:self action:@selector(toggleSwitch:) forControlEvents:UIControlEventValueChanged];
                 cell.accessoryView = onOff;
                 [onOff release];
@@ -260,7 +260,7 @@
             [cell.imageView.layer setMasksToBounds:YES];
             cell.textLabel.text = [[self.gameModifierArray objectAtIndex:row] objectForKey:@"title"];
             cell.detailTextLabel.text = [[self.gameModifierArray objectAtIndex:row] objectForKey:@"description"];
-            [(UISwitch *)cell.accessoryView setOn:[[self.schemeArray objectAtIndex:row] boolValue] animated:NO];
+            [(UISwitch *)cell.accessoryView setOn:[[[self.schemeDictionary objectForKey:@"gamemod"] objectAtIndex:row] boolValue] animated:NO];
 
             cell.selectionStyle = UITableViewCellSelectionStyleNone;
         }
@@ -270,24 +270,24 @@
 
 -(void) toggleSwitch:(id) sender {
     UISwitch *theSwitch = (UISwitch *)sender;
-    [self.schemeArray replaceObjectAtIndex:theSwitch.tag withObject:[NSNumber numberWithBool:theSwitch.on]];
+    NSMutableArray *array = [self.schemeDictionary objectForKey:@"gamemod"];
+    [array replaceObjectAtIndex:theSwitch.tag-SWITCH_TAG withObject:[NSNumber numberWithBool:theSwitch.on]];
 }
 
 -(void) sliderChanged:(id) sender {
-    // need to offset this section (see format in CommodityFunctions.m and above)
-    NSInteger gmSize = [self.gameModifierArray count];
     // the slider that changed is sent as object
     UISlider *theSlider = (UISlider *)sender;
     // create the indexPath of the row of the slider
-    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:theSlider.tag-gmSize inSection:1];
+    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:theSlider.tag-SLIDER_TAG inSection:1];
     // get its cell
     UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
     // grab the associated label
     UILabel *label = (UILabel *)cell.detailTextLabel;
     // modify it
-    label.text = [NSString stringWithFormat:@"%d",(int)theSlider.value];
-    // save changes in the main array (remember that you need to offset it)
-    [self.schemeArray replaceObjectAtIndex:theSlider.tag withObject:[NSNumber numberWithInt:(int)theSlider.value]];
+    label.text = [NSString stringWithFormat:@"%d",(NSInteger) theSlider.value];
+    // save changes in the main array
+    NSMutableArray *array = [self.schemeDictionary objectForKey:@"basic"];
+    [array replaceObjectAtIndex:theSlider.tag-SLIDER_TAG withObject:[NSNumber numberWithInt:(NSInteger) theSlider.value]];
 }
 
 #pragma mark -
@@ -303,7 +303,7 @@
             [editableCell replyKeyboard];
             break;
         case 1:
-            cellSlider = (UISlider *)[cell.contentView viewWithTag:[indexPath row]+[self.gameModifierArray count]];
+            cellSlider = (UISlider *)[cell.contentView viewWithTag:[indexPath row]+SLIDER_TAG];
             [cellSlider setValue:[[[self.basicSettingList objectAtIndex:[indexPath row]] objectForKey:@"default"] floatValue] animated:YES];
             [self sliderChanged:cellSlider];
             //cell.detailTextLabel.text = [[[self.basicSettingList objectAtIndex:[indexPath row]] objectForKey:@"default"] stringValue];
@@ -343,11 +343,13 @@
 #pragma mark Memory management
 -(void) didReceiveMemoryWarning {
     [super didReceiveMemoryWarning];
+    self.basicSettingList = nil;
+    self.gameModifierArray = nil;
 }
 
 -(void) viewDidUnload {
     self.schemeName = nil;
-    self.schemeArray = nil;
+    self.schemeDictionary = nil;
     self.basicSettingList = nil;
     self.gameModifierArray = nil;
     MSG_DIDUNLOAD();
@@ -356,7 +358,7 @@
 
 -(void) dealloc {
     [schemeName release];
-    [schemeArray release];
+    [schemeDictionary release];
     [basicSettingList release];
     [gameModifierArray release];
     [super dealloc];