cocoaTouch/FortsViewController.m
changeset 3325 652a8ebdf667
child 3335 2520ee7a5484
equal deleted inserted replaced
3324:339b271d6641 3325:652a8ebdf667
       
     1 //
       
     2 //  FlagsViewController.m
       
     3 //  HedgewarsMobile
       
     4 //
       
     5 //  Created by Vittorio on 08/04/10.
       
     6 //  Copyright 2010 __MyCompanyName__. All rights reserved.
       
     7 //
       
     8 
       
     9 #import "FortsViewController.h"
       
    10 #import "CommodityFunctions.h"
       
    11 #import "UIImageScale.h"
       
    12 
       
    13 @implementation FortsViewController
       
    14 @synthesize teamDictionary, fortArray, fortSprites, lastIndexPath;
       
    15 
       
    16 
       
    17 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
       
    18     return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
       
    19 }
       
    20 
       
    21 #pragma mark -
       
    22 #pragma mark View lifecycle
       
    23 - (void)viewDidLoad {
       
    24     [super viewDidLoad];
       
    25 
       
    26     NSString *fortsDirectory = FORTS_DIRECTORY();
       
    27     NSArray *directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: fortsDirectory error:NULL];
       
    28     NSMutableArray *filteredContents = [[NSMutableArray alloc] initWithCapacity:([directoryContents count] / 2)];
       
    29     // we need to remove the double entries and the L.png suffix
       
    30     for (int i = 0; i < [directoryContents count]; i++) {
       
    31         if (i % 2) {
       
    32             NSString *currentName = [directoryContents objectAtIndex:i];
       
    33             NSString *correctName = [currentName substringToIndex:([currentName length] - 5)];
       
    34             [filteredContents addObject:correctName];
       
    35         } 
       
    36     }
       
    37     
       
    38     self.fortArray = filteredContents;
       
    39     [filteredContents release];
       
    40     
       
    41     //NSLog(@"%@",fortArray);
       
    42     
       
    43     // this creates a scaled down version of the image
       
    44     NSMutableArray *spriteArray = [[NSMutableArray alloc] initWithCapacity:[fortArray count]];
       
    45     for (NSString *fortName in fortArray) {
       
    46         NSString *fortFile = [[NSString alloc] initWithFormat:@"%@/%@L.png", fortsDirectory, fortName];
       
    47         UIImage *fortSprite = [[UIImage alloc] initWithContentsOfFile:fortFile];
       
    48         [fortFile release];
       
    49         [spriteArray addObject:[fortSprite scaleToSize:CGSizeMake(196,196)]];
       
    50         [fortSprite release];
       
    51     }
       
    52     self.fortSprites = spriteArray;
       
    53     [spriteArray release];
       
    54 }
       
    55 
       
    56 
       
    57 - (void)viewWillAppear:(BOOL)animated {
       
    58     [super viewWillAppear:animated];
       
    59     [self.tableView reloadData];
       
    60     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
       
    61 }
       
    62 
       
    63 /*
       
    64 - (void)viewDidAppear:(BOOL)animated {
       
    65     [super viewDidAppear:animated];
       
    66 }
       
    67 */
       
    68 /*
       
    69 - (void)viewWillDisappear:(BOOL)animated {
       
    70     [super viewWillDisappear:animated];
       
    71 }
       
    72 */
       
    73 /*
       
    74 - (void)viewDidDisappear:(BOOL)animated {
       
    75     [super viewDidDisappear:animated];
       
    76 }
       
    77 */
       
    78 
       
    79 
       
    80 #pragma mark -
       
    81 #pragma mark Table view data source
       
    82 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
       
    83     return 1;
       
    84 }
       
    85 
       
    86 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
       
    87     return [fortArray count];
       
    88 }
       
    89 
       
    90 // Customize the appearance of table view cells.
       
    91 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       
    92     
       
    93     static NSString *CellIdentifier = @"Cell";
       
    94     
       
    95     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
       
    96     if (cell == nil) {
       
    97         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
       
    98                                        reuseIdentifier:CellIdentifier] autorelease];
       
    99     }
       
   100     
       
   101     cell.imageView.image = [fortSprites objectAtIndex:[indexPath row]];
       
   102     cell.textLabel.text = [fortArray objectAtIndex:[indexPath row]];
       
   103     cell.detailTextLabel.text = @"Insert funny description here";
       
   104     if ([cell.textLabel.text isEqualToString:[self.teamDictionary objectForKey:@"fort"]]) {
       
   105         cell.accessoryType = UITableViewCellAccessoryCheckmark;
       
   106         self.lastIndexPath = indexPath;
       
   107     } else {
       
   108         cell.accessoryType = UITableViewCellAccessoryNone;
       
   109     }
       
   110     
       
   111     return cell;
       
   112 }
       
   113 
       
   114 
       
   115 /*
       
   116 // Override to support conditional editing of the table view.
       
   117 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
       
   118     // Return NO if you do not want the specified item to be editable.
       
   119     return YES;
       
   120 }
       
   121 */
       
   122 
       
   123 
       
   124 /*
       
   125 // Override to support editing the table view.
       
   126 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
       
   127     
       
   128     if (editingStyle == UITableViewCellEditingStyleDelete) {
       
   129         // Delete the row from the data source
       
   130         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
       
   131     }   
       
   132     else if (editingStyle == UITableViewCellEditingStyleInsert) {
       
   133         // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
       
   134     }   
       
   135 }
       
   136 */
       
   137 
       
   138 
       
   139 /*
       
   140 // Override to support rearranging the table view.
       
   141 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
       
   142 }
       
   143 */
       
   144 
       
   145 
       
   146 /*
       
   147 // Override to support conditional rearranging of the table view.
       
   148 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
       
   149     // Return NO if you do not want the item to be re-orderable.
       
   150     return YES;
       
   151 }
       
   152 */
       
   153 
       
   154 
       
   155 #pragma mark -
       
   156 #pragma mark Table view delegate
       
   157 -(void) tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       
   158     int newRow = [indexPath row];
       
   159     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
       
   160     
       
   161     if (newRow != oldRow) {
       
   162         // if the two selected rows differ update data on the hog dictionary and reload table content
       
   163         [self.teamDictionary setValue:[fortArray objectAtIndex:newRow] forKey:@"fort"];
       
   164 
       
   165         // tell our boss to write this new stuff on disk
       
   166         [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
       
   167         [self.tableView reloadData];
       
   168 
       
   169         self.lastIndexPath = indexPath;
       
   170         [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
       
   171     }
       
   172     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
       
   173     [self.navigationController popViewControllerAnimated:YES];
       
   174 }
       
   175 
       
   176 -(CGFloat) tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
       
   177     return 200;
       
   178 }
       
   179 
       
   180 #pragma mark -
       
   181 #pragma mark Memory management
       
   182 -(void) didReceiveMemoryWarning {
       
   183     // Releases the view if it doesn't have a superview.
       
   184     [super didReceiveMemoryWarning];
       
   185     // Relinquish ownership any cached data, images, etc that aren't in use.
       
   186 }
       
   187 
       
   188 -(void) viewDidUnload {
       
   189     self.teamDictionary = nil;
       
   190     self.lastIndexPath = nil;
       
   191     self.fortArray = nil;
       
   192     self.fortSprites = nil;
       
   193     [super viewDidUnload];
       
   194 }
       
   195 
       
   196 
       
   197 - (void)dealloc {
       
   198     [teamDictionary release];
       
   199     [lastIndexPath release];
       
   200     [fortArray release];
       
   201     [fortSprites release];
       
   202     [super dealloc];
       
   203 }
       
   204 
       
   205 
       
   206 @end
       
   207