project_files/HedgewarsMobile/Classes/FortsViewController.m
author smaxx
Wed, 04 Aug 2010 17:35:45 +0200
changeset 3724 b8678667e7dc
parent 3697 d5b30d6373fc
child 3789 c3eb56754e92
permissions -rw-r--r--
Engine: * Added Lua's table module
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
//  FortsViewController.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 08/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 "FortsViewController.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 FortsViewController
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    14
@synthesize teamDictionary, fortArray, 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
    NSArray *directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:FORTS_DIRECTORY() error:NULL];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    28
    NSMutableArray *filteredContents = [[NSMutableArray alloc] initWithCapacity:([directoryContents count] / 2)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    29
    // we need to remove the double entries and the L.png suffix
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    30
    for (int i = 0; i < [directoryContents count]; i++) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    31
        if (i % 2) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    32
            NSString *currentName = [directoryContents objectAtIndex:i];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    33
            NSString *correctName = [currentName substringToIndex:([currentName length] - 5)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    34
            [filteredContents addObject:correctName];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3663
diff changeset
    35
        }
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    36
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    37
    self.fortArray = filteredContents;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    38
    [filteredContents release];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3663
diff changeset
    39
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    40
    /*
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    41
    // this creates a scaled down version of the image
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    42
    NSMutableArray *spriteArray = [[NSMutableArray alloc] initWithCapacity:[fortArray count]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    43
    for (NSString *fortName in fortArray) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    44
        NSString *fortFile = [[NSString alloc] initWithFormat:@"%@/%@L.png", fortsDirectory, fortName];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    45
        UIImage *fortSprite = [[UIImage alloc] initWithContentsOfFile:fortFile];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    46
        [fortFile release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    47
        [spriteArray addObject:[fortSprite scaleToSize:CGSizeMake(196,196)]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    48
        [fortSprite release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    50
    self.fortSprites = spriteArray;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    51
    [spriteArray release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
    */
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3663
diff changeset
    53
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    54
    // statically set row height instead of using delegate method for performance reasons
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
    self.tableView.rowHeight = 200;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3663
diff changeset
    56
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
    57
    self.title = NSLocalizedString(@"Choose team fort",@"");
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    58
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    59
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    60
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    61
- (void)viewWillAppear:(BOOL)animated {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    62
    [super viewWillAppear:animated];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    63
    [self.tableView reloadData];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    64
    [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    65
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    66
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    67
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    68
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    69
#pragma mark Table view data source
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    70
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    71
    return 1;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    72
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    73
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    74
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    75
    return [self.fortArray count];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    76
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    77
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    78
// Customize the appearance of table view cells.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    79
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    80
    static NSString *CellIdentifier = @"Cell";
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3663
diff changeset
    81
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    82
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    83
    if (cell == nil) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    84
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    85
                                       reuseIdentifier:CellIdentifier] autorelease];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    86
    }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3663
diff changeset
    87
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    88
    NSString *fortName = [fortArray objectAtIndex:[indexPath row]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    89
    cell.textLabel.text = fortName;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3663
diff changeset
    90
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    91
    // this creates a scaled down version of the image
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    92
    // TODO: create preview files, scaling is way too slow!
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    93
    NSString *fortFile = [[NSString alloc] initWithFormat:@"%@/%@L.png", FORTS_DIRECTORY(), fortName];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    94
    UIImage *fortSprite = [[UIImage alloc] initWithContentsOfFile:fortFile];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    95
    [fortFile release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    96
    cell.imageView.image = [fortSprite scaleToSize:CGSizeMake(196,196)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    97
    [fortSprite release];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3663
diff changeset
    98
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    99
    cell.detailTextLabel.text = @"Insert funny description here";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   100
    if ([cell.textLabel.text isEqualToString:[self.teamDictionary objectForKey:@"fort"]]) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   101
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   102
        self.lastIndexPath = indexPath;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   103
    } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   104
        cell.accessoryType = UITableViewCellAccessoryNone;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   105
    }
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3663
diff changeset
   106
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   107
    return cell;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   108
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   109
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   110
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   111
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   112
#pragma mark Table view delegate
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   113
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   114
    int newRow = [indexPath row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   115
    int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3663
diff changeset
   116
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   117
    if (newRow != oldRow) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   118
        // if the two selected rows differ update data on the hog dictionary and reload table content
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   119
        [self.teamDictionary setValue:[fortArray objectAtIndex:newRow] forKey:@"fort"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   120
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   121
        // tell our boss to write this new stuff on disk
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   122
        [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3663
diff changeset
   123
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   124
        UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   125
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   126
        UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   127
        oldCell.accessoryType = UITableViewCellAccessoryNone;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   128
        self.lastIndexPath = indexPath;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   129
        [aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   130
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   131
    [aTableView deselectRowAtIndexPath:indexPath animated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   132
    [self.navigationController popViewControllerAnimated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   133
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   134
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   135
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   136
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   137
#pragma mark Memory management
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   138
-(void) didReceiveMemoryWarning {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   139
    // Releases the view if it doesn't have a superview.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   140
    [super didReceiveMemoryWarning];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   141
    // Relinquish ownership any cached data, images, etc that aren't in use.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   142
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   143
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   144
-(void) viewDidUnload {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   145
    self.teamDictionary = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   146
    self.lastIndexPath = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   147
    self.fortArray = 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
   148
    MSG_DIDUNLOAD();
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   149
    [super viewDidUnload];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   150
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   151
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   152
3663
8c28abf427f5 reduce the number of keywords used and switch to BMP format for screenshots
koda
parents: 3662
diff changeset
   153
-(void) dealloc {
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   154
    [teamDictionary release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   155
    [lastIndexPath release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   156
    [fortArray release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   157
//    [fortSprites release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   158
    [super dealloc];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   159
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   160
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   161
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   162
@end
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   163