project_files/HedgewarsMobile/Classes/TeamConfigViewController.m
author koda
Wed, 23 Jun 2010 22:03:56 +0200
changeset 3547 02875b1145b7
parent 3546 ccf4854df294
child 3551 d4de36b3801a
permissions -rw-r--r--
i <3 mercurial
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
//  TeamConfigViewController.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 20/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 "TeamConfigViewController.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 "HogButtonView.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    12
#import "SquareButtonView.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    13
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    14
@implementation TeamConfigViewController
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    15
@synthesize listOfTeams, listOfSelectedTeams;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    16
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    17
#define NUMBERBUTTON_TAG 123456
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    18
#define SQUAREBUTTON_TAG 654321
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    19
#define LABEL_TAG        456123
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    20
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    21
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    22
#pragma mark View lifecycle
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    23
-(void) viewDidLoad {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    24
    [super viewDidLoad];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    25
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    26
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    27
    self.view.frame = CGRectMake(0, 0, screenSize.height, screenSize.width - 44);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    28
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    29
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    30
-(void) viewWillAppear:(BOOL)animated {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    31
    [super viewWillAppear:animated];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    32
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    33
    // integer representation of various color (defined in SquareButtonView)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    34
    NSUInteger colors[6] = { 4421353, 4100897, 10632635, 16749353, 14483456, 7566195 };
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    35
    NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:TEAMS_DIRECTORY() error:NULL];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    36
    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[contentsOfDir count]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    37
    for (int i = 0; i < [contentsOfDir count]; i++) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    38
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    39
                                                                  [contentsOfDir objectAtIndex:i],@"team",
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    40
                                                                  [NSNumber numberWithInt:4],@"number",
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    41
                                                                  [NSNumber numberWithInt:colors[i%6]],@"color",nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    42
        [array addObject:dict];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    43
        [dict release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    44
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    45
    self.listOfTeams = array;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    46
    [array release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    47
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    48
    NSMutableArray *emptyArray = [[NSMutableArray alloc] initWithObjects:nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
    self.listOfSelectedTeams = emptyArray;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    50
    [emptyArray release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    51
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
    [self.tableView reloadData];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    53
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    54
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    56
    return rotationManager(interfaceOrientation);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    57
}
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
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    61
#pragma mark Table view data source
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    62
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    63
    return 2;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    64
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    65
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    66
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    67
    if (section == 0)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    68
        return [listOfSelectedTeams count] ;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    69
    else
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    70
        return [listOfTeams count];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    71
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    72
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    73
-(NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    74
    if (section == 0)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    75
        return NSLocalizedString(@"Playing Teams",@"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    76
    else
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    77
        return NSLocalizedString(@"Available Teams",@"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    78
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    79
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    80
// Customize the appearance of table view cells.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    81
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    82
    static NSString *CellIdentifier0 = @"Cell0";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    83
    static NSString *CellIdentifier1 = @"Cell1";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    84
    NSInteger section = [indexPath section];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    85
    UITableViewCell *cell;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    86
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    87
    if (section == 0) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    88
        cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier0];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    89
        if (cell == nil) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    90
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier0] autorelease];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    91
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    92
            UIButton *numberButton = [[HogButtonView alloc] initWithFrame:CGRectMake(12, 5, 88, 32)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    93
            numberButton.tag = NUMBERBUTTON_TAG;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    94
            [cell addSubview:numberButton];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    95
            [numberButton release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    96
            
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    97
            SquareButtonView *squareButton = [[SquareButtonView alloc] initWithFrame:CGRectMake(12+88+7, 5, 36, 36)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    98
            squareButton.tag = SQUAREBUTTON_TAG;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    99
            [cell addSubview:squareButton];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   100
            [squareButton release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   101
            
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   102
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12+88+7+36+7, 10, 250, 25)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   103
            label.textAlignment = UITextAlignmentLeft;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   104
            label.backgroundColor = [UIColor clearColor];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   105
            label.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize] + 2];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   106
            label.tag = LABEL_TAG;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   107
            [cell.contentView addSubview:label];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   108
            [label release];
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
        NSMutableDictionary *selectedRow = [listOfSelectedTeams objectAtIndex:[indexPath row]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   112
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   113
        UILabel *cellLabel = (UILabel *)[cell viewWithTag:LABEL_TAG];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   114
        cellLabel.text = [[selectedRow objectForKey:@"team"] stringByDeletingPathExtension];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   115
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   116
        HogButtonView *numberButton = (HogButtonView *)[cell viewWithTag:NUMBERBUTTON_TAG];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   117
        [numberButton drawManyHogs:[[selectedRow objectForKey:@"number"] intValue]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   118
        numberButton.ownerDictionary = selectedRow;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   119
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   120
        SquareButtonView *squareButton = (SquareButtonView *)[cell viewWithTag:SQUAREBUTTON_TAG];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   121
        [squareButton selectColor:[[selectedRow objectForKey:@"color"] intValue]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   122
        squareButton.ownerDictionary = selectedRow;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   123
    } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   124
        cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier1];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   125
        if (cell == nil) 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   126
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   127
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   128
        cell.textLabel.text = [[[listOfTeams objectAtIndex:[indexPath row]] objectForKey:@"team"] stringByDeletingPathExtension];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   129
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   130
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   131
    return cell;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   132
}
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
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   136
#pragma mark Table view delegate
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   137
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   138
    NSInteger row = [indexPath row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   139
    NSInteger section = [indexPath section];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   140
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   141
    if (section == 0) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   142
        [self.listOfTeams addObject:[self.listOfSelectedTeams objectAtIndex:row]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   143
        [self.listOfSelectedTeams removeObjectAtIndex:row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   144
    } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   145
        [self.listOfSelectedTeams addObject:[self.listOfTeams objectAtIndex:row]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   146
        [self.listOfTeams removeObjectAtIndex:row];      
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   147
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   148
    [aTableView reloadData];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   149
}
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
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   153
#pragma mark Memory management
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   154
-(void) didReceiveMemoryWarning {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   155
    // Releases the view if it doesn't have a superview.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   156
    [super didReceiveMemoryWarning];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   157
    // Relinquish ownership any cached data, images, etc that aren't in use.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   158
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   159
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   160
-(void) viewDidUnload {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   161
    self.listOfTeams = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   162
    [super viewDidUnload];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   163
    MSG_DIDUNLOAD();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   164
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   165
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   166
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   167
-(void) dealloc {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   168
    [self.listOfTeams release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   169
    [super dealloc];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   170
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   171
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   172
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   173
@end
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   174