project_files/HedgewarsMobile/Classes/FlagsViewController.m
changeset 3950 296cbdd85e50
parent 3829 81db3c85784b
child 3971 5c82ee165ed5
equal deleted inserted replaced
3949:4c4c0a2507cc 3950:296cbdd85e50
    21 
    21 
    22 #import "FlagsViewController.h"
    22 #import "FlagsViewController.h"
    23 #import "CommodityFunctions.h"
    23 #import "CommodityFunctions.h"
    24 
    24 
    25 @implementation FlagsViewController
    25 @implementation FlagsViewController
    26 @synthesize teamDictionary, flagArray, lastIndexPath;
    26 @synthesize teamDictionary, flagArray, communityArray, lastIndexPath;
    27 
    27 
    28 
    28 
    29 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    29 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    30     return rotationManager(interfaceOrientation);
    30     return rotationManager(interfaceOrientation);
    31 }
    31 }
    34 #pragma mark -
    34 #pragma mark -
    35 #pragma mark View lifecycle
    35 #pragma mark View lifecycle
    36 -(void) viewDidLoad {
    36 -(void) viewDidLoad {
    37     [super viewDidLoad];
    37     [super viewDidLoad];
    38 
    38 
    39     self.flagArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:FLAGS_DIRECTORY() error:NULL];
    39     NSMutableArray *array_na = [[NSMutableArray alloc] init];
       
    40     NSMutableArray *array_cm = [[NSMutableArray alloc] init];
       
    41     
       
    42     for (NSString *name in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:FLAGS_DIRECTORY() error:NULL]) {
       
    43         if ([name hasPrefix:@"cm_"]) {
       
    44             NSString *processed = [name substringFromIndex:3];
       
    45             [array_cm addObject:processed];
       
    46         } else
       
    47              [array_na addObject:name];
       
    48     }
       
    49     
       
    50     self.flagArray = array_na;
       
    51     [array_na release];
       
    52     self.communityArray = array_cm;
       
    53     [array_cm release];
    40 
    54 
    41     self.title = NSLocalizedString(@"Set team flag",@"");
    55     self.title = NSLocalizedString(@"Set team flag",@"");
    42 }
    56 }
    43 
    57 
    44 -(void) viewWillAppear:(BOOL)animated {
    58 -(void) viewWillAppear:(BOOL)animated {
    45     [super viewWillAppear:animated];
    59     [super viewWillAppear:animated];
       
    60     // reloadData needed because team might change
    46     [self.tableView reloadData];
    61     [self.tableView reloadData];
    47     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    62     //[self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    48 }
    63 }
    49 
    64 
    50 
    65 
    51 #pragma mark -
    66 #pragma mark -
    52 #pragma mark Table view data source
    67 #pragma mark Table view data source
    53 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    68 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    54     return 1;
    69     return 2;
    55 }
    70 }
    56 
    71 
    57 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    72 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    58     return [flagArray count];
    73     if (section == 0)
       
    74         return [self.flagArray count];
       
    75     else
       
    76         return [self.communityArray count];
    59 }
    77 }
    60 
    78 
    61 // Customize the appearance of table view cells.
    79 // Customize the appearance of table view cells.
    62 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    80 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    63 
       
    64     static NSString *CellIdentifier = @"Cell";
    81     static NSString *CellIdentifier = @"Cell";
       
    82     NSInteger row = [indexPath row];
    65 
    83 
    66     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    84     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    67     if (cell == nil) {
    85     if (cell == nil) {
    68         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    86         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    69     }
    87     }
    70 
    88 
    71     NSString *flag = [flagArray objectAtIndex:[indexPath row]];
    89     NSString *flagName = nil;
    72 
    90     NSArray *source = nil;
    73     NSString *flagFile = [[NSString alloc] initWithFormat:@"%@/%@", FLAGS_DIRECTORY(), flag];
    91     if ([indexPath section] == 0) {
       
    92         source = self.flagArray;
       
    93         flagName = [source objectAtIndex:row];
       
    94     } else {
       
    95         source = self.communityArray;
       
    96         flagName = [NSString stringWithFormat:@"cm_%@",[source objectAtIndex:row]];
       
    97     }
       
    98     NSString *flagFile = [[NSString alloc] initWithFormat:@"%@/%@", FLAGS_DIRECTORY(), flagName];
    74     UIImage *flagSprite = [[UIImage alloc] initWithContentsOfFile:flagFile];
    99     UIImage *flagSprite = [[UIImage alloc] initWithContentsOfFile:flagFile];
    75     [flagFile release];
   100     [flagFile release];
    76     cell.imageView.image = flagSprite;
   101     cell.imageView.image = flagSprite;
    77     [flagSprite release];
   102     [flagSprite release];
    78 
   103 
    79     cell.textLabel.text = [flag stringByDeletingPathExtension];
   104     cell.textLabel.text = [[source objectAtIndex:row] stringByDeletingPathExtension];
    80     if ([cell.textLabel.text isEqualToString:[self.teamDictionary objectForKey:@"flag"]]) {
   105     if ([[flagName stringByDeletingPathExtension] isEqualToString:[self.teamDictionary objectForKey:@"flag"]]) {
    81         cell.accessoryType = UITableViewCellAccessoryCheckmark;
   106         cell.accessoryType = UITableViewCellAccessoryCheckmark;
    82         self.lastIndexPath = indexPath;
   107         self.lastIndexPath = indexPath;
    83     } else {
   108     } else {
    84         cell.accessoryType = UITableViewCellAccessoryNone;
   109         cell.accessoryType = UITableViewCellAccessoryNone;
    85     }
   110     }
    86 
   111 
    87     return cell;
   112     return cell;
    88 }
   113 }
    89 
   114 
       
   115 -(NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section {
       
   116     NSString *sectionTitle = nil;
       
   117     switch (section) {
       
   118         case 0:
       
   119             sectionTitle = NSLocalizedString(@"Worldwide", @"");
       
   120             break;
       
   121         case 1:
       
   122             sectionTitle = NSLocalizedString(@"Community", @"");
       
   123             break;
       
   124         default:
       
   125             DLog(@"nope");
       
   126             break;
       
   127     }
       
   128     return sectionTitle;
       
   129 }
       
   130 
    90 
   131 
    91 #pragma mark -
   132 #pragma mark -
    92 #pragma mark Table view delegate
   133 #pragma mark Table view delegate
    93 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   134 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    94     int newRow = [indexPath row];
   135     int newRow = [indexPath row];
    95     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
   136     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
    96 
   137     int newSection = [indexPath section];
    97     if (newRow != oldRow) {
   138     int oldSection = (lastIndexPath != nil) ? [lastIndexPath section] : -1;
       
   139     
       
   140     if (newRow != oldRow || newSection != oldSection) {
       
   141         NSString *flag = nil;
       
   142         if ([indexPath section] == 0)
       
   143             flag = [self.flagArray objectAtIndex:newRow];
       
   144         else
       
   145             flag = [NSString stringWithFormat:@"cm_%@",[self.communityArray objectAtIndex:newRow]];
       
   146         
    98         // if the two selected rows differ update data on the hog dictionary and reload table content
   147         // if the two selected rows differ update data on the hog dictionary and reload table content
    99         [self.teamDictionary setValue:[[flagArray objectAtIndex:newRow] stringByDeletingPathExtension] forKey:@"flag"];
   148         [self.teamDictionary setValue:[flag stringByDeletingPathExtension] forKey:@"flag"];
   100 
   149 
   101         // tell our boss to write this new stuff on disk
   150         // tell our boss to write this new stuff on disk
   102         [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
   151         [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
   103 
   152 
   104         UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath];
   153         UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath];
   115 
   164 
   116 #pragma mark -
   165 #pragma mark -
   117 #pragma mark Memory management
   166 #pragma mark Memory management
   118 -(void) didReceiveMemoryWarning {
   167 -(void) didReceiveMemoryWarning {
   119     // Releases the view if it doesn't have a superview.
   168     // Releases the view if it doesn't have a superview.
       
   169     // Relinquish ownership any cached data, images, etc that aren't in use.
       
   170     self.lastIndexPath = nil;
       
   171     MSG_MEMCLEAN();
   120     [super didReceiveMemoryWarning];
   172     [super didReceiveMemoryWarning];
   121     // Relinquish ownership any cached data, images, etc that aren't in use.
       
   122 }
   173 }
   123 
   174 
   124 -(void) viewDidUnload {
   175 -(void) viewDidUnload {
   125     self.teamDictionary = nil;
   176     self.teamDictionary = nil;
   126     self.lastIndexPath = nil;
   177     self.lastIndexPath = nil;
   127     self.flagArray = nil;
   178     self.flagArray = nil;
       
   179     self.communityArray = nil;
   128     MSG_DIDUNLOAD();
   180     MSG_DIDUNLOAD();
   129     [super viewDidUnload];
   181     [super viewDidUnload];
   130 }
   182 }
   131 
   183 
   132 -(void) dealloc {
   184 -(void) dealloc {
   133     [teamDictionary release];
   185     [teamDictionary release];
   134     [lastIndexPath release];
   186     [lastIndexPath release];
   135     [flagArray release];
   187     [flagArray release];
       
   188     [communityArray release];
   136     [super dealloc];
   189     [super dealloc];
   137 }
   190 }
   138 
   191 
   139 
   192 
   140 @end
   193 @end