cocoaTouch/FlagsViewController.m
author koda
Thu, 29 Apr 2010 02:43:28 +0000
changeset 3373 c1ff724a5c34
parent 3335 2520ee7a5484
child 3374 0d522416d97f
permissions -rw-r--r--
use a proper bundle identifier fix land preview add theme selection and map preview
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 "FlagsViewController.h"
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    10
#import "CommodityFunctions.h"
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    11
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    12
@implementation FlagsViewController
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    13
@synthesize teamDictionary, flagArray, flagSprites, lastIndexPath;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    14
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    15
3335
2520ee7a5484 new manager for handling (future) rotations
koda
parents: 3325
diff changeset
    16
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
2520ee7a5484 new manager for handling (future) rotations
koda
parents: 3325
diff changeset
    17
	return rotationManager(interfaceOrientation);
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    18
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    19
3335
2520ee7a5484 new manager for handling (future) rotations
koda
parents: 3325
diff changeset
    20
3325
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 *flagsDirectory = FLAGS_DIRECTORY();
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    27
    NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:flagsDirectory error:NULL];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    28
    self.flagArray = array;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    29
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    30
    NSMutableArray *spriteArray = [[NSMutableArray alloc] initWithCapacity:[flagArray count]];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    31
    for (NSString *flagName in flagArray) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    32
        NSString *flagFile = [[NSString alloc] initWithFormat:@"%@/%@", flagsDirectory, flagName];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    33
        UIImage *flagSprite = [[UIImage alloc] initWithContentsOfFile:flagFile];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    34
        [flagFile release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    35
        [spriteArray addObject:flagSprite];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    36
        [flagSprite release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    37
    }
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    38
    self.flagSprites = spriteArray;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    39
    [spriteArray release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    40
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    41
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    42
- (void)viewWillAppear:(BOOL)animated {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    43
    [super viewWillAppear:animated];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    44
    [self.tableView reloadData];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    45
    [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    46
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    47
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    48
/*
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    49
- (void)viewDidAppear:(BOOL)animated {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    50
    [super viewDidAppear:animated];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    51
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    52
*/
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    53
/*
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    54
- (void)viewWillDisappear:(BOOL)animated {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    55
    [super viewWillDisappear:animated];
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
/*
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    59
- (void)viewDidDisappear:(BOOL)animated {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    60
    [super viewDidDisappear:animated];
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
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    65
#pragma mark -
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    66
#pragma mark Table view data source
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    67
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    68
    return 1;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    69
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    70
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    71
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    72
    return [flagArray count];
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
// Customize the appearance of table view cells.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    76
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    77
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    78
    static NSString *CellIdentifier = @"Cell";
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    79
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    80
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    81
    if (cell == nil) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    82
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    83
    }
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    84
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    85
    cell.imageView.image = [flagSprites objectAtIndex:[indexPath row]];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    86
    cell.textLabel.text = [[flagArray objectAtIndex:[indexPath row]] stringByDeletingPathExtension];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    87
    if ([cell.textLabel.text isEqualToString:[self.teamDictionary objectForKey:@"flag"]]) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    88
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    89
        self.lastIndexPath = indexPath;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    90
    } else {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    91
        cell.accessoryType = UITableViewCellAccessoryNone;
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
    return cell;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    95
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    96
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    97
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    98
/*
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    99
// Override to support conditional editing of the table view.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   100
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   101
    // Return NO if you do not want the specified item to be editable.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   102
    return YES;
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
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   106
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   107
/*
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   108
// Override to support editing the table view.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   109
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   110
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   111
    if (editingStyle == UITableViewCellEditingStyleDelete) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   112
        // Delete the row from the data source
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   113
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   114
    }   
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   115
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   116
        // 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
   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
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
// Override to support rearranging the table view.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   124
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
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
/*
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   130
// Override to support conditional rearranging of the table view.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   131
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   132
    // Return NO if you do not want the item to be re-orderable.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   133
    return YES;
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
#pragma mark -
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   139
#pragma mark Table view delegate
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   140
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   141
    int newRow = [indexPath row];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   142
    int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   143
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   144
    if (newRow != oldRow) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   145
        // 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
   146
        [self.teamDictionary setValue:[[flagArray objectAtIndex:newRow] stringByDeletingPathExtension] forKey:@"flag"];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   147
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   148
        // tell our boss to write this new stuff on disk
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   149
        [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   150
        [self.tableView reloadData];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   151
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   152
        self.lastIndexPath = indexPath;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   153
        [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   154
    }
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   155
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   156
    [self.navigationController popViewControllerAnimated:YES];
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
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   160
#pragma mark -
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   161
#pragma mark Memory management
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   162
- (void)didReceiveMemoryWarning {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   163
    // Releases the view if it doesn't have a superview.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   164
    [super didReceiveMemoryWarning];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   165
    // Relinquish ownership any cached data, images, etc that aren't in use.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   166
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   167
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   168
- (void)viewDidUnload {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   169
    self.teamDictionary = nil;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   170
    self.lastIndexPath = nil;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   171
    self.flagArray = nil;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   172
    self.flagSprites = nil;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   173
    [super viewDidUnload];
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
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   177
- (void)dealloc {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   178
    [teamDictionary release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   179
    [lastIndexPath release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   180
    [flagArray release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   181
    [flagSprites release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   182
    [super dealloc];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   183
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   184
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   185
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   186
@end
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   187