project_files/HedgewarsMobile/Classes/HogHatViewController.m
changeset 3697 d5b30d6373fc
parent 3662 a44406f4369b
child 3829 81db3c85784b
equal deleted inserted replaced
3695:c11abf387a7d 3697:d5b30d6373fc
    26 
    26 
    27     // load all the hat file names and store them into hatArray
    27     // load all the hat file names and store them into hatArray
    28     NSString *hatsDirectory = HATS_DIRECTORY();
    28     NSString *hatsDirectory = HATS_DIRECTORY();
    29     NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:hatsDirectory error:NULL];
    29     NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:hatsDirectory error:NULL];
    30     self.hatArray = array;
    30     self.hatArray = array;
    31     
    31 
    32     // load the base hog image, drawing will occure in cellForRow...
    32     // load the base hog image, drawing will occure in cellForRow...
    33     NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/Hedgehog.png",GRAPHICS_DIRECTORY()];
    33     NSString *normalHogFile = [[NSString alloc] initWithFormat:@"%@/Hedgehog.png",GRAPHICS_DIRECTORY()];
    34     UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:normalHogFile andCutAt:CGRectMake(96, 0, 32, 32)];
    34     UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:normalHogFile andCutAt:CGRectMake(96, 0, 32, 32)];
    35     [normalHogFile release];
    35     [normalHogFile release];
    36     self.normalHogSprite = hogSprite;
    36     self.normalHogSprite = hogSprite;
    39     self.title = NSLocalizedString(@"Change hedgehog's hat",@"");
    39     self.title = NSLocalizedString(@"Change hedgehog's hat",@"");
    40 }
    40 }
    41 
    41 
    42 - (void)viewWillAppear:(BOOL)animated {
    42 - (void)viewWillAppear:(BOOL)animated {
    43     [super viewWillAppear:animated];
    43     [super viewWillAppear:animated];
    44     
    44 
    45     // this updates the hog name and its hat
    45     // this updates the hog name and its hat
    46     [self.tableView reloadData];
    46     [self.tableView reloadData];
    47     // this moves the tableview to the top
    47     // this moves the tableview to the top
    48     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    48     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    49 }
    49 }
    59     return [self.hatArray count];
    59     return [self.hatArray count];
    60 }
    60 }
    61 
    61 
    62 // Customize the appearance of table view cells.
    62 // Customize the appearance of table view cells.
    63 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    63 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    64     
    64 
    65     static NSString *CellIdentifier = @"Cell";
    65     static NSString *CellIdentifier = @"Cell";
    66     
    66 
    67     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    67     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    68     if (cell == nil) 
    68     if (cell == nil)
    69         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    69         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    70     
    70 
    71     NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog];
    71     NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog];
    72     NSString *hat = [hatArray objectAtIndex:[indexPath row]];
    72     NSString *hat = [hatArray objectAtIndex:[indexPath row]];
    73     cell.textLabel.text = [hat stringByDeletingPathExtension];
    73     cell.textLabel.text = [hat stringByDeletingPathExtension];
    74     
    74 
    75     NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", HATS_DIRECTORY(), hat];
    75     NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", HATS_DIRECTORY(), hat];
    76     UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)];
    76     UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)];
    77     [hatFile release];
    77     [hatFile release];
    78     cell.imageView.image = [self.normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, -5)];
    78     cell.imageView.image = [self.normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, -5)];
    79     [hatSprite release];
    79     [hatSprite release];
    80         
    80 
    81     if ([hat isEqualToString:[hog objectForKey:@"hat"]]) {
    81     if ([hat isEqualToString:[hog objectForKey:@"hat"]]) {
    82         cell.accessoryType = UITableViewCellAccessoryCheckmark;
    82         cell.accessoryType = UITableViewCellAccessoryCheckmark;
    83         self.lastIndexPath = indexPath;
    83         self.lastIndexPath = indexPath;
    84     } else {
    84     } else {
    85         cell.accessoryType = UITableViewCellAccessoryNone;
    85         cell.accessoryType = UITableViewCellAccessoryNone;
    92 #pragma mark -
    92 #pragma mark -
    93 #pragma mark Table view delegate
    93 #pragma mark Table view delegate
    94 - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    94 - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    95     int newRow = [indexPath row];
    95     int newRow = [indexPath row];
    96     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
    96     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
    97     
    97 
    98     if (newRow != oldRow) {
    98     if (newRow != oldRow) {
    99         // if the two selected rows differ update data on the hog dictionary and reload table content
    99         // if the two selected rows differ update data on the hog dictionary and reload table content
   100         // TODO: maybe this section could be cleaned up
   100         // TODO: maybe this section could be cleaned up
   101         NSDictionary *oldHog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog];
   101         NSDictionary *oldHog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog];
   102         
   102 
   103         NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary: oldHog];
   103         NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary: oldHog];
   104         [newHog setObject:[[hatArray objectAtIndex:newRow] stringByDeletingPathExtension] forKey:@"hat"];
   104         [newHog setObject:[[hatArray objectAtIndex:newRow] stringByDeletingPathExtension] forKey:@"hat"];
   105         [[teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:selectedHog withObject:newHog];
   105         [[teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:selectedHog withObject:newHog];
   106         [newHog release];
   106         [newHog release];
   107         
   107 
   108         // tell our boss to write this new stuff on disk
   108         // tell our boss to write this new stuff on disk
   109         [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
   109         [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
   110         
   110 
   111         UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath];
   111         UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath];
   112         newCell.accessoryType = UITableViewCellAccessoryCheckmark;
   112         newCell.accessoryType = UITableViewCellAccessoryCheckmark;
   113         UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath];
   113         UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath];
   114         oldCell.accessoryType = UITableViewCellAccessoryNone;
   114         oldCell.accessoryType = UITableViewCellAccessoryNone;
   115         self.lastIndexPath = indexPath;
   115         self.lastIndexPath = indexPath;
   116         [aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
   116         [aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
   117     } 
   117     }
   118     [aTableView deselectRowAtIndexPath:indexPath animated:YES];
   118     [aTableView deselectRowAtIndexPath:indexPath animated:YES];
   119     [self.navigationController popViewControllerAnimated:YES];
   119     [self.navigationController popViewControllerAnimated:YES];
   120 }
   120 }
   121 
   121 
   122 
   122