project_files/HedgewarsMobile/Classes/TeamConfigViewController.m
changeset 6115 485cfecadc9a
parent 6108 7a8da11a6144
child 6209 074ab6ebcb3e
equal deleted inserted replaced
6114:443cce9e060f 6115:485cfecadc9a
    23 #import <QuartzCore/QuartzCore.h>
    23 #import <QuartzCore/QuartzCore.h>
    24 #import "SquareButtonView.h"
    24 #import "SquareButtonView.h"
    25 
    25 
    26 
    26 
    27 @implementation TeamConfigViewController
    27 @implementation TeamConfigViewController
    28 @synthesize listOfTeams, listOfSelectedTeams, cachedContentsOfDir;
    28 @synthesize tableView, selectedTeamsCount, allTeamsCount, listOfAllTeams, listOfSelectedTeams, cachedContentsOfDir;
       
    29 
       
    30 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
       
    31     return rotationManager(interfaceOrientation);
       
    32 }
    29 
    33 
    30 #pragma mark -
    34 #pragma mark -
    31 #pragma mark View lifecycle
    35 #pragma mark View lifecycle
    32 -(void) viewDidLoad {
    36 -(void) viewDidLoad {
    33     [super viewDidLoad];
    37 /*
    34 
       
    35     CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    38     CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    36     self.view.frame = CGRectMake(0, 0, screenSize.height, screenSize.width - 44);
    39     self.view.frame = CGRectMake(0, 0, screenSize.height, screenSize.width - 44);
    37 
    40 */
       
    41     UITableView *aTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
       
    42                                                            style:UITableViewStyleGrouped];
       
    43     aTableView.delegate = self;
       
    44     aTableView.dataSource = self;
    38     if (IS_IPAD()) {
    45     if (IS_IPAD()) {
    39         [self.tableView setBackgroundColorForAnyTable:[UIColor darkBlueColorTransparent]];
    46         [aTableView setBackgroundColorForAnyTable:[UIColor darkBlueColorTransparent]];
    40         self.tableView.layer.borderColor = [[UIColor darkYellowColor] CGColor];
    47         aTableView.layer.borderColor = [[UIColor darkYellowColor] CGColor];
    41         self.tableView.layer.borderWidth = 2.7f;
    48         aTableView.layer.borderWidth = 2.7f;
    42         self.tableView.layer.cornerRadius = 8;
    49         aTableView.layer.cornerRadius = 8;
    43         self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 10, 0);
    50         aTableView.contentInset = UIEdgeInsetsMake(10, 0, 10, 0);
    44     } else {
    51     } else {
    45         UIImage *backgroundImage = [[UIImage alloc] initWithContentsOfFile:@"background~iphone.png"];
    52         UIImage *backgroundImage = [[UIImage alloc] initWithContentsOfFile:@"background~iphone.png"];
    46         UIImageView *background = [[UIImageView alloc] initWithImage:backgroundImage];
    53         UIImageView *background = [[UIImageView alloc] initWithImage:backgroundImage];
    47         [backgroundImage release];
    54         [backgroundImage release];
    48         [self.tableView setBackgroundView:background];
    55         [aTableView setBackgroundView:background];
    49         [background release];
    56         [background release];
    50     }
    57     }
    51 
    58 
    52     self.tableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    59     aTableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    53     self.tableView.separatorColor = [UIColor whiteColor];
    60     aTableView.separatorColor = [UIColor whiteColor];
    54     self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    61     aTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
       
    62     self.tableView = aTableView;
       
    63     [aTableView release];
       
    64 
       
    65     [self.view addSubview:self.tableView];
       
    66     [super viewDidLoad];
    55 }
    67 }
    56 
    68 
    57 -(void) viewWillAppear:(BOOL)animated {
    69 -(void) viewWillAppear:(BOOL)animated {
    58     [super viewWillAppear:animated];
       
    59 
       
    60     NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:TEAMS_DIRECTORY() error:NULL];
    70     NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:TEAMS_DIRECTORY() error:NULL];
    61     // avoid overwriting selected teams when returning on this view
       
    62     if ([self.cachedContentsOfDir isEqualToArray:contentsOfDir] == NO) {
    71     if ([self.cachedContentsOfDir isEqualToArray:contentsOfDir] == NO) {
       
    72         self.cachedContentsOfDir = contentsOfDir;
    63         NSArray *colors = [HWUtils teamColors];
    73         NSArray *colors = [HWUtils teamColors];
    64         NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[contentsOfDir count]];
    74         NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[contentsOfDir count]];
    65         for (int i = 0; i < [contentsOfDir count]; i++) {
    75         for (int i = 0; i < [contentsOfDir count]; i++) {
    66             NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
    76             NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
    67                                          [contentsOfDir objectAtIndex:i],@"team",
    77                                          [contentsOfDir objectAtIndex:i],@"team",
    68                                          [NSNumber numberWithInt:4],@"number",
    78                                          [NSNumber numberWithInt:4],@"number",
    69                                          [colors objectAtIndex:i%[colors count]],@"color",nil];
    79                                          [colors objectAtIndex:i%[colors count]],@"color",nil];
    70             [array addObject:dict];
    80             [array addObject:dict];
    71             [dict release];
    81             [dict release];
    72         }
    82         }
    73         self.listOfTeams = array;
    83         self.listOfAllTeams = array;
    74         [array release];
    84         [array release];
    75 
    85 
    76         NSMutableArray *emptyArray = [[NSMutableArray alloc] initWithObjects:nil];
    86         NSMutableArray *emptyArray = [[NSMutableArray alloc] initWithObjects:nil];
    77         self.listOfSelectedTeams = emptyArray;
    87         self.listOfSelectedTeams = emptyArray;
    78         [emptyArray release];
    88         [emptyArray release];
    79 
    89 
    80         selectedTeamsCount = [self.listOfSelectedTeams count];
    90         self.selectedTeamsCount = [self.listOfSelectedTeams count];
    81         allTeamsCount = [self.listOfTeams count];
    91         self.allTeamsCount = [self.listOfAllTeams count];
    82 
    92         [self.tableView reloadData];
    83         NSArray *contents = [[NSArray alloc] initWithArray:contentsOfDir copyItems:YES];
    93     }
    84         self.cachedContentsOfDir = contents;
    94 
    85         [contents release];
    95     [super viewWillAppear:animated];
    86     }
       
    87     [self.tableView reloadData];
       
    88 }
       
    89 
       
    90 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
       
    91     return rotationManager(interfaceOrientation);
       
    92 }
    96 }
    93 
    97 
    94 -(NSInteger) filterNumberOfHogs:(NSInteger) hogs {
    98 -(NSInteger) filterNumberOfHogs:(NSInteger) hogs {
    95     NSInteger numberOfHogs;
    99     NSInteger numberOfHogs;
    96     if (hogs <= HW_getMaxNumberOfHogs() && hogs >= 1)
   100     if (hogs <= HW_getMaxNumberOfHogs() && hogs >= 1)
   138 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   142 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   139     return 2;
   143     return 2;
   140 }
   144 }
   141 
   145 
   142 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   146 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   143     return (section == 0 ? selectedTeamsCount : allTeamsCount);
   147     return (section == 0 ? self.selectedTeamsCount : self.allTeamsCount);
   144 }
   148 }
   145 
   149 
   146 // Customize the appearance of table view cells.
   150 // Customize the appearance of table view cells.
   147 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   151 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   148     static NSString *CellIdentifier0 = @"Cell0";
   152     static NSString *CellIdentifier0 = @"Cell0";
   175     } else {
   179     } else {
   176         cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier1];
   180         cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier1];
   177         if (cell == nil)
   181         if (cell == nil)
   178             cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
   182             cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
   179 
   183 
   180         cell.textLabel.text = [[[self.listOfTeams objectAtIndex:[indexPath row]] objectForKey:@"team"] stringByDeletingPathExtension];
   184         cell.textLabel.text = [[[self.listOfAllTeams objectAtIndex:[indexPath row]] objectForKey:@"team"] stringByDeletingPathExtension];
   181         cell.textLabel.backgroundColor = [UIColor clearColor];
   185         cell.textLabel.backgroundColor = [UIColor clearColor];
   182         
   186         
   183         NSString *teamPath = [NSString stringWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),cell.textLabel.text];
   187         NSString *teamPath = [NSString stringWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),cell.textLabel.text];
   184         NSDictionary *firstHog = [[[NSDictionary dictionaryWithContentsOfFile:teamPath] objectForKey:@"hedgehogs"] objectAtIndex:0];
   188         NSDictionary *firstHog = [[[NSDictionary dictionaryWithContentsOfFile:teamPath] objectForKey:@"hedgehogs"] objectAtIndex:0];
   185         if ([[firstHog objectForKey:@"level"] intValue] != 0) {
   189         if ([[firstHog objectForKey:@"level"] intValue] != 0) {
   220 
   224 
   221 -(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
   225 -(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
   222     return IS_IPAD() ? 40 : 20;
   226     return IS_IPAD() ? 40 : 20;
   223 }
   227 }
   224 
   228 
   225 -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger) section {
   229 -(UIView *)tableView:(UITableView *)aTableView viewForFooterInSection:(NSInteger) section {
   226     NSInteger height = IS_IPAD() ? 40 : 20;
   230     NSInteger height = IS_IPAD() ? 40 : 20;
   227     UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, height)];
   231     UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, aTableView.frame.size.width, height)];
   228     footer.backgroundColor = [UIColor clearColor];
   232     footer.backgroundColor = [UIColor clearColor];
   229 
   233 
   230     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width*80/100, height)];
   234     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, aTableView.frame.size.width*80/100, height)];
   231     label.center = CGPointMake(self.tableView.frame.size.width/2, height/2);
   235     label.center = CGPointMake(aTableView.frame.size.width/2, height/2);
   232     label.textAlignment = UITextAlignmentCenter;
   236     label.textAlignment = UITextAlignmentCenter;
   233     label.font = [UIFont italicSystemFontOfSize:12];
   237     label.font = [UIFont italicSystemFontOfSize:12];
   234     label.textColor = [UIColor whiteColor];
   238     label.textColor = [UIColor whiteColor];
   235     label.numberOfLines = 2;
   239     label.numberOfLines = 2;
   236     if (section == 0)
   240     if (section == 0)
   249 #pragma mark Table view delegate
   253 #pragma mark Table view delegate
   250 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   254 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   251     NSInteger row = [indexPath row];
   255     NSInteger row = [indexPath row];
   252     NSInteger section = [indexPath section];
   256     NSInteger section = [indexPath section];
   253 
   257 
   254     if (section == 1 && [self.listOfTeams count] > row) {
   258     if (section == 1 && [self.listOfAllTeams count] > row) {
   255         [self.listOfSelectedTeams addObject:[self.listOfTeams objectAtIndex:row]];
   259         [self.listOfSelectedTeams addObject:[self.listOfAllTeams objectAtIndex:row]];
   256         [self.listOfTeams removeObjectAtIndex:row];
   260         [self.listOfAllTeams removeObjectAtIndex:row];
   257 
   261 
   258         NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:selectedTeamsCount inSection:0];
   262         NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:selectedTeamsCount inSection:0];
   259         allTeamsCount--;
   263         allTeamsCount--;
   260         selectedTeamsCount++;
   264         selectedTeamsCount++;
   261         [aTableView beginUpdates];
   265         [aTableView beginUpdates];
   275 
   279 
   276         cell.imageView.image = [self drawHogsRepeated:[newNumber intValue]];
   280         cell.imageView.image = [self drawHogsRepeated:[newNumber intValue]];
   277     }
   281     }
   278 }
   282 }
   279 
   283 
   280 -(void) holdAction:(NSString *)content {
   284 -(void) holdAction:(NSString *)content onTable:(UITableView *)aTableView {
   281     NSInteger row;
   285     NSInteger row;
   282     for (row = 0; row < [self.listOfSelectedTeams count]; row++) {
   286     for (row = 0; row < [self.listOfSelectedTeams count]; row++) {
   283         NSDictionary *dict = [self.listOfSelectedTeams objectAtIndex:row];
   287         NSDictionary *dict = [self.listOfSelectedTeams objectAtIndex:row];
   284         if ([content isEqualToString:[[dict objectForKey:@"team"] stringByDeletingPathExtension]])
   288         if ([content isEqualToString:[[dict objectForKey:@"team"] stringByDeletingPathExtension]])
   285             break;
   289             break;
   286     }
   290     }
   287 
   291 
   288     [self.listOfTeams addObject:[self.listOfSelectedTeams objectAtIndex:row]];
   292     [self.listOfAllTeams addObject:[self.listOfSelectedTeams objectAtIndex:row]];
   289     [self.listOfSelectedTeams removeObjectAtIndex:row];
   293     [self.listOfSelectedTeams removeObjectAtIndex:row];
   290 
   294 
   291     [self.tableView beginUpdates];
   295     [aTableView beginUpdates];
   292     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:row inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
   296     [aTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:row inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
   293     [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:allTeamsCount inSection:1]] withRowAnimation:UITableViewRowAnimationLeft];
   297     [aTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:allTeamsCount inSection:1]] withRowAnimation:UITableViewRowAnimationLeft];
   294     allTeamsCount++;
   298     self.allTeamsCount++;
   295     selectedTeamsCount--;
   299     self.selectedTeamsCount--;
   296     [self.tableView endUpdates];
   300     [aTableView endUpdates];
   297 }
   301 }
   298 
   302 
   299 #pragma mark -
   303 #pragma mark -
   300 #pragma mark Memory management
   304 #pragma mark Memory management
   301 -(void) didReceiveMemoryWarning {
   305 -(void) didReceiveMemoryWarning {
   302     if ([[HedgewarsAppDelegate sharedAppDelegate] isInGame]) {
   306     if ([[HedgewarsAppDelegate sharedAppDelegate] isInGame]) {
   303         self.listOfSelectedTeams = nil;
   307         self.listOfSelectedTeams = nil;
   304         self.listOfTeams = nil;
   308         self.listOfAllTeams = nil;
       
   309         self.tableView = nil;
   305     }
   310     }
   306     self.cachedContentsOfDir = nil;
   311     self.cachedContentsOfDir = nil;
   307     MSG_MEMCLEAN();
   312     MSG_MEMCLEAN();
   308     [super didReceiveMemoryWarning];
   313     [super didReceiveMemoryWarning];
   309 }
   314 }
   310 
   315 
   311 -(void) viewDidUnload {
   316 -(void) viewDidUnload {
   312     self.listOfTeams = nil;
   317     self.tableView = nil;
       
   318     self.listOfAllTeams = nil;
   313     self.listOfSelectedTeams = nil;
   319     self.listOfSelectedTeams = nil;
   314     self.cachedContentsOfDir = nil;
   320     self.cachedContentsOfDir = nil;
   315     MSG_DIDUNLOAD();
   321     MSG_DIDUNLOAD();
   316     [super viewDidUnload];
   322     [super viewDidUnload];
   317 }
   323 }
   318 
   324 
   319 
   325 
   320 -(void) dealloc {
   326 -(void) dealloc {
   321     releaseAndNil(listOfTeams);
   327     releaseAndNil(tableView);
       
   328     releaseAndNil(listOfAllTeams);
   322     releaseAndNil(listOfSelectedTeams);
   329     releaseAndNil(listOfSelectedTeams);
   323     releaseAndNil(cachedContentsOfDir);
   330     releaseAndNil(cachedContentsOfDir);
   324     [super dealloc];
   331     [super dealloc];
   325 }
   332 }
   326 
   333