project_files/HedgewarsMobile/Classes/HogHatViewController.m
branchios-revival
changeset 11183 b2112ed988cb
parent 11182 55f20efae2e4
child 12872 00215a7ec5f5
equal deleted inserted replaced
11182:55f20efae2e4 11183:b2112ed988cb
    75 
    75 
    76     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    76     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    77     if (cell == nil)
    77     if (cell == nil)
    78         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    78         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    79 
    79 
    80     NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:self.selectedHog];
       
    81     NSString *hat = [self.hatArray objectAtIndex:[indexPath row]];
    80     NSString *hat = [self.hatArray objectAtIndex:[indexPath row]];
    82     cell.textLabel.text = [hat stringByDeletingPathExtension];
    81     cell.textLabel.text = [hat stringByDeletingPathExtension];
    83 
    82 
    84     NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", HATS_DIRECTORY(), hat];
    83     NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", HATS_DIRECTORY(), hat];
    85     UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)];
    84     UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)];
    86     [hatFile release];
    85     [hatFile release];
    87     cell.imageView.image = [self.normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, 5)];
    86     cell.imageView.image = [self.normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, 5)];
    88     [hatSprite release];
    87     [hatSprite release];
    89 
    88 
       
    89     NSDictionary *hog = (self.selectedHog != -1) ? [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:self.selectedHog] : nil;
    90     if ([[hat stringByDeletingPathExtension] isEqualToString:[hog objectForKey:@"hat"]]) {
    90     if ([[hat stringByDeletingPathExtension] isEqualToString:[hog objectForKey:@"hat"]]) {
    91         cell.accessoryType = UITableViewCellAccessoryCheckmark;
    91         cell.accessoryType = UITableViewCellAccessoryCheckmark;
    92     } else {
    92     } else {
    93         cell.accessoryType = UITableViewCellAccessoryNone;
    93         cell.accessoryType = UITableViewCellAccessoryNone;
    94     }
    94     }
   100 #pragma mark -
   100 #pragma mark -
   101 #pragma mark Table view delegate
   101 #pragma mark Table view delegate
   102 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
   102 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
   103 {
   103 {
   104     NSInteger selectedRow = [indexPath row];
   104     NSInteger selectedRow = [indexPath row];
       
   105     NSString *newHat = [[self.hatArray objectAtIndex:selectedRow] stringByDeletingPathExtension];
   105     
   106     
   106     // update data on the hog dictionary
   107     // update data on the hogs dictionary
   107     NSDictionary *oldHog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:self.selectedHog];
   108     if (self.selectedHog != -1)
   108 
   109     {
   109     NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary:oldHog];
   110         // update only selected hog with new hat
   110     [newHog setObject:[[self.hatArray objectAtIndex:selectedRow] stringByDeletingPathExtension] forKey:@"hat"];
   111         [self updateTeamDictionaryWithNewHat:newHat forStartHogIndex:self.selectedHog toEndHogIndex:self.selectedHog];
   111     [[self.teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:self.selectedHog withObject:newHog];
   112     }
   112     [newHog release];
   113     else
       
   114     {
       
   115         // update all hogs with new hat
       
   116         NSInteger startIndex = 0;
       
   117         NSInteger endIndex = [[self.teamDictionary objectForKey:@"hedgehogs"] count] - 1;
       
   118         [self updateTeamDictionaryWithNewHat:newHat forStartHogIndex:startIndex toEndHogIndex:endIndex];
       
   119     }
   113 
   120 
   114     // tell our boss to write this new stuff on disk
   121     // tell our boss to write this new stuff on disk
   115     [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
   122     [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
   116     
   123     
   117     [self.navigationController popViewControllerAnimated:YES];
   124     [self.navigationController popViewControllerAnimated:YES];
   118 }
   125 }
   119 
   126 
       
   127 - (void)updateTeamDictionaryWithNewHat:(NSString *)newHat forStartHogIndex:(NSInteger)startIndex toEndHogIndex:(NSInteger)endIndex
       
   128 {
       
   129     NSMutableArray *hogsArray = [self.teamDictionary objectForKey:@"hedgehogs"];
       
   130     
       
   131     for (NSInteger i=startIndex; i <= endIndex; i++)
       
   132     {
       
   133         NSDictionary *oldHog = [hogsArray objectAtIndex:i];
       
   134         NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary:oldHog];
       
   135         [newHog setObject:newHat forKey:@"hat"];
       
   136         [hogsArray replaceObjectAtIndex:i withObject:newHog];
       
   137         [newHog release];
       
   138     }
       
   139 }
   120 
   140 
   121 #pragma mark -
   141 #pragma mark -
   122 #pragma mark Memory management
   142 #pragma mark Memory management
   123 -(void) didReceiveMemoryWarning {
   143 -(void) didReceiveMemoryWarning {
   124     MSG_MEMCLEAN();
   144     MSG_MEMCLEAN();