project_files/HedgewarsMobile/Classes/TeamConfigViewController.m
changeset 4476 4bf74e158f44
parent 4356 d1d26f8963a3
child 4486 2c8e4d859d37
equal deleted inserted replaced
4474:499748f6e80f 4476:4bf74e158f44
    19  */
    19  */
    20 
    20 
    21 
    21 
    22 #import "TeamConfigViewController.h"
    22 #import "TeamConfigViewController.h"
    23 #import "CommodityFunctions.h"
    23 #import "CommodityFunctions.h"
    24 #import "HogButtonView.h"
       
    25 #import "SquareButtonView.h"
    24 #import "SquareButtonView.h"
    26 
    25 
    27 @implementation TeamConfigViewController
    26 @implementation TeamConfigViewController
    28 @synthesize listOfTeams, listOfSelectedTeams, cachedContentsOfDir;
    27 @synthesize listOfTeams, listOfSelectedTeams, cachedContentsOfDir;
    29 
       
    30 #define NUMBERBUTTON_TAG 123456
       
    31 #define SQUAREBUTTON_TAG 654321
       
    32 #define LABEL_TAG        456123
       
    33 
    28 
    34 #pragma mark -
    29 #pragma mark -
    35 #pragma mark View lifecycle
    30 #pragma mark View lifecycle
    36 -(void) viewDidLoad {
    31 -(void) viewDidLoad {
    37     [super viewDidLoad];
    32     [super viewDidLoad];
    47             UIImageView *background = [[UIImageView alloc] initWithImage:backgroundImage];
    42             UIImageView *background = [[UIImageView alloc] initWithImage:backgroundImage];
    48             [backgroundImage release];
    43             [backgroundImage release];
    49             [self.tableView setBackgroundView:background];
    44             [self.tableView setBackgroundView:background];
    50             [background release];
    45             [background release];
    51         }
    46         }
    52     } else {
    47     } else
    53         self.view.backgroundColor = [UIColor blackColor];
    48         self.view.backgroundColor = [UIColor blackColor];
    54     }
       
    55 
    49 
    56     self.tableView.separatorColor = UICOLOR_HW_YELLOW_BODER;
    50     self.tableView.separatorColor = UICOLOR_HW_YELLOW_BODER;
    57     self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    51     self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    58 }
    52 }
    59 
    53 
    77         [array release];
    71         [array release];
    78 
    72 
    79         NSMutableArray *emptyArray = [[NSMutableArray alloc] initWithObjects:nil];
    73         NSMutableArray *emptyArray = [[NSMutableArray alloc] initWithObjects:nil];
    80         self.listOfSelectedTeams = emptyArray;
    74         self.listOfSelectedTeams = emptyArray;
    81         [emptyArray release];
    75         [emptyArray release];
    82         
    76 
    83         cachedContentsOfDir = [[NSArray alloc] initWithArray:contentsOfDir copyItems:YES];
    77         selectedTeamsCount = [self.listOfSelectedTeams count];
       
    78         allTeamsCount = [self.listOfTeams count];
       
    79 
       
    80         self.cachedContentsOfDir = [[NSArray alloc] initWithArray:contentsOfDir copyItems:YES];
    84     }
    81     }
    85     [self.tableView reloadData];
    82     [self.tableView reloadData];
    86 }
    83 }
    87 
    84 
    88 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    85 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    89     return rotationManager(interfaceOrientation);
    86     return rotationManager(interfaceOrientation);
    90 }
    87 }
    91 
    88 
       
    89 -(NSInteger) filterNumberOfHogs:(NSInteger) hogs {
       
    90     NSInteger numberOfHogs;
       
    91     if (hogs <= HW_getMaxNumberOfHogs() && hogs >= 1)
       
    92         numberOfHogs = hogs;
       
    93     else {
       
    94         if (hogs > HW_getMaxNumberOfHogs())
       
    95             numberOfHogs = 1;
       
    96         else
       
    97             numberOfHogs = HW_getMaxNumberOfHogs();
       
    98     }
       
    99     return numberOfHogs;
       
   100 }
       
   101 
       
   102 -(UIImage *)drawHogsRepeated:(NSInteger) manyTimes {
       
   103     UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:HEDGEHOG_FILE()];
       
   104     CGFloat screenScale = getScreenScale();
       
   105     int w = hogSprite.size.width * screenScale;
       
   106     int h = hogSprite.size.height * screenScale;
       
   107     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
       
   108     CGContextRef context = CGBitmapContextCreate(NULL, w * 3, h, 8, 4 * w * 3, colorSpace, kCGImageAlphaPremultipliedFirst);
       
   109     
       
   110     // draw the two images in the current context
       
   111     for (int i = 0; i < manyTimes; i++)
       
   112         CGContextDrawImage(context, CGRectMake(i*8*screenScale, 0, w, h), [hogSprite CGImage]);
       
   113     [hogSprite release];
       
   114     
       
   115     // Create bitmap image info from pixel data in current context
       
   116     CGImageRef imageRef = CGBitmapContextCreateImage(context);
       
   117     
       
   118     // Create a new UIImage object
       
   119     UIImage *resultImage;
       
   120     if ([self respondsToSelector:@selector(imageWithCGImage:scale:orientation:)])
       
   121         resultImage = [UIImage imageWithCGImage:imageRef scale:screenScale orientation:UIImageOrientationUp];
       
   122     else
       
   123         resultImage = [UIImage imageWithCGImage:imageRef];
       
   124     
       
   125     // Release colorspace, context and bitmap information
       
   126     CGColorSpaceRelease(colorSpace);
       
   127     CGContextRelease(context);
       
   128     CFRelease(imageRef);
       
   129 
       
   130     return resultImage;
       
   131 }
    92 
   132 
    93 #pragma mark -
   133 #pragma mark -
    94 #pragma mark Table view data source
   134 #pragma mark Table view data source
    95 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   135 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    96     return 2;
   136     return 2;
    97 }
   137 }
    98 
   138 
    99 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   139 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   100     if (section == 0)
   140     if (section == 0)
   101         return [listOfSelectedTeams count] ;
   141         return selectedTeamsCount;
   102     else
   142     else
   103         return [listOfTeams count];
   143         return allTeamsCount;
   104 }
   144 }
   105 
   145 
   106 // Customize the appearance of table view cells.
   146 // Customize the appearance of table view cells.
   107 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   147 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   108     static NSString *CellIdentifier0 = @"Cell0";
   148     static NSString *CellIdentifier0 = @"Cell0";
   111     UITableViewCell *cell;
   151     UITableViewCell *cell;
   112 
   152 
   113     if (section == 0) {
   153     if (section == 0) {
   114         cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier0];
   154         cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier0];
   115         if (cell == nil) {
   155         if (cell == nil) {
   116             cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier0] autorelease];
   156             cell = [[[HoldTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier0] autorelease];
   117 
   157 
   118             UIButton *numberButton = [[HogButtonView alloc] initWithFrame:CGRectMake(12, 5, 88, 32)];
   158             SquareButtonView *squareButton = [[SquareButtonView alloc] initWithFrame:CGRectMake(0, 0, 36, 36)];
   119             numberButton.tag = NUMBERBUTTON_TAG;
   159             cell.accessoryView = squareButton;
   120             [cell addSubview:numberButton];
       
   121             [numberButton release];
       
   122 
       
   123             SquareButtonView *squareButton = [[SquareButtonView alloc] initWithFrame:CGRectMake(12+88+6, 5, 36, 36)];
       
   124             squareButton.tag = SQUAREBUTTON_TAG;
       
   125             [cell addSubview:squareButton];
       
   126             [squareButton release];
   160             [squareButton release];
   127 
       
   128             NSInteger length;
       
   129             if (IS_IPAD())
       
   130                 length = 103;
       
   131             else
       
   132                 length = 285;
       
   133             UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12+88+6+36, 10, length, 25)];
       
   134             label.textAlignment = UITextAlignmentLeft;
       
   135             label.minimumFontSize = 11;
       
   136             label.adjustsFontSizeToFitWidth = YES;
       
   137             label.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
       
   138             label.backgroundColor = [UIColor clearColor];
       
   139             label.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]];
       
   140             label.textColor = UICOLOR_HW_YELLOW_TEXT;
       
   141             label.tag = LABEL_TAG;
       
   142             [cell.contentView addSubview:label];
       
   143             [label release];
       
   144         }
   161         }
   145 
   162 
   146         NSMutableDictionary *selectedRow = [listOfSelectedTeams objectAtIndex:[indexPath row]];
   163         NSMutableDictionary *selectedRow = [listOfSelectedTeams objectAtIndex:[indexPath row]];
   147 
   164         cell.textLabel.text = [[selectedRow objectForKey:@"team"] stringByDeletingPathExtension];
   148         UILabel *cellLabel = (UILabel *)[cell viewWithTag:LABEL_TAG];
   165         cell.textLabel.backgroundColor = [UIColor clearColor];
   149         cellLabel.text = [[selectedRow objectForKey:@"team"] stringByDeletingPathExtension];
   166 
   150 
   167         SquareButtonView *squareButton = (SquareButtonView *)cell.accessoryView;
   151         HogButtonView *numberButton = (HogButtonView *)[cell viewWithTag:NUMBERBUTTON_TAG];
       
   152         [numberButton drawManyHogs:[[selectedRow objectForKey:@"number"] intValue]];
       
   153         numberButton.ownerDictionary = selectedRow;
       
   154 
       
   155         SquareButtonView *squareButton = (SquareButtonView *)[cell viewWithTag:SQUAREBUTTON_TAG];
       
   156         [squareButton selectColor:[[selectedRow objectForKey:@"color"] intValue]];
   168         [squareButton selectColor:[[selectedRow objectForKey:@"color"] intValue]];
       
   169         NSNumber *hogNumber = [selectedRow objectForKey:@"number"];
       
   170         [squareButton setTitle:[hogNumber stringValue] forState:UIControlStateNormal];
   157         squareButton.ownerDictionary = selectedRow;
   171         squareButton.ownerDictionary = selectedRow;
       
   172 
       
   173         cell.imageView.image = [self drawHogsRepeated:[hogNumber intValue]];
       
   174         ((HoldTableViewCell *)cell).delegate = self;
       
   175     } else {
       
   176         cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier1];
       
   177         if (cell == nil)
       
   178             cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
       
   179 
       
   180         cell.textLabel.text = [[[listOfTeams objectAtIndex:[indexPath row]] objectForKey:@"team"] stringByDeletingPathExtension];
       
   181         cell.textLabel.backgroundColor = [UIColor clearColor];
   158         
   182         
   159         NSString *teamPath = [NSString stringWithFormat:@"%@/%@",TEAMS_DIRECTORY(),[selectedRow objectForKey:@"team"]];
   183         NSString *teamPath = [NSString stringWithFormat:@"%@/%@.plist",TEAMS_DIRECTORY(),cell.textLabel.text];
   160         NSDictionary *firstHog = [[[NSDictionary dictionaryWithContentsOfFile:teamPath] objectForKey:@"hedgehogs"] objectAtIndex:0];
   184         NSDictionary *firstHog = [[[NSDictionary dictionaryWithContentsOfFile:teamPath] objectForKey:@"hedgehogs"] objectAtIndex:0];
   161         if ([[firstHog objectForKey:@"level"] intValue]> 0) {
   185         if ([[firstHog objectForKey:@"level"] intValue] != 0) {
   162             NSString *filePath = [NSString stringWithFormat:@"%@/cyborg.png",HATS_DIRECTORY()];
   186             NSString *filePath = [NSString stringWithFormat:@"%@/cyborg.png",HATS_DIRECTORY()];
   163             UIImage *sprite = [[UIImage alloc] initWithContentsOfFile:filePath andCutAt:CGRectMake(0, 2, 32, 32)];
   187             UIImage *sprite = [[UIImage alloc] initWithContentsOfFile:filePath andCutAt:CGRectMake(0, 2, 32, 32)];
   164             UIImageView *spriteView = [[UIImageView alloc] initWithImage:sprite];
   188             UIImageView *spriteView = [[UIImageView alloc] initWithImage:sprite];
   165             [sprite release];
   189             [sprite release];
   166             
   190             
   167             cell.accessoryView = spriteView;
   191             cell.accessoryView = spriteView;
   168             [spriteView release];
   192             [spriteView release];
   169         } else
   193         } else
   170             cell.accessoryView = nil;
   194             cell.accessoryView = nil;
   171     } else {
   195     }
   172         cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier1];
   196 
   173         if (cell == nil)
       
   174             cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
       
   175 
       
   176         cell.textLabel.text = [[[listOfTeams objectAtIndex:[indexPath row]] objectForKey:@"team"] stringByDeletingPathExtension];
       
   177         cell.accessoryView = nil;
       
   178     }
       
   179     
       
   180     cell.textLabel.textColor = UICOLOR_HW_YELLOW_TEXT;
   197     cell.textLabel.textColor = UICOLOR_HW_YELLOW_TEXT;
   181     cell.backgroundColor = [UIColor blackColor];
   198     cell.backgroundColor = UICOLOR_HW_ALMOSTBLACK;
   182     
   199     cell.selectionStyle = UITableViewCellSelectionStyleNone;
       
   200 
   183     return cell;
   201     return cell;
   184 }
   202 }
   185 
   203 
   186 -(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
   204 -(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
   187     return 40.0;
   205     return 40.0;
   188 }
   206 }
   189 
   207 
   190 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
   208 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
   191     CGRect frame = CGRectMake(0, 0, self.view.frame.size.width * 80/100, 30);
   209     CGRect frame = CGRectMake(0, 0, self.view.frame.size.width * 80/100, 30);
   192     NSString *text;
   210     NSString *text;
   193     if (section == 0) 
   211     if (section == 0)
   194         text = NSLocalizedString(@"Playing Teams",@"");
   212         text = NSLocalizedString(@"Playing Teams",@"");
   195     else
   213     else
   196         text = NSLocalizedString(@"Available Teams",@"");
   214         text = NSLocalizedString(@"Available Teams",@"");
   197     UILabel *theLabel = createBlueLabel(text, frame);
   215     UILabel *theLabel = createBlueLabel(text, frame);
   198     theLabel.center = CGPointMake(self.view.frame.size.width/2, 20);
   216     theLabel.center = CGPointMake(self.view.frame.size.width/2, 20);
   201     [theView addSubview:theLabel];
   219     [theView addSubview:theLabel];
   202     [theLabel release];
   220     [theLabel release];
   203     return theView;
   221     return theView;
   204 }
   222 }
   205 
   223 
       
   224 -(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
       
   225     return 20;
       
   226 }
       
   227 
       
   228 -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger) section {
       
   229     UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 20)];
       
   230     footer.backgroundColor = [UIColor clearColor];
       
   231 
       
   232     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width*80/100, 20)];
       
   233     label.center = CGPointMake(self.tableView.frame.size.width/2, 20/2);
       
   234     label.textAlignment = UITextAlignmentCenter;
       
   235     label.font = [UIFont italicSystemFontOfSize:12];
       
   236     label.textColor = [UIColor whiteColor];
       
   237     label.numberOfLines = 1;
       
   238     if (section == 0)
       
   239         label.text = NSLocalizedString(@"Tap to add hogs or change color, hold tap to remove team.",@"");
       
   240     else
       
   241         label.text = NSLocalizedString(@"The robot badge indicates an AI-controlled team.",@"");
       
   242 
       
   243     label.backgroundColor = [UIColor clearColor];
       
   244     [footer addSubview:label];
       
   245     [label release];
       
   246     return [footer autorelease];
       
   247 }
       
   248 
       
   249 
   206 #pragma mark -
   250 #pragma mark -
   207 #pragma mark Table view delegate
   251 #pragma mark Table view delegate
   208 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   252 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   209     NSInteger row = [indexPath row];
   253     NSInteger row = [indexPath row];
   210     NSInteger section = [indexPath section];
   254     NSInteger section = [indexPath section];
   211 
   255 
   212     if (section == 0) {
   256     if (section == 0) {
   213         [self.listOfTeams addObject:[self.listOfSelectedTeams objectAtIndex:row]];
   257         NSMutableDictionary *selectedRow = [self.listOfSelectedTeams objectAtIndex:[indexPath row]];
   214         [self.listOfSelectedTeams removeObjectAtIndex:row];
   258         UITableViewCell *cell = [aTableView cellForRowAtIndexPath:indexPath];
       
   259         SquareButtonView *squareButton = (SquareButtonView *)cell.accessoryView;
       
   260 
       
   261         NSInteger increaseNumber = [[selectedRow objectForKey:@"number"] intValue] + 1;
       
   262         NSNumber *newNumber = [NSNumber numberWithInt:[self filterNumberOfHogs:increaseNumber]];
       
   263         [squareButton setTitle:[newNumber stringValue] forState:UIControlStateNormal];
       
   264         [selectedRow setObject:newNumber forKey:@"number"];
       
   265 
       
   266         cell.imageView.image = [self drawHogsRepeated:[newNumber intValue]];
       
   267         [cell setNeedsLayout];
   215     } else {
   268     } else {
   216         [self.listOfSelectedTeams addObject:[self.listOfTeams objectAtIndex:row]];
   269         [self.listOfSelectedTeams addObject:[self.listOfTeams objectAtIndex:row]];
   217         [self.listOfTeams removeObjectAtIndex:row];
   270         [self.listOfTeams removeObjectAtIndex:row];
   218     }
   271 
   219     [aTableView reloadData];
   272         NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:selectedTeamsCount inSection:0];
   220 }
   273         allTeamsCount--;
   221 
   274         selectedTeamsCount++;
       
   275         [aTableView beginUpdates];
       
   276         [aTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationRight];
       
   277         [aTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
       
   278         [aTableView endUpdates];
       
   279     }
       
   280 }
       
   281 
       
   282 -(void) holdAction:(NSString *)content {
       
   283     NSInteger row;
       
   284     for (row = 0; row < [self.listOfSelectedTeams count]; row++) {
       
   285         NSDictionary *dict = [self.listOfSelectedTeams objectAtIndex:row];
       
   286         if ([content isEqualToString:[[dict objectForKey:@"team"] stringByDeletingPathExtension]])
       
   287             break;
       
   288     }
       
   289 
       
   290     [self.listOfTeams addObject:[self.listOfSelectedTeams objectAtIndex:row]];
       
   291     [self.listOfSelectedTeams removeObjectAtIndex:row];
       
   292 
       
   293     [self.tableView beginUpdates];
       
   294     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:row inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
       
   295     [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:allTeamsCount inSection:1]] withRowAnimation:UITableViewRowAnimationLeft];
       
   296     allTeamsCount++;
       
   297     selectedTeamsCount--;
       
   298     [self.tableView endUpdates];
       
   299 }
   222 
   300 
   223 #pragma mark -
   301 #pragma mark -
   224 #pragma mark Memory management
   302 #pragma mark Memory management
   225 -(void) didReceiveMemoryWarning {
   303 -(void) didReceiveMemoryWarning {
   226     // Relinquish ownership any cached data, images, etc that aren't in use.
   304     // Relinquish ownership any cached data, images, etc that aren't in use.