cocoaTouch/FortsViewController.m
author koda
Tue, 13 Apr 2010 22:45:46 +0000
changeset 3340 96dd168b080b
parent 3335 2520ee7a5484
child 3352 ac5d14a35482
permissions -rw-r--r--
fix all of what was committed before
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
     1
//
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
     2
//  FlagsViewController.m
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"
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    11
#import "UIImageScale.h"
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];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    55
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    56
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    57
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    58
- (void)viewWillAppear:(BOOL)animated {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    59
    [super viewWillAppear:animated];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    60
    [self.tableView reloadData];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    61
    [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    62
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    63
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    64
/*
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    65
- (void)viewDidAppear:(BOOL)animated {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    66
    [super viewDidAppear:animated];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    67
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    68
*/
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    69
/*
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    70
- (void)viewWillDisappear:(BOOL)animated {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    71
    [super viewWillDisappear:animated];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    72
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    73
*/
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    74
/*
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    75
- (void)viewDidDisappear:(BOOL)animated {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    76
    [super viewDidDisappear:animated];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    77
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    78
*/
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    79
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    80
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    81
#pragma mark -
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    82
#pragma mark Table view data source
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    83
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    84
    return 1;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    85
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    86
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    87
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    88
    return [fortArray count];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    89
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    90
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    91
// Customize the appearance of table view cells.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    92
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    93
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    94
    static NSString *CellIdentifier = @"Cell";
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    95
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    96
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    97
    if (cell == nil) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    98
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    99
                                       reuseIdentifier:CellIdentifier] autorelease];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   100
    }
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   101
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   102
    cell.imageView.image = [fortSprites objectAtIndex:[indexPath row]];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   103
    cell.textLabel.text = [fortArray objectAtIndex:[indexPath row]];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   104
    cell.detailTextLabel.text = @"Insert funny description here";
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   105
    if ([cell.textLabel.text isEqualToString:[self.teamDictionary objectForKey:@"fort"]]) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   106
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   107
        self.lastIndexPath = indexPath;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   108
    } else {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   109
        cell.accessoryType = UITableViewCellAccessoryNone;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   110
    }
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   111
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   112
    return cell;
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
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   116
/*
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   117
// Override to support conditional editing of the table view.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   118
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   119
    // Return NO if you do not want the specified item to be editable.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   120
    return YES;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   121
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   122
*/
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   123
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
// Override to support editing the table view.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   127
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   128
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   129
    if (editingStyle == UITableViewCellEditingStyleDelete) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   130
        // Delete the row from the data source
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   131
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   132
    }   
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   133
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   134
        // 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
   135
    }   
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   136
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   137
*/
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
// Override to support rearranging the table view.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   142
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   143
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   144
*/
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   145
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
// Override to support conditional rearranging of the table view.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   149
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   150
    // Return NO if you do not want the item to be re-orderable.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   151
    return YES;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   152
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   153
*/
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   154
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   155
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   156
#pragma mark -
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   157
#pragma mark Table view delegate
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   158
-(void) tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   159
    int newRow = [indexPath row];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   160
    int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   161
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   162
    if (newRow != oldRow) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   163
        // 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
   164
        [self.teamDictionary setValue:[fortArray objectAtIndex:newRow] forKey:@"fort"];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   165
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   166
        // tell our boss to write this new stuff on disk
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   167
        [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   168
        [self.tableView reloadData];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   169
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   170
        self.lastIndexPath = indexPath;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   171
        [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   172
    }
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   173
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   174
    [self.navigationController popViewControllerAnimated:YES];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   175
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   176
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   177
-(CGFloat) tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   178
    return 200;
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