project_files/HedgewarsMobile/Classes/HogHatViewController.m
branchios-revival
changeset 11182 55f20efae2e4
parent 11148 064a53861759
child 11183 b2112ed988cb
equal deleted inserted replaced
11181:a515139fc8de 11182:55f20efae2e4
    19 
    19 
    20 #import "HogHatViewController.h"
    20 #import "HogHatViewController.h"
    21 
    21 
    22 
    22 
    23 @implementation HogHatViewController
    23 @implementation HogHatViewController
    24 @synthesize teamDictionary, hatArray, normalHogSprite, lastIndexPath, selectedHog;
    24 @synthesize teamDictionary, hatArray, normalHogSprite, selectedHog;
    25 
    25 
    26 
    26 
    27 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    27 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    28     return rotationManager(interfaceOrientation);
    28     return rotationManager(interfaceOrientation);
    29 }
    29 }
    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:selectedHog];
    80     NSDictionary *hog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:self.selectedHog];
    81     NSString *hat = [hatArray objectAtIndex:[indexPath row]];
    81     NSString *hat = [self.hatArray objectAtIndex:[indexPath row]];
    82     cell.textLabel.text = [hat stringByDeletingPathExtension];
    82     cell.textLabel.text = [hat stringByDeletingPathExtension];
    83 
    83 
    84     NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", HATS_DIRECTORY(), hat];
    84     NSString *hatFile = [[NSString alloc] initWithFormat:@"%@/%@", HATS_DIRECTORY(), hat];
    85     UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)];
    85     UIImage *hatSprite = [[UIImage alloc] initWithContentsOfFile: hatFile andCutAt:CGRectMake(0, 0, 32, 32)];
    86     [hatFile release];
    86     [hatFile release];
    87     cell.imageView.image = [self.normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, 5)];
    87     cell.imageView.image = [self.normalHogSprite mergeWith:hatSprite atPoint:CGPointMake(0, 5)];
    88     [hatSprite release];
    88     [hatSprite release];
    89 
    89 
    90     if ([hat isEqualToString:[hog objectForKey:@"hat"]]) {
    90     if ([[hat stringByDeletingPathExtension] isEqualToString:[hog objectForKey:@"hat"]]) {
    91         cell.accessoryType = UITableViewCellAccessoryCheckmark;
    91         cell.accessoryType = UITableViewCellAccessoryCheckmark;
    92         self.lastIndexPath = indexPath;
       
    93     } else {
    92     } else {
    94         cell.accessoryType = UITableViewCellAccessoryNone;
    93         cell.accessoryType = UITableViewCellAccessoryNone;
    95     }
    94     }
    96 
    95 
    97     return cell;
    96     return cell;
    98 }
    97 }
    99 
    98 
   100 
    99 
   101 #pragma mark -
   100 #pragma mark -
   102 #pragma mark Table view delegate
   101 #pragma mark Table view delegate
   103 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   102 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
   104     NSInteger newRow = [indexPath row];
   103 {
   105     NSInteger oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
   104     NSInteger selectedRow = [indexPath row];
       
   105     
       
   106     // update data on the hog dictionary
       
   107     NSDictionary *oldHog = [[self.teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:self.selectedHog];
   106 
   108 
   107     if (newRow != oldRow) {
   109     NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary:oldHog];
   108         // if the two selected rows differ update data on the hog dictionary and reload table content
   110     [newHog setObject:[[self.hatArray objectAtIndex:selectedRow] stringByDeletingPathExtension] forKey:@"hat"];
   109         // TODO: maybe this section could be cleaned up
   111     [[self.teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:self.selectedHog withObject:newHog];
   110         NSDictionary *oldHog = [[teamDictionary objectForKey:@"hedgehogs"] objectAtIndex:selectedHog];
   112     [newHog release];
   111 
   113 
   112         NSMutableDictionary *newHog = [[NSMutableDictionary alloc] initWithDictionary: oldHog];
   114     // tell our boss to write this new stuff on disk
   113         [newHog setObject:[[hatArray objectAtIndex:newRow] stringByDeletingPathExtension] forKey:@"hat"];
   115     [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
   114         [[teamDictionary objectForKey:@"hedgehogs"] replaceObjectAtIndex:selectedHog withObject:newHog];
   116     
   115         [newHog release];
       
   116 
       
   117         // tell our boss to write this new stuff on disk
       
   118         [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
       
   119 
       
   120         UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath];
       
   121         newCell.accessoryType = UITableViewCellAccessoryCheckmark;
       
   122         UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath];
       
   123         oldCell.accessoryType = UITableViewCellAccessoryNone;
       
   124         self.lastIndexPath = indexPath;
       
   125         [aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
       
   126     }
       
   127     [aTableView deselectRowAtIndexPath:indexPath animated:YES];
       
   128     [self.navigationController popViewControllerAnimated:YES];
   117     [self.navigationController popViewControllerAnimated:YES];
   129 }
   118 }
   130 
   119 
   131 
   120 
   132 #pragma mark -
   121 #pragma mark -
   133 #pragma mark Memory management
   122 #pragma mark Memory management
   134 -(void) didReceiveMemoryWarning {
   123 -(void) didReceiveMemoryWarning {
   135     self.lastIndexPath = nil;
       
   136     MSG_MEMCLEAN();
   124     MSG_MEMCLEAN();
   137     [super didReceiveMemoryWarning];
   125     [super didReceiveMemoryWarning];
   138 }
   126 }
   139 
   127 
   140 -(void) viewDidUnload {
   128 -(void) viewDidUnload {
   141     self.lastIndexPath = nil;
       
   142     self.normalHogSprite = nil;
   129     self.normalHogSprite = nil;
   143     self.teamDictionary = nil;
   130     self.teamDictionary = nil;
   144     self.hatArray = nil;
   131     self.hatArray = nil;
   145     MSG_DIDUNLOAD();
   132     MSG_DIDUNLOAD();
   146     [super viewDidUnload];
   133     [super viewDidUnload];
   148 
   135 
   149 -(void) dealloc {
   136 -(void) dealloc {
   150     releaseAndNil(hatArray);
   137     releaseAndNil(hatArray);
   151     releaseAndNil(teamDictionary);
   138     releaseAndNil(teamDictionary);
   152     releaseAndNil(normalHogSprite);
   139     releaseAndNil(normalHogSprite);
   153     releaseAndNil(lastIndexPath);
       
   154     [super dealloc];
   140     [super dealloc];
   155 }
   141 }
   156 
   142 
   157 
   143 
   158 @end
   144 @end