cocoaTouch/FortsViewController.m
author koda
Sat, 05 Jun 2010 14:07:58 +0000
changeset 3495 a6b4f351d400
parent 3490 016b3172b645
permissions -rw-r--r--
now engine can be optionally built as library, there's an example wrapper of how to use it building server is now disabled by default, saves users some headaches
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
     1
//
3352
ac5d14a35482 complete previews on the team settings
koda
parents: 3335
diff changeset
     2
//  FortsViewController.m
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
     3
//  HedgewarsMobile
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
     4
//
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
     5
//  Created by Vittorio on 08/04/10.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
     6
//  Copyright 2010 __MyCompanyName__. All rights reserved.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
     7
//
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
     8
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
     9
#import "FortsViewController.h"
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    10
#import "CommodityFunctions.h"
3352
ac5d14a35482 complete previews on the team settings
koda
parents: 3335
diff changeset
    11
#import "UIImageExtra.h"
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    12
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    13
@implementation FortsViewController
3374
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    14
@synthesize teamDictionary, fortArray, lastIndexPath;
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    15
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    16
3335
2520ee7a5484 new manager for handling (future) rotations
koda
parents: 3325
diff changeset
    17
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
2520ee7a5484 new manager for handling (future) rotations
koda
parents: 3325
diff changeset
    18
	return rotationManager(interfaceOrientation);
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    19
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    20
3335
2520ee7a5484 new manager for handling (future) rotations
koda
parents: 3325
diff changeset
    21
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    22
#pragma mark -
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    23
#pragma mark View lifecycle
3374
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    24
-(void) viewDidLoad {
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    25
    [super viewDidLoad];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    26
3374
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    27
    NSArray *directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:FORTS_DIRECTORY() error:NULL];
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    28
    NSMutableArray *filteredContents = [[NSMutableArray alloc] initWithCapacity:([directoryContents count] / 2)];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    29
    // we need to remove the double entries and the L.png suffix
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    30
    for (int i = 0; i < [directoryContents count]; i++) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    31
        if (i % 2) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    32
            NSString *currentName = [directoryContents objectAtIndex:i];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    33
            NSString *correctName = [currentName substringToIndex:([currentName length] - 5)];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    34
            [filteredContents addObject:correctName];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    35
        } 
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    36
    }
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    37
    self.fortArray = filteredContents;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    38
    [filteredContents release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    39
    
3374
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    40
    /*
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    41
    // this creates a scaled down version of the image
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    42
    NSMutableArray *spriteArray = [[NSMutableArray alloc] initWithCapacity:[fortArray count]];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    43
    for (NSString *fortName in fortArray) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    44
        NSString *fortFile = [[NSString alloc] initWithFormat:@"%@/%@L.png", fortsDirectory, fortName];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    45
        UIImage *fortSprite = [[UIImage alloc] initWithContentsOfFile:fortFile];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    46
        [fortFile release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    47
        [spriteArray addObject:[fortSprite scaleToSize:CGSizeMake(196,196)]];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    48
        [fortSprite release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    49
    }
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    50
    self.fortSprites = spriteArray;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    51
    [spriteArray release];
3374
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    52
    */
3357
3836a31879e7 draw a hedgehog when presenting hats
koda
parents: 3352
diff changeset
    53
    
3836a31879e7 draw a hedgehog when presenting hats
koda
parents: 3352
diff changeset
    54
    // statically set row height instead of using delegate method for performance reasons
3836a31879e7 draw a hedgehog when presenting hats
koda
parents: 3352
diff changeset
    55
    self.tableView.rowHeight = 200;
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    56
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    57
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    58
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    59
- (void)viewWillAppear:(BOOL)animated {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    60
    [super viewWillAppear:animated];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    61
    [self.tableView reloadData];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    62
    [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    63
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    64
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    65
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    66
#pragma mark -
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    67
#pragma mark Table view data source
3374
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    68
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    69
    return 1;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    70
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    71
3374
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    72
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    73
    return [self.fortArray count];
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    74
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    75
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    76
// Customize the appearance of table view cells.
3374
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    77
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    78
    static NSString *CellIdentifier = @"Cell";
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    79
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    80
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    81
    if (cell == nil) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    82
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    83
                                       reuseIdentifier:CellIdentifier] autorelease];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    84
    }
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    85
    
3374
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    86
    NSString *fortName = [fortArray objectAtIndex:[indexPath row]];
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    87
    cell.textLabel.text = fortName;
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    88
    
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    89
    // this creates a scaled down version of the image
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    90
    // TODO: create preview files, scaling is way too slow!
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    91
    NSString *fortFile = [[NSString alloc] initWithFormat:@"%@/%@L.png", FORTS_DIRECTORY(), fortName];
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    92
    UIImage *fortSprite = [[UIImage alloc] initWithContentsOfFile:fortFile];
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    93
    [fortFile release];
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    94
    cell.imageView.image = [fortSprite scaleToSize:CGSizeMake(196,196)];
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    95
    [fortSprite release];
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
    96
    
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    97
    cell.detailTextLabel.text = @"Insert funny description here";
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    98
    if ([cell.textLabel.text isEqualToString:[self.teamDictionary objectForKey:@"fort"]]) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
    99
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   100
        self.lastIndexPath = indexPath;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   101
    } else {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   102
        cell.accessoryType = UITableViewCellAccessoryNone;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   103
    }
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   104
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   105
    return cell;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   106
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   107
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   108
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   109
#pragma mark -
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   110
#pragma mark Table view delegate
3374
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
   111
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   112
    int newRow = [indexPath row];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   113
    int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   114
    
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   115
    if (newRow != oldRow) {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   116
        // if the two selected rows differ update data on the hog dictionary and reload table content
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   117
        [self.teamDictionary setValue:[fortArray objectAtIndex:newRow] forKey:@"fort"];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   118
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   119
        // tell our boss to write this new stuff on disk
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   120
        [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
3374
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
   121
        
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
   122
        UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath];
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
   123
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
   124
        UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath];
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
   125
        oldCell.accessoryType = UITableViewCellAccessoryNone;
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   126
        self.lastIndexPath = indexPath;
3374
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
   127
        [aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   128
    }
3374
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
   129
    [aTableView deselectRowAtIndexPath:indexPath animated:YES];
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   130
    [self.navigationController popViewControllerAnimated:YES];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   131
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   132
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   133
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   134
#pragma mark -
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   135
#pragma mark Memory management
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   136
-(void) didReceiveMemoryWarning {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   137
    // Releases the view if it doesn't have a superview.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   138
    [super didReceiveMemoryWarning];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   139
    // Relinquish ownership any cached data, images, etc that aren't in use.
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   140
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   141
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   142
-(void) viewDidUnload {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   143
    self.teamDictionary = nil;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   144
    self.lastIndexPath = nil;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   145
    self.fortArray = nil;
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   146
    [super viewDidUnload];
3490
016b3172b645 a bunch of minor stuff
koda
parents: 3374
diff changeset
   147
    MSG_DIDUNLOAD();
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   148
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   149
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   150
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   151
- (void)dealloc {
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   152
    [teamDictionary release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   153
    [lastIndexPath release];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   154
    [fortArray release];
3374
0d522416d97f lazy loading for all the tables with images (might affect performance but ui feels much more responsive)
koda
parents: 3357
diff changeset
   155
//    [fortSprites release];
3325
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   156
    [super dealloc];
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   157
}
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   158
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   159
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   160
@end
652a8ebdf667 moved around team creation
koda
parents:
diff changeset
   161