cocoaTouch/FortsViewController.m
author koda
Mon, 19 Apr 2010 01:39:55 +0000
changeset 3357 3836a31879e7
parent 3352 ac5d14a35482
child 3374 0d522416d97f
permissions -rw-r--r--
draw a hedgehog when presenting hats other lazy loading procedure fix crash when loading multiple games
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
     1
//
3352
ac5d14a35482 complete previews on the team settings
koda
parents: 3335
diff changeset
     2
//  FortsViewController.m
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
     3
//  HedgewarsMobile
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
     4
//
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
     5
//  Created by Vittorio on 08/04/10.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
     6
//  Copyright 2010 __MyCompanyName__. All rights reserved.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
     7
//
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
     8
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
     9
#import "FortsViewController.h"
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    10
#import "CommodityFunctions.h"
3352
ac5d14a35482 complete previews on the team settings
koda
parents: 3335
diff changeset
    11
#import "UIImageExtra.h"
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    12
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    13
@implementation FortsViewController
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    14
@synthesize teamDictionary, fortArray, fortSprites, lastIndexPath;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    15
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    16
3335
2520ee7a5484 new manager for handling (future) rotations
koda
parents: 3325
diff changeset
    17
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
2520ee7a5484 new manager for handling (future) rotations
koda
parents: 3325
diff changeset
    18
	return rotationManager(interfaceOrientation);
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    19
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    20
3335
2520ee7a5484 new manager for handling (future) rotations
koda
parents: 3325
diff changeset
    21
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    22
#pragma mark -
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    23
#pragma mark View lifecycle
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    24
- (void)viewDidLoad {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    25
    [super viewDidLoad];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    26
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    27
    NSString *fortsDirectory = FORTS_DIRECTORY();
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    28
    NSArray *directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: fortsDirectory error:NULL];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    29
    NSMutableArray *filteredContents = [[NSMutableArray alloc] initWithCapacity:([directoryContents count] / 2)];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    30
    // we need to remove the double entries and the L.png suffix
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    31
    for (int i = 0; i < [directoryContents count]; i++) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    32
        if (i % 2) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    33
            NSString *currentName = [directoryContents objectAtIndex:i];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    34
            NSString *correctName = [currentName substringToIndex:([currentName length] - 5)];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    35
            [filteredContents addObject:correctName];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    36
        } 
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    37
    }
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    38
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    39
    self.fortArray = filteredContents;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    40
    [filteredContents release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    41
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    42
    //NSLog(@"%@",fortArray);
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    43
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    44
    // this creates a scaled down version of the image
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    45
    NSMutableArray *spriteArray = [[NSMutableArray alloc] initWithCapacity:[fortArray count]];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    46
    for (NSString *fortName in fortArray) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    47
        NSString *fortFile = [[NSString alloc] initWithFormat:@"%@/%@L.png", fortsDirectory, fortName];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    48
        UIImage *fortSprite = [[UIImage alloc] initWithContentsOfFile:fortFile];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    49
        [fortFile release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    50
        [spriteArray addObject:[fortSprite scaleToSize:CGSizeMake(196,196)]];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    51
        [fortSprite release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    52
    }
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    53
    self.fortSprites = spriteArray;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    54
    [spriteArray release];
3357
3836a31879e7 draw a hedgehog when presenting hats
koda
parents: 3352
diff changeset
    55
    
3836a31879e7 draw a hedgehog when presenting hats
koda
parents: 3352
diff changeset
    56
    // statically set row height instead of using delegate method for performance reasons
3836a31879e7 draw a hedgehog when presenting hats
koda
parents: 3352
diff changeset
    57
    self.tableView.rowHeight = 200;
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    58
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    59
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    60
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    61
- (void)viewWillAppear:(BOOL)animated {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    62
    [super viewWillAppear:animated];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    63
    [self.tableView reloadData];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    64
    [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    65
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    66
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    67
/*
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    68
- (void)viewDidAppear:(BOOL)animated {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    69
    [super viewDidAppear:animated];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    70
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    71
*/
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    72
/*
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    73
- (void)viewWillDisappear:(BOOL)animated {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    74
    [super viewWillDisappear:animated];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    75
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    76
*/
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    77
/*
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    78
- (void)viewDidDisappear:(BOOL)animated {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    79
    [super viewDidDisappear:animated];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    80
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    81
*/
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    82
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    83
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    84
#pragma mark -
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    85
#pragma mark Table view data source
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    86
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    87
    return 1;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    88
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    89
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    90
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    91
    return [fortArray count];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    92
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    93
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    94
// Customize the appearance of table view cells.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    95
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    96
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    97
    static NSString *CellIdentifier = @"Cell";
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    98
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    99
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   100
    if (cell == nil) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   101
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   102
                                       reuseIdentifier:CellIdentifier] autorelease];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   103
    }
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   104
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   105
    cell.imageView.image = [fortSprites objectAtIndex:[indexPath row]];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   106
    cell.textLabel.text = [fortArray objectAtIndex:[indexPath row]];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   107
    cell.detailTextLabel.text = @"Insert funny description here";
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   108
    if ([cell.textLabel.text isEqualToString:[self.teamDictionary objectForKey:@"fort"]]) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   109
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   110
        self.lastIndexPath = indexPath;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   111
    } else {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   112
        cell.accessoryType = UITableViewCellAccessoryNone;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   113
    }
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   114
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   115
    return cell;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   116
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   117
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   118
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   119
/*
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   120
// Override to support conditional editing of the table view.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   121
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   122
    // Return NO if you do not want the specified item to be editable.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   123
    return YES;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   124
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   125
*/
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   126
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   127
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   128
/*
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   129
// Override to support editing the table view.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   130
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   131
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   132
    if (editingStyle == UITableViewCellEditingStyleDelete) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   133
        // Delete the row from the data source
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   134
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   135
    }   
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   136
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   137
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   138
    }   
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   139
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   140
*/
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   141
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   142
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   143
/*
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   144
// Override to support rearranging the table view.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   145
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   146
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   147
*/
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   148
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   149
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   150
/*
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   151
// Override to support conditional rearranging of the table view.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   152
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   153
    // Return NO if you do not want the item to be re-orderable.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   154
    return YES;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   155
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   156
*/
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   157
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   158
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   159
#pragma mark -
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   160
#pragma mark Table view delegate
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   161
-(void) tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   162
    int newRow = [indexPath row];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   163
    int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   164
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   165
    if (newRow != oldRow) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   166
        // if the two selected rows differ update data on the hog dictionary and reload table content
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   167
        [self.teamDictionary setValue:[fortArray objectAtIndex:newRow] forKey:@"fort"];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   168
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   169
        // tell our boss to write this new stuff on disk
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   170
        [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   171
        [self.tableView reloadData];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   172
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   173
        self.lastIndexPath = indexPath;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   174
        [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   175
    }
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   176
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   177
    [self.navigationController popViewControllerAnimated:YES];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   178
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   179
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   180
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   181
#pragma mark -
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   182
#pragma mark Memory management
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   183
-(void) didReceiveMemoryWarning {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   184
    // Releases the view if it doesn't have a superview.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   185
    [super didReceiveMemoryWarning];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   186
    // Relinquish ownership any cached data, images, etc that aren't in use.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   187
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   188
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   189
-(void) viewDidUnload {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   190
    self.teamDictionary = nil;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   191
    self.lastIndexPath = nil;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   192
    self.fortArray = nil;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   193
    self.fortSprites = nil;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   194
    [super viewDidUnload];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   195
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   196
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   197
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   198
- (void)dealloc {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   199
    [teamDictionary release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   200
    [lastIndexPath release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   201
    [fortArray release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   202
    [fortSprites release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   203
    [super dealloc];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   204
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   205
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   206
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   207
@end
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   208