cocoaTouch/FortsViewController.m
author koda
Thu, 08 Apr 2010 22:45:18 +0000
changeset 3325 652a8ebdf667
child 3335 2520ee7a5484
permissions -rw-r--r--
moved around team creation add flags support add forts support need to find another way to save on file
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
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    17
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    18
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    19
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    20
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    21
#pragma mark -
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    22
#pragma mark View lifecycle
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    23
- (void)viewDidLoad {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    24
    [super viewDidLoad];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    25
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    26
    NSString *fortsDirectory = FORTS_DIRECTORY();
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    27
    NSArray *directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: fortsDirectory error:NULL];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    28
    NSMutableArray *filteredContents = [[NSMutableArray alloc] initWithCapacity:([directoryContents count] / 2)];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    29
    // we need to remove the double entries and the L.png suffix
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    30
    for (int i = 0; i < [directoryContents count]; i++) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    31
        if (i % 2) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    32
            NSString *currentName = [directoryContents objectAtIndex:i];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    33
            NSString *correctName = [currentName substringToIndex:([currentName length] - 5)];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    34
            [filteredContents addObject:correctName];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    35
        } 
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
    self.fortArray = filteredContents;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    39
    [filteredContents release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    40
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    41
    //NSLog(@"%@",fortArray);
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    42
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    43
    // this creates a scaled down version of the image
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    44
    NSMutableArray *spriteArray = [[NSMutableArray alloc] initWithCapacity:[fortArray count]];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    45
    for (NSString *fortName in fortArray) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    46
        NSString *fortFile = [[NSString alloc] initWithFormat:@"%@/%@L.png", fortsDirectory, fortName];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    47
        UIImage *fortSprite = [[UIImage alloc] initWithContentsOfFile:fortFile];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    48
        [fortFile release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    49
        [spriteArray addObject:[fortSprite scaleToSize:CGSizeMake(196,196)]];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    50
        [fortSprite release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    51
    }
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    52
    self.fortSprites = spriteArray;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    53
    [spriteArray release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    54
}
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
- (void)viewWillAppear:(BOOL)animated {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    58
    [super viewWillAppear:animated];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    59
    [self.tableView reloadData];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    60
    [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    61
}
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
- (void)viewDidAppear:(BOOL)animated {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    65
    [super viewDidAppear:animated];
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
/*
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    69
- (void)viewWillDisappear:(BOOL)animated {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    70
    [super viewWillDisappear:animated];
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
/*
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    74
- (void)viewDidDisappear:(BOOL)animated {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    75
    [super viewDidDisappear:animated];
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
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    79
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    80
#pragma mark -
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    81
#pragma mark Table view data source
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    82
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    83
    return 1;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    84
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    85
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    86
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    87
    return [fortArray count];
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
// Customize the appearance of table view cells.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    91
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    92
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    93
    static NSString *CellIdentifier = @"Cell";
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    94
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    95
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    96
    if (cell == nil) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    97
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    98
                                       reuseIdentifier:CellIdentifier] autorelease];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    99
    }
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   100
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   101
    cell.imageView.image = [fortSprites objectAtIndex:[indexPath row]];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   102
    cell.textLabel.text = [fortArray objectAtIndex:[indexPath row]];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   103
    cell.detailTextLabel.text = @"Insert funny description here";
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   104
    if ([cell.textLabel.text isEqualToString:[self.teamDictionary objectForKey:@"fort"]]) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   105
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   106
        self.lastIndexPath = indexPath;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   107
    } else {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   108
        cell.accessoryType = UITableViewCellAccessoryNone;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   109
    }
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   110
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   111
    return cell;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   112
}
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
// Override to support conditional editing of the table view.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   117
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   118
    // Return NO if you do not want the specified item to be editable.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   119
    return YES;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   120
}
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
// Override to support editing the table view.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   126
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   127
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   128
    if (editingStyle == UITableViewCellEditingStyleDelete) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   129
        // Delete the row from the data source
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   130
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   131
    }   
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   132
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   133
        // 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
   134
    }   
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
// Override to support rearranging the table view.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   141
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
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
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
// Override to support conditional rearranging of the table view.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   148
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   149
    // Return NO if you do not want the item to be re-orderable.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   150
    return YES;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   151
}
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
#pragma mark -
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   156
#pragma mark Table view delegate
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   157
-(void) tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   158
    int newRow = [indexPath row];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   159
    int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   160
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   161
    if (newRow != oldRow) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   162
        // 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
   163
        [self.teamDictionary setValue:[fortArray objectAtIndex:newRow] forKey:@"fort"];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   164
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   165
        // tell our boss to write this new stuff on disk
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   166
        [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   167
        [self.tableView reloadData];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   168
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   169
        self.lastIndexPath = indexPath;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   170
        [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   171
    }
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   172
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   173
    [self.navigationController popViewControllerAnimated:YES];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   174
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   175
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   176
-(CGFloat) tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   177
    return 200;
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
#pragma mark -
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   181
#pragma mark Memory management
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   182
-(void) didReceiveMemoryWarning {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   183
    // Releases the view if it doesn't have a superview.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   184
    [super didReceiveMemoryWarning];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   185
    // Relinquish ownership any cached data, images, etc that aren't in use.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   186
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   187
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   188
-(void) viewDidUnload {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   189
    self.teamDictionary = nil;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   190
    self.lastIndexPath = nil;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   191
    self.fortArray = nil;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   192
    self.fortSprites = nil;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   193
    [super viewDidUnload];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   194
}
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
- (void)dealloc {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   198
    [teamDictionary release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   199
    [lastIndexPath release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   200
    [fortArray release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   201
    [fortSprites release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   202
    [super dealloc];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   203
}
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
@end
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   207