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