project_files/HedgewarsMobile/Classes/SingleSchemeViewController.m
changeset 3697 d5b30d6373fc
parent 3662 a44406f4369b
child 3752 73c2d7d5643b
equal deleted inserted replaced
3695:c11abf387a7d 3697:d5b30d6373fc
    79                       NSLocalizedString(@"Disable land objects when generating maps",@""),@"description",
    79                       NSLocalizedString(@"Disable land objects when generating maps",@""),@"description",
    80                       @"DisableLandObjects",@"image",nil],
    80                       @"DisableLandObjects",@"image",nil],
    81                      nil];
    81                      nil];
    82     self.gameModifierArray = mods;
    82     self.gameModifierArray = mods;
    83     [mods release];
    83     [mods release];
    84     
    84 
    85     NSArray *basicSettings = [[NSArray alloc] initWithObjects:
    85     NSArray *basicSettings = [[NSArray alloc] initWithObjects:
    86                               [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Damage Modifier",@""),@"title",@"Damage",@"image",
    86                               [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Damage Modifier",@""),@"title",@"Damage",@"image",
    87                                [NSNumber numberWithInt:100],@"default",[NSNumber numberWithInt:10],@"min",[NSNumber numberWithInt:300],@"max",nil],
    87                                [NSNumber numberWithInt:100],@"default",[NSNumber numberWithInt:10],@"min",[NSNumber numberWithInt:300],@"max",nil],
    88                               [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Turn Time",@""),@"title",@"Time",@"image",
    88                               [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Turn Time",@""),@"title",@"Time",@"image",
    89                                [NSNumber numberWithInt:45],@"default",[NSNumber numberWithInt:1],@"min",[NSNumber numberWithInt:99],@"max",nil],
    89                                [NSNumber numberWithInt:45],@"default",[NSNumber numberWithInt:1],@"min",[NSNumber numberWithInt:99],@"max",nil],
   102                               [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Explosives",@""),@"title",@"Damage",@"image",
   102                               [NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Explosives",@""),@"title",@"Damage",@"image",
   103                                [NSNumber numberWithInt:2],@"default",[NSNumber numberWithInt:0],@"min",[NSNumber numberWithInt:40],@"max",nil],
   103                                [NSNumber numberWithInt:2],@"default",[NSNumber numberWithInt:0],@"min",[NSNumber numberWithInt:40],@"max",nil],
   104                               nil];
   104                               nil];
   105     self.basicSettingList = basicSettings;
   105     self.basicSettingList = basicSettings;
   106     [basicSettings release];
   106     [basicSettings release];
   107     
   107 
   108     self.title = NSLocalizedString(@"Edit scheme preferences",@"");
   108     self.title = NSLocalizedString(@"Edit scheme preferences",@"");
   109 }
   109 }
   110 
   110 
   111 // load from file
   111 // load from file
   112 -(void) viewWillAppear:(BOOL) animated {
   112 -(void) viewWillAppear:(BOOL) animated {
   113     [super viewWillAppear:animated];
   113     [super viewWillAppear:animated];
   114     
   114 
   115     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName];
   115     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName];
   116     NSMutableArray *scheme = [[NSMutableArray alloc] initWithContentsOfFile:schemeFile];
   116     NSMutableArray *scheme = [[NSMutableArray alloc] initWithContentsOfFile:schemeFile];
   117     [schemeFile release];
   117     [schemeFile release];
   118     self.schemeArray = scheme;
   118     self.schemeArray = scheme;
   119     [scheme release];
   119     [scheme release];
   120     
   120 
   121     [self.tableView reloadData];
   121     [self.tableView reloadData];
   122 }
   122 }
   123 
   123 
   124 // save to file
   124 // save to file
   125 -(void) viewWillDisappear:(BOOL) animated {
   125 -(void) viewWillDisappear:(BOOL) animated {
   126     [super viewWillDisappear:animated];
   126     [super viewWillDisappear:animated];
   127     
   127 
   128     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName];
   128     NSString *schemeFile = [[NSString alloc] initWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName];
   129     [self.schemeArray writeToFile:schemeFile atomically:YES];
   129     [self.schemeArray writeToFile:schemeFile atomically:YES];
   130     [schemeFile release];
   130     [schemeFile release];
   131 }
   131 }
   132 
   132 
   133 #pragma mark -
   133 #pragma mark -
   134 #pragma mark editableCellView delegate
   134 #pragma mark editableCellView delegate
   135 // set the new value
   135 // set the new value
   136 -(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue {    
   136 -(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue {
   137     // delete old file
   137     // delete old file
   138     [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName] error:NULL];
   138     [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.plist",SCHEMES_DIRECTORY(),self.schemeName] error:NULL];
   139     // update filename
   139     // update filename
   140     self.schemeName = textString;
   140     self.schemeName = textString;
   141     // save new file
   141     // save new file
   166 
   166 
   167 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   167 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   168     static NSString *CellIdentifier0 = @"Cell0";
   168     static NSString *CellIdentifier0 = @"Cell0";
   169     static NSString *CellIdentifier1 = @"Cell1";
   169     static NSString *CellIdentifier1 = @"Cell1";
   170     static NSString *CellIdentifier2 = @"Cell2";
   170     static NSString *CellIdentifier2 = @"Cell2";
   171     
   171 
   172     UITableViewCell *cell = nil;
   172     UITableViewCell *cell = nil;
   173     EditableCellView *editableCell = nil;
   173     EditableCellView *editableCell = nil;
   174     NSInteger row = [indexPath row];
   174     NSInteger row = [indexPath row];
   175     
   175 
   176     switch ([indexPath section]) {
   176     switch ([indexPath section]) {
   177         case 0:
   177         case 0:
   178             editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier0];
   178             editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier0];
   179             if (editableCell == nil) {
   179             if (editableCell == nil) {
   180                 editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault 
   180                 editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault
   181                                                reuseIdentifier:CellIdentifier0] autorelease];
   181                                                reuseIdentifier:CellIdentifier0] autorelease];
   182                 editableCell.delegate = self;
   182                 editableCell.delegate = self;
   183             }
   183             }
   184             
   184 
   185             editableCell.textField.text = self.schemeName;
   185             editableCell.textField.text = self.schemeName;
   186             editableCell.detailTextLabel.text = nil;
   186             editableCell.detailTextLabel.text = nil;
   187             editableCell.imageView.image = nil;
   187             editableCell.imageView.image = nil;
   188             editableCell.selectionStyle = UITableViewCellSelectionStyleNone;
   188             editableCell.selectionStyle = UITableViewCellSelectionStyleNone;
   189             cell = editableCell;
   189             cell = editableCell;
   192             cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier1];
   192             cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier1];
   193             NSDictionary *detail = [self.basicSettingList objectAtIndex:row];
   193             NSDictionary *detail = [self.basicSettingList objectAtIndex:row];
   194             // need to offset this section (see format in CommodityFunctions.m and above)
   194             // need to offset this section (see format in CommodityFunctions.m and above)
   195             NSInteger gmSize = [self.gameModifierArray count];
   195             NSInteger gmSize = [self.gameModifierArray count];
   196             if (cell == nil) {
   196             if (cell == nil) {
   197                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 
   197                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
   198                                                reuseIdentifier:CellIdentifier1] autorelease];
   198                                                reuseIdentifier:CellIdentifier1] autorelease];
   199                 
   199 
   200                 int offset = 0;
   200                 int offset = 0;
   201                 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
   201                 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
   202                     offset = 50;
   202                     offset = 50;
   203                 
   203 
   204                 UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(offset+260, 12, offset+150, 23)];
   204                 UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(offset+260, 12, offset+150, 23)];
   205                 slider.maximumValue = [[detail objectForKey:@"max"] floatValue];
   205                 slider.maximumValue = [[detail objectForKey:@"max"] floatValue];
   206                 slider.minimumValue = [[detail objectForKey:@"min"] floatValue];
   206                 slider.minimumValue = [[detail objectForKey:@"min"] floatValue];
   207                 slider.tag = row+gmSize;
   207                 slider.tag = row+gmSize;
   208                 [slider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
   208                 [slider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
   209                 [cell.contentView addSubview:slider];
   209                 [cell.contentView addSubview:slider];
   210                 [slider release];
   210                 [slider release];
   211                 
   211 
   212                 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 7, 200, 30)];
   212                 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 7, 200, 30)];
   213                 label.tag = LABEL_TAG;
   213                 label.tag = LABEL_TAG;
   214                 label.backgroundColor = [UIColor clearColor];
   214                 label.backgroundColor = [UIColor clearColor];
   215                 label.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]];
   215                 label.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]];
   216                 [cell.contentView addSubview:label];
   216                 [cell.contentView addSubview:label];
   217                 [label release];
   217                 [label release];
   218             }
   218             }
   219             
   219 
   220             UIImage *img = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/icon%@.png",BTN_DIRECTORY(),[[self.basicSettingList objectAtIndex:row] objectForKey:@"image"]]];
   220             UIImage *img = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/icon%@.png",BTN_DIRECTORY(),[[self.basicSettingList objectAtIndex:row] objectForKey:@"image"]]];
   221             cell.imageView.image = img;
   221             cell.imageView.image = img;
   222             [img release];
   222             [img release];
   223             
   223 
   224             UILabel *cellLabel = (UILabel *)[cell.contentView viewWithTag:LABEL_TAG];
   224             UILabel *cellLabel = (UILabel *)[cell.contentView viewWithTag:LABEL_TAG];
   225             cellLabel.text = [[self.basicSettingList objectAtIndex:row] objectForKey:@"title"];
   225             cellLabel.text = [[self.basicSettingList objectAtIndex:row] objectForKey:@"title"];
   226             
   226 
   227             UISlider *cellSlider = (UISlider *)[cell.contentView viewWithTag:row+gmSize];
   227             UISlider *cellSlider = (UISlider *)[cell.contentView viewWithTag:row+gmSize];
   228             cellSlider.value = [[self.schemeArray objectAtIndex:row+gmSize] floatValue];
   228             cellSlider.value = [[self.schemeArray objectAtIndex:row+gmSize] floatValue];
   229             
   229 
   230             // forced to use this weird format otherwise the label disappears when size of the text is bigger than the original
   230             // forced to use this weird format otherwise the label disappears when size of the text is bigger than the original
   231             NSString *prestring = [NSString stringWithFormat:@"%d",[[self.schemeArray objectAtIndex:row+gmSize] intValue]];
   231             NSString *prestring = [NSString stringWithFormat:@"%d",[[self.schemeArray objectAtIndex:row+gmSize] intValue]];
   232             while ([prestring length] <= 4)
   232             while ([prestring length] <= 4)
   233                 prestring = [NSString stringWithFormat:@" %@",prestring];
   233                 prestring = [NSString stringWithFormat:@" %@",prestring];
   234             cell.detailTextLabel.text = prestring;
   234             cell.detailTextLabel.text = prestring;
   235             
   235 
   236             cell.selectionStyle = UITableViewCellSelectionStyleBlue;
   236             cell.selectionStyle = UITableViewCellSelectionStyleBlue;
   237             break;
   237             break;
   238         case 2:
   238         case 2:
   239             cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier2];
   239             cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier2];
   240             if (cell == nil) {
   240             if (cell == nil) {
   244                 onOff.tag = row;
   244                 onOff.tag = row;
   245                 [onOff addTarget:self action:@selector(toggleSwitch:) forControlEvents:UIControlEventValueChanged];
   245                 [onOff addTarget:self action:@selector(toggleSwitch:) forControlEvents:UIControlEventValueChanged];
   246                 cell.accessoryView = onOff;
   246                 cell.accessoryView = onOff;
   247                 [onOff release];
   247                 [onOff release];
   248             }
   248             }
   249             
   249 
   250             UIImage *image = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/btn%@.png",BTN_DIRECTORY(),[[self.gameModifierArray objectAtIndex:row] objectForKey:@"image"]]];
   250             UIImage *image = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/btn%@.png",BTN_DIRECTORY(),[[self.gameModifierArray objectAtIndex:row] objectForKey:@"image"]]];
   251             cell.imageView.image = image;
   251             cell.imageView.image = image;
   252             [image release];
   252             [image release];
   253             [cell.imageView.layer setCornerRadius:7.0f];
   253             [cell.imageView.layer setCornerRadius:7.0f];
   254             [cell.imageView.layer setBorderWidth:1];
   254             [cell.imageView.layer setBorderWidth:1];
   255             [cell.imageView.layer setMasksToBounds:YES];
   255             [cell.imageView.layer setMasksToBounds:YES];
   256             cell.textLabel.text = [[self.gameModifierArray objectAtIndex:row] objectForKey:@"title"];
   256             cell.textLabel.text = [[self.gameModifierArray objectAtIndex:row] objectForKey:@"title"];
   257             cell.detailTextLabel.text = [[self.gameModifierArray objectAtIndex:row] objectForKey:@"description"];
   257             cell.detailTextLabel.text = [[self.gameModifierArray objectAtIndex:row] objectForKey:@"description"];
   258             [(UISwitch *)cell.accessoryView setOn:[[self.schemeArray objectAtIndex:row] boolValue] animated:NO];
   258             [(UISwitch *)cell.accessoryView setOn:[[self.schemeArray objectAtIndex:row] boolValue] animated:NO];
   259             
   259 
   260             cell.selectionStyle = UITableViewCellSelectionStyleNone;
   260             cell.selectionStyle = UITableViewCellSelectionStyleNone;
   261         }
   261         }
   262     
   262 
   263     return cell;
   263     return cell;
   264 }
   264 }
   265 
   265 
   266 -(void) toggleSwitch:(id) sender {
   266 -(void) toggleSwitch:(id) sender {
   267     UISwitch *theSwitch = (UISwitch *)sender;
   267     UISwitch *theSwitch = (UISwitch *)sender;
   289 #pragma mark Table view delegate
   289 #pragma mark Table view delegate
   290 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   290 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   291     UITableViewCell *cell = [aTableView cellForRowAtIndexPath:indexPath];
   291     UITableViewCell *cell = [aTableView cellForRowAtIndexPath:indexPath];
   292     EditableCellView *editableCell = nil;
   292     EditableCellView *editableCell = nil;
   293     UISlider *cellSlider = nil;
   293     UISlider *cellSlider = nil;
   294     
   294 
   295     switch ([indexPath section]) {
   295     switch ([indexPath section]) {
   296         case 0:
   296         case 0:
   297             editableCell = (EditableCellView *)cell;
   297             editableCell = (EditableCellView *)cell;
   298             [editableCell replyKeyboard];
   298             [editableCell replyKeyboard];
   299             break;
   299             break;
   309             [self toggleSwitch:sw];*/
   309             [self toggleSwitch:sw];*/
   310             break;
   310             break;
   311         default:
   311         default:
   312             break;
   312             break;
   313     }
   313     }
   314     
   314 
   315     [aTableView deselectRowAtIndexPath:indexPath animated:YES];
   315     [aTableView deselectRowAtIndexPath:indexPath animated:YES];
   316 }
   316 }
   317 
   317 
   318 -(NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section {
   318 -(NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section {
   319     NSString *sectionTitle = nil;
   319     NSString *sectionTitle = nil;