project_files/HedgewarsMobile/Classes/LevelViewController.m
changeset 8441 a00b0fa0dbd7
parent 6832 fae8fd118da9
child 10108 c68cf030eded
equal deleted inserted replaced
8440:ea4d6a7a2937 8441:a00b0fa0dbd7
    46     self.title = NSLocalizedString(@"Set difficulty level",@"");
    46     self.title = NSLocalizedString(@"Set difficulty level",@"");
    47 }
    47 }
    48 
    48 
    49 -(void) viewWillAppear:(BOOL)animated {
    49 -(void) viewWillAppear:(BOOL)animated {
    50     [super viewWillAppear:animated];
    50     [super viewWillAppear:animated];
    51     
    51 
    52     if ([[[[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:0] objectForKey:@"level"] intValue] == 0)
    52     if ([[[[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:0] objectForKey:@"level"] intValue] == 0)
    53         numberOfSections = 1;
    53         numberOfSections = 1;
    54     else
    54     else
    55         numberOfSections = 2;
    55         numberOfSections = 2;
    56     
    56 
    57     [self.tableView reloadData];
    57     [self.tableView reloadData];
    58     // this moves the tableview to the top
    58     // this moves the tableview to the top
    59     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    59     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    60 }
    60 }
    61 
    61 
    62 -(void) viewWillDisappear:(BOOL)animated {
    62 -(void) viewWillDisappear:(BOOL)animated {
    63  // stuff like checking that at least 1 field was selected   
    63  // stuff like checking that at least 1 field was selected
    64 }
    64 }
    65 
    65 
    66 #pragma mark -
    66 #pragma mark -
    67 #pragma mark Table view data source
    67 #pragma mark Table view data source
    68 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    68 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    82     static NSString *CellIdentifier1 = @"Cell1";
    82     static NSString *CellIdentifier1 = @"Cell1";
    83 
    83 
    84     NSInteger row = [indexPath row];
    84     NSInteger row = [indexPath row];
    85     NSInteger section = [indexPath section];
    85     NSInteger section = [indexPath section];
    86     UITableViewCell *cell;
    86     UITableViewCell *cell;
    87     
    87 
    88     if (section == 0) {
    88     if (section == 0) {
    89         cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier0];
    89         cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier0];
    90         if (cell == nil) {
    90         if (cell == nil) {
    91             cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier0] autorelease];
    91             cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier0] autorelease];
    92             UISwitch *theSwitch = [[UISwitch alloc] init];
    92             UISwitch *theSwitch = [[UISwitch alloc] init];
   102         cell.textLabel.text = NSLocalizedString(@"Hogs controlled by AI",@"");
   102         cell.textLabel.text = NSLocalizedString(@"Hogs controlled by AI",@"");
   103     } else {
   103     } else {
   104         cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
   104         cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
   105         if (cell == nil)
   105         if (cell == nil)
   106             cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
   106             cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
   107         
   107 
   108         cell.textLabel.text = [levelArray objectAtIndex:row];
   108         cell.textLabel.text = [levelArray objectAtIndex:row];
   109         NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:0];
   109         NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:0];
   110         if ([[hog objectForKey:@"level"] intValue] == row+1) {
   110         if ([[hog objectForKey:@"level"] intValue] == row+1) {
   111             cell.accessoryType = UITableViewCellAccessoryCheckmark;
   111             cell.accessoryType = UITableViewCellAccessoryCheckmark;
   112             self.lastIndexPath = indexPath;
   112             self.lastIndexPath = indexPath;
   113         } else {
   113         } else {
   114             cell.accessoryType = UITableViewCellAccessoryNone;
   114             cell.accessoryType = UITableViewCellAccessoryNone;
   115         }
   115         }
   116         
   116 
   117         NSString *botlevelPath = [[NSString alloc] initWithFormat:@"%@/bot%d.png",[[NSBundle mainBundle] resourcePath],row+1];
   117         NSString *botlevelPath = [[NSString alloc] initWithFormat:@"%@/bot%d.png",[[NSBundle mainBundle] resourcePath],row+1];
   118         UIImage *levelImage = [[UIImage alloc] initWithContentsOfFile:botlevelPath];
   118         UIImage *levelImage = [[UIImage alloc] initWithContentsOfFile:botlevelPath];
   119         [botlevelPath release];
   119         [botlevelPath release];
   120         cell.imageView.image = levelImage;
   120         cell.imageView.image = levelImage;
   121         [levelImage release];
   121         [levelImage release];
   122     }
   122     }
   123     
   123 
   124     return cell;
   124     return cell;
   125 }
   125 }
   126 
   126 
   127 -(void) switchValueChanged:(id) sender {
   127 -(void) switchValueChanged:(id) sender {
   128     UISwitch *theSwitch = (UISwitch *)sender;
   128     UISwitch *theSwitch = (UISwitch *)sender;
   129     NSIndexSet *sections = [[NSIndexSet alloc] initWithIndex:1];
   129     NSIndexSet *sections = [[NSIndexSet alloc] initWithIndex:1];
   130     NSMutableArray *hogs = [self.teamDictionary objectForKey:@"hedgehogs"];
   130     NSMutableArray *hogs = [self.teamDictionary objectForKey:@"hedgehogs"];
   131     NSInteger level;
   131     NSInteger level;
   132     
   132 
   133     if (theSwitch.on) {
   133     if (theSwitch.on) {
   134         numberOfSections = 2;
   134         numberOfSections = 2;
   135         [self.tableView insertSections:sections withRowAnimation:UITableViewRowAnimationFade];
   135         [self.tableView insertSections:sections withRowAnimation:UITableViewRowAnimationFade];
   136         level = 1 + (random() % ([levelArray count] - 1));
   136         level = 1 + (random() % ([levelArray count] - 1));
   137     } else {
   137     } else {
   154 #pragma mark Table view delegate
   154 #pragma mark Table view delegate
   155 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   155 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   156     int newRow = [indexPath row];
   156     int newRow = [indexPath row];
   157     int oldRow = (self.lastIndexPath != nil) ? [self.lastIndexPath row] : -1;
   157     int oldRow = (self.lastIndexPath != nil) ? [self.lastIndexPath row] : -1;
   158 
   158 
   159     if ([indexPath section] != 0) { 
   159     if ([indexPath section] != 0) {
   160         if (newRow != oldRow) {
   160         if (newRow != oldRow) {
   161             NSMutableArray *hogs = [self.teamDictionary objectForKey:@"hedgehogs"];
   161             NSMutableArray *hogs = [self.teamDictionary objectForKey:@"hedgehogs"];
   162             
   162 
   163             NSInteger level = newRow + 1;
   163             NSInteger level = newRow + 1;
   164             for (NSMutableDictionary *hog in hogs)
   164             for (NSMutableDictionary *hog in hogs)
   165                 [hog setObject:[NSNumber numberWithInt:level] forKey:@"level"];
   165                 [hog setObject:[NSNumber numberWithInt:level] forKey:@"level"];
   166             DLog(@"New level is %d",level);
   166             DLog(@"New level is %d",level);
   167             
   167 
   168             // tell our boss to write this new stuff on disk
   168             // tell our boss to write this new stuff on disk
   169             [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
   169             [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
   170             [self.tableView reloadData];
   170             [self.tableView reloadData];
   171             
   171 
   172             self.lastIndexPath = indexPath;
   172             self.lastIndexPath = indexPath;
   173             [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
   173             [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
   174         }
   174         }
   175     }
   175     }
   176     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
   176     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];