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