project_files/HedgewarsMobile/Classes/TeamConfigViewController.m
branchios-develop
changeset 12872 00215a7ec5f5
parent 11549 893722a2a1f9
child 13166 ba5c794adae3
equal deleted inserted replaced
12871:2c06b1120749 12872:00215a7ec5f5
    23 
    23 
    24 
    24 
    25 @implementation TeamConfigViewController
    25 @implementation TeamConfigViewController
    26 @synthesize tableView, selectedTeamsCount, allTeamsCount, listOfAllTeams, listOfSelectedTeams, cachedContentsOfDir;
    26 @synthesize tableView, selectedTeamsCount, allTeamsCount, listOfAllTeams, listOfSelectedTeams, cachedContentsOfDir;
    27 
    27 
    28 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    28 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    29     return rotationManager(interfaceOrientation);
    29     return rotationManager(interfaceOrientation);
    30 }
    30 }
    31 
    31 
    32 #pragma mark -
    32 #pragma mark -
    33 #pragma mark View lifecycle
    33 #pragma mark View lifecycle
    34 -(void) viewDidLoad {
    34 - (void)viewDidLoad {
    35     UITableView *aTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
    35     UITableView *aTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
    36                                                            style:UITableViewStyleGrouped];
    36                                                            style:UITableViewStyleGrouped];
    37     aTableView.delegate = self;
    37     aTableView.delegate = self;
    38     aTableView.dataSource = self;
    38     aTableView.dataSource = self;
    39     if (IS_IPAD()) {
    39     if (IS_IPAD()) {
    45     } else {
    45     } else {
    46         UIImage *backgroundImage = [[UIImage alloc] initWithContentsOfFile:@"background~iphone.png"];
    46         UIImage *backgroundImage = [[UIImage alloc] initWithContentsOfFile:@"background~iphone.png"];
    47         UIImageView *background = [[UIImageView alloc] initWithImage:backgroundImage];
    47         UIImageView *background = [[UIImageView alloc] initWithImage:backgroundImage];
    48         background.contentMode = UIViewContentModeScaleAspectFill;
    48         background.contentMode = UIViewContentModeScaleAspectFill;
    49         background.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    49         background.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    50         [backgroundImage release];
       
    51         [self.view addSubview:background];
    50         [self.view addSubview:background];
    52         [background release];
       
    53         [aTableView setBackgroundColorForAnyTable:[UIColor clearColor]];
    51         [aTableView setBackgroundColorForAnyTable:[UIColor clearColor]];
    54     }
    52     }
    55 
    53 
    56     aTableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    54     aTableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    57     aTableView.separatorColor = [UIColor whiteColor];
    55     aTableView.separatorColor = [UIColor whiteColor];
    58     aTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    56     aTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    59     aTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    57     aTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    60     self.tableView = aTableView;
    58     self.tableView = aTableView;
    61     [aTableView release];
       
    62 
    59 
    63     [self.view addSubview:self.tableView];
    60     [self.view addSubview:self.tableView];
    64     [super viewDidLoad];
    61     [super viewDidLoad];
    65 }
    62 }
    66 
    63 
    67 -(void) viewWillAppear:(BOOL)animated {
    64 - (void)viewWillAppear:(BOOL)animated {
    68     NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:TEAMS_DIRECTORY() error:NULL];
    65     NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:TEAMS_DIRECTORY() error:NULL];
    69     if ([self.cachedContentsOfDir isEqualToArray:contentsOfDir] == NO) {
    66     if ([self.cachedContentsOfDir isEqualToArray:contentsOfDir] == NO) {
    70         self.cachedContentsOfDir = contentsOfDir;
    67         self.cachedContentsOfDir = contentsOfDir;
    71         NSArray *colors = [HWUtils teamColors];
    68         NSArray *colors = [HWUtils teamColors];
    72         NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[contentsOfDir count]];
    69         NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:[contentsOfDir count]];
    74             NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
    71             NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
    75                                          [contentsOfDir objectAtIndex:i],@"team",
    72                                          [contentsOfDir objectAtIndex:i],@"team",
    76                                          [NSNumber numberWithInt:4],@"number",
    73                                          [NSNumber numberWithInt:4],@"number",
    77                                          [colors objectAtIndex:i%[colors count]],@"color",nil];
    74                                          [colors objectAtIndex:i%[colors count]],@"color",nil];
    78             [array addObject:dict];
    75             [array addObject:dict];
    79             [dict release];
       
    80         }
    76         }
    81         self.listOfAllTeams = array;
    77         self.listOfAllTeams = array;
    82         [array release];
       
    83 
    78 
    84         NSMutableArray *emptyArray = [[NSMutableArray alloc] initWithObjects:nil];
    79         NSMutableArray *emptyArray = [[NSMutableArray alloc] initWithObjects:nil];
    85         self.listOfSelectedTeams = emptyArray;
    80         self.listOfSelectedTeams = emptyArray;
    86         [emptyArray release];
       
    87 
    81 
    88         self.selectedTeamsCount = [self.listOfSelectedTeams count];
    82         self.selectedTeamsCount = [self.listOfSelectedTeams count];
    89         self.allTeamsCount = [self.listOfAllTeams count];
    83         self.allTeamsCount = [self.listOfAllTeams count];
    90         [self.tableView reloadData];
    84         [self.tableView reloadData];
    91     }
    85     }
    92 
    86 
    93     [super viewWillAppear:animated];
    87     [super viewWillAppear:animated];
    94 }
    88 }
    95 
    89 
    96 -(NSInteger) filterNumberOfHogs:(NSInteger) hogs {
    90 - (NSInteger)filterNumberOfHogs:(NSInteger)hogs {
    97     NSInteger numberOfHogs;
    91     NSInteger numberOfHogs;
    98     if (hogs <= HW_getMaxNumberOfHogs() && hogs >= 1)
    92     if (hogs <= HW_getMaxNumberOfHogs() && hogs >= 1)
    99         numberOfHogs = hogs;
    93         numberOfHogs = hogs;
   100     else {
    94     else {
   101         if (hogs > HW_getMaxNumberOfHogs())
    95         if (hogs > HW_getMaxNumberOfHogs())
   106     return numberOfHogs;
   100     return numberOfHogs;
   107 }
   101 }
   108 
   102 
   109 #pragma mark -
   103 #pragma mark -
   110 #pragma mark Table view data source
   104 #pragma mark Table view data source
   111 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   105 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   112     return 2;
   106     return 2;
   113 }
   107 }
   114 
   108 
   115 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   109 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   116     return (section == 0 ? self.selectedTeamsCount : self.allTeamsCount);
   110     return (section == 0 ? self.selectedTeamsCount : self.allTeamsCount);
   117 }
   111 }
   118 
   112 
   119 // Customize the appearance of table view cells.
   113 // Customize the appearance of table view cells.
   120 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   114 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   124     UITableViewCell *cell;
   118     UITableViewCell *cell;
   125 
   119 
   126     if (section == 0) {
   120     if (section == 0) {
   127         cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier0];
   121         cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier0];
   128         if (cell == nil) {
   122         if (cell == nil) {
   129             cell = [[[HoldTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier0] autorelease];
   123             cell = [[HoldTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier0];
   130 
   124 
   131             SquareButtonView *squareButton = [[SquareButtonView alloc] initWithFrame:CGRectMake(0, 0, 36, 36)];
   125             SquareButtonView *squareButton = [[SquareButtonView alloc] initWithFrame:CGRectMake(0, 0, 36, 36)];
   132             cell.accessoryView = squareButton;
   126             cell.accessoryView = squareButton;
   133             [squareButton release];
       
   134         }
   127         }
   135 
   128 
   136         NSMutableDictionary *selectedRow = [listOfSelectedTeams objectAtIndex:[indexPath row]];
   129         NSMutableDictionary *selectedRow = [listOfSelectedTeams objectAtIndex:[indexPath row]];
   137         cell.textLabel.text = [[selectedRow objectForKey:@"team"] stringByDeletingPathExtension];
   130         cell.textLabel.text = [[selectedRow objectForKey:@"team"] stringByDeletingPathExtension];
   138         cell.textLabel.backgroundColor = [UIColor clearColor];
   131         cell.textLabel.backgroundColor = [UIColor clearColor];
   146         cell.imageView.image = [UIImage drawHogsRepeated:[hogNumber intValue]];
   139         cell.imageView.image = [UIImage drawHogsRepeated:[hogNumber intValue]];
   147         ((HoldTableViewCell *)cell).delegate = self;
   140         ((HoldTableViewCell *)cell).delegate = self;
   148     } else {
   141     } else {
   149         cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier1];
   142         cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier1];
   150         if (cell == nil)
   143         if (cell == nil)
   151             cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
   144             cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
   152 
   145 
   153         cell.textLabel.text = [[[self.listOfAllTeams objectAtIndex:[indexPath row]] objectForKey:@"team"] stringByDeletingPathExtension];
   146         cell.textLabel.text = [[[self.listOfAllTeams objectAtIndex:[indexPath row]] objectForKey:@"team"] stringByDeletingPathExtension];
   154         cell.textLabel.backgroundColor = [UIColor clearColor];
   147         cell.textLabel.backgroundColor = [UIColor clearColor];
   155 
   148 
   156         NSString *teamPath = [NSString stringWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),cell.textLabel.text];
   149         NSString *teamPath = [NSString stringWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),cell.textLabel.text];
   157         NSDictionary *firstHog = [[[NSDictionary dictionaryWithContentsOfFile:teamPath] objectForKey:@"hedgehogs"] objectAtIndex:0];
   150         NSDictionary *firstHog = [[[NSDictionary dictionaryWithContentsOfFile:teamPath] objectForKey:@"hedgehogs"] objectAtIndex:0];
   158         if ([[firstHog objectForKey:@"level"] intValue] != 0) {
   151         if ([[firstHog objectForKey:@"level"] intValue] != 0) {
   159             NSString *imgString = [[NSString alloc] initWithFormat:@"%@/robotBadge.png",[[NSBundle mainBundle] resourcePath]];
   152             NSString *imgString = [[NSString alloc] initWithFormat:@"%@/robotBadge.png",[[NSBundle mainBundle] resourcePath]];
   160             UIImage *sprite = [[UIImage alloc] initWithContentsOfFile:imgString];
   153             UIImage *sprite = [[UIImage alloc] initWithContentsOfFile:imgString];
   161             [imgString release];
       
   162             UIImageView *spriteView = [[UIImageView alloc] initWithImage:sprite];
   154             UIImageView *spriteView = [[UIImageView alloc] initWithImage:sprite];
   163             [sprite release];
       
   164 
   155 
   165             cell.accessoryView = spriteView;
   156             cell.accessoryView = spriteView;
   166             [spriteView release];
       
   167         } else
   157         } else
   168             cell.accessoryView = nil;
   158             cell.accessoryView = nil;
   169     }
   159     }
   170 
   160 
   171     cell.textLabel.textColor = [UIColor lightYellowColor];
   161     cell.textLabel.textColor = [UIColor lightYellowColor];
   187     theLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
   177     theLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
   188 
   178 
   189     UIView *theView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, aTableView.frame.size.width, 30)];
   179     UIView *theView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, aTableView.frame.size.width, 30)];
   190     theView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
   180     theView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
   191     [theView addSubview:theLabel];
   181     [theView addSubview:theLabel];
   192     [theLabel release];
   182     return theView;
   193     return [theView autorelease];
       
   194 }
   183 }
   195 
   184 
   196 -(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
   185 -(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
   197     return IS_IPAD() ? 40 : 30;
   186     return IS_IPAD() ? 40 : 30;
   198 }
   187 }
   199 
   188 
   200 -(UIView *)tableView:(UITableView *)aTableView viewForFooterInSection:(NSInteger) section {
   189 -(UIView *)tableView:(UITableView *)aTableView viewForFooterInSection:(NSInteger)section {
   201     NSInteger height = IS_IPAD() ? 40 : 30;
   190     NSInteger height = IS_IPAD() ? 40 : 30;
   202     UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, aTableView.frame.size.width, height)];
   191     UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, aTableView.frame.size.width, height)];
   203     footer.backgroundColor = [UIColor clearColor];
   192     footer.backgroundColor = [UIColor clearColor];
   204     footer.autoresizingMask = UIViewAutoresizingFlexibleWidth;
   193     footer.autoresizingMask = UIViewAutoresizingFlexibleWidth;
   205 
   194 
   216         label.text = NSLocalizedString(@"Tap to add hogs or change color, touch and hold to remove a team.",@"");
   205         label.text = NSLocalizedString(@"Tap to add hogs or change color, touch and hold to remove a team.",@"");
   217     else
   206     else
   218         label.text = NSLocalizedString(@"The robot badge indicates an AI-controlled team.",@"");
   207         label.text = NSLocalizedString(@"The robot badge indicates an AI-controlled team.",@"");
   219 
   208 
   220     [footer addSubview:label];
   209     [footer addSubview:label];
   221     [label release];
   210     return footer;
   222     return [footer autorelease];
       
   223 }
   211 }
   224 
   212 
   225 
   213 
   226 #pragma mark -
   214 #pragma mark -
   227 #pragma mark Table view delegate
   215 #pragma mark Table view delegate
   228 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   216 - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   229     NSUInteger row = [indexPath row];
   217     NSUInteger row = [indexPath row];
   230     NSUInteger section = [indexPath section];
   218     NSUInteger section = [indexPath section];
   231 
   219 
   232     if (section == 1 && [self.listOfAllTeams count] > row) {
   220     if (section == 1 && [self.listOfAllTeams count] > row) {
   233         [self.listOfSelectedTeams addObject:[self.listOfAllTeams objectAtIndex:row]];
   221         [self.listOfSelectedTeams addObject:[self.listOfAllTeams objectAtIndex:row]];
   253 
   241 
   254         cell.imageView.image = [UIImage drawHogsRepeated:[newNumber intValue]];
   242         cell.imageView.image = [UIImage drawHogsRepeated:[newNumber intValue]];
   255     }
   243     }
   256 }
   244 }
   257 
   245 
   258 -(void) holdAction:(NSString *)content onTable:(UITableView *)aTableView {
   246 - (void)holdAction:(NSString *)content onTable:(UITableView *)aTableView {
   259     NSUInteger row;
   247     NSUInteger row;
   260     for (row = 0; row < [self.listOfSelectedTeams count]; row++) {
   248     for (row = 0; row < [self.listOfSelectedTeams count]; row++) {
   261         NSDictionary *dict = [self.listOfSelectedTeams objectAtIndex:row];
   249         NSDictionary *dict = [self.listOfSelectedTeams objectAtIndex:row];
   262         if ([content isEqualToString:[[dict objectForKey:@"team"] stringByDeletingPathExtension]])
   250         if ([content isEqualToString:[[dict objectForKey:@"team"] stringByDeletingPathExtension]])
   263             break;
   251             break;
   274     [aTableView endUpdates];
   262     [aTableView endUpdates];
   275 }
   263 }
   276 
   264 
   277 #pragma mark -
   265 #pragma mark -
   278 #pragma mark Memory management
   266 #pragma mark Memory management
   279 -(void) didReceiveMemoryWarning {
   267 
       
   268 - (void)didReceiveMemoryWarning {
   280     self.cachedContentsOfDir = nil;
   269     self.cachedContentsOfDir = nil;
   281     MSG_MEMCLEAN();
   270     MSG_MEMCLEAN();
   282     [super didReceiveMemoryWarning];
   271     [super didReceiveMemoryWarning];
   283 }
   272 }
   284 
   273 
   285 -(void) viewDidUnload {
       
   286     self.tableView = nil;
       
   287     self.listOfAllTeams = nil;
       
   288     self.listOfSelectedTeams = nil;
       
   289     self.cachedContentsOfDir = nil;
       
   290     MSG_DIDUNLOAD();
       
   291     [super viewDidUnload];
       
   292 }
       
   293 
       
   294 
       
   295 -(void) dealloc {
       
   296     releaseAndNil(tableView);
       
   297     releaseAndNil(listOfAllTeams);
       
   298     releaseAndNil(listOfSelectedTeams);
       
   299     releaseAndNil(cachedContentsOfDir);
       
   300     [super dealloc];
       
   301 }
       
   302 
       
   303 
       
   304 @end
   274 @end
   305 
   275