project_files/HedgewarsMobile/Classes/TeamConfigViewController.m
author koda
Tue, 20 Jul 2010 05:37:58 +0200
changeset 3660 bc125bea5849
parent 3659 f8d5ac50e307
child 3662 a44406f4369b
permissions -rw-r--r--
complete settings page rework
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);
3551
d4de36b3801a moar zoom, fixed fort mode, other glitches
koda
parents: 3547
diff changeset
    28
    isFirstLoad = YES;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    29
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    30
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    31
-(void) viewWillAppear:(BOOL)animated {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    32
    [super viewWillAppear:animated];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    33
3551
d4de36b3801a moar zoom, fixed fort mode, other glitches
koda
parents: 3547
diff changeset
    34
    // avoid overwriting selected teams when returning on this view
d4de36b3801a moar zoom, fixed fort mode, other glitches
koda
parents: 3547
diff changeset
    35
    if (isFirstLoad) {
d4de36b3801a moar zoom, fixed fort mode, other glitches
koda
parents: 3547
diff changeset
    36
        // integer representation of various color (defined in SquareButtonView)
d4de36b3801a moar zoom, fixed fort mode, other glitches
koda
parents: 3547
diff changeset
    37
        NSUInteger colors[6] = { 4421353, 4100897, 10632635, 16749353, 14483456, 7566195 };
d4de36b3801a moar zoom, fixed fort mode, other glitches
koda
parents: 3547
diff changeset
    38
        NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:TEAMS_DIRECTORY() error:NULL];
d4de36b3801a moar zoom, fixed fort mode, other glitches
koda
parents: 3547
diff changeset
    39
        NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[contentsOfDir count]];
d4de36b3801a moar zoom, fixed fort mode, other glitches
koda
parents: 3547
diff changeset
    40
        for (int i = 0; i < [contentsOfDir count]; i++) {
d4de36b3801a moar zoom, fixed fort mode, other glitches
koda
parents: 3547
diff changeset
    41
            NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
d4de36b3801a moar zoom, fixed fort mode, other glitches
koda
parents: 3547
diff changeset
    42
                                         [contentsOfDir objectAtIndex:i],@"team",
d4de36b3801a moar zoom, fixed fort mode, other glitches
koda
parents: 3547
diff changeset
    43
                                         [NSNumber numberWithInt:4],@"number",
d4de36b3801a moar zoom, fixed fort mode, other glitches
koda
parents: 3547
diff changeset
    44
                                         [NSNumber numberWithInt:colors[i%6]],@"color",nil];
d4de36b3801a moar zoom, fixed fort mode, other glitches
koda
parents: 3547
diff changeset
    45
            [array addObject:dict];
d4de36b3801a moar zoom, fixed fort mode, other glitches
koda
parents: 3547
diff changeset
    46
            [dict release];
d4de36b3801a moar zoom, fixed fort mode, other glitches
koda
parents: 3547
diff changeset
    47
        }
d4de36b3801a moar zoom, fixed fort mode, other glitches
koda
parents: 3547
diff changeset
    48
        self.listOfTeams = array;
d4de36b3801a moar zoom, fixed fort mode, other glitches
koda
parents: 3547
diff changeset
    49
        [array release];
d4de36b3801a moar zoom, fixed fort mode, other glitches
koda
parents: 3547
diff changeset
    50
        
d4de36b3801a moar zoom, fixed fort mode, other glitches
koda
parents: 3547
diff changeset
    51
        NSMutableArray *emptyArray = [[NSMutableArray alloc] initWithObjects:nil];
d4de36b3801a moar zoom, fixed fort mode, other glitches
koda
parents: 3547
diff changeset
    52
        self.listOfSelectedTeams = emptyArray;
d4de36b3801a moar zoom, fixed fort mode, other glitches
koda
parents: 3547
diff changeset
    53
        [emptyArray release];
d4de36b3801a moar zoom, fixed fort mode, other glitches
koda
parents: 3547
diff changeset
    54
        isFirstLoad = NO;
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    56
    [self.tableView reloadData];
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
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    60
    return rotationManager(interfaceOrientation);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    61
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    62
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    63
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    64
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    65
#pragma mark Table view data source
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    66
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    67
    return 2;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    68
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    69
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    70
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    71
    if (section == 0)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    72
        return [listOfSelectedTeams count] ;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    73
    else
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    74
        return [listOfTeams count];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    75
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    76
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    77
-(NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    78
    if (section == 0)
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    79
        return NSLocalizedString(@"Playing Teams",@"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    80
    else
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    81
        return NSLocalizedString(@"Available Teams",@"");
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    82
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    83
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    84
// Customize the appearance of table view cells.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    85
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    86
    static NSString *CellIdentifier0 = @"Cell0";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    87
    static NSString *CellIdentifier1 = @"Cell1";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    88
    NSInteger section = [indexPath section];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    89
    UITableViewCell *cell;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    90
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    91
    if (section == 0) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    92
        cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier0];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    93
        if (cell == nil) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    94
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier0] autorelease];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    95
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    96
            UIButton *numberButton = [[HogButtonView alloc] initWithFrame:CGRectMake(12, 5, 88, 32)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    97
            numberButton.tag = NUMBERBUTTON_TAG;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    98
            [cell addSubview:numberButton];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    99
            [numberButton release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   100
            
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   101
            SquareButtonView *squareButton = [[SquareButtonView alloc] initWithFrame:CGRectMake(12+88+7, 5, 36, 36)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   102
            squareButton.tag = SQUAREBUTTON_TAG;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   103
            [cell addSubview:squareButton];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   104
            [squareButton release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   105
            
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   106
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12+88+7+36+7, 10, 250, 25)];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   107
            label.textAlignment = UITextAlignmentLeft;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   108
            label.backgroundColor = [UIColor clearColor];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   109
            label.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize] + 2];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   110
            label.tag = LABEL_TAG;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   111
            [cell.contentView addSubview:label];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   112
            [label release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   113
        }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   114
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   115
        NSMutableDictionary *selectedRow = [listOfSelectedTeams objectAtIndex:[indexPath row]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   116
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   117
        UILabel *cellLabel = (UILabel *)[cell viewWithTag:LABEL_TAG];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   118
        cellLabel.text = [[selectedRow objectForKey:@"team"] stringByDeletingPathExtension];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   119
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   120
        HogButtonView *numberButton = (HogButtonView *)[cell viewWithTag:NUMBERBUTTON_TAG];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   121
        [numberButton drawManyHogs:[[selectedRow objectForKey:@"number"] intValue]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   122
        numberButton.ownerDictionary = selectedRow;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   123
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   124
        SquareButtonView *squareButton = (SquareButtonView *)[cell viewWithTag:SQUAREBUTTON_TAG];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   125
        [squareButton selectColor:[[selectedRow objectForKey:@"color"] intValue]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   126
        squareButton.ownerDictionary = selectedRow;
3625
9f1d79e01a60 icons \o/
koda
parents: 3551
diff changeset
   127
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
3659
f8d5ac50e307 half reworking of the settings page with delegation
koda
parents: 3625
diff changeset
   128
            cellLabel.textColor = [UIColor colorWithRed:(CGFloat)0xFE/255 green:(CGFloat)0xCB/255 blue:0 alpha:1];
3625
9f1d79e01a60 icons \o/
koda
parents: 3551
diff changeset
   129
        }
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   130
    } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   131
        cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier1];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   132
        if (cell == nil) 
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   133
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   134
        
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   135
        cell.textLabel.text = [[[listOfTeams objectAtIndex:[indexPath row]] objectForKey:@"team"] stringByDeletingPathExtension];
3625
9f1d79e01a60 icons \o/
koda
parents: 3551
diff changeset
   136
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
9f1d79e01a60 icons \o/
koda
parents: 3551
diff changeset
   137
            cell.textLabel.textColor = [UIColor colorWithRed:(CGFloat)0xFE/255 green:(CGFloat)0xCB/255 blue:0 alpha:1 ];
9f1d79e01a60 icons \o/
koda
parents: 3551
diff changeset
   138
        }
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   139
    }
3625
9f1d79e01a60 icons \o/
koda
parents: 3551
diff changeset
   140
    
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   141
    return cell;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   142
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   143
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   144
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   145
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   146
#pragma mark Table view delegate
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   147
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   148
    NSInteger row = [indexPath row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   149
    NSInteger section = [indexPath section];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   150
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   151
    if (section == 0) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   152
        [self.listOfTeams addObject:[self.listOfSelectedTeams objectAtIndex:row]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   153
        [self.listOfSelectedTeams removeObjectAtIndex:row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   154
    } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   155
        [self.listOfSelectedTeams addObject:[self.listOfTeams objectAtIndex:row]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   156
        [self.listOfTeams removeObjectAtIndex:row];      
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   157
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   158
    [aTableView reloadData];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   159
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   160
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   161
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   162
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   163
#pragma mark Memory management
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   164
-(void) didReceiveMemoryWarning {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   165
    // Releases the view if it doesn't have a superview.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   166
    [super didReceiveMemoryWarning];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   167
    // Relinquish ownership any cached data, images, etc that aren't in use.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   168
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   169
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   170
-(void) viewDidUnload {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   171
    self.listOfTeams = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   172
    [super viewDidUnload];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   173
    MSG_DIDUNLOAD();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   174
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   175
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   176
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   177
-(void) dealloc {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   178
    [self.listOfTeams release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   179
    [super dealloc];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   180
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   181
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   182
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   183
@end
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   184