project_files/HedgewarsMobile/Classes/SingleSchemeViewController.m
changeset 3781 2bfda544ae48
parent 3753 bb5a0252fb56
child 3782 dc3531e49e4c
equal deleted inserted replaced
3780:7c704e69242e 3781:2bfda544ae48
    11 #import "CommodityFunctions.h"
    11 #import "CommodityFunctions.h"
    12 #import "UIImageExtra.h"
    12 #import "UIImageExtra.h"
    13 
    13 
    14 #define LABEL_TAG  12345
    14 #define LABEL_TAG  12345
    15 #define SLIDER_TAG 54321
    15 #define SLIDER_TAG 54321
       
    16 #define SWITCH_TAG 67890
    16 
    17 
    17 @implementation SingleSchemeViewController
    18 @implementation SingleSchemeViewController
    18 @synthesize schemeName, schemeArray, basicSettingList, gameModifierArray;
    19 @synthesize schemeName, schemeDictionary, basicSettingList, gameModifierArray;
    19 
    20 
    20 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    21 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    21     return rotationManager(interfaceOrientation);
    22     return rotationManager(interfaceOrientation);
    22 }
    23 }
    23 
    24 
   116 // load from file
   117 // load from file
   117 -(void) viewWillAppear:(BOOL) animated {
   118 -(void) viewWillAppear:(BOOL) animated {
   118     [super viewWillAppear:animated];
   119     [super viewWillAppear:animated];
   119 
   120 
   120     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName];
   121     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName];
   121     NSMutableArray *scheme = [[NSMutableArray alloc] initWithContentsOfFile:schemeFile];
   122     NSMutableDictionary *scheme = [[NSMutableDictionary alloc] initWithContentsOfFile:schemeFile];
   122     [schemeFile release];
   123     [schemeFile release];
   123     self.schemeArray = scheme;
   124     self.schemeDictionary = scheme;
   124     [scheme release];
   125     [scheme release];
   125 
   126 
   126     [self.tableView reloadData];
   127     [self.tableView reloadData];
   127 }
   128 }
   128 
   129 
   129 // save to file
   130 // save to file
   130 -(void) viewWillDisappear:(BOOL) animated {
   131 -(void) viewWillDisappear:(BOOL) animated {
   131     [super viewWillDisappear:animated];
   132     [super viewWillDisappear:animated];
   132 
   133 
   133     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName];
   134     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName];
   134     [self.schemeArray writeToFile:schemeFile atomically:YES];
   135     [self.schemeDictionary writeToFile:schemeFile atomically:YES];
   135     [schemeFile release];
   136     [schemeFile release];
   136 }
   137 }
   137 
   138 
   138 #pragma mark -
   139 #pragma mark -
   139 #pragma mark editableCellView delegate
   140 #pragma mark editableCellView delegate
   142     // delete old file
   143     // delete old file
   143     [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName] error:NULL];
   144     [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName] error:NULL];
   144     // update filename
   145     // update filename
   145     self.schemeName = textString;
   146     self.schemeName = textString;
   146     // save new file
   147     // save new file
   147     [self.schemeArray writeToFile:[NSString stringWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName] atomically:YES];
   148     [self.schemeDictionary writeToFile:[NSString stringWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName] atomically:YES];
   148 }
   149 }
   149 
   150 
   150 #pragma mark -
   151 #pragma mark -
   151 #pragma mark Table view data source
   152 #pragma mark Table view data source
   152 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   153 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   195             break;
   196             break;
   196         case 1:
   197         case 1:
   197             cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier1];
   198             cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier1];
   198             NSDictionary *detail = [self.basicSettingList objectAtIndex:row];
   199             NSDictionary *detail = [self.basicSettingList objectAtIndex:row];
   199             // need to offset this section (see format in CommodityFunctions.m and above)
   200             // need to offset this section (see format in CommodityFunctions.m and above)
   200             NSInteger gmSize = [self.gameModifierArray count];
       
   201             if (cell == nil) {
   201             if (cell == nil) {
   202                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
   202                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
   203                                                reuseIdentifier:CellIdentifier1] autorelease];
   203                                                reuseIdentifier:CellIdentifier1] autorelease];
   204 
   204 
   205                 int offset = 0;
   205                 int offset = 0;
   207                     offset = 50;
   207                     offset = 50;
   208 
   208 
   209                 UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(offset+260, 12, offset+150, 23)];
   209                 UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(offset+260, 12, offset+150, 23)];
   210                 slider.maximumValue = [[detail objectForKey:@"max"] floatValue];
   210                 slider.maximumValue = [[detail objectForKey:@"max"] floatValue];
   211                 slider.minimumValue = [[detail objectForKey:@"min"] floatValue];
   211                 slider.minimumValue = [[detail objectForKey:@"min"] floatValue];
   212                 slider.tag = row+gmSize;
   212                 slider.tag = SLIDER_TAG+row;
   213                 [slider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
   213                 [slider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
   214                 [cell.contentView addSubview:slider];
   214                 [cell.contentView addSubview:slider];
   215                 [slider release];
   215                 [slider release];
   216 
   216 
   217                 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 7, 200, 30)];
   217                 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 7, 200, 30)];
   227             [img release];
   227             [img release];
   228 
   228 
   229             UILabel *cellLabel = (UILabel *)[cell.contentView viewWithTag:LABEL_TAG];
   229             UILabel *cellLabel = (UILabel *)[cell.contentView viewWithTag:LABEL_TAG];
   230             cellLabel.text = [[self.basicSettingList objectAtIndex:row] objectForKey:@"title"];
   230             cellLabel.text = [[self.basicSettingList objectAtIndex:row] objectForKey:@"title"];
   231 
   231 
   232             UISlider *cellSlider = (UISlider *)[cell.contentView viewWithTag:row+gmSize];
   232             UISlider *cellSlider = (UISlider *)[cell.contentView viewWithTag:SLIDER_TAG+row];
   233             cellSlider.value = [[self.schemeArray objectAtIndex:row+gmSize] floatValue];
   233             cellSlider.value = [[[self.schemeDictionary objectForKey:@"basic"] objectAtIndex:row] floatValue];
   234 
   234 
   235             // forced to use this weird format otherwise the label disappears when size of the text is bigger than the original
   235             // forced to use this weird format otherwise the label disappears when size of the text is bigger than the original
   236             NSString *prestring = [NSString stringWithFormat:@"%d",[[self.schemeArray objectAtIndex:row+gmSize] intValue]];
   236             NSString *prestring = [NSString stringWithFormat:@"%d",(NSInteger) cellSlider.value];
   237             while ([prestring length] <= 4)
   237             while ([prestring length] <= 4)
   238                 prestring = [NSString stringWithFormat:@" %@",prestring];
   238                 prestring = [NSString stringWithFormat:@" %@",prestring];
   239             cell.detailTextLabel.text = prestring;
   239             cell.detailTextLabel.text = prestring;
   240 
   240 
   241             cell.selectionStyle = UITableViewCellSelectionStyleBlue;
   241             cell.selectionStyle = UITableViewCellSelectionStyleBlue;
   244             cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier2];
   244             cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier2];
   245             if (cell == nil) {
   245             if (cell == nil) {
   246                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
   246                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
   247                                                reuseIdentifier:CellIdentifier2] autorelease];
   247                                                reuseIdentifier:CellIdentifier2] autorelease];
   248                 UISwitch *onOff = [[UISwitch alloc] init];
   248                 UISwitch *onOff = [[UISwitch alloc] init];
   249                 onOff.tag = row;
   249                 onOff.tag = SWITCH_TAG+row;
   250                 [onOff addTarget:self action:@selector(toggleSwitch:) forControlEvents:UIControlEventValueChanged];
   250                 [onOff addTarget:self action:@selector(toggleSwitch:) forControlEvents:UIControlEventValueChanged];
   251                 cell.accessoryView = onOff;
   251                 cell.accessoryView = onOff;
   252                 [onOff release];
   252                 [onOff release];
   253             }
   253             }
   254 
   254 
   258             [cell.imageView.layer setCornerRadius:7.0f];
   258             [cell.imageView.layer setCornerRadius:7.0f];
   259             [cell.imageView.layer setBorderWidth:1];
   259             [cell.imageView.layer setBorderWidth:1];
   260             [cell.imageView.layer setMasksToBounds:YES];
   260             [cell.imageView.layer setMasksToBounds:YES];
   261             cell.textLabel.text = [[self.gameModifierArray objectAtIndex:row] objectForKey:@"title"];
   261             cell.textLabel.text = [[self.gameModifierArray objectAtIndex:row] objectForKey:@"title"];
   262             cell.detailTextLabel.text = [[self.gameModifierArray objectAtIndex:row] objectForKey:@"description"];
   262             cell.detailTextLabel.text = [[self.gameModifierArray objectAtIndex:row] objectForKey:@"description"];
   263             [(UISwitch *)cell.accessoryView setOn:[[self.schemeArray objectAtIndex:row] boolValue] animated:NO];
   263             [(UISwitch *)cell.accessoryView setOn:[[[self.schemeDictionary objectForKey:@"gamemod"] objectAtIndex:row] boolValue] animated:NO];
   264 
   264 
   265             cell.selectionStyle = UITableViewCellSelectionStyleNone;
   265             cell.selectionStyle = UITableViewCellSelectionStyleNone;
   266         }
   266         }
   267 
   267 
   268     return cell;
   268     return cell;
   269 }
   269 }
   270 
   270 
   271 -(void) toggleSwitch:(id) sender {
   271 -(void) toggleSwitch:(id) sender {
   272     UISwitch *theSwitch = (UISwitch *)sender;
   272     UISwitch *theSwitch = (UISwitch *)sender;
   273     [self.schemeArray replaceObjectAtIndex:theSwitch.tag withObject:[NSNumber numberWithBool:theSwitch.on]];
   273     NSMutableArray *array = [self.schemeDictionary objectForKey:@"gamemod"];
       
   274     [array replaceObjectAtIndex:theSwitch.tag-SWITCH_TAG withObject:[NSNumber numberWithBool:theSwitch.on]];
   274 }
   275 }
   275 
   276 
   276 -(void) sliderChanged:(id) sender {
   277 -(void) sliderChanged:(id) sender {
   277     // need to offset this section (see format in CommodityFunctions.m and above)
       
   278     NSInteger gmSize = [self.gameModifierArray count];
       
   279     // the slider that changed is sent as object
   278     // the slider that changed is sent as object
   280     UISlider *theSlider = (UISlider *)sender;
   279     UISlider *theSlider = (UISlider *)sender;
   281     // create the indexPath of the row of the slider
   280     // create the indexPath of the row of the slider
   282     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:theSlider.tag-gmSize inSection:1];
   281     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:theSlider.tag-SLIDER_TAG inSection:1];
   283     // get its cell
   282     // get its cell
   284     UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
   283     UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
   285     // grab the associated label
   284     // grab the associated label
   286     UILabel *label = (UILabel *)cell.detailTextLabel;
   285     UILabel *label = (UILabel *)cell.detailTextLabel;
   287     // modify it
   286     // modify it
   288     label.text = [NSString stringWithFormat:@"%d",(int)theSlider.value];
   287     label.text = [NSString stringWithFormat:@"%d",(NSInteger) theSlider.value];
   289     // save changes in the main array (remember that you need to offset it)
   288     // save changes in the main array
   290     [self.schemeArray replaceObjectAtIndex:theSlider.tag withObject:[NSNumber numberWithInt:(int)theSlider.value]];
   289     NSMutableArray *array = [self.schemeDictionary objectForKey:@"basic"];
       
   290     [array replaceObjectAtIndex:theSlider.tag-SLIDER_TAG withObject:[NSNumber numberWithInt:(NSInteger) theSlider.value]];
   291 }
   291 }
   292 
   292 
   293 #pragma mark -
   293 #pragma mark -
   294 #pragma mark Table view delegate
   294 #pragma mark Table view delegate
   295 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   295 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   301         case 0:
   301         case 0:
   302             editableCell = (EditableCellView *)cell;
   302             editableCell = (EditableCellView *)cell;
   303             [editableCell replyKeyboard];
   303             [editableCell replyKeyboard];
   304             break;
   304             break;
   305         case 1:
   305         case 1:
   306             cellSlider = (UISlider *)[cell.contentView viewWithTag:[indexPath row]+[self.gameModifierArray count]];
   306             cellSlider = (UISlider *)[cell.contentView viewWithTag:[indexPath row]+SLIDER_TAG];
   307             [cellSlider setValue:[[[self.basicSettingList objectAtIndex:[indexPath row]] objectForKey:@"default"] floatValue] animated:YES];
   307             [cellSlider setValue:[[[self.basicSettingList objectAtIndex:[indexPath row]] objectForKey:@"default"] floatValue] animated:YES];
   308             [self sliderChanged:cellSlider];
   308             [self sliderChanged:cellSlider];
   309             //cell.detailTextLabel.text = [[[self.basicSettingList objectAtIndex:[indexPath row]] objectForKey:@"default"] stringValue];
   309             //cell.detailTextLabel.text = [[[self.basicSettingList objectAtIndex:[indexPath row]] objectForKey:@"default"] stringValue];
   310             break;
   310             break;
   311         case 2:
   311         case 2:
   341 
   341 
   342 #pragma mark -
   342 #pragma mark -
   343 #pragma mark Memory management
   343 #pragma mark Memory management
   344 -(void) didReceiveMemoryWarning {
   344 -(void) didReceiveMemoryWarning {
   345     [super didReceiveMemoryWarning];
   345     [super didReceiveMemoryWarning];
       
   346     self.basicSettingList = nil;
       
   347     self.gameModifierArray = nil;
   346 }
   348 }
   347 
   349 
   348 -(void) viewDidUnload {
   350 -(void) viewDidUnload {
   349     self.schemeName = nil;
   351     self.schemeName = nil;
   350     self.schemeArray = nil;
   352     self.schemeDictionary = nil;
   351     self.basicSettingList = nil;
   353     self.basicSettingList = nil;
   352     self.gameModifierArray = nil;
   354     self.gameModifierArray = nil;
   353     MSG_DIDUNLOAD();
   355     MSG_DIDUNLOAD();
   354     [super viewDidUnload];
   356     [super viewDidUnload];
   355 }
   357 }
   356 
   358 
   357 -(void) dealloc {
   359 -(void) dealloc {
   358     [schemeName release];
   360     [schemeName release];
   359     [schemeArray release];
   361     [schemeDictionary release];
   360     [basicSettingList release];
   362     [basicSettingList release];
   361     [gameModifierArray release];
   363     [gameModifierArray release];
   362     [super dealloc];
   364     [super dealloc];
   363 }
   365 }
   364 
   366