project_files/HedgewarsMobile/Classes/GravesViewController.m
changeset 3546 ccf4854df294
parent 3514 59dbd31e9953
child 3547 02875b1145b7
equal deleted inserted replaced
3545:b07ee704f35d 3546:ccf4854df294
     1 //
       
     2 //  GravesViewController.m
       
     3 //  HedgewarsMobile
       
     4 //
       
     5 //  Created by Vittorio on 02/04/10.
       
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
       
     7 //
       
     8 
       
     9 #import "GravesViewController.h"
       
    10 #import "CommodityFunctions.h"
       
    11 #import "UIImageExtra.h"
       
    12 
       
    13 @implementation GravesViewController
       
    14 @synthesize teamDictionary, graveArray, lastIndexPath;
       
    15 
       
    16 
       
    17 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
       
    18 	return rotationManager(interfaceOrientation);
       
    19 }
       
    20 
       
    21 
       
    22 #pragma mark -
       
    23 #pragma mark View lifecycle
       
    24 -(void) viewDidLoad {
       
    25     [super viewDidLoad];
       
    26 
       
    27     // load all the grave names and store them into graveArray
       
    28     self.graveArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:GRAVES_DIRECTORY() error:NULL];
       
    29 }
       
    30 
       
    31 -(void) viewWillAppear:(BOOL)animated {
       
    32     [super viewWillAppear:animated];
       
    33     [self.tableView reloadData];
       
    34     // this moves the tableview to the top
       
    35     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
       
    36 }
       
    37 
       
    38 
       
    39 #pragma mark -
       
    40 #pragma mark Table view data source
       
    41 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
       
    42     return 1;
       
    43 }
       
    44 
       
    45 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
       
    46     return [self.graveArray count];
       
    47 }
       
    48 
       
    49 // Customize the appearance of table view cells.
       
    50 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       
    51     static NSString *CellIdentifier = @"Cell";
       
    52     
       
    53     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
       
    54     if (cell == nil)
       
    55         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
       
    56     
       
    57     NSString *grave = [self.graveArray objectAtIndex:[indexPath row]];
       
    58     cell.textLabel.text = [grave stringByDeletingPathExtension];
       
    59     
       
    60     if ([grave isEqualToString:[self.teamDictionary objectForKey:@"grave"]]) {
       
    61         cell.accessoryType = UITableViewCellAccessoryCheckmark;
       
    62         self.lastIndexPath = indexPath;
       
    63     } else {
       
    64         cell.accessoryType = UITableViewCellAccessoryNone;
       
    65     }
       
    66 
       
    67     NSString *graveFilePath = [[NSString alloc] initWithFormat:@"%@/%@",GRAVES_DIRECTORY(),grave];
       
    68     // because we also have multi frame graves, let's take the first one only
       
    69     UIImage *graveSprite = [[UIImage alloc] initWithContentsOfFile:graveFilePath andCutAt:CGRectMake(0, 0, 32, 32)];
       
    70     [graveFilePath release];        
       
    71     cell.imageView.image = graveSprite;
       
    72     [graveSprite release];
       
    73     
       
    74     return cell;
       
    75 }
       
    76 
       
    77 
       
    78 #pragma mark -
       
    79 #pragma mark Table view delegate
       
    80 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       
    81     int newRow = [indexPath row];
       
    82     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
       
    83     
       
    84     if (newRow != oldRow) {
       
    85         [teamDictionary setObject:[[graveArray objectAtIndex:newRow] stringByDeletingPathExtension] forKey:@"grave"];
       
    86         
       
    87         // tell our boss to write this new stuff on disk
       
    88         [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
       
    89         
       
    90         UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath];
       
    91         newCell.accessoryType = UITableViewCellAccessoryCheckmark;
       
    92         UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath];
       
    93         oldCell.accessoryType = UITableViewCellAccessoryNone;
       
    94         self.lastIndexPath = indexPath;
       
    95         [aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
       
    96     }
       
    97     [aTableView deselectRowAtIndexPath:indexPath animated:YES];
       
    98     [self.navigationController popViewControllerAnimated:YES];
       
    99 }
       
   100 
       
   101 
       
   102 #pragma mark -
       
   103 #pragma mark Memory management
       
   104 - (void)didReceiveMemoryWarning {
       
   105     // Releases the view if it doesn't have a superview.
       
   106     [super didReceiveMemoryWarning];
       
   107     // Relinquish ownership any cached data, images, etc that aren't in use.
       
   108 }
       
   109 
       
   110 - (void)viewDidUnload {
       
   111     self.lastIndexPath = nil;
       
   112     self.teamDictionary = nil;
       
   113     self.graveArray = nil;
       
   114     [super viewDidUnload];
       
   115     MSG_DIDUNLOAD();
       
   116 }
       
   117 
       
   118 - (void)dealloc {
       
   119     [graveArray release];
       
   120     [teamDictionary release];
       
   121     [lastIndexPath release];
       
   122     [super dealloc];
       
   123 }
       
   124 
       
   125 
       
   126 @end
       
   127